slugger

package
v1.32.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 8 Imported by: 0

README

Slugger

The slugger package provides a simple and efficient way to generate URL-friendly slugs from strings. Below are the main features and methods of the package:

Slugger Constructor

  • New(substitutions map[string]string, withEmoji bool) *Slugger:
    Creates a new Slugger instance.
    • substitutions: A map of string replacements to apply before generating the slug.
    • withEmoji: If true, emojis will be included in a slug-friendly format.

Slugger Methods

  • Slug(s, separator string) string:
    Generates a slugified version of the input string s. If separator is provided, it will be used to separate words in the slug; otherwise, a default separator(-) is applied.

  • AddSubstitution(key, value string):
    Adds a new substitution pair to the Slugger instance. This allows you to define custom replacements that will be applied when generating slugs.

  • RemoveSubstitution(key string):
    Removes a substitution pair from the Slugger instance. This is useful for cleaning up or modifying existing substitutions.

  • ReplaceSubstitution(key, newValue string):
    Replaces an existing substitution value with a new value. This allows you to update the slug generation rules without having to recreate the Slugger instance.

  • SetSubstitutions(substitutions map[string]string):
    Sets the entire map of substitutions for the Slugger instance. This can be used to initialize or update multiple substitutions at once.

Notes

  • If a substitutions map is provided, it will replace all occurrences of the specified keys with their corresponding values. For example, given a substitution pair {"the": ""} and the input string over there, the resulting slug will be over-re.

Examples:

For examples of each function, please check out EXAMPLES.md


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Slugger

type Slugger struct {
	Separator     string            // Default character(s) used to separate words in the slug if not explicitly provided
	Substitutions map[string]string // A map of string replacements to apply before generating the slug
	WithEmoji     bool              // If true, emojis will be included in a slug-friendly format
	// contains filtered or unexported fields
}

Slugger provides functionality to generate URL-friendly slugs from strings. It supports configurable substitutions, optional emoji handling, and custom separators.

func New

func New(substitutions map[string]string, withEmoji bool) *Slugger

New creates and returns a new Slugger instance with optional substitutions and emoji handling.

func (*Slugger) AddSubstitution added in v1.29.0

func (sl *Slugger) AddSubstitution(key, value string)

AddSubstitution adds a new substitution to the Slugger and resets the replacer cache.

func (*Slugger) RemoveSubstitution added in v1.29.0

func (sl *Slugger) RemoveSubstitution(key string)

RemoveSubstitution deletes a substitution by key and resets the replacer cache.

func (*Slugger) ReplaceSubstitution added in v1.29.0

func (sl *Slugger) ReplaceSubstitution(key, newValue string)

ReplaceSubstitution updates the value of an existing substitution and resets the replacer cache.

func (*Slugger) SetSubstitutions added in v1.29.0

func (sl *Slugger) SetSubstitutions(substitutions map[string]string)

SetSubstitutions replaces all current substitutions with the provided map and resets the replacer cache.

func (*Slugger) Slug

func (sl *Slugger) Slug(s, separator string) string

Slug converts the given string `s` into a URL-friendly slug. - Applies emoji replacement if enabled - Lowercases the string - Applies configured substitutions - Normalizes to safe ASCII - Joins words with the provided separator (or the default)

Jump to

Keyboard shortcuts

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