Slugify
Transform strings in any language into slugs.
It works by transliterating Unicode characters into alphanumeric strings (e.g.
字 into zi). All punctuation is stripped and whitespace between words are
replaced by separators.
This package has no dependencies.
Examples
Slug.slugify("Hello, World!")
"hello-world"
Slug.slugify("你好,世界")
"nihaoshijie"
Slug.slugify("Wikipedia case", separator: ?_, lowercase: false)
"Wikipedia_case"
# Remember to check for nil if a valid slug is important!
Slug.slugify("🙅")
nil
Options
Whitespaces are replaced by separators (defaults to -). Pass any codepoint or
string to customize the separator, or pass "" to have none.
Slug.slugify(" How are you? ")
"how-are-you"
Slug.slugify("John Doe", separator: ?.)
"john.doe"
Slug.slugify("Wide open spaces", separator: "%20")
"wide%20open%20spaces"
Slug.slugify("Madam, I'm Adam", separator: "")
"madamimadam"
Slugs are forced lowercase by default, unless lowercase: false is passed.
Slug.slugify("StUdLy CaPs", lowercase: false)
"StUdLy-CaPs"
To avoid transforming certain characters, pass a string (or a list of strings)
of graphemes to ignore.
Slug.slugify("你好,世界", ignore: "你好")
"你好shijie"
Slug.slugify("你好,世界", ignore: ["你", "好"])
"你好shijie"
Caveats
Slugify cannot differentiate between Chinese characters and Japanese Kanji. In the same vein as other libraries, Japanese Kanji will transform into Chinese pinyin.
Installation
Add slugify to your list of dependencies in mix.exs:
def deps do
[{:slugify, "~> 1.1"}]
end
Don’t forget to update your dependencies.
$ mix deps.get
Links
License
Slugify is released under MIT license.
Credits
-
Inspired by Unidecode, Transliteration and Slugger.
-
Data ported from andyhu's excellent Transliteration package.