Skip to main content
slugify turns a Persian string into a URL-safe slug. Companion helpers (createSlug, slugifyWithNumbers, slugifySimple) are thin wrappers around the same engine.

Functions

slugify(text, options?)

With options

Convenience exports

What slugify does internally

  1. Validate input is a non-empty string (otherwise throws).
  2. Normalize Arabic-script characters → Persian via toPersianChars.
  3. Apply SLUG_REPLACEMENTS (e.g. آ → ا, ة → ه, strip Arabic diacritics).
  4. Apply PUNCTUATION_REPLACEMENTS (strip ؟ ، « » etc.; convert Arabic-Indic digits to English).
  5. Apply any customReplacements.
  6. Collapse whitespace to separator, optionally dedupe consecutive separators, optionally truncate to maxLength.

Pitfalls

  • Throws on empty string. Pre-check text.trim().length > 0.
  • lowercase: true affects only Latin — Persian has no case.
  • preserveNumbers: true keeps Persian digits, which browsers handle but some legacy servers may reject. For pure-ASCII URLs, set preserveNumbers: false AND digitsFaToEn upstream.
  • maxLength truncates after slug building, possibly leaving a trailing separator — trim it yourself if it matters.
  • Custom replacements run after defaults and can override punctuation handling.

Source

src/modules/slugify/index.ts · Tests: test/slugify.spec.ts