From df17925d3a24520d8bcc675c23957a4f8ae22504 Mon Sep 17 00:00:00 2001 From: Alireza Hassani Date: Sun, 23 Aug 2026 00:04:07 +0330 Subject: [PATCH] Keep Farsi characters readable in sitemap loc URLs. Stop percent-encoding path slugs so shows UTF-8 Persian text while still sanitizing unsafe URL characters. Co-authored-by: Cursor --- src/sitemap/seo-slug.util.ts | 4 ---- src/sitemap/sitemap-xml.util.ts | 16 ++++++++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/sitemap/seo-slug.util.ts b/src/sitemap/seo-slug.util.ts index 161e6d3..beeadee 100644 --- a/src/sitemap/seo-slug.util.ts +++ b/src/sitemap/seo-slug.util.ts @@ -16,7 +16,3 @@ export function slugifyForUrl(value: string, fallback = 'item'): string { return slug || fallback; } - -export function encodePathSegment(value: string): string { - return encodeURIComponent(value); -} diff --git a/src/sitemap/sitemap-xml.util.ts b/src/sitemap/sitemap-xml.util.ts index 5c1540b..ec672e5 100644 --- a/src/sitemap/sitemap-xml.util.ts +++ b/src/sitemap/sitemap-xml.util.ts @@ -51,13 +51,25 @@ export function buildSitemapXml(entries: SitemapUrlEntry[]): string { ].join('\n'); } +/** + * Build a path from a template. Keep Unicode (e.g. Farsi) readable in sitemap + * `` — do not percent-encode letters. XML escaping happens in buildSitemapXml. + * Only encode characters that break URL/path parsing. + */ export function applyPathTemplate( template: string, vars: { id?: string; slug: string }, ): string { return template - .replaceAll('{id}', vars.id != null ? encodeURIComponent(vars.id) : '') - .replaceAll('{slug}', encodeURIComponent(vars.slug)); + .replaceAll('{id}', sanitizePathSegment(vars.id ?? '')) + .replaceAll('{slug}', sanitizePathSegment(vars.slug)); +} + +function sanitizePathSegment(value: string): string { + return value + .replace(/\\/g, '') + .replace(/[/?#]/g, '') + .replace(/\s+/g, '-'); } export function buildAbsoluteUrl(baseUrl: string, path: string): string {