fix(webui): render CJK bold labels before Latin text (#6099)
* fix(webui): render CJK bold labels before Latin text * fix(webui): preserve decoded text during bold recovery
This commit is contained in:
1 parent
5c71b87a3f
commit
59b89a834c
4 files changed
+82
-18
No files matched your search
+4
-3
@@ -19,6 +19,7 @@
|
|||||||
"i18next": "^26.0.6",
|
"i18next": "^26.0.6",
|
||||||
"lucide-react": "^0.469.0",
|
"lucide-react": "^0.469.0",
|
||||||
"micromark-factory-space": "^2.0.1",
|
"micromark-factory-space": "^2.0.1",
|
||||||
|
"micromark-util-decode-string": "^2.0.1",
|
||||||
"qrcode": "^1.5.4",
|
"qrcode": "^1.5.4",
|
||||||
"react": "^18.3.1",
|
"react": "^18.3.1",
|
||||||
"react-dom": "^18.3.1",
|
"react-dom": "^18.3.1",
|
||||||
@@ -556,7 +557,7 @@
|
|||||||
|
|
||||||
"chai": ["chai@5.3.3", "", { "dependencies": { "assertion-error": "^2.0.1", "check-error": "^2.1.1", "deep-eql": "^5.0.1", "loupe": "^3.1.0", "pathval": "^2.0.0" } }, "sha512-4zNhdJD/iOjSH0A05ea+Ke6MU5mmpQcbQsSOkgdaUMJ9zTlDTD/GYlwohmIE2u0gaxHYiVHEn1Fw9mZ/ktJWgw=="],
|
"chai": ["chai@5.3.3", "", { "dependencies": { "assertion-error": "^2.0.1", "check-error": "^2.1.1", "deep-eql": "^5.0.1", "loupe": "^3.1.0", "pathval": "^2.0.0" } }, "sha512-4zNhdJD/iOjSH0A05ea+Ke6MU5mmpQcbQsSOkgdaUMJ9zTlDTD/GYlwohmIE2u0gaxHYiVHEn1Fw9mZ/ktJWgw=="],
|
||||||
|
|
||||||
"character-entities": ["character-entities@1.2.4", "", {}, "sha512-iBMyeEHxfVnIakwOuDXpVkc54HijNgCyQB2w0VfGQThle6NXn50zU6V/u+LDhxHcDUPojn6Kpga3PTAD8W1bQw=="],
|
"character-entities": ["character-entities@2.0.2", "", {}, "sha512-shx7oQ0Awen/BRIdkjkvz54PnEEI/EjwXDSIZp86/KKdbafHh1Df/RYGBhn4hbe2+uKC9FnT5UCEdyPz3ai9hQ=="],
|
||||||
|
|
||||||
"character-entities-html4": ["character-entities-html4@2.1.0", "", {}, "sha512-1v7fgQRj6hnSwFpq1Eu0ynr/CDEw0rXo2B61qXrLNdHZmPKgb7fqS1a2JwF0rISo9q77jDI8VMEHoApn8qDoZA=="],
|
"character-entities-html4": ["character-entities-html4@2.1.0", "", {}, "sha512-1v7fgQRj6hnSwFpq1Eu0ynr/CDEw0rXo2B61qXrLNdHZmPKgb7fqS1a2JwF0rISo9q77jDI8VMEHoApn8qDoZA=="],
|
||||||
|
|
||||||
@@ -1424,8 +1425,6 @@
|
|||||||
|
|
||||||
"d3-sankey/d3-shape": ["d3-shape@1.3.7", "", { "dependencies": { "d3-path": "1" } }, "sha512-EUkvKjqPFUAZyOlhY5gzCxCeI0Aep04LwIRpsZ/mLFelJiUfnK56jo5JMDSE7yyP2kLSb6LtF+S5chMk7uqPqw=="],
|
"d3-sankey/d3-shape": ["d3-shape@1.3.7", "", { "dependencies": { "d3-path": "1" } }, "sha512-EUkvKjqPFUAZyOlhY5gzCxCeI0Aep04LwIRpsZ/mLFelJiUfnK56jo5JMDSE7yyP2kLSb6LtF+S5chMk7uqPqw=="],
|
||||||
|
|
||||||
"decode-named-character-reference/character-entities": ["character-entities@2.0.2", "", {}, "sha512-shx7oQ0Awen/BRIdkjkvz54PnEEI/EjwXDSIZp86/KKdbafHh1Df/RYGBhn4hbe2+uKC9FnT5UCEdyPz3ai9hQ=="],
|
|
||||||
|
|
||||||
"fast-glob/glob-parent": ["glob-parent@5.1.2", "", { "dependencies": { "is-glob": "^4.0.1" } }, "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow=="],
|
"fast-glob/glob-parent": ["glob-parent@5.1.2", "", { "dependencies": { "is-glob": "^4.0.1" } }, "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow=="],
|
||||||
|
|
||||||
"glob/minimatch": ["minimatch@9.0.9", "", { "dependencies": { "brace-expansion": "^2.0.2" } }, "sha512-OBwBN9AL4dqmETlpS2zasx+vTeWclWzkblfZk7KTA5j3jeOONz/tRCnZomUyvNg83wL5Zv9Ss6HMJXAgL8R2Yg=="],
|
"glob/minimatch": ["minimatch@9.0.9", "", { "dependencies": { "brace-expansion": "^2.0.2" } }, "sha512-OBwBN9AL4dqmETlpS2zasx+vTeWclWzkblfZk7KTA5j3jeOONz/tRCnZomUyvNg83wL5Zv9Ss6HMJXAgL8R2Yg=="],
|
||||||
@@ -1450,6 +1449,8 @@
|
|||||||
|
|
||||||
"mermaid/marked": ["marked@16.4.2", "", { "bin": { "marked": "bin/marked.js" } }, "sha512-TI3V8YYWvkVf3KJe1dRkpnjs68JUPyEa5vjKrp1XEEJUAOaQc+Qj+L1qWbPd0SJuAdQkFU0h73sXXqwDYxsiDA=="],
|
"mermaid/marked": ["marked@16.4.2", "", { "bin": { "marked": "bin/marked.js" } }, "sha512-TI3V8YYWvkVf3KJe1dRkpnjs68JUPyEa5vjKrp1XEEJUAOaQc+Qj+L1qWbPd0SJuAdQkFU0h73sXXqwDYxsiDA=="],
|
||||||
|
|
||||||
|
"parse-entities/character-entities": ["character-entities@1.2.4", "", {}, "sha512-iBMyeEHxfVnIakwOuDXpVkc54HijNgCyQB2w0VfGQThle6NXn50zU6V/u+LDhxHcDUPojn6Kpga3PTAD8W1bQw=="],
|
||||||
|
|
||||||
"path-scurry/lru-cache": ["lru-cache@10.4.3", "", {}, "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ=="],
|
"path-scurry/lru-cache": ["lru-cache@10.4.3", "", {}, "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ=="],
|
||||||
|
|
||||||
"postcss-nested/postcss-selector-parser": ["postcss-selector-parser@6.1.2", "", { "dependencies": { "cssesc": "^3.0.0", "util-deprecate": "^1.0.2" } }, "sha512-Q8qQfPiZ+THO/3ZrOrO0cJJKfpYCagtMUkXbnEfmgUjwXg6z/WBeOyS9APBBPCTSiDV+s4SwQGu8yFsiMRIudg=="],
|
"postcss-nested/postcss-selector-parser": ["postcss-selector-parser@6.1.2", "", { "dependencies": { "cssesc": "^3.0.0", "util-deprecate": "^1.0.2" } }, "sha512-Q8qQfPiZ+THO/3ZrOrO0cJJKfpYCagtMUkXbnEfmgUjwXg6z/WBeOyS9APBBPCTSiDV+s4SwQGu8yFsiMRIudg=="],
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
"i18next": "^26.0.6",
|
"i18next": "^26.0.6",
|
||||||
"lucide-react": "^0.469.0",
|
"lucide-react": "^0.469.0",
|
||||||
"micromark-factory-space": "^2.0.1",
|
"micromark-factory-space": "^2.0.1",
|
||||||
|
"micromark-util-decode-string": "^2.0.1",
|
||||||
"qrcode": "^1.5.4",
|
"qrcode": "^1.5.4",
|
||||||
"react": "^18.3.1",
|
"react": "^18.3.1",
|
||||||
"react-dom": "^18.3.1",
|
"react-dom": "^18.3.1",
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
} from "react";
|
} from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { Check, Globe2 } from "lucide-react";
|
import { Check, Globe2 } from "lucide-react";
|
||||||
|
import { decodeString } from "micromark-util-decode-string";
|
||||||
import remarkBreaks from "remark-breaks";
|
import remarkBreaks from "remark-breaks";
|
||||||
import remarkGfm from "remark-gfm";
|
import remarkGfm from "remark-gfm";
|
||||||
import remarkMath from "remark-math";
|
import remarkMath from "remark-math";
|
||||||
@@ -54,6 +55,10 @@ type MarkdownAstNode = {
|
|||||||
type: string;
|
type: string;
|
||||||
value?: string;
|
value?: string;
|
||||||
children?: MarkdownAstNode[];
|
children?: MarkdownAstNode[];
|
||||||
|
position?: {
|
||||||
|
start: { offset: number };
|
||||||
|
end: { offset: number };
|
||||||
|
};
|
||||||
data?: {
|
data?: {
|
||||||
hName?: string;
|
hName?: string;
|
||||||
};
|
};
|
||||||
@@ -245,48 +250,58 @@ function remarkSafeHtmlSubset() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recover a common model-output edge case that CommonMark leaves as literal
|
// Model output can put CJK punctuation or whitespace before a closing `**`
|
||||||
// text: `**结论。**如果`, with no separator after the closing delimiter.
|
// followed immediately by a word, which CommonMark leaves as literal text.
|
||||||
const CJK_AFTER_STRONG =
|
const STRONG_BEFORE_WORD = /(?<!\\)\*\*([^\s*][^*\r\n]*?)(?<!\\)\*\*(?=[\p{L}\p{N}])/gu;
|
||||||
/(?<!\\)\*\*([^*\r\n]+?)(?<!\\)\*\*(?=[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}])/gu;
|
const CJK_CHARACTER = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
|
||||||
|
|
||||||
function normalizeCjkStrongBoundaries(node: MarkdownAstNode): void {
|
function normalizeCjkStrongBoundaries(node: MarkdownAstNode, source: string): void {
|
||||||
if (!node.children) return;
|
if (!node.children) return;
|
||||||
node.children = node.children.flatMap((child) => {
|
node.children = node.children.flatMap((child) => {
|
||||||
if (child.type !== "text" || !child.value?.includes("**")) {
|
if (child.type !== "text" || !child.value?.includes("**") || !child.position) {
|
||||||
normalizeCjkStrongBoundaries(child);
|
normalizeCjkStrongBoundaries(child, source);
|
||||||
return [child];
|
return [child];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Match source delimiters before decoding escapes and character references.
|
||||||
|
// Paragraph continuation indentation is not part of the parsed text value.
|
||||||
|
const textSource = source.slice(child.position.start.offset, child.position.end.offset)
|
||||||
|
.replace(/(\r\n?|\n)[\t ]+/g, "$1");
|
||||||
|
|
||||||
const replacement: MarkdownAstNode[] = [];
|
const replacement: MarkdownAstNode[] = [];
|
||||||
let cursor = 0;
|
let cursor = 0;
|
||||||
for (const match of child.value.matchAll(CJK_AFTER_STRONG)) {
|
for (const match of textSource.matchAll(STRONG_BEFORE_WORD)) {
|
||||||
const start = match.index;
|
const start = match.index;
|
||||||
if (start > cursor) replacement.push(safeText(child.value.slice(cursor, start)));
|
const end = start + match[0].length;
|
||||||
|
const content = decodeString(match[1]);
|
||||||
|
if (!CJK_CHARACTER.test(content + textSource.slice(end, end + 1))) continue;
|
||||||
|
const label = content.trimEnd();
|
||||||
|
if (start > cursor) replacement.push(safeText(decodeString(textSource.slice(cursor, start))));
|
||||||
replacement.push({
|
replacement.push({
|
||||||
type: "strong",
|
type: "strong",
|
||||||
children: [safeText(match[1])],
|
children: [safeText(label)],
|
||||||
});
|
});
|
||||||
cursor = start + match[0].length;
|
if (label.length < content.length) replacement.push(safeText(content.slice(label.length)));
|
||||||
|
cursor = end;
|
||||||
}
|
}
|
||||||
if (cursor === 0) return [child];
|
if (cursor === 0) return [child];
|
||||||
if (cursor < child.value.length) replacement.push(safeText(child.value.slice(cursor)));
|
if (cursor < textSource.length) replacement.push(safeText(decodeString(textSource.slice(cursor))));
|
||||||
return replacement;
|
return replacement;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function remarkCjkStrongBoundaries() {
|
function remarkCjkStrongBoundaries() {
|
||||||
return (tree: MarkdownAstNode) => {
|
return (tree: MarkdownAstNode, file: { toString(): string }) => {
|
||||||
normalizeCjkStrongBoundaries(tree);
|
normalizeCjkStrongBoundaries(tree, file.toString());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const remarkPlugins: NonNullable<StreamdownProps["remarkPlugins"]> = [
|
const remarkPlugins: NonNullable<StreamdownProps["remarkPlugins"]> = [
|
||||||
|
remarkCjkStrongBoundaries,
|
||||||
remarkBreaks,
|
remarkBreaks,
|
||||||
remarkGfm,
|
remarkGfm,
|
||||||
[remarkMath, { singleDollarTextMath: false }],
|
[remarkMath, { singleDollarTextMath: false }],
|
||||||
remarkTexMath,
|
remarkTexMath,
|
||||||
remarkCjkStrongBoundaries,
|
|
||||||
remarkSafeHtmlSubset,
|
remarkSafeHtmlSubset,
|
||||||
];
|
];
|
||||||
type MathPlugin = typeof import("@/lib/markdown-math").default;
|
type MathPlugin = typeof import("@/lib/markdown-math").default;
|
||||||
|
|||||||
@@ -601,6 +601,53 @@ describe("MarkdownTextRenderer", () => {
|
|||||||
expect(screen.getByText(/如果你之后不想再用/)).toBeInTheDocument();
|
expect(screen.getByText(/如果你之后不想再用/)).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
["**边界说明:**issue 只有截图,没有原始消息。", "issue 只有截图,没有原始消息。"],
|
||||||
|
["**边界说明: **issue 只有截图,没有原始消息。", " issue 只有截图,没有原始消息。"],
|
||||||
|
])("renders CJK bold labels before Latin text through stream completion: %s", (source, following) => {
|
||||||
|
const { container, rerender } = render(<MarkdownTextRenderer>{source}</MarkdownTextRenderer>);
|
||||||
|
expect(container.querySelector("strong")).toHaveTextContent("边界说明:");
|
||||||
|
expect(container.textContent).toBe(`边界说明:${following}`);
|
||||||
|
|
||||||
|
for (let end = 1; end <= source.length; end += 1) {
|
||||||
|
rerender(<MarkdownTextRenderer streaming preserveStreamingLayout>{source.slice(0, end)}</MarkdownTextRenderer>);
|
||||||
|
}
|
||||||
|
expect(container.querySelector("strong")).toHaveTextContent("边界说明:");
|
||||||
|
rerender(<MarkdownTextRenderer preserveStreamingLayout>{source}</MarkdownTextRenderer>);
|
||||||
|
expect(container.querySelector("strong")).toHaveTextContent("边界说明:");
|
||||||
|
expect(container.textContent).toBe(`边界说明:${following}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps literal bold markers in code and escaped text", () => {
|
||||||
|
const { container } = render(
|
||||||
|
<MarkdownTextRenderer highlightCode={false}>
|
||||||
|
{"`**边界说明:**issue`\n\n```text\n**边界说明: **issue\n```\n\n\\*\\*边界说明:\\*\\*issue"}
|
||||||
|
</MarkdownTextRenderer>,
|
||||||
|
);
|
||||||
|
expect(container.querySelector("strong")).toBeNull();
|
||||||
|
expect(container).toHaveTextContent("**边界说明:**issue");
|
||||||
|
expect(container).toHaveTextContent("**边界说明: **issue");
|
||||||
|
expect(container.querySelector("p:last-child")?.textContent).toBe("**边界说明:**issue");
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
["**结论:**继续 & 其他说明。", "结论:继续 & 其他说明。"],
|
||||||
|
["**边界说明:**issue & details", "边界说明:issue & details"],
|
||||||
|
["先写转义标点 \\#,然后 **结论:**继续说明。", "先写转义标点 #,然后 结论:继续说明。"],
|
||||||
|
["\\*\\*字面符号:\\*\\*issue 和 **结论:**继续说明。", "**字面符号:**issue 和 结论:继续说明。"],
|
||||||
|
["**字面符号:**issue 和 **结论:**继续说明。", "**字面符号:**issue 和 结论:继续说明。"],
|
||||||
|
["- 第一行\n **结论:**继续 & 其他说明。", "第一行\n结论:继续 & 其他说明。"],
|
||||||
|
])("recovers CJK bold labels without reinterpreting decoded text: %s", (source, text) => {
|
||||||
|
const { container, rerender } = render(<MarkdownTextRenderer>{source}</MarkdownTextRenderer>);
|
||||||
|
expect(container.querySelectorAll("strong")).toHaveLength(1);
|
||||||
|
expect(container.textContent?.trim()).toBe(text);
|
||||||
|
rerender(<MarkdownTextRenderer streaming preserveStreamingLayout>{source}</MarkdownTextRenderer>);
|
||||||
|
expect(container.querySelectorAll("strong")).toHaveLength(1);
|
||||||
|
rerender(<MarkdownTextRenderer preserveStreamingLayout>{source}</MarkdownTextRenderer>);
|
||||||
|
expect(container.querySelectorAll("strong")).toHaveLength(1);
|
||||||
|
expect(container.textContent?.trim()).toBe(text);
|
||||||
|
});
|
||||||
|
|
||||||
it("adds line numbers to multiline fenced code without changing inline code", () => {
|
it("adds line numbers to multiline fenced code without changing inline code", () => {
|
||||||
render(
|
render(
|
||||||
<MarkdownTextRenderer highlightCode={false}>
|
<MarkdownTextRenderer highlightCode={false}>
|
||||||
|
|||||||
Reference in new issue
Block a user