@intlayer/core 8.1.8 → 8.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,6 +7,7 @@
7
7
  <h1 align="center">
8
8
  <strong>Per-component i18n</strong>
9
9
  </h1>
10
+
10
11
  <h2 align="center">
11
12
  <strong>AI-powered translation. Visual Editor. Multilingual CMS.</strong>
12
13
  </h2>
@@ -1 +1 @@
1
- {"version":3,"file":"rewriteUtils.cjs","names":[],"sources":["../../../src/localization/rewriteUtils.ts"],"sourcesContent":["import type {\n Locale,\n RewriteObject,\n RewriteRules,\n RoutingConfig,\n} from '@intlayer/types';\n\nexport type LocalizedPathResult = {\n path: string;\n isRewritten: boolean;\n};\n\n/**\n * Normalizes legacy Record format or extracts specialized rules from RewriteObject.\n */\nexport const getRewriteRules = (\n rewrite: RoutingConfig['rewrite'],\n context: keyof RewriteObject = 'url'\n): RewriteRules | undefined => {\n if (!rewrite) return undefined;\n\n if ('url' in rewrite) {\n return (rewrite as RewriteObject)[context];\n }\n\n // Normalize legacy format\n return {\n rules: Object.entries(rewrite).map(([canonical, localized]) => ({\n // Normalize canonical path\n canonical: canonical.startsWith('/')\n ? canonical.replace(/\\[([^\\]]+)\\]/g, ':$1')\n : `/${canonical.replace(/\\[([^\\]]+)\\]/g, ':$1')}`,\n\n // Normalize localized path\n localized: Object.fromEntries(\n Object.entries(localized).map(([locale, pattern]) => {\n const normalizedPattern = pattern?.startsWith('/')\n ? pattern.replace(/\\[([^\\]]+)\\]/g, ':$1')\n : `/${(pattern || '').replace(/\\[([^\\]]+)\\]/g, ':$1')}`;\n return [locale, normalizedPattern];\n })\n ),\n })),\n };\n};\n\n/**\n * Converts normalized pattern to Regex.\n * Internal syntax supports:\n * - :param -> ([^/]+) (one segment)\n * - :param* -> (.*) (zero or more segments)\n * - :param+ -> (.+) (one or more segments)\n * - :param? -> ([^/]*) (zero or one segment)\n */\nconst patternToRegex = (pattern: string) => {\n const regexString = pattern\n .replace(/\\//g, '\\\\/') // Escape slashes\n .replace(/\\\\\\/:(?:[^/\\\\*+?]+)\\*/g, '(?:\\\\/(.*))?') // /:param*\n .replace(/\\\\\\/:(?:[^/\\\\*+?]+)\\?/g, '(?:\\\\/([^\\\\/]+))?') // /:param?\n .replace(/:([^/\\\\*+?]+)\\*/g, '(.*)') // :param* (if no leading slash)\n .replace(/:([^/\\\\*+?]+)\\?/g, '([^\\\\/]*)') // :param? (if no leading slash)\n .replace(/:([^/\\\\*+?]+)\\+/g, '(.+)') // :param+\n .replace(/:([^/\\\\*+?]+)/g, '([^\\\\/]+)'); // :param\n\n return new RegExp(`^${regexString}$`);\n};\n\n/**\n * Replaces route parameters in a path with provided values.\n */\nconst fillPath = (pattern: string, params: string[]) => {\n let index = 0;\n return (\n pattern\n .replace(/:([^/\\\\*+?]+)[*+?]?/g, () => params[index++] ?? '')\n .replace(/\\/+/g, '/')\n .replace(/\\/$/, '') || '/'\n );\n};\n\n/**\n * Extract values from a URL based on a pattern.\n */\nconst extractParams = (url: string, pattern: string): string[] | null => {\n const regex = patternToRegex(pattern);\n const match = url.match(regex);\n return match ? match.slice(1) : null;\n};\n\n/**\n * Given a localized URL (e.g., \"/produits/123\"), finds the canonical internal path (e.g., \"/products/123\").\n * If locale is provided, only check for that locale. Otherwise, check for all locales.\n */\nexport const getCanonicalPath = (\n localizedPath: string,\n locale?: Locale,\n rewriteRules?: RewriteRules\n): string => {\n if (!rewriteRules) return localizedPath;\n\n for (const rule of rewriteRules.rules) {\n const { canonical, localized } = rule;\n const localesToCheck = locale ? [locale] : Object.keys(localized);\n\n for (const loc of localesToCheck) {\n const localizedPattern = localized[loc as keyof typeof localized];\n\n if (!localizedPattern) continue;\n\n const params = extractParams(localizedPath, localizedPattern);\n\n if (params) {\n return fillPath(canonical, params);\n }\n }\n }\n\n return localizedPath;\n};\n\n/**\n * Given a canonical path (e.g., \"/products/123\"), finds the localized URL pattern (e.g., \"/produits/123\").\n */\nexport const getLocalizedPath = (\n canonicalPath: string,\n locale: Locale,\n rewriteRules?: RewriteRules\n): LocalizedPathResult => {\n if (!rewriteRules) return { path: canonicalPath, isRewritten: false };\n\n for (const rule of rewriteRules.rules) {\n const { canonical, localized } = rule;\n\n // Check if the input path matches a configured canonical pattern\n const params = extractParams(canonicalPath, canonical);\n\n if (params) {\n const targetPattern = localized[locale as keyof typeof localized];\n\n if (targetPattern) {\n return {\n path: fillPath(targetPattern, params),\n isRewritten: true,\n };\n }\n }\n }\n\n return { path: canonicalPath, isRewritten: false };\n};\n\n/**\n * Returns the internal path for a given canonical path and locale.\n * Ensures the locale prefix is present exactly once.\n */\nexport const getInternalPath = (\n canonicalPath: string,\n locale: Locale\n): string => {\n const pathWithLeadingSlash = canonicalPath.startsWith('/')\n ? canonicalPath\n : `/${canonicalPath}`;\n\n if (\n pathWithLeadingSlash.startsWith(`/${locale}/`) ||\n pathWithLeadingSlash === `/${locale}`\n ) {\n return pathWithLeadingSlash;\n }\n\n return `/${locale}${pathWithLeadingSlash === '/' ? '' : pathWithLeadingSlash}`;\n};\n\n/**\n * Given a current pathname and locale, returns the pretty localized path if a rewrite rule exists and the path is not already localized.\n */\nexport const getRewritePath = (\n pathname: string,\n locale: Locale,\n rewrite?: RoutingConfig['rewrite']\n): string | undefined => {\n const rules = getRewriteRules(rewrite, 'url');\n if (!rules) return undefined;\n\n // Identify canonical path (relative to root, no locale prefix expected in 'url' context)\n const canonicalPath = getCanonicalPath(pathname, undefined, rules);\n\n // Get the localized path for the current locale\n const { path: localizedPath, isRewritten } = getLocalizedPath(\n canonicalPath,\n locale,\n rules\n );\n\n if (isRewritten && localizedPath !== pathname) {\n return localizedPath;\n }\n\n return undefined;\n};\n"],"mappings":"mEAeA,MAAa,GACX,EACA,EAA+B,QACF,CACxB,KAOL,MALI,QAAS,EACH,EAA0B,GAI7B,CACL,MAAO,OAAO,QAAQ,EAAQ,CAAC,KAAK,CAAC,EAAW,MAAgB,CAE9D,UAAW,EA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O,GAMI,GACX,EACA,EACA,IACwB,CACxB,GAAI,CAAC,EAAc,MAAO,CAAE,KAAM,EAAe,YAAa,GAAO,CAErE,IAAK,IAAM,KAAQ,EAAa,MAAO,CACrC,GAAM,CAAE,YAAW,aAAc,EAG3B,EAAS,EAAc,EAAe,EAAU,CAEtD,GAAI,EAAQ,CACV,IAAM,EAAgB,EAAU,GAEhC,GAAI,EACF,MAAO,CACL,KAAM,EAAS,EAAe,EAAO,CACrC,YAAa,GACd,EAKP,MAAO,CAAE,KAAM,EAAe,YAAa,GAAO,EAOvC,GACX,EACA,IACW,CACX,IAAM,EAAuB,EAAc,WAAW,IAAI,CACtD,EACA,IAAI,IASR,OANE,EAAqB,WAAW,IAAI,EAAO,GAAG,EAC9C,IAAyB,IAAI,IAEtB,EAGF,IAAI,IAAS,IAAyB,IAAM,GAAK,KAM7C,GACX,EACA,EACA,IACuB,CACvB,IAAM,EAAQ,EAAgB,EAAS,MAAM,CAC7C,GAAI,CAAC,EAAO,OAMZ,GAAM,CAAE,KAAM,EAAe,eAAgB,EAHvB,EAAiB,EAAU,IAAA,GAAW,EAAM,CAKhE,EACA,EACD,CAED,GAAI,GAAe,IAAkB,EACnC,OAAO"}
1
+ {"version":3,"file":"rewriteUtils.cjs","names":[],"sources":["../../../src/localization/rewriteUtils.ts"],"sourcesContent":["import type {\n Locale,\n LocalesValues,\n RewriteObject,\n RewriteRules,\n RoutingConfig,\n} from '@intlayer/types';\n\nexport type LocalizedPathResult = {\n path: string;\n isRewritten: boolean;\n};\n\n/**\n * Normalizes legacy Record format or extracts specialized rules from RewriteObject.\n */\nexport const getRewriteRules = (\n rewrite: RoutingConfig['rewrite'],\n context: keyof RewriteObject = 'url'\n): RewriteRules | undefined => {\n if (!rewrite) return undefined;\n\n if ('url' in rewrite) {\n return (rewrite as RewriteObject)[context];\n }\n\n // Normalize legacy format\n return {\n rules: Object.entries(rewrite).map(([canonical, localized]) => ({\n // Normalize canonical path\n canonical: canonical.startsWith('/')\n ? canonical.replace(/\\[([^\\]]+)\\]/g, ':$1')\n : `/${canonical.replace(/\\[([^\\]]+)\\]/g, ':$1')}`,\n\n // Normalize localized path\n localized: Object.fromEntries(\n Object.entries(localized).map(([locale, pattern]) => {\n const normalizedPattern = pattern?.startsWith('/')\n ? pattern.replace(/\\[([^\\]]+)\\]/g, ':$1')\n : `/${(pattern || '').replace(/\\[([^\\]]+)\\]/g, ':$1')}`;\n return [locale, normalizedPattern];\n })\n ),\n })),\n };\n};\n\n/**\n * Converts normalized pattern to Regex.\n * Internal syntax supports:\n * - :param -> ([^/]+) (one segment)\n * - :param* -> (.*) (zero or more segments)\n * - :param+ -> (.+) (one or more segments)\n * - :param? -> ([^/]*) (zero or one segment)\n */\nconst patternToRegex = (pattern: string) => {\n const regexString = pattern\n .replace(/\\//g, '\\\\/') // Escape slashes\n .replace(/\\\\\\/:(?:[^/\\\\*+?]+)\\*/g, '(?:\\\\/(.*))?') // /:param*\n .replace(/\\\\\\/:(?:[^/\\\\*+?]+)\\?/g, '(?:\\\\/([^\\\\/]+))?') // /:param?\n .replace(/:([^/\\\\*+?]+)\\*/g, '(.*)') // :param* (if no leading slash)\n .replace(/:([^/\\\\*+?]+)\\?/g, '([^\\\\/]*)') // :param? (if no leading slash)\n .replace(/:([^/\\\\*+?]+)\\+/g, '(.+)') // :param+\n .replace(/:([^/\\\\*+?]+)/g, '([^\\\\/]+)'); // :param\n\n return new RegExp(`^${regexString}$`);\n};\n\n/**\n * Replaces route parameters in a path with provided values.\n */\nconst fillPath = (pattern: string, params: string[]) => {\n let index = 0;\n return (\n pattern\n .replace(/:([^/\\\\*+?]+)[*+?]?/g, () => params[index++] ?? '')\n .replace(/\\/+/g, '/')\n .replace(/\\/$/, '') || '/'\n );\n};\n\n/**\n * Extract values from a URL based on a pattern.\n */\nconst extractParams = (url: string, pattern: string): string[] | null => {\n const regex = patternToRegex(pattern);\n const match = url.match(regex);\n return match ? match.slice(1) : null;\n};\n\n/**\n * Given a localized URL (e.g., \"/produits/123\"), finds the canonical internal path (e.g., \"/products/123\").\n * If locale is provided, only check for that locale. Otherwise, check for all locales.\n */\nexport const getCanonicalPath = (\n localizedPath: string,\n locale?: Locale,\n rewriteRules?: RewriteRules\n): string => {\n if (!rewriteRules) return localizedPath;\n\n for (const rule of rewriteRules.rules) {\n const { canonical, localized } = rule;\n const localesToCheck = locale ? [locale] : Object.keys(localized);\n\n for (const loc of localesToCheck) {\n const localizedPattern = localized[loc as keyof typeof localized];\n\n if (!localizedPattern) continue;\n\n const params = extractParams(localizedPath, localizedPattern);\n\n if (params) {\n return fillPath(canonical, params);\n }\n }\n }\n\n return localizedPath;\n};\n\n/**\n * Given a canonical path (e.g., \"/products/123\"), finds the localized URL pattern (e.g., \"/produits/123\").\n */\nexport const getLocalizedPath = (\n canonicalPath: string,\n locale: LocalesValues,\n rewriteRules?: RewriteRules\n): LocalizedPathResult => {\n if (!rewriteRules) return { path: canonicalPath, isRewritten: false };\n\n for (const rule of rewriteRules.rules) {\n const { canonical, localized } = rule;\n\n // Check if the input path matches a configured canonical pattern\n const params = extractParams(canonicalPath, canonical);\n\n if (params) {\n const targetPattern = localized[locale as keyof typeof localized];\n\n if (targetPattern) {\n return {\n path: fillPath(targetPattern, params),\n isRewritten: true,\n };\n }\n }\n }\n\n return { path: canonicalPath, isRewritten: false };\n};\n\n/**\n * Returns the internal path for a given canonical path and locale.\n * Ensures the locale prefix is present exactly once.\n */\nexport const getInternalPath = (\n canonicalPath: string,\n locale: Locale\n): string => {\n const pathWithLeadingSlash = canonicalPath.startsWith('/')\n ? canonicalPath\n : `/${canonicalPath}`;\n\n if (\n pathWithLeadingSlash.startsWith(`/${locale}/`) ||\n pathWithLeadingSlash === `/${locale}`\n ) {\n return pathWithLeadingSlash;\n }\n\n return `/${locale}${pathWithLeadingSlash === '/' ? '' : pathWithLeadingSlash}`;\n};\n\n/**\n * Given a current pathname and locale, returns the pretty localized path if a rewrite rule exists and the path is not already localized.\n */\nexport const getRewritePath = (\n pathname: string,\n locale: Locale,\n rewrite?: RoutingConfig['rewrite']\n): string | undefined => {\n const rules = getRewriteRules(rewrite, 'url');\n if (!rules) return undefined;\n\n // Identify canonical path (relative to root, no locale prefix expected in 'url' context)\n const canonicalPath = getCanonicalPath(pathname, undefined, rules);\n\n // Get the localized path for the current locale\n const { path: localizedPath, isRewritten } = getLocalizedPath(\n canonicalPath,\n locale,\n rules\n );\n\n if (isRewritten && localizedPath !== pathname) {\n return localizedPath;\n }\n\n return undefined;\n};\n"],"mappings":"mEAgBA,MAAa,GACX,EACA,EAA+B,QACF,CACxB,KAOL,MALI,QAAS,EACH,EAA0B,GAI7B,CACL,MAAO,OAAO,QAAQ,EAAQ,CAAC,KAAK,CAAC,EAAW,MAAgB,CAE9D,UAAW,EAAU,WAAW,IAAI,CAChC,EAAU,QAAQ,gBAAiB,MAAM,CACzC,IAAI,EAAU,QAAQ,gBAAiB,MAAM,GAGjD,UAAW,OAAO,YAChB,OAAO,QAAQ,EAAU,CAAC,KAAK,CAAC,EAAQ,KAI/B,CAAC,EAHkB,GAAS,WAAW,IAAI,CAC9C,EAAQ,QAAQ,gBAAiB,MAAM,CACvC,KAAK,GAAW,IAAI,QAAQ,gBAAiB,MAAM,GACrB,CAClC,CACH,CACF,EAAE,CACJ,EAWG,EAAkB,GAAoB,CAC1C,IAAM,EAAc,EACjB,QAAQ,MAAO,MAAM,CACrB,QAAQ,yBAA0B,eAAe,CACjD,QAAQ,yBAA0B,oBAAoB,CACtD,QAAQ,mBAAoB,OAAO,CACnC,QAAQ,mBAAoB,YAAY,CACxC,QAAQ,mBAAoB,OAAO,CACnC,QAAQ,iBAAkB,YAAY,CAEzC,OAAW,OAAO,IAAI,EAAY,GAAG,EAMjC,GAAY,EAAiB,IAAqB,CACtD,IAAI,EAAQ,EACZ,OACE,EACG,QAAQ,2BAA8B,EAAO,MAAY,GAAG,CAC5D,QAAQ,OAAQ,IAAI,CACpB,QAAQ,MAAO,GAAG,EAAI,KAOvB,GAAiB,EAA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1 @@
1
- {"version":3,"file":"rewriteUtils.mjs","names":[],"sources":["../../../src/localization/rewriteUtils.ts"],"sourcesContent":["import type {\n Locale,\n RewriteObject,\n RewriteRules,\n RoutingConfig,\n} from '@intlayer/types';\n\nexport type LocalizedPathResult = {\n path: string;\n isRewritten: boolean;\n};\n\n/**\n * Normalizes legacy Record format or extracts specialized rules from RewriteObject.\n */\nexport const getRewriteRules = (\n rewrite: RoutingConfig['rewrite'],\n context: keyof RewriteObject = 'url'\n): RewriteRules | undefined => {\n if (!rewrite) return undefined;\n\n if ('url' in rewrite) {\n return (rewrite as RewriteObject)[context];\n }\n\n // Normalize legacy format\n return {\n rules: Object.entries(rewrite).map(([canonical, localized]) => ({\n // Normalize canonical path\n canonical: canonical.startsWith('/')\n ? canonical.replace(/\\[([^\\]]+)\\]/g, ':$1')\n : `/${canonical.replace(/\\[([^\\]]+)\\]/g, ':$1')}`,\n\n // Normalize localized path\n localized: Object.fromEntries(\n Object.entries(localized).map(([locale, pattern]) => {\n const normalizedPattern = pattern?.startsWith('/')\n ? pattern.replace(/\\[([^\\]]+)\\]/g, ':$1')\n : `/${(pattern || '').replace(/\\[([^\\]]+)\\]/g, ':$1')}`;\n return [locale, normalizedPattern];\n })\n ),\n })),\n };\n};\n\n/**\n * Converts normalized pattern to Regex.\n * Internal syntax supports:\n * - :param -> ([^/]+) (one segment)\n * - :param* -> (.*) (zero or more segments)\n * - :param+ -> (.+) (one or more segments)\n * - :param? -> ([^/]*) (zero or one segment)\n */\nconst patternToRegex = (pattern: string) => {\n const regexString = pattern\n .replace(/\\//g, '\\\\/') // Escape slashes\n .replace(/\\\\\\/:(?:[^/\\\\*+?]+)\\*/g, '(?:\\\\/(.*))?') // /:param*\n .replace(/\\\\\\/:(?:[^/\\\\*+?]+)\\?/g, '(?:\\\\/([^\\\\/]+))?') // /:param?\n .replace(/:([^/\\\\*+?]+)\\*/g, '(.*)') // :param* (if no leading slash)\n .replace(/:([^/\\\\*+?]+)\\?/g, '([^\\\\/]*)') // :param? (if no leading slash)\n .replace(/:([^/\\\\*+?]+)\\+/g, '(.+)') // :param+\n .replace(/:([^/\\\\*+?]+)/g, '([^\\\\/]+)'); // :param\n\n return new RegExp(`^${regexString}$`);\n};\n\n/**\n * Replaces route parameters in a path with provided values.\n */\nconst fillPath = (pattern: string, params: string[]) => {\n let index = 0;\n return (\n pattern\n .replace(/:([^/\\\\*+?]+)[*+?]?/g, () => params[index++] ?? '')\n .replace(/\\/+/g, '/')\n .replace(/\\/$/, '') || '/'\n );\n};\n\n/**\n * Extract values from a URL based on a pattern.\n */\nconst extractParams = (url: string, pattern: string): string[] | null => {\n const regex = patternToRegex(pattern);\n const match = url.match(regex);\n return match ? match.slice(1) : null;\n};\n\n/**\n * Given a localized URL (e.g., \"/produits/123\"), finds the canonical internal path (e.g., \"/products/123\").\n * If locale is provided, only check for that locale. Otherwise, check for all locales.\n */\nexport const getCanonicalPath = (\n localizedPath: string,\n locale?: Locale,\n rewriteRules?: RewriteRules\n): string => {\n if (!rewriteRules) return localizedPath;\n\n for (const rule of rewriteRules.rules) {\n const { canonical, localized } = rule;\n const localesToCheck = locale ? [locale] : Object.keys(localized);\n\n for (const loc of localesToCheck) {\n const localizedPattern = localized[loc as keyof typeof localized];\n\n if (!localizedPattern) continue;\n\n const params = extractParams(localizedPath, localizedPattern);\n\n if (params) {\n return fillPath(canonical, params);\n }\n }\n }\n\n return localizedPath;\n};\n\n/**\n * Given a canonical path (e.g., \"/products/123\"), finds the localized URL pattern (e.g., \"/produits/123\").\n */\nexport const getLocalizedPath = (\n canonicalPath: string,\n locale: Locale,\n rewriteRules?: RewriteRules\n): LocalizedPathResult => {\n if (!rewriteRules) return { path: canonicalPath, isRewritten: false };\n\n for (const rule of rewriteRules.rules) {\n const { canonical, localized } = rule;\n\n // Check if the input path matches a configured canonical pattern\n const params = extractParams(canonicalPath, canonical);\n\n if (params) {\n const targetPattern = localized[locale as keyof typeof localized];\n\n if (targetPattern) {\n return {\n path: fillPath(targetPattern, params),\n isRewritten: true,\n };\n }\n }\n }\n\n return { path: canonicalPath, isRewritten: false };\n};\n\n/**\n * Returns the internal path for a given canonical path and locale.\n * Ensures the locale prefix is present exactly once.\n */\nexport const getInternalPath = (\n canonicalPath: string,\n locale: Locale\n): string => {\n const pathWithLeadingSlash = canonicalPath.startsWith('/')\n ? canonicalPath\n : `/${canonicalPath}`;\n\n if (\n pathWithLeadingSlash.startsWith(`/${locale}/`) ||\n pathWithLeadingSlash === `/${locale}`\n ) {\n return pathWithLeadingSlash;\n }\n\n return `/${locale}${pathWithLeadingSlash === '/' ? '' : pathWithLeadingSlash}`;\n};\n\n/**\n * Given a current pathname and locale, returns the pretty localized path if a rewrite rule exists and the path is not already localized.\n */\nexport const getRewritePath = (\n pathname: string,\n locale: Locale,\n rewrite?: RoutingConfig['rewrite']\n): string | undefined => {\n const rules = getRewriteRules(rewrite, 'url');\n if (!rules) return undefined;\n\n // Identify canonical path (relative to root, no locale prefix expected in 'url' context)\n const canonicalPath = getCanonicalPath(pathname, undefined, rules);\n\n // Get the localized path for the current locale\n const { path: localizedPath, isRewritten } = getLocalizedPath(\n canonicalPath,\n locale,\n rules\n );\n\n if (isRewritten && localizedPath !== pathname) {\n return localizedPath;\n }\n\n return undefined;\n};\n"],"mappings":"AAeA,MAAa,GACX,EACA,EAA+B,QACF,CACxB,KAOL,MALI,QAAS,EACH,EAA0B,GAI7B,CACL,MAAO,OAAO,QAAQ,EAAQ,CAAC,KAAK,CAAC,EAAW,MAAgB,CAE9D,UAAW,EAAU,WAAW,IAAI,CAChC,EAAU,QAAQ,gBAAiB,MAAM,CACzC,IAAI,EAAU,QAAQ,gBAAiB,MAAM,GAGjD,UAAW,OAAO,YAChB,OAAO,QAAQ,EAAU,CAAC,KAAK,CAAC,EAAQ,KAI/B,CAAC,EAHkB,GAAS,WAAW,IAAI,CAC9C,EAAQ,QAAQ,gBAAiB,MAAM,CACvC,KAAK,GAAW,IAAI,QAAQ,gBAAiB,MAAM,GACrB,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CACrC,YAAa,GACd,EAKP,MAAO,CAAE,KAAM,EAAe,YAAa,GAAO,EAOvC,GACX,EACA,IACW,CACX,IAAM,EAAuB,EAAc,WAAW,IAAI,CACtD,EACA,IAAI,IASR,OANE,EAAqB,WAAW,IAAI,EAAO,GAAG,EAC9C,IAAyB,IAAI,IAEtB,EAGF,IAAI,IAAS,IAAyB,IAAM,GAAK,KAM7C,GACX,EACA,EACA,IACuB,CACvB,IAAM,EAAQ,EAAgB,EAAS,MAAM,CAC7C,GAAI,CAAC,EAAO,OAMZ,GAAM,CAAE,KAAM,EAAe,eAAgB,EAHvB,EAAiB,EAAU,IAAA,GAAW,EAAM,CAKhE,EACA,EACD,CAED,GAAI,GAAe,IAAkB,EACnC,OAAO"}
1
+ {"version":3,"file":"rewriteUtils.mjs","names":[],"sources":["../../../src/localization/rewriteUtils.ts"],"sourcesContent":["import type {\n Locale,\n LocalesValues,\n RewriteObject,\n RewriteRules,\n RoutingConfig,\n} from '@intlayer/types';\n\nexport type LocalizedPathResult = {\n path: string;\n isRewritten: boolean;\n};\n\n/**\n * Normalizes legacy Record format or extracts specialized rules from RewriteObject.\n */\nexport const getRewriteRules = (\n rewrite: RoutingConfig['rewrite'],\n context: keyof RewriteObject = 'url'\n): RewriteRules | undefined => {\n if (!rewrite) return undefined;\n\n if ('url' in rewrite) {\n return (rewrite as RewriteObject)[context];\n }\n\n // Normalize legacy format\n return {\n rules: Object.entries(rewrite).map(([canonical, localized]) => ({\n // Normalize canonical path\n canonical: canonical.startsWith('/')\n ? canonical.replace(/\\[([^\\]]+)\\]/g, ':$1')\n : `/${canonical.replace(/\\[([^\\]]+)\\]/g, ':$1')}`,\n\n // Normalize localized path\n localized: Object.fromEntries(\n Object.entries(localized).map(([locale, pattern]) => {\n const normalizedPattern = pattern?.startsWith('/')\n ? pattern.replace(/\\[([^\\]]+)\\]/g, ':$1')\n : `/${(pattern || '').replace(/\\[([^\\]]+)\\]/g, ':$1')}`;\n return [locale, normalizedPattern];\n })\n ),\n })),\n };\n};\n\n/**\n * Converts normalized pattern to Regex.\n * Internal syntax supports:\n * - :param -> ([^/]+) (one segment)\n * - :param* -> (.*) (zero or more segments)\n * - :param+ -> (.+) (one or more segments)\n * - :param? -> ([^/]*) (zero or one segment)\n */\nconst patternToRegex = (pattern: string) => {\n const regexString = pattern\n .replace(/\\//g, '\\\\/') // Escape slashes\n .replace(/\\\\\\/:(?:[^/\\\\*+?]+)\\*/g, '(?:\\\\/(.*))?') // /:param*\n .replace(/\\\\\\/:(?:[^/\\\\*+?]+)\\?/g, '(?:\\\\/([^\\\\/]+))?') // /:param?\n .replace(/:([^/\\\\*+?]+)\\*/g, '(.*)') // :param* (if no leading slash)\n .replace(/:([^/\\\\*+?]+)\\?/g, '([^\\\\/]*)') // :param? (if no leading slash)\n .replace(/:([^/\\\\*+?]+)\\+/g, '(.+)') // :param+\n .replace(/:([^/\\\\*+?]+)/g, '([^\\\\/]+)'); // :param\n\n return new RegExp(`^${regexString}$`);\n};\n\n/**\n * Replaces route parameters in a path with provided values.\n */\nconst fillPath = (pattern: string, params: string[]) => {\n let index = 0;\n return (\n pattern\n .replace(/:([^/\\\\*+?]+)[*+?]?/g, () => params[index++] ?? '')\n .replace(/\\/+/g, '/')\n .replace(/\\/$/, '') || '/'\n );\n};\n\n/**\n * Extract values from a URL based on a pattern.\n */\nconst extractParams = (url: string, pattern: string): string[] | null => {\n const regex = patternToRegex(pattern);\n const match = url.match(regex);\n return match ? match.slice(1) : null;\n};\n\n/**\n * Given a localized URL (e.g., \"/produits/123\"), finds the canonical internal path (e.g., \"/products/123\").\n * If locale is provided, only check for that locale. Otherwise, check for all locales.\n */\nexport const getCanonicalPath = (\n localizedPath: string,\n locale?: Locale,\n rewriteRules?: RewriteRules\n): string => {\n if (!rewriteRules) return localizedPath;\n\n for (const rule of rewriteRules.rules) {\n const { canonical, localized } = rule;\n const localesToCheck = locale ? [locale] : Object.keys(localized);\n\n for (const loc of localesToCheck) {\n const localizedPattern = localized[loc as keyof typeof localized];\n\n if (!localizedPattern) continue;\n\n const params = extractParams(localizedPath, localizedPattern);\n\n if (params) {\n return fillPath(canonical, params);\n }\n }\n }\n\n return localizedPath;\n};\n\n/**\n * Given a canonical path (e.g., \"/products/123\"), finds the localized URL pattern (e.g., \"/produits/123\").\n */\nexport const getLocalizedPath = (\n canonicalPath: string,\n locale: LocalesValues,\n rewriteRules?: RewriteRules\n): LocalizedPathResult => {\n if (!rewriteRules) return { path: canonicalPath, isRewritten: false };\n\n for (const rule of rewriteRules.rules) {\n const { canonical, localized } = rule;\n\n // Check if the input path matches a configured canonical pattern\n const params = extractParams(canonicalPath, canonical);\n\n if (params) {\n const targetPattern = localized[locale as keyof typeof localized];\n\n if (targetPattern) {\n return {\n path: fillPath(targetPattern, params),\n isRewritten: true,\n };\n }\n }\n }\n\n return { path: canonicalPath, isRewritten: false };\n};\n\n/**\n * Returns the internal path for a given canonical path and locale.\n * Ensures the locale prefix is present exactly once.\n */\nexport const getInternalPath = (\n canonicalPath: string,\n locale: Locale\n): string => {\n const pathWithLeadingSlash = canonicalPath.startsWith('/')\n ? canonicalPath\n : `/${canonicalPath}`;\n\n if (\n pathWithLeadingSlash.startsWith(`/${locale}/`) ||\n pathWithLeadingSlash === `/${locale}`\n ) {\n return pathWithLeadingSlash;\n }\n\n return `/${locale}${pathWithLeadingSlash === '/' ? '' : pathWithLeadingSlash}`;\n};\n\n/**\n * Given a current pathname and locale, returns the pretty localized path if a rewrite rule exists and the path is not already localized.\n */\nexport const getRewritePath = (\n pathname: string,\n locale: Locale,\n rewrite?: RoutingConfig['rewrite']\n): string | undefined => {\n const rules = getRewriteRules(rewrite, 'url');\n if (!rules) return undefined;\n\n // Identify canonical path (relative to root, no locale prefix expected in 'url' context)\n const canonicalPath = getCanonicalPath(pathname, undefined, rules);\n\n // Get the localized path for the current locale\n const { path: localizedPath, isRewritten } = getLocalizedPath(\n canonicalPath,\n locale,\n rules\n );\n\n if (isRewritten && localizedPath !== pathname) {\n return localized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,KAAK,EAAU,CAEjE,IAAK,IAAM,KAAO,EAAgB,CAChC,IAAM,EAAmB,EAAU,GAEnC,GAAI,CAAC,EAAkB,SAEvB,IAAM,EAAS,EAAc,EAAe,EAAiB,CAE7D,GAAI,EACF,OAAO,EAAS,EAAW,EAAO,EAKxC,OAAO,GAMI,GACX,EACA,EACA,IACwB,CACxB,GAAI,CAAC,EAAc,MAAO,CAAE,KAAM,EAAe,YAAa,GAAO,CAErE,IAAK,IAAM,KAAQ,EAAa,MAAO,CACrC,GAAM,CAAE,YAAW,aAAc,EAG3B,EAAS,EAAc,EAAe,EAAU,CAEtD,GAAI,EAAQ,CACV,IAAM,EAAgB,EAAU,GAEhC,GAAI,EACF,MAAO,CACL,KAAM,EAAS,EAAe,EAAO,CACrC,YAAa,GACd,EAKP,MAAO,CAAE,KAAM,EAAe,YAAa,GAAO,EAOvC,GACX,EACA,IACW,CACX,IAAM,EAAuB,EAAc,WAAW,IAAI,CACtD,EACA,IAAI,IASR,OANE,EAAqB,WAAW,IAAI,EAAO,GAAG,EAC9C,IAAyB,IAAI,IAEtB,EAGF,IAAI,IAAS,IAAyB,IAAM,GAAK,KAM7C,GACX,EACA,EACA,IACuB,CACvB,IAAM,EAAQ,EAAgB,EAAS,MAAM,CAC7C,GAAI,CAAC,EAAO,OAMZ,GAAM,CAAE,KAAM,EAAe,eAAgB,EAHvB,EAAiB,EAAU,IAAA,GAAW,EAAM,CAKhE,EACA,EACD,CAED,GAAI,GAAe,IAAkB,EACnC,OAAO"}
@@ -1,4 +1,4 @@
1
- import { Locale, RewriteObject, RewriteRules, RoutingConfig } from "@intlayer/types";
1
+ import { Locale, LocalesValues, RewriteObject, RewriteRules, RoutingConfig } from "@intlayer/types";
2
2
 
3
3
  //#region src/localization/rewriteUtils.d.ts
4
4
  type LocalizedPathResult = {
@@ -17,7 +17,7 @@ declare const getCanonicalPath: (localizedPath: string, locale?: Locale, rewrite
17
17
  /**
18
18
  * Given a canonical path (e.g., "/products/123"), finds the localized URL pattern (e.g., "/produits/123").
19
19
  */
20
- declare const getLocalizedPath: (canonicalPath: string, locale: Locale, rewriteRules?: RewriteRules) => LocalizedPathResult;
20
+ declare const getLocalizedPath: (canonicalPath: string, locale: LocalesValues, rewriteRules?: RewriteRules) => LocalizedPathResult;
21
21
  /**
22
22
  * Returns the internal path for a given canonical path and locale.
23
23
  * Ensures the locale prefix is present exactly once.
@@ -1 +1 @@
1
- {"version":3,"file":"rewriteUtils.d.ts","names":[],"sources":["../../../src/localization/rewriteUtils.ts"],"mappings":";;;KAOY,mBAAA;EACV,IAAA;EACA,WAAA;AAAA;;;;cAMW,eAAA,GACX,OAAA,EAAS,aAAA,aACT,OAAA,SAAe,aAAA,KACd,YAAA;;;;;cA2EU,gBAAA,GACX,aAAA,UACA,MAAA,GAAS,MAAA,EACT,YAAA,GAAe,YAAA;;;;cA2BJ,gBAAA,GACX,aAAA,UACA,MAAA,EAAQ,MAAA,EACR,YAAA,GAAe,YAAA,KACd,mBAAA;;;;;cA4BU,eAAA,GACX,aAAA,UACA,MAAA,EAAQ,MAAA;AAhEV;;;AAAA,cAmFa,cAAA,GACX,QAAA,UACA,MAAA,EAAQ,MAAA,EACR,OAAA,GAAU,aAAA"}
1
+ {"version":3,"file":"rewriteUtils.d.ts","names":[],"sources":["../../../src/localization/rewriteUtils.ts"],"mappings":";;;KAQY,mBAAA;EACV,IAAA;EACA,WAAA;AAAA;;;;cAMW,eAAA,GACX,OAAA,EAAS,aAAA,aACT,OAAA,SAAe,aAAA,KACd,YAAA;;;;;cA2EU,gBAAA,GACX,aAAA,UACA,MAAA,GAAS,MAAA,EACT,YAAA,GAAe,YAAA;;;;cA2BJ,gBAAA,GACX,aAAA,UACA,MAAA,EAAQ,aAAA,EACR,YAAA,GAAe,YAAA,KACd,mBAAA;;;;;cA4BU,eAAA,GACX,aAAA,UACA,MAAA,EAAQ,MAAA;AAhEV;;;AAAA,cAmFa,cAAA,GACX,QAAA,UACA,MAAA,EAAQ,MAAA,EACR,OAAA,GAAU,aAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlayer/core",
3
- "version": "8.1.8",
3
+ "version": "8.1.9",
4
4
  "private": false,
5
5
  "description": "Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.",
6
6
  "keywords": [
@@ -168,11 +168,11 @@
168
168
  "typecheck": "tsc --noEmit --project tsconfig.types.json"
169
169
  },
170
170
  "dependencies": {
171
- "@intlayer/api": "8.1.8",
172
- "@intlayer/config": "8.1.8",
173
- "@intlayer/dictionaries-entry": "8.1.8",
174
- "@intlayer/types": "8.1.8",
175
- "@intlayer/unmerged-dictionaries-entry": "8.1.8",
171
+ "@intlayer/api": "8.1.9",
172
+ "@intlayer/config": "8.1.9",
173
+ "@intlayer/dictionaries-entry": "8.1.9",
174
+ "@intlayer/types": "8.1.9",
175
+ "@intlayer/unmerged-dictionaries-entry": "8.1.9",
176
176
  "defu": "6.1.4"
177
177
  },
178
178
  "devDependencies": {