diff --git a/web/lib/markdown-display.ts b/web/lib/markdown-display.ts index 0d451cc42..cd6cf6687 100644 --- a/web/lib/markdown-display.ts +++ b/web/lib/markdown-display.ts @@ -1,6 +1,7 @@ "use client"; -const ZERO_WIDTH_REGEX = /[\u200B-\u200D\uFEFF]/g; +const INVISIBLE_CONTROL_REGEX = + /[\u200B-\u200F\u202A-\u202E\u2060\u2066-\u2069\uFEFF]/g; const EMPTY_DETAILS_REGEX = /]*)?>\s*(]*)?>\s*(?: |\s|)*\s*<\/summary>\s*)?<\/details>/gi; const EMPTY_SUMMARY_REGEX = @@ -16,7 +17,7 @@ const EMPTY_HTML_BLOCK_REGEX = const HTML_TABLE_REGEX = /]*)?>[\s\S]*?<\/table>/gi; function stripInvisibleCharacters(value: string): string { - return value.replace(ZERO_WIDTH_REGEX, ""); + return value.replace(INVISIBLE_CONTROL_REGEX, ""); } // Tags that the renderer (rehype-raw + react-markdown) is allowed to render @@ -151,6 +152,7 @@ const MATH_SPAN_REGEX = // two delimiters the author meant to pair (see repairStrongEmphasisLine). const MALFORMED_STRONG_EMPHASIS_REGEX = /(? + INDENTED_CODE_LINE_REGEX.test(line) + ? line + : line.replace(ESCAPED_UNICODE_RUN_REGEX, decodeEscapedUnicodeRun), + ) + .join("\n"); + return fenced.restore(math.restore(inline.restore(decoded))); +} + +function decodeEscapedUnicodeRun(escaped: string): string { + try { + const value = JSON.parse(`"${escaped}"`) as string; + const containsNonAscii = Array.from(value).some( + (character) => character.charCodeAt(0) > 0x7f, + ); + return containsNonAscii ? value : escaped; + } catch { + return escaped; + } +} + export function normalizeMarkdownForDisplay(content: string): string { if (!content) return ""; - const normalized = stripInvisibleCharacters(String(content)) - .replace(/\r\n/g, "\n") + const normalized = stripInvisibleCharacters( + decodeEscapedUnicodeRuns(String(content).replace(/\r\n/g, "\n")), + ) .replace(EMPTY_DETAILS_REGEX, "") .replace(EMPTY_SUMMARY_REGEX, "") .replace(EMPTY_PROGRESS_REGEX, "") diff --git a/web/tests/markdown-display.test.ts b/web/tests/markdown-display.test.ts index 7a93220e9..a30557490 100644 --- a/web/tests/markdown-display.test.ts +++ b/web/tests/markdown-display.test.ts @@ -85,6 +85,52 @@ test("normalizeMarkdownForDisplay removes empty details blocks", () => { assert.equal(normalizeMarkdownForDisplay(input), "Before\n\nAfter"); }); +test("normalizeMarkdownForDisplay decodes dense non-ASCII JSON escapes", () => { + const input = "\\u300c\\u6570\\u5236\\u8f6c\\u6362\\u300d"; + assert.equal(normalizeMarkdownForDisplay(input), "「数制转换」"); +}); + +test("decoded unicode cannot reintroduce invisible or bidi controls", () => { + assert.equal( + normalizeMarkdownForDisplay("\\u200b\\u4e2d\\u6587"), + "中文", + ); + assert.equal( + normalizeMarkdownForDisplay("\\u202e\\u0061\\u0062"), + "ab", + ); +}); + +test("normalizeMarkdownForDisplay keeps isolated and ASCII unicode escape examples", () => { + const inputs = [ + "A JSON string can encode A as \\u0041.", + "Three ASCII escapes: \\u0041\\u0042\\u0043.", + ]; + + for (const input of inputs) { + assert.equal(normalizeMarkdownForDisplay(input), input); + } +}); + +test("normalizeMarkdownForDisplay keeps unicode escapes inside code verbatim", () => { + const input = [ + "Escaped text: `\\u300c\\u6570\\u5236\\u8f6c\\u6362\\u300d`", + "", + "```json", + '"label": "\\u300c\\u6570\\u5236\\u8f6c\\u6362\\u300d"', + "```", + ].join("\n"); + + assert.equal(normalizeMarkdownForDisplay(input), input); +}); + +test("normalizeMarkdownForDisplay keeps unicode escapes in indented code verbatim", () => { + const input = + 'Example:\n\n "label": "\\u300c\\u6570\\u5236\\u8f6c\\u6362\\u300d"'; + + assert.equal(normalizeMarkdownForDisplay(input), input); +}); + test("normalizeMarkdownForDisplay removes raw html control placeholders", () => { const input = 'Before\n\n\n\n\n\nAfter';