Files
Tom Moor bd7fc38ddb feat: Generate Pinyin slugs for Chinese document titles (#14375)
Slugs are now generated only on the server, which lazily loads pinyin-pro.
The client reads the path from the API rather than computing it.
2026-10-31 11:12:24 -04:00

39 lines
1.1 KiB
TypeScript

import type * as PinyinPro from "pinyin-pro";
import slug from "slug";
slug.defaults.mode = "rfc3986";
const hanRegex = /\p{Script=Han}/u;
const kanaRegex = /[\p{Script=Hiragana}\p{Script=Katakana}]/u;
/**
* Convert a string to a slug that can be used in a URL in kebab-case format,
* and remove periods. Chinese characters are converted to tone-free Pinyin.
*
* @param text The text to convert
* @returns The slugified text
*/
export default function slugify(text: string): string {
// slug throws on malformed strings, so replace unpaired surrogates first.
let input = text.toWellFormed();
// Pinyin readings are incorrect for Japanese, so skip text with kana.
if (hanRegex.test(input) && !kanaRegex.test(input)) {
input = loadPinyin()(input, {
toneType: "none",
nonZh: "consecutive",
v: true,
});
}
return slug(input, {
remove: /[.]/g,
});
}
function loadPinyin(): typeof PinyinPro.pinyin {
// oxlint-disable-next-line typescript/no-require-imports -- kept lazy so the Pinyin dictionary stays off the startup path
const pinyinPro = require("pinyin-pro") as typeof PinyinPro;
return pinyinPro.pinyin;
}