URL-safe slug generation with locale-aware Unicode folding.
Produces a lowercase, separator-joined ASCII string suitable for URLs, filenames, and other identifiers from any UTF-8 input. Unlike a naive ASCII filter, this module handles:
Latin diacritics —
"café résumé"→"cafe-resume".Locale-specific folding rules baked into CLDR — German
"ß"folds to"ss", Turkish"İ"to"i", Vietnamese"Đ"to"D", etc. (via theunicode_transformpackage'sLatin-ASCIItransform).Cross-script transliteration —
"Привет мир"→"privet-mir","北京"→"beijing","こんにちは"→"konnichiha"— for inputs in any scriptunicode_transformhas a<Script>-Latintransform for. Disable withtransliterate: falseto drop non-Latin characters instead.
Algorithm
(Optional) per-script transliteration via
unicode_transform, leaving Latin runs untouched.ASCII folding via
Latin-ASCII(handles diacritics and the locale-specific cases above).Lowercase.
Replace each maximal run of non-
[a-z0-9]characters with the separator.Trim leading and trailing separators.
Summary
Functions
Returns a URL-safe slug for the given string.
Functions
Returns a URL-safe slug for the given string.
Arguments
textis a UTF-8 string.
Options
:separator— the joining string used between word runs and inserted in place of non-alphanumeric characters. Defaults to"-". Any non-empty string is allowed;"_"is the common alternative.:transliterate— whentrue(default), characters in non-Latin scripts are first transliterated to Latin viaunicode_transform's<Script>-Latintransforms (for those scripts that have one). Whenfalse, non-Latin characters are dropped during the ASCII folding step.
Returns
- A non-empty lowercase ASCII string. Returns
""if the input has no characters that survive the pipeline (e.g. all-emoji input withtransliterate: false).
Examples
iex> Text.Slug.slugify("Hello World")
"hello-world"
iex> Text.Slug.slugify("café résumé")
"cafe-resume"
iex> Text.Slug.slugify("Straße München")
"strasse-munchen"
iex> Text.Slug.slugify("İstanbul Çanakkale")
"istanbul-canakkale"
iex> Text.Slug.slugify("Đà Nẵng")
"da-nang"
iex> Text.Slug.slugify("Hello World", separator: "_")
"hello_world"
iex> Text.Slug.slugify(" multiple spaces ")
"multiple-spaces"
iex> Text.Slug.slugify("a/b\\c")
"a-b-c"