Options

v0.0.1

These are the options of MishkaSlug.slugify/2 and MishkaSlug.slugify!/2. MishkaSlug.Change accepts all of them. MishkaSlug.Options, a guarded_struct, checks them, fills the defaults and raises ArgumentError for an unknown option or a value outside the accepted ones. All options are optional, and the defaults match WordPress.

Options

OptionTypeDefaultWhat it does
:languageString.t() | nilnilThe language of the text, such as "fa" or "tr". tr and az lowercase I to ı. fa, ur and ckb fold the Arabic ي ى ك into the Persian ی ک. de, da, ca, sr and bs spell Latin letters their own way with latin: :ascii.
:locales[String.t()][]The site's languages, such as ["fa", "en"]. Only the letters and digits of their scripts, and Latin, are kept. [] keeps every script. A script subtag picks the script ("sr-Latn" is Latin). Without a :language, a Persian-family locale without ar turns on the Persian fold. An unknown code gives {:error, {:unknown_locale, code}}.
:script_resolver{module, function, args} | nilnilDecides the scripts of each :locales code instead of the built-in table. nil uses the :script_resolver of the :mishka_slug application config, and without one the built-in table.
:mixed_scripts:split | :reject:splitWhat happens to a word that changes script. :split cuts it at the change. :reject returns {:error, :mixed_scripts}.
:zwnj:remove | :separator:removeThe Persian half-space (U+200C). :remove glues the parts. :separator makes it a word break.
:digits:keep | :ascii:keep:keep leaves digits as typed. :ascii writes every decimal digit as 0-9. With :keep, a digit of a script outside :locales is dropped like a letter. :ascii writes it as 0-9 first, so it stays.
:latin:ascii | :keep:asciiWhat happens to accented Latin letters. :ascii removes the accents like WordPress. :keep leaves them as typed. Other scripts never change.
:keeplist of ".", "_", "~"["_"]Characters kept inside a word. The list replaces the default: [] makes _ a break, and [".", "_"] keeps both.
:trim_keptboolean()falseWhether kept characters at the start or end of a word are removed. false leaves them like WordPress. true trims them. An edge - is always removed.
:max_lengthpos_integer() | nilnilThe longest slug, in code points. Whole words are kept while they fit. A first word that is too long is cut between two graphemes.

Check options without a call

MishkaSlug.Options.validate/1 checks a keyword list and returns it with every default filled in, or an error message that names the options that failed. validate!/1 returns the options or raises ArgumentError.

elixir
MishkaSlug.Options.validate(language: "fa")
#=> {:ok,
#    [
#      keep: ["_"],
#      max_length: nil,
#      latin: :ascii,
#      language: "fa",
#      trim_kept: false,
#      locales: [],
#      zwnj: :remove,
#      digits: :keep,
#      mixed_scripts: :split,
#      script_resolver: nil
#    ]}

MishkaSlug.Options.validate(foo: 1)
#=> {:error, "invalid options: foo: Unauthorized keys are present in the sent data."}