Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add transliteration

Marc Seitzdc4588bd9ae8
diff --git a/lib/utils.ts b/lib/utils.ts
index 7efadf12..0813eff0 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -2,6 +2,7 @@ import { NextRouter } from "next/router";
import slugify from "@sindresorhus/slugify";
import { upload } from "@vercel/blob/client";
+import { transliterate } from "transliteration";
import bcrypt from "bcryptjs";
import * as chrono from "chrono-node";
import { type ClassValue, clsx } from "clsx";
@@ -351,18 +352,15 @@ export const nanoid = customAlphabet(
7,
); // 7-character random string
-const nanoidSlug = customAlphabet(
- "0123456789abcdefghijklmnopqrstuvwxyz",
- 12,
-);
-
/**
- * CJK-safe slugify: falls back to a nanoid when @sindresorhus/slugify
- * strips all characters (e.g. for purely CJK filenames / folder names).
+ * CJK-safe slugify: transliterates non-Latin characters (CJK, Cyrillic, etc.)
+ * to their romanized equivalents before slugifying, so the same input always
+ * produces the same slug. e.g. "文件报告" → "wen-jian-bao-gao"
*/
export function safeSlugify(input: string): string {
const slug = slugify(input);
- return slug.length > 0 ? slug : nanoidSlug();
+ if (slug.length > 0) return slug;
+ return slugify(transliterate(input)) || nanoid();
}
export const daysLeft = (
diff --git a/package-lock.json b/package-lock.json
index 1bc0df76..b3dc9a0a 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -145,6 +145,7 @@
"tailwindcss": "^3.4.19",
"tailwindcss-animate": "^1.0.7",
"tokenlens": "^1.3.1",
+ "transliteration": "^2.6.1",
"ts-pattern": "^5.9.0",
"tus-js-client": "^4.3.1",
"ua-parser-js": "^1.0.41",
@@ -9140,9 +9141,9 @@
}
},
"node_modules/@sindresorhus/transliterate": {
- "version": "2.2.0",
- "resolved": "https://registry.npmjs.org/@sindresorhus/transliterate/-/transliterate-2.2.0.tgz",
- "integrity": "sha512-aLtANYAx3qvFilasPhZke27+Cm7WawGGuGiOd2EAp0lg1NdWKfulslcql/Qi7lNQ9odkPjwRbwk9c4CYsTh+Rg==",
+ "version": "2.3.1",
+ "resolved": "https://registry.npmjs.org/@sindresorhus/transliterate/-/transliterate-2.3.1.tgz",
+ "integrity": "sha512-gVaaGtKYMYAMmI8buULVH3A2TXVJ98QiwGwI7ddrWGuGidGC2uRt4FHs22+8iROJ0QTzju9CuMjlVsrvpqsdhA==",
"license": "MIT",
"engines": {
"node": ">=20"
@@ -25234,6 +25235,19 @@
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw=="
},
+ "node_modules/transliteration": {
+ "version": "2.6.1",
+ "resolved": "https://registry.npmjs.org/transliteration/-/transliteration-2.6.1.tgz",
+ "integrity": "sha512-hJ9BhrQAOnNTbpOr1MxsNjZISkn7ppvF5TKUeFmTE1mG4ZPD/XVxF0L0LUoIUCWmQyxH0gJpVtfYLAWf298U9w==",
+ "license": "MIT",
+ "bin": {
+ "slugify": "dist/bin/slugify",
+ "transliterate": "dist/bin/transliterate"
+ },
+ "engines": {
+ "node": ">=20.0.0"
+ }
+ },
"node_modules/traverse": {
"version": "0.3.9",
"resolved": "https://registry.npmjs.org/traverse/-/traverse-0.3.9.tgz",
diff --git a/package.json b/package.json
index eecfe9e8..5ef43b0e 100644
--- a/package.json
+++ b/package.json
@@ -156,6 +156,7 @@
"tailwindcss": "^3.4.19",
"tailwindcss-animate": "^1.0.7",
"tokenlens": "^1.3.1",
+ "transliteration": "^2.6.1",
"ts-pattern": "^5.9.0",
"tus-js-client": "^4.3.1",
"ua-parser-js": "^1.0.41",