PhoenixKit.Utils.Slug (phoenix_kit v1.7.235)

Copy Markdown View Source

Helpers for generating consistent, URL-friendly slugs across PhoenixKit.

Provides functions to slugify arbitrary text, enforce separator rules, and ensure uniqueness by delegating existence checks via callback.

Summary

Functions

Ensures the provided slug is unique by calling exists_fun.

Converts the given text into a slug.

Maps lowercase Cyrillic characters to Latin and strips Latin diacritics.

Functions

ensure_unique(slug, exists_fun)

@spec ensure_unique(String.t(), (String.t() -> boolean())) :: String.t()

Ensures the provided slug is unique by calling exists_fun.

exists_fun should return truthy when the slug is already taken.

slugify(text, opts \\ [])

@spec slugify(
  String.t() | nil,
  keyword()
) :: String.t()

Converts the given text into a slug.

Options:

  • :separator - character used between words (defaults to "-")
  • :transliterate - map Cyrillic to Latin and strip Latin diacritics before the ASCII pass (defaults to false)

Returns an empty string when the input is blank or cannot be converted.

transliterate(text)

@spec transliterate(String.t()) :: String.t()

Maps lowercase Cyrillic characters to Latin and strips Latin diacritics.

Anything outside those scripts is returned unchanged — the caller's slug pass still has to remove it.