mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
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.
39 lines
1.1 KiB
TypeScript
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;
|
|
}
|