Control accents, separators and length

v0.0.3

The defaults of MishkaSlug.slugify/2 match WordPress, so a migrated slug stays the same. This guide changes them one at a time: Latin accents, the characters kept inside a word, digits, the Persian half-space, words that mix scripts, the separator, the case of the letters and the length of the slug.

Before you begin

Mishka Slug installed. See Get Started. Every option is optional, and an unknown option or a value outside the accepted ones raises ArgumentError.

Keep or remove accents

By default (latin: :ascii) Latin letters lose their accents. latin: :keep leaves them as typed. Only Latin changes: Greek, Cyrillic, Arabic and every other script keep their letters and marks.

elixir
MishkaSlug.slugify("Café Crème")
#=> {:ok, "cafe-creme"}

MishkaSlug.slugify("Café Crème", latin: :keep)
#=> {:ok, "café-crème"}

MishkaSlug.slugify("Straße")
#=> {:ok, "strase"}

MishkaSlug.slugify("Straße", language: "de")
#=> {:ok, "strasse"}

Keep underscores, dots and tildes

The keep: option lists the characters kept inside a word. Only ".", "_" and "~" are accepted, and the list replaces the default ["_"].

elixir
MishkaSlug.slugify("snake_case_name")
#=> {:ok, "snake_case_name"}

MishkaSlug.slugify("snake_case_name", keep: [])
#=> {:ok, "snake-case-name"}

MishkaSlug.slugify("a_b.c")
#=> {:ok, "a_b-c"}

MishkaSlug.slugify("a_b.c", keep: [".", "_"])
#=> {:ok, "a_b.c"}

MishkaSlug.slugify("Release v1.2", keep: ["."])
#=> {:ok, "release-v1.2"}

By default a kept character at the start or end of a word stays, as WordPress leaves it. Set trim_kept: true to remove it.

elixir
MishkaSlug.slugify("__init__ my_post_")
#=> {:ok, "__init__-my_post_"}

MishkaSlug.slugify("__init__ my_post_", trim_kept: true)
#=> {:ok, "init-my_post"}

Other characters always separate words. & is dropped, an apostrophe inside a word is removed, and a comma is a separator.

elixir
MishkaSlug.slugify("Q&A")
#=> {:ok, "q-a"}

MishkaSlug.slugify("don't")
#=> {:ok, "dont"}

MishkaSlug.slugify("1,000")
#=> {:ok, "1-000"}

Write digits as 0-9

digits: :keep leaves digits as typed. digits: :ascii writes every decimal digit as 0-9.

elixir
MishkaSlug.slugify("سنة ٢٠٢٥ كتاب", digits: :ascii)
#=> {:ok, "سنة-2025-كتاب"}

Glue or split the Persian half-space

The half-space (U+200C) joins the parts of a Persian word. zwnj: :remove, the default, glues them. zwnj: :separator turns it into a word break.

elixir
MishkaSlug.slugify("قیمت‌گذاری نرم‌افزار")
#=> {:ok, "قیمتگذاری-نرمافزار"}

MishkaSlug.slugify("قیمت‌گذاری نرم‌افزار", zwnj: :separator)
#=> {:ok, "قیمت-گذاری-نرم-افزار"}

Refuse words that mix scripts

A word never mixes scripts in a slug. By default (mixed_scripts: :split) the word is cut where the script changes. Set mixed_scripts: :reject to refuse the title instead. The a in these examples is the Cyrillic letter U+0430, a look-alike of the Latin a.

elixir
MishkaSlug.slugify("pаypal")
#=> {:ok, "p-а-ypal"}

MishkaSlug.slugify("pаypal", mixed_scripts: :reject)
#=> {:error, :mixed_scripts}

Change the separator

Words are joined by -. separator: changes the joiner, in every script. It takes "-", ".", "_" and "~", alone or repeated, such as "--". Any other character raises ArgumentError, so the slug stays URL-safe.

elixir
MishkaSlug.slugify("Hello World: Getting Started with Elixir!", separator: "_")
#=> {:ok, "hello_world_getting_started_with_elixir"}

MishkaSlug.slugify("سلام دنیا", separator: "_")
#=> {:ok, "سلام_دنیا"}

MishkaSlug.slugify("Hello World", separator: "--")
#=> {:ok, "hello--world"}

A character of the separator is always a word break, even when keep: lists it. That keeps a slug stable: slugging it again gives the same slug.

elixir
MishkaSlug.slugify("snake_case title", separator: "_")
#=> {:ok, "snake_case_title"}

MishkaSlug.slugify("__init__", separator: "_")
#=> {:ok, "init"}

With zwnj: :separator, the Persian half-space gets the separator too.

elixir
MishkaSlug.slugify("قیمت‌گذاری نرم‌افزار", separator: "_", zwnj: :separator)
#=> {:ok, "قیمت_گذاری_نرم_افزار"}

Keep the case of the letters

Slugs are lowercase by default. lowercase?: false keeps the case of the letters as typed. Scripts without case, such as Persian, Hindi and Japanese, are the same either way.

elixir
MishkaSlug.slugify("Hello World", lowercase?: false)
#=> {:ok, "Hello-World"}

MishkaSlug.slugify("Привет Мир", lowercase?: false)
#=> {:ok, "Привет-Мир"}

With the default latin: :ascii, a Latin letter that is spelled out keeps its case too.

elixir
MishkaSlug.slugify("Ærø Ångström", lowercase?: false)
#=> {:ok, "Aero-Angstrom"}

Limit the length

max_length: is the longest slug, counted in code points, separators included. Whole words are kept while they fit. A first word that is too long is cut between two graphemes.

elixir
MishkaSlug.slugify("hello world foo", max_length: 13)
#=> {:ok, "hello-world"}

MishkaSlug.slugify("supercalifragilistic", max_length: 5)
#=> {:ok, "super"}

MishkaSlug.slugify("hello world foo", max_length: 12, separator: "--")
#=> {:ok, "hello--world"}

truncate: is the same option under the name ash_slug uses, with the same cut. Give one of the two. Both together raise ArgumentError.

elixir
MishkaSlug.slugify("hello world foo", truncate: 13)
#=> {:ok, "hello-world"}

Next steps