Documentation
¶
Overview ¶
Package slug provides URL-safe slug generation from strings.
It handles Unicode transliteration, configurable separators, max length truncation at word boundaries, and unique slug generation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultStopWords ¶ added in v0.2.0
func DefaultStopWords() []string
DefaultStopWords returns a built-in list of common English stop words.
func IsSlug ¶ added in v0.2.0
IsSlug checks if a string is a valid slug. A valid slug contains only lowercase ASCII letters, digits, and hyphens, with no leading, trailing, or consecutive hyphens, and is not empty.
func Make ¶
Make generates a URL-safe slug from the given string using default settings. It transliterates Unicode to ASCII, lowercases the result, replaces non-alphanumeric characters with hyphens, collapses consecutive hyphens, and trims hyphens from the start and end.
func ToKebab ¶ added in v0.2.0
ToKebab converts a string to kebab-case. It splits on spaces, underscores, and camelCase boundaries. No Unicode transliteration is performed.
Types ¶
type Option ¶
type Option func(*Slugger)
Option configures a Slugger.
func WithCustomSubs ¶
WithCustomSubs sets custom string substitutions that are applied before transliteration. Keys are matched case-sensitively.
func WithMaxLen ¶
WithMaxLen sets the maximum length of the generated slug. The slug is truncated at the last word boundary (separator) before maxLen. A value of 0 means no limit.
func WithSeparator ¶
WithSeparator sets the separator character used between words. The default separator is "-".
func WithStopWords ¶ added in v0.2.0
WithStopWords sets words to be removed from the slug. Words are removed after splitting but before joining. Matching is case-insensitive.
func WithStrict ¶ added in v0.2.0
func WithStrict() Option
WithStrict enables strict mode that only allows ASCII lowercase letters, digits, and the separator. Any non-matching characters are removed entirely (no transliteration).
type Slugger ¶
type Slugger struct {
// contains filtered or unexported fields
}
Slugger generates URL-safe slugs with configurable options.
func (*Slugger) Make ¶
Make generates a URL-safe slug from the given string.
The algorithm is:
- Apply custom substitutions
- Transliterate Unicode to ASCII (skipped in strict mode)
- Lowercase
- Replace any non [a-z0-9] with separator
- Collapse consecutive separators
- Trim separators from start/end
- Remove stop words
- If maxLen is set, truncate at word boundary