Slug Utilities
The slug utilities provide URL-safe string generation for creating SEO-friendly paths from Guatemalan words and phrases.slugify
Converts a string into a URL-safe slug by removing accents, special characters, and normalizing whitespace.Parameters
The string to convert into a slug. Can contain accents, spaces, and special characters.
Returns
URL-safe slug with:
- Accents and diacritics removed (NFD normalization)
- Lowercase characters only
- Spaces converted to hyphens
- Special characters removed
- Leading/trailing whitespace trimmed
- Multiple spaces collapsed to single hyphens
Algorithm Steps
- Normalize - Uses Unicode NFD (Normalization Form Decomposition) to separate base characters from diacritics
- Remove Diacritics - Removes all diacritic marks using regex
/\p{Diacritic}/gu - Lowercase - Converts all characters to lowercase
- Remove Special Characters - Keeps only alphanumeric characters, spaces, and hyphens
- Trim - Removes leading and trailing whitespace
- Collapse Spaces - Replaces multiple spaces with single hyphens
Examples
Real-World Usage
Technical Details
Unicode Normalization (NFD)
Unicode Normalization (NFD)
The function uses NFD (Normalization Form Decomposition) to separate base characters from combining diacritical marks.For example:
"á"becomes"a" + combining acute accent"ü"becomes"u" + combining diaeresis
Regex Pattern Explanation
Regex Pattern Explanation
The regex
/\p{Diacritic}/gu uses:\p{Diacritic}- Unicode property escape matching all diacritical marksg- Global flag to replace all occurrencesu- Unicode flag for full Unicode support
/[^a-z0-9\s-]/g removes:- Everything except lowercase letters (a-z)
- Digits (0-9)
- Whitespace (\s)
- Hyphens (-)
Browser Compatibility
Browser Compatibility
The function uses:
String.prototype.normalize()- Supported in all modern browsers (IE11+)- Unicode property escapes (
\p{}) - Requires ES2018+
Related Functions
While this is the primary slug utility, slugs are also used in:getLocalizedPath()from@/utils/i18n- for adding language prefixes- Astro’s built-in
entry.slug- automatically generated from content collection file names setupShareImage()from@/utils/shareImage- uses slugs for image filenames