API reference

v0.0.3

These are the public modules of Mishka Slug 0.0.3: MishkaSlug, MishkaSlug.Options, MishkaSlug.Scripts, MishkaSlug.Messages, MishkaSlug.Change and MishkaSlug.Ash. Each function lists its return values. For the options, see Options.

MishkaSlug

FunctionReturns
slugify(text, opts \\ []){:ok, slug} or {:error, reason}. Use it for anything a user typed.
slugify!(text, opts \\ [])The slug, or it raises ArgumentError ("cannot make a slug: :empty"). Use it only when the text is known to have letters or digits.

Invalid options raise ArgumentError in both. A text that is not a string raises FunctionClauseError.

Error reasons

ReasonWhen
:emptyNothing is left: "", "😀🚀", "---".
:no_allowed_scriptThe text has letters or digits, but none is in a script of :locales.
:invalid_utf8The binary is not UTF-8.
:mixed_scriptsA word mixes scripts and mixed_scripts: :reject is set.
{:unknown_locale, code}A code in :locales is neither in the built-in table nor known to the resolver.

MishkaSlug.Options

FunctionReturns
validate(opts){:ok, opts} with every default filled in, or {:error, message}. A :truncate comes back as :max_length.
validate!(opts)The options, or it raises ArgumentError.

MishkaSlug.Scripts

FunctionReturns
for_locales(locales, resolver \\ nil){:ok, scripts} with :latin always added, {:ok, :any} for an empty list, or {:error, {:unknown_locale, code}}.
from_iso15924(code){:ok, [script]} for a script code ("Arab" or :Cyrl, in any case), or :error for a script the library does not know.
language(locale)The lowercased language part of a code.
split(word, allowed, mode){:ok, parts}, or {:error, :mixed_scripts} when mode is :reject and the word changes script.
elixir
MishkaSlug.Scripts.for_locales(["fa", "en"])
#=> {:ok, [:arabic, :latin]}

MishkaSlug.Scripts.for_locales([])
#=> {:ok, :any}

MishkaSlug.Scripts.for_locales(["ja"])
#=> {:ok, [:han, :hiragana, :katakana, :latin]}

MishkaSlug.Scripts.for_locales(["sr-Latn"])
#=> {:ok, [:latin]}

MishkaSlug.Scripts.for_locales(["xx"])
#=> {:error, {:unknown_locale, "xx"}}

MishkaSlug.Scripts.from_iso15924("Arab")
#=> {:ok, [:arabic]}

MishkaSlug.Scripts.from_iso15924(:Jpan)
#=> {:ok, [:han, :hiragana, :katakana]}

MishkaSlug.Scripts.from_iso15924("Tibt")
#=> :error

MishkaSlug.Scripts.language("pt-BR")
#=> "pt"

MishkaSlug.Scripts.split("pаypal", [:latin], :split)
#=> {:ok, ["p", "ypal"]}

The script atoms are :latin, :arabic, :hebrew, :cyrillic, :greek, :armenian, :georgian, :han, :hiragana, :katakana, :hangul, :bopomofo, :devanagari, :bengali, :gujarati, :gurmukhi, :kannada, :malayalam, :sinhala, :tamil, :telugu, :thai, :lao, :khmer, :myanmar and :ethiopic.

MishkaSlug.Messages

FunctionReturns
error(reason)The translated message for a reason of slugify/2.
not_a_string()The message for a value that is not a string: "is not a string value".
gettext_backend()The Gettext backend in use: the :gettext_backend of the :mishka_slug config, or MishkaSlug.Gettext.

See Handle errors and translate messages for the texts.

MishkaSlug.Change

An Ash change that writes a slug. It is compiled only when the project has Ash. It accepts every option of slugify/2, except that :language and :locales take more here. init/1 returns an error for an unknown option or an invalid value, so a typo fails when the resource compiles.

OptionDefaultMeaning
:fromrequiredThe attribute or argument the slug is made from.
:intothe :from valueThe attribute that gets the slug.
:presetnilA name in config :mishka_slug, :presets, or {module, function, args} that returns the options. Options next to it win. An unknown name, or a preset that is not a keyword list, is an init/1 error that names it.
:unique_suffixfalsetrue saves a slug that a unique identity refused as slug-2, slug-3 and so on.
:max_suffix10The highest suffix :unique_suffix tries. When every one is taken, the error of the first save is returned.
:skip_if_present?falsetrue leaves the slug alone when the action already sets :into.
:languagenilAn attribute or argument name read from the changeset, or a fixed code such as "fa".
:locales[]A list of codes, or {module, function, args} called as module.function(changeset, ...args).

What the change does:

  • It reads :from from the action's arguments or changes. When it is not there, the change does nothing, so an update that does not touch the title keeps its slug.
  • A value that is not a string, or a title that gives no slug, is a changeset error on :from. The slug attribute is never set to nil or an empty string.
  • Error texts go through Gettext in the locale of the calling process.
  • When you pass neither :max_length nor :truncate, the max_length constraint of the :into attribute is used.
  • A ci_string attribute works as :from.
  • It supports atomic updates when :from is a plain value. An expression is :not_atomic.
  • unique_suffix: true retries through Ash.Changeset.around_transaction/2. An update action that uses it needs require_atomic? false, and it does nothing in bulk actions.
FunctionReturns
slug_options(opts, changeset)The slugify/2 options the change would use: the preset merged in, language and locales resolved against the changeset, and :max_length filled from the attribute.

MishkaSlug.Ash

An Ash extension that brings slugify/2 into the actions of a resource. It is compiled only when the project has Ash. It adds no section of its own to the resource.

elixir
use Ash.Resource, extensions: [MishkaSlug.Ash]

change slugify(:title, into: :slug, language: :language)
FunctionReturns
MishkaSlug.Ash.Changes.slugify(from, opts \\ []){MishkaSlug.Change, [from: from] ++ opts}. from is an atom and opts a keyword list of the options of MishkaSlug.Change. The extension imports it, so change slugify(:title, into: :slug) is the same change as change {MishkaSlug.Change, from: :title, into: :slug}.

See Write slugs with the Ash extension for presets, the suffix and moving from ash_slug.