{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "persian-slug",
  "title": "Persian Slug",
  "description": "Converts Persian/mixed Persian+English text into a URL-safe slug, keeping Persian script by default or transliterating to Latin/ASCII.",
  "registryDependencies": [
    "@persianlabsui/normalize-persian-digits"
  ],
  "files": [
    {
      "path": "registry/base/lib/persian-slug.ts",
      "content": "import { normalizePersianDigits } from \"@/lib/normalize-persian-digits\"\n\nexport interface PersianSlugOptions {\n  /**\n   * Produce a fully Latin/ASCII slug by transliterating Persian letters\n   * instead of keeping the Persian script. The transliteration is a simple\n   * per-letter map (no short-vowel reconstruction), so it's a rough\n   * approximation, not a linguistically perfect romanization.\n   * @default false\n   */\n  transliterate?: boolean\n  /** The character used to join words. @default \"-\" */\n  separator?: string\n  /** Convert Persian (۰-۹) and Arabic-Indic (٠-٩) digits to plain 0-9. @default true */\n  normalizeDigits?: boolean\n}\n\nconst arabicToPersianCharacters: Record<string, string> = {\n  ي: \"ی\",\n  ك: \"ک\",\n  ة: \"ه\",\n  ؤ: \"و\",\n  إ: \"ا\",\n  أ: \"ا\",\n  ٱ: \"ا\",\n}\n\n/** Normalizes Arabic-form characters that commonly leak into Persian text (ي → ی, ك → ک, ...) to their correct Persian Unicode forms. */\nfunction normalizePersianCharacters(value: string): string {\n  return value.replace(\n    /[يكةؤإأٱ]/g,\n    (char) => arabicToPersianCharacters[char] ?? char\n  )\n}\n\nconst persianToLatinTransliterationMap: Record<string, string> = {\n  آ: \"a\",\n  ا: \"a\",\n  ب: \"b\",\n  پ: \"p\",\n  ت: \"t\",\n  ث: \"s\",\n  ج: \"j\",\n  چ: \"ch\",\n  ح: \"h\",\n  خ: \"kh\",\n  د: \"d\",\n  ذ: \"z\",\n  ر: \"r\",\n  ز: \"z\",\n  ژ: \"zh\",\n  س: \"s\",\n  ش: \"sh\",\n  ص: \"s\",\n  ض: \"z\",\n  ط: \"t\",\n  ظ: \"z\",\n  ع: \"a\",\n  غ: \"gh\",\n  ف: \"f\",\n  ق: \"gh\",\n  ک: \"k\",\n  گ: \"g\",\n  ل: \"l\",\n  م: \"m\",\n  ن: \"n\",\n  و: \"v\",\n  ه: \"h\",\n  ی: \"y\",\n  ء: \"\",\n  ئ: \"y\",\n}\n\nfunction transliteratePersian(value: string): string {\n  return value.replace(\n    /[؀-ۿ]/g,\n    (char) => persianToLatinTransliterationMap[char] ?? \"\"\n  )\n}\n\nfunction escapeForRegExp(value: string) {\n  return value.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\")\n}\n\nconst PERSIAN_LETTERS = \"آابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهی\"\nconst PERSIAN_DIGITS = \"۰-۹٠-٩\"\n\n/**\n * Converts Farsi/Persian (or mixed Persian+English) text into a URL-safe\n * slug, e.g. for blog post or product URLs.\n *\n * By default, Persian characters are kept as-is rather than transliterated\n * to Latin — percent-encoded Persian slugs are common and SEO-accepted on\n * Iranian sites, and a forced transliteration produces awkward Latin\n * approximations. Pass `{ transliterate: true }` for a fully Latin/ASCII\n * slug instead.\n *\n * Normalization applied: trims the input, corrects Arabic-form Persian\n * characters (ي → ی, ك → ک, ...), converts Persian/Arabic-Indic digits to\n * plain 0-9 (unless `normalizeDigits: false`), collapses whitespace/\n * underscore runs into a single separator, strips any character that isn't\n * a Persian letter/digit, Latin letter/digit, or the separator, collapses\n * repeated separators, trims leading/trailing separators, and lowercases\n * any Latin portion.\n *\n * @example\n * toPersianSlug(\"راهنمای خرید گوشی موبایل\")\n * // \"راهنمای-خرید-گوشی-موبایل\"\n * @example\n * toPersianSlug(\"بهترین لپ‌تاپ های Gaming 2024\")\n * // \"بهترین-لپتاپ-های-gaming-2024\"\n * @example\n * toPersianSlug(\"مقاله ۱۲۳\")\n * // \"مقاله-123\"\n * @example\n * toPersianSlug(\"  چطور   کیک، بپزیم؟!  \")\n * // \"چطور-کیک-بپزیم\"\n * @example\n * toPersianSlug(\"سلام دنیا\", { transliterate: true })\n * // \"slam-dnya\"\n */\nexport function toPersianSlug(\n  value: string,\n  options: PersianSlugOptions = {}\n): string {\n  const {\n    transliterate = false,\n    separator = \"-\",\n    normalizeDigits = true,\n  } = options\n\n  let result = normalizePersianCharacters(value.trim())\n\n  if (normalizeDigits) result = normalizePersianDigits(result)\n\n  if (transliterate) result = transliteratePersian(result)\n\n  result = result.replace(/[\\s_]+/g, separator)\n\n  const escapedSeparator = escapeForRegExp(separator)\n  const allowedCharacters = transliterate\n    ? \"a-zA-Z0-9\"\n    : `${PERSIAN_LETTERS}${PERSIAN_DIGITS}a-zA-Z0-9`\n\n  result = result\n    .replace(new RegExp(`[^${allowedCharacters}${escapedSeparator}]`, \"g\"), \"\")\n    .replace(new RegExp(`${escapedSeparator}{2,}`, \"g\"), separator)\n    .replace(new RegExp(`^${escapedSeparator}+|${escapedSeparator}+$`, \"g\"), \"\")\n\n  return transliterate\n    ? result.toLowerCase()\n    : result.replace(/[A-Z]/g, (char) => char.toLowerCase())\n}\n\n/**\n * Convenience wrapper for `toPersianSlug(value, { transliterate: true })` —\n * always produces a fully Latin/ASCII slug.\n *\n * @example\n * toLatinSlug(\"سلام دنیا\")\n * // \"slam-dnya\"\n */\nexport function toLatinSlug(\n  value: string,\n  options: Omit<PersianSlugOptions, \"transliterate\"> = {}\n): string {\n  return toPersianSlug(value, { ...options, transliterate: true })\n}\n",
      "type": "registry:lib"
    }
  ],
  "type": "registry:lib"
}