diff --git a/src/parse/krc.ts b/src/parse/krc.ts index fb5de76..2647a6c 100644 --- a/src/parse/krc.ts +++ b/src/parse/krc.ts @@ -39,8 +39,6 @@ export const parseKRC = (text: string, options: ParseOptions = {}): LyricResult cleanKangxi = false, applyOffset = false, } = options; - const content = cleanKangxi ? normalizeKangxi(text) : text; - const metadata: LyricMetadata = extractMetadata ? { timingMode: "Word" } : {}; const lines: LyricLine[] = []; let krcTranslations: string[] = []; @@ -48,7 +46,7 @@ export const parseKRC = (text: string, options: ParseOptions = {}): LyricResult const mainLines: LyricLine[] = []; let kanaTag = ""; - for (const raw of content.split("\n")) { + for (const raw of text.split("\n")) { const trimmed = raw.trim(); if (!trimmed) continue; @@ -163,6 +161,14 @@ export const parseKRC = (text: string, options: ParseOptions = {}): LyricResult applyKanaToLines(lines, kanaTag); } + if (cleanKangxi) { + for (const line of lines) { + for (const word of line.words) { + word.word = normalizeKangxi(word.word); + } + } + } + if (applyOffset && metadata.offset) { applyTimestampOffset(lines, metadata.offset); } diff --git a/src/parse/qrc.ts b/src/parse/qrc.ts index dacb849..d13a66d 100644 --- a/src/parse/qrc.ts +++ b/src/parse/qrc.ts @@ -92,8 +92,7 @@ export const parseQRC = (text: string, options: ParseOptions = {}): LyricResult cleanKangxi = false, applyOffset = false, } = options; - const normalized = cleanKangxi ? normalizeKangxi(text) : text; - const { content, xmlMeta } = extractFromXml(normalized); + const { content, xmlMeta } = extractFromXml(text); const metadata: LyricMetadata = extractMetadata && xmlMeta ? { ...xmlMeta, timingMode: "Word" } @@ -149,6 +148,14 @@ export const parseQRC = (text: string, options: ParseOptions = {}): LyricResult applyKanaToLines(lines, kanaTag); } + if (cleanKangxi) { + for (const line of lines) { + for (const word of line.words) { + word.word = normalizeKangxi(word.word); + } + } + } + if (applyOffset && metadata.offset) { applyTimestampOffset(lines, metadata.offset); } diff --git a/tests/kana.spec.ts b/tests/kana.spec.ts index 4e50fe1..5a4c67c 100644 --- a/tests/kana.spec.ts +++ b/tests/kana.spec.ts @@ -120,6 +120,22 @@ describe("Kana / Ruby Furigana Support", () => { expect(result.lines).toHaveLength(1); expect(result.lines[0].words[0].ruby).toBeUndefined(); }); + + it("应在康熙部首归一化前判断 kana 基础字符", () => { + const qrcText = `[offset:0] +[kana:1は(150865,168)な(151033,252)1お(153326,189)ど(153515,243)] +[150865,1633]離(150865,420)さ(151285,168)な(151453,260)い(151713,268)と(151981,249)⾔(152230,38)う(152268,230) +[152498,2173]((152498,192)Memento (152690,328)mori)(153018,308)躍(153326,432)る(153758,177)よ(153935,78)う(154013,78)に(154091,580)`; + + const result = parseQRC(qrcText, { cleanKangxi: true }); + const [line0, line1] = result.lines; + expect(line0.words[0].word).toBe("離"); + expect(line0.words[0].ruby?.map((ruby) => ruby.word)).toEqual(["は", "な"]); + expect(line0.words[5].word).toBe("言"); + expect(line0.words[5].ruby).toBeUndefined(); + expect(line1.words[3].word).toBe("躍"); + expect(line1.words[3].ruby?.map((ruby) => ruby.word)).toEqual(["お", "ど"]); + }); }); describe("parseKRC with Kana", () => {