Slugify v1.0.0 Slug View Source

Transform strings in any language into slugs.

It works by transliterating any Unicode character to alphanumeric ones, and replacing whitespaces with hyphens.

The goal is to generate general purpose human and machine-readable slugs. So -, ., _ and ~ characters are stripped from input even though they are “unreserved” characters for URLs (see RFC 3986). Having said that, any character can be used as a separator, including the ones above.

Link to this section Summary

Functions

Returns string as a slug or nil if it failed

Link to this section Functions

Link to this function slugify(string, opts \\ []) View Source
slugify(String.t, Keyword.t) :: String.t | nil

Returns string as a slug or nil if it failed.

Options

  • separator - Replace whitespaces with this string. Leading, trailing or repeated whitespaces are still trimmed. Defaults to -.
  • lowercase - Set to false if you wish to retain your uppercase letters. Defaults to true.
  • ignore - Pass in a string (or list of strings) of characters to ignore.

Examples

iex> Slug.slugify("Hello, World!")
"hello-world"

iex> Slug.slugify("Madam, I'm Adam", separator: "")
"madamimadam"

iex> Slug.slugify("StUdLy CaPs", lowercase: false)
"StUdLy-CaPs"

iex> Slug.slugify("你好,世界")
"nihaoshijie"

iex> Slug.slugify("你好,世界", ignore: ["你", "好"])
"你好shijie"