PhoenixKit.Modules.Publishing.SlugHelpers (PhoenixKitPublishing v0.1.15)

Copy Markdown View Source

Slug validation and generation for the Publishing module.

Handles slug format validation, uniqueness checking (DB-only), URL slug validation for per-language slugs, and slug generation.

Summary

Functions

Clears custom url_slugs that conflict with a given post slug.

Clears a specific url_slug from all translations of a single post (DB-only).

Generates a unique slug based on title and optional preferred slug.

Returns the HTML5 pattern attribute source matching the active slug style.

Returns true when the slug matches the active style's shape pattern.

Checks if a slug already exists within the given publishing group (DB-only).

Returns the configured slug style as an atom.

Converts text into a slug honoring the configured (or given) style.

Validates whether the given string is a slug and not a reserved language code.

Validates whether the given string is a valid slug format and not a reserved language code.

Validates a per-language URL slug for uniqueness within a group+language combination.

Functions

clear_conflicting_url_slugs(group_slug, post_slug)

@spec clear_conflicting_url_slugs(String.t(), String.t()) :: [
  {String.t(), String.t()}
]

Clears custom url_slugs that conflict with a given post slug.

clear_url_slug_from_post(group_slug, post_slug, url_slug_to_clear)

@spec clear_url_slug_from_post(String.t(), String.t(), String.t()) :: [String.t()]

Clears a specific url_slug from all translations of a single post (DB-only).

generate_unique_slug(group_slug, title, preferred_slug \\ nil, opts \\ [])

@spec generate_unique_slug(String.t(), String.t(), String.t() | nil, keyword()) ::
  {:ok, String.t()} | {:error, :invalid_format | :reserved_language_code}

Generates a unique slug based on title and optional preferred slug.

html_input_pattern()

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

Returns the HTML5 pattern attribute source matching the active slug style.

Keeps the editor's url_slug input client-side validation in sync with the server-side validate_url_slug/4 — ASCII for :transliterate/:ascii, Unicode letters/numbers for :unicode. Without this the input would reject a valid Unicode slug the server accepts.

matches_shape?(slug)

@spec matches_shape?(any()) :: boolean()

Returns true when the slug matches the active style's shape pattern.

Style-aware companion for group-slug validation in the facade.

slug_exists?(group_slug, post_slug)

@spec slug_exists?(String.t(), String.t()) :: boolean()

Checks if a slug already exists within the given publishing group (DB-only).

slug_style()

@spec slug_style() :: :transliterate | :unicode | :ascii

Returns the configured slug style as an atom.

:transliterate (default) | :unicode | :ascii. Controlled by the publishing_slug_style setting; falls back to :transliterate on any error.

slugify(text, opts \\ [])

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

Converts text into a slug honoring the configured (or given) style.

  • :transliterate — map Cyrillic + strip Latin diacritics to ASCII, then [a-z0-9-]. Default; keeps URLs ASCII and validation simple.
  • :unicode — keep letters/numbers from any script (e.g. привет-мир).
  • :ascii — legacy behavior: strip everything non-ASCII.

Pass style: :unicode to override the setting (used by tests).

valid_slug?(slug)

@spec valid_slug?(String.t()) :: boolean()

Validates whether the given string is a slug and not a reserved language code.

validate_slug(slug)

@spec validate_slug(String.t()) ::
  {:ok, String.t()} | {:error, :invalid_format | :reserved_language_code}

Validates whether the given string is a valid slug format and not a reserved language code.

validate_url_slug(group_slug, url_slug, language, exclude_post_slug \\ nil)

@spec validate_url_slug(String.t(), String.t(), String.t(), String.t() | nil) ::
  {:ok, String.t()} | {:error, atom()}

Validates a per-language URL slug for uniqueness within a group+language combination.