From 4b356dd937b9692fbdaf2054b6775faafcc44e9d Mon Sep 17 00:00:00 2001 From: Ayush Mainali <49358949+AyushMainali123@users.noreply.github.com> Date: Wed, 17 May 2023 18:50:14 +0545 Subject: [PATCH] chore: extended slugify function (#8740) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: sean-brydon <55134778+sean-brydon@users.noreply.github.com> Co-authored-by: Omar López --- packages/lib/slugify.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/lib/slugify.ts b/packages/lib/slugify.ts index c6fba1f4f0..70b5191043 100644 --- a/packages/lib/slugify.ts +++ b/packages/lib/slugify.ts @@ -1,5 +1,13 @@ export const slugify = (str: string) => { - return str.replace(/[^a-zA-Z0-9-]/g, "-").toLowerCase(); + return str + .toLowerCase() // Convert to lowercase + .trim() // Remove whitespace from both sides + .normalize("NFD") // Normalize to decomposed form for handling accents + .replace(/\p{Diacritic}/gu, "") // Remove any diacritics (accents) from characters + .replace(/[^\p{L}\p{N}\p{Zs}\p{Emoji}]+/gu, "-") // Replace any non-alphanumeric characters (including Unicode) with a dash + .replace(/[\s_]+/g, "-") // Replace whitespace and underscores with a single dash + .replace(/^-+/, "") // Remove dashes from start + .replace(/-+$/, ""); // Remove dashes from end }; export default slugify;