Add Slug Generator

This commit is contained in:
Dan Jones 2025-03-13 16:58:55 -05:00
commit 1d235af876
8 changed files with 124 additions and 1 deletions

View file

@ -6,6 +6,7 @@ import (
"time"
"github.com/google/uuid"
"github.com/gosimple/slug"
)
// Generator is a function that returns the "random" portion of the returned filename.
@ -123,3 +124,33 @@ func IncrementalWithStartAndStep(start, step int) Generator {
return out, nil
}
}
// ErrMissingOriginal is the error returned by Slug if there is no filename
var ErrMissingOriginal = errors.New("missing original filename")
func getOriginal(c *Config) (string, error) {
if c.original == "" {
return "", ErrMissingOriginal
}
name := c.original
c.original = ""
return name, nil
}
// Slug generates a name from the original filename.
// When this is used, the original filename will be removed from the final filename.
func Slug() Generator {
return func(c *Config) (string, error) {
name, err := getOriginal(c)
return slug.Make(name), err
}
}
// SlugWithLang generates a name from the original filename, accounting for the given language.
// When this is used, the original filename will be removed from the final filename.
func SlugWithLang(lang string) Generator {
return func(c *Config) (string, error) {
name, err := getOriginal(c)
return slug.MakeLang(name, lang), err
}
}