feat: support furigana and romaji generation on web (#2232)

* feat: support furigana and romaji generation on web
This commit is contained in:
York
2026-07-16 13:26:05 +08:00
committed by GitHub
parent 763e7d5638
commit 62c1ac04e4
12 changed files with 133 additions and 27 deletions
+14 -1
View File
@@ -17,6 +17,14 @@ export type RomajiToken = LyricTextToken & {
let kuroshiroInstance: any = null;
let initPromise: null | Promise<void> = null;
const getDictionaryPath = (): string | undefined => {
if (typeof document === 'undefined') {
return undefined;
}
return new URL('./assets/kuromoji/', document.baseURI).href;
};
const getKuroshiro = async () => {
if (initPromise) {
await initPromise;
@@ -26,8 +34,13 @@ const getKuroshiro = async () => {
if (kuroshiroInstance) return kuroshiroInstance;
const KuroshiroClass = (Kuroshiro as any).default || Kuroshiro;
const dictionaryPath = getDictionaryPath();
const analyzer = dictionaryPath
? new KuromojiAnalyzer({ dictPath: dictionaryPath })
: new KuromojiAnalyzer();
kuroshiroInstance = new KuroshiroClass();
initPromise = kuroshiroInstance.init(new KuromojiAnalyzer());
initPromise = kuroshiroInstance.init(analyzer);
await initPromise;
initPromise = null;