slug

package
v0.3.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Make

func Make(s string, opts ...Option) string

Make creates a URL-safe slug from the input string. It normalizes the string by replacing spaces and special characters with the separator (default "-"), and optionally converts to lowercase.

Types

type Option

type Option func(*config)

Option configures the slug generation behavior.

func WithCustomReplace

func WithCustomReplace(replacements map[string]string) Option

WithCustomReplace sets custom string replacements to apply before slugification. For example: {"&": "and", "@": "at"}

func WithLowercase

func WithLowercase(enabled bool) Option

WithLowercase controls whether the slug should be converted to lowercase. Default is true.

func WithMaxLength

func WithMaxLength(n int) Option

WithMaxLength sets the maximum length of the generated slug. If the slug exceeds this length, it will be truncated.

func WithMinLength

func WithMinLength(n int) Option

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

func WithReservedSlugs(slugs ...string) Option

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

func WithSeparator(s string) Option

WithSeparator sets the separator character for the slug. Default is "-".

func WithStripChars

func WithStripChars(chars string) Option

WithStripChars sets additional characters to strip from the slug.

func WithSuffix

func WithSuffix(length int) Option

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)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL