Documentation
¶
Overview ¶
Package slug generates URL-safe slugs from arbitrary strings with Unicode normalization.
This package converts text to web-friendly identifiers by normalizing diacritics, replacing special characters with separators, and offering configurable options for length limits and collision-resistant suffixes.
Basic usage:
import "github.com/dmitrymomot/forge/pkg/slug"
// Simple slug generation
s := slug.Make("Hello, World!")
// Output: "hello-world"
// With Unicode normalization
s = slug.Make("Café & Restaurant")
// Output: "cafe-restaurant"
// With configuration options
s = slug.Make("Long Article Title",
slug.WithMaxLength(20),
slug.WithSuffix(6),
)
// Output: "long-article-x3k7f9"
Configuration Options ¶
WithMaxLength limits the slug length (rune-based):
slug.Make("Very long title", slug.WithMaxLength(15))
// Output: "very-long-title"
WithMinLength sets the minimum slug length, padding with a random suffix if needed:
slug.Make("hi", slug.WithMinLength(10))
// Output: "hi-a3f7k2m9" (padded to reach 10 runes)
WithSeparator sets the character used between words:
slug.Make("Product Name", slug.WithSeparator("_"))
// Output: "product_name"
WithLowercase controls case conversion:
slug.Make("Product Name", slug.WithLowercase(false))
// Output: "Product-Name"
WithStripChars removes specific characters before processing:
slug.Make("Price: $100", slug.WithStripChars("$:"))
// Output: "price-100"
WithCustomReplace applies string replacements before slugification:
replacements := map[string]string{"&": "and", "@": "at"}
slug.Make("Fish & Chips @ Home", slug.WithCustomReplace(replacements))
// Output: "fish-and-chips-at-home"
WithSuffix adds a random alphanumeric suffix for uniqueness:
slug.Make("Article Title", slug.WithSuffix(8))
// Output: "article-title-a3f7k2m9"
WithReservedSlugs prevents use of specified slugs (case-insensitive) by appending a suffix:
slug.Make("admin", slug.WithReservedSlugs("admin", "api", "system"))
// Output: "admin-k7x2m4" (suffix added to avoid reserved slug)
Unicode Support ¶
The package normalizes common Latin diacritics to ASCII equivalents:
slug.Make("München straße") // "munchen-strase"
slug.Make("naïve résumé") // "naive-resume"
slug.Make("Ñoño español") // "nono-espanol"
Unsupported character sets (Cyrillic, CJK, etc.) are replaced with separators.
Index ¶
- func Make(s string, opts ...Option) string
- type Option
- func WithCustomReplace(replacements map[string]string) Option
- func WithLowercase(enabled bool) Option
- func WithMaxLength(n int) Option
- func WithMinLength(n int) Option
- func WithReservedSlugs(slugs ...string) Option
- func WithSeparator(s string) Option
- func WithStripChars(chars string) Option
- func WithSuffix(length int) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Option ¶
type Option func(*config)
Option configures the slug generation behavior.
func WithCustomReplace ¶
WithCustomReplace sets custom string replacements to apply before slugification. For example: {"&": "and", "@": "at"}
func WithLowercase ¶
WithLowercase controls whether the slug should be converted to lowercase. Default is true.
func WithMaxLength ¶
WithMaxLength sets the maximum length of the generated slug. If the slug exceeds this length, it will be truncated.
func WithMinLength ¶
WithMinLength sets the minimum length of the generated slug. If the slug is shorter than this length, a random suffix will be appended.
func WithReservedSlugs ¶
WithReservedSlugs sets a list of reserved slugs that cannot be used. If the generated slug matches any reserved slug (case-insensitive), a random suffix will be automatically appended. Example: slug.Make("admin", WithReservedSlugs("admin", "api")) returns "admin-x7g3k2"
func WithSeparator ¶
WithSeparator sets the separator character for the slug. Default is "-".
func WithStripChars ¶
WithStripChars sets additional characters to strip from the slug.
func WithSuffix ¶
WithSuffix adds a random alphanumeric suffix to reduce collision possibility. The suffix is separated by the configured separator. Example: "hello-world-x7g3k2" (with length=6)