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
@@ -8,6 +8,7 @@ import {
|
||||
} from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Check, Globe2 } from "lucide-react";
|
||||
import { decodeString } from "micromark-util-decode-string";
|
||||
import remarkBreaks from "remark-breaks";
|
||||
import remarkGfm from "remark-gfm";
|
||||
import remarkMath from "remark-math";
|
||||
@@ -54,6 +55,10 @@ type MarkdownAstNode = {
|
||||
type: string;
|
||||
value?: string;
|
||||
children?: MarkdownAstNode[];
|
||||
position?: {
|
||||
start: { offset: number };
|
||||
end: { offset: number };
|
||||
};
|
||||
data?: {
|
||||
hName?: string;
|
||||
};
|
||||
@@ -245,48 +250,58 @@ function remarkSafeHtmlSubset() {
|
||||
};
|
||||
}
|
||||
|
||||
// Recover a common model-output edge case that CommonMark leaves as literal
|
||||
// text: `**结论。**如果`, with no separator after the closing delimiter.
|
||||
const CJK_AFTER_STRONG =
|
||||
/(?<!\\)\*\*([^*\r\n]+?)(?<!\\)\*\*(?=[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}])/gu;
|
||||
// Model output can put CJK punctuation or whitespace before a closing `**`
|
||||
// followed immediately by a word, which CommonMark leaves as literal text.
|
||||
const STRONG_BEFORE_WORD = /(?<!\\)\*\*([^\s*][^*\r\n]*?)(?<!\\)\*\*(?=[\p{L}\p{N}])/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;
|
||||
node.children = node.children.flatMap((child) => {
|
||||
if (child.type !== "text" || !child.value?.includes("**")) {
|
||||
normalizeCjkStrongBoundaries(child);
|
||||
if (child.type !== "text" || !child.value?.includes("**") || !child.position) {
|
||||
normalizeCjkStrongBoundaries(child, source);
|
||||
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[] = [];
|
||||
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;
|
||||
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({
|
||||
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 < child.value.length) replacement.push(safeText(child.value.slice(cursor)));
|
||||
if (cursor < textSource.length) replacement.push(safeText(decodeString(textSource.slice(cursor))));
|
||||
return replacement;
|
||||
});
|
||||
}
|
||||
|
||||
function remarkCjkStrongBoundaries() {
|
||||
return (tree: MarkdownAstNode) => {
|
||||
normalizeCjkStrongBoundaries(tree);
|
||||
return (tree: MarkdownAstNode, file: { toString(): string }) => {
|
||||
normalizeCjkStrongBoundaries(tree, file.toString());
|
||||
};
|
||||
}
|
||||
|
||||
const remarkPlugins: NonNullable<StreamdownProps["remarkPlugins"]> = [
|
||||
remarkCjkStrongBoundaries,
|
||||
remarkBreaks,
|
||||
remarkGfm,
|
||||
[remarkMath, { singleDollarTextMath: false }],
|
||||
remarkTexMath,
|
||||
remarkCjkStrongBoundaries,
|
||||
remarkSafeHtmlSubset,
|
||||
];
|
||||
type MathPlugin = typeof import("@/lib/markdown-math").default;
|
||||
|
||||
Reference in new issue
Block a user