chore(share): improve slugification to strip diacritics cleanly

This commit is contained in:
Elian Doran 2025-10-23 14:26:15 +03:00
parent d2b6014b49
commit 0fa1c0f5c4
No known key found for this signature in database
2 changed files with 32 additions and 1 deletions

View File

@ -681,3 +681,33 @@ describe("#normalizeCustomHandlerPattern", () => {
});
});
});
describe("#slugify", () => {
it("should return a slugified string", () => {
const testString = "This is a Test String! With unicode & Special #Chars.";
const expectedSlug = "this-is-a-test-string-with-unicode-special-chars";
const result = utils.slugify(testString);
expect(result).toBe(expectedSlug);
});
it("supports CJK characters without alteration", () => {
const testString = "测试中文字符";
const expectedSlug = "测试中文字符";
const result = utils.slugify(testString);
expect(result).toBe(expectedSlug);
});
it("supports Cyrillic characters without alteration", () => {
const testString = "Тестирование кириллических символов";
const expectedSlug = "тестирование-кириллических-символов";
const result = utils.slugify(testString);
expect(result).toBe(expectedSlug);
});
it("removes diacritic marks from characters", () => {
const testString = "Café naïve façade jalapeño";
const expectedSlug = "cafe-naive-facade-jalapeno";
const result = utils.slugify(testString);
expect(result).toBe(expectedSlug);
});
});

View File

@ -499,7 +499,8 @@ export function formatSize(size: number | null | undefined) {
function slugify(text: string) {
return text
.normalize("NFKD") // handles accents like é → e
.normalize("NFKD") // decompose accents
.replace(/\p{Mark}/gu, "") // remove diacritics cleanly
.toLowerCase()
.replace(/[^\p{Letter}\p{Number}]+/gu, "-") // keep Unicode letters/numbers
.replace(/(^-|-$)+/g, ""); // trim leading/trailing dashes