Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(document): validate referenced word ownership and join CJK independently of language tag

sunyuchenyaobo097684edd075
diff --git a/src/lib/ai-edition/document/transcript.test.ts b/src/lib/ai-edition/document/transcript.test.ts
index 1678374bc..f9ed108a7 100644
--- a/src/lib/ai-edition/document/transcript.test.ts
+++ b/src/lib/ai-edition/document/transcript.test.ts
@@ -151,6 +151,8 @@ describe("setWordText", () => {
"zh-CN",
"zh-TW",
"ZH-cn",
+ "auto",
+ "yue",
])("does not add artificial spaces between adjacent Chinese content for %s", (language) => {
const result = setWordText(transcriptForTokens(language, ["你", "好", "世界"]), "word_2", "们");
@@ -252,6 +254,15 @@ describe("setWordText", () => {
);
});
+ it("rejects an owning segment that references a word owned by another segment", () => {
+ const transcript = fixture();
+ transcript.segments[0].wordIds.push("word_4");
+
+ expect(() => setWordText(transcript, "word_2", "replacement")).toThrowError(
+ /segment_1.*word_4.*segment_2/,
+ );
+ });
+
it("rejects an owning segment that omits the target word", () => {
const transcript = fixture();
transcript.segments[0].wordIds = ["word_1", "word_3"];
diff --git a/src/lib/ai-edition/document/transcript.ts b/src/lib/ai-edition/document/transcript.ts
index 45e538d88..7d516dde1 100644
--- a/src/lib/ai-edition/document/transcript.ts
+++ b/src/lib/ai-edition/document/transcript.ts
@@ -5,18 +5,20 @@ const CLOSING_PUNCTUATION = /^[,.;:!?%。,、;:!?…))\]}>》」』
const TRAILING_CLOSING_PUNCTUATION = /[,.;:!?%。,、;:!?…))\]}>》」』】〕]+$/u;
const OPENING_PUNCTUATION = /[([<{《「『【〔(]$/u;
-function joinSegmentText(language: string, texts: string[]): string {
+// The CJK-compaction rule is deliberately LANGUAGE-AGNOSTIC: two adjacent Han /
+// Hiragana / Katakana characters never carry a space between them in any script
+// that uses them. Gating it on the `language` tag would corrupt transcripts whose
+// stored tag is "auto" (a real persisted value — see transcribe.ts's language
+// fallback) or "yue": the join would inject ASCII spaces between Chinese runs.
+function joinSegmentText(texts: string[]): string {
const tokens = texts.map((text) => text.trim()).filter((text) => text.length > 0);
- const primaryLanguage = language.split("-")[0].toLowerCase();
- const compactCjk = primaryLanguage === "zh" || primaryLanguage === "ja";
-
return tokens.reduce((joined, token) => {
if (joined.length === 0) return token;
if (CLOSING_PUNCTUATION.test(token) || OPENING_PUNCTUATION.test(joined)) {
return joined + token;
}
const leftContentEdge = joined.replace(TRAILING_CLOSING_PUNCTUATION, "").at(-1) ?? "";
- if (compactCjk && CJK_EDGE.test(leftContentEdge) && CJK_EDGE.test(token[0] ?? "")) {
+ if (CJK_EDGE.test(leftContentEdge) && CJK_EDGE.test(token[0] ?? "")) {
return joined + token;
}
return `${joined} ${token}`;
@@ -45,17 +47,22 @@ export function setWordText(
const wordsById = new Map(transcript.words.map((word) => [word.id, word]));
for (const referencedWordId of owningSegment.wordIds) {
- if (!wordsById.has(referencedWordId)) {
+ const referencedWord = wordsById.get(referencedWordId);
+ if (!referencedWord) {
throw new Error(
`Segment "${owningSegment.id}" references missing word "${referencedWordId}"`,
);
}
+ if (referencedWord.segmentId !== owningSegment.id) {
+ throw new Error(
+ `Segment "${owningSegment.id}" references word "${referencedWordId}" which belongs to segment "${referencedWord.segmentId}"`,
+ );
+ }
}
const words = transcript.words.map((word) => (word.id === wordId ? { ...word, text } : word));
const updatedWordsById = new Map(words.map((word) => [word.id, word]));
const segmentText = joinSegmentText(
- transcript.language,
owningSegment.wordIds.map(
(referencedWordId) => updatedWordsById.get(referencedWordId)?.text ?? "",
),