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): read CJK segment edges by code point for non-BMP Han

sunyuchenyaobobfa1dc990b00
diff --git a/src/lib/ai-edition/document/transcript.test.ts b/src/lib/ai-edition/document/transcript.test.ts
index f9ed108a7..a1c872ffb 100644
--- a/src/lib/ai-edition/document/transcript.test.ts
+++ b/src/lib/ai-edition/document/transcript.test.ts
@@ -159,6 +159,22 @@ describe("setWordText", () => {
expect(result.segments[0].text).toBe("你们世界");
});
+ it("does not add a space after a non-BMP Han word (edge read by code point)", () => {
+ const result = setWordText(transcriptForTokens("zh", ["\u{20000}", "好"]), "word_2", "世界");
+
+ expect(result.segments[0].text).toBe("\u{20000}世界");
+ });
+
+ it("does not add a space before a token starting with a non-BMP Han character", () => {
+ const result = setWordText(
+ transcriptForTokens("zh", ["好", "\u{20000}"]),
+ "word_2",
+ "\u{20000}",
+ );
+
+ expect(result.segments[0].text).toBe("好\u{20000}");
+ });
+
it.each([
"ja",
"ja-JP",
diff --git a/src/lib/ai-edition/document/transcript.ts b/src/lib/ai-edition/document/transcript.ts
index 7d516dde1..f64588f9e 100644
--- a/src/lib/ai-edition/document/transcript.ts
+++ b/src/lib/ai-edition/document/transcript.ts
@@ -17,8 +17,11 @@ function joinSegmentText(texts: string[]): string {
if (CLOSING_PUNCTUATION.test(token) || OPENING_PUNCTUATION.test(joined)) {
return joined + token;
}
- const leftContentEdge = joined.replace(TRAILING_CLOSING_PUNCTUATION, "").at(-1) ?? "";
- if (CJK_EDGE.test(leftContentEdge) && CJK_EDGE.test(token[0] ?? "")) {
+ const leftContentEdge = [...joined.replace(TRAILING_CLOSING_PUNCTUATION, "")].at(-1) ?? "";
+ // Spread reads the edges by CODE POINT: `.at(-1)` / `[0]` would return half a
+ // surrogate pair, so a non-BMP Han edge (e.g. U+20000) would miss CJK_EDGE
+ // and receive an ASCII space.
+ if (CJK_EDGE.test(leftContentEdge) && CJK_EDGE.test([...token][0] ?? "")) {
return joined + token;
}
return `${joined} ${token}`;