Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): keep malformed math text in normal color (#2583)

hushaowu-rh82a04d4ab6ae
diff --git a/apps/desktop/src/renderer/__tests__/markdownMathRendering.test.ts b/apps/desktop/src/renderer/__tests__/markdownMathRendering.test.ts
index 39e1bddc94..818f405614 100644
--- a/apps/desktop/src/renderer/__tests__/markdownMathRendering.test.ts
+++ b/apps/desktop/src/renderer/__tests__/markdownMathRendering.test.ts
@@ -32,7 +32,7 @@ function renderMath(markdown: string): string {
return renderToStaticMarkup(
createElement(ReactMarkdown, {
remarkPlugins: [remarkGfm, remarkMath, remarkStrictInlineMath],
- rehypePlugins: [[rehypeKatex, { strict: 'ignore' }]],
+ rehypePlugins: [[rehypeKatex, { strict: 'ignore', errorColor: 'inherit' }]],
children: normalizeMathDelimiters(markdown),
}),
);
@@ -86,10 +86,33 @@ describe('math rendering pipeline (remark-math + rehype-katex)', () => {
expect(html).not.toContain('class="katex"');
});
- it('非法 LaTeX 不抛异常,渲染为错误标记', () => {
+ it('跨行 inline math 降级为正文,不触发 KaTeX', () => {
+ const html = renderMath('说明 $第一行\n第二行$ 结束');
+ expect(html).not.toContain('class="katex"');
+ expect(html).not.toContain('katex-error');
+ expect(html).toContain('第一行');
+ expect(html).toContain('第二行');
+ });
+
+ it('截图同形的非法 display LaTeX 保持正文色', () => {
+ const html = renderMath(String.raw`$$
+\boxed{\begin{array}{l}
+(\psi^{(0)},x_0) \xrightarrow{\text{流出}} \text{背景} \\
+\underset{\displaystyle \rotatebox[origin=c]{-90}{$\rightsquigarrow$}}{R_1}
+\end{array}}
+$$`);
+
+ expect(html).toContain('katex-error');
+ expect(html).toContain('\\rotatebox');
+ expect(html).toContain('style="color:inherit"');
+ expect(html).not.toContain('var(--error-fg)');
+ });
+
+ it('非法单行 LaTeX 保持正文色,不使用错误红 token', () => {
const html = renderMath('$\\frac{$');
- // rehype-katex 捕获解析错误,输出 katex-error 或原文,组件不崩
- expect(typeof html).toBe('string');
+ expect(html).toContain('katex-error');
+ expect(html).toContain('style="color:inherit"');
+ expect(html).not.toContain('var(--error-fg)');
});
});
diff --git a/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsx b/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsx
index 6bddbe0cf9..7308a7fa1c 100644
--- a/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsx
+++ b/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsx
@@ -228,7 +228,7 @@ const REMARK_PLUGINS_PRIVILEGED: PluggableList = [
// rehypeKatex 必须排在 rehypeHighlight 之前:remark-math 产出的 hast 是
// `<code class="language-math ...">`,先让 katex 消费掉,否则 highlight 会往
// 里面塞 hljs span 破坏纯文本结构。strict:'ignore' 静默非致命 LaTeX 告警;
-// errorColor 走语义豁免 error token,解析失败的公式以错误色显示原文。
+// 解析失败的公式回落为正文色原文,避免模型格式错误把普通聊天染成错误红。
// rehypeMathBlockMarker 紧随 rehypeKatex:把裸 `<span class="katex-display">`
// 包进 `<div data-math-block>`,让下方 div 渲染器能挂「复制为图片」工具栏
// (components 映射只认 tagName,认不了 class)。
@@ -237,7 +237,7 @@ const REMARK_PLUGINS_PRIVILEGED: PluggableList = [
// 打 data-fenced-code 供下方 code 渲染器按结构(而非语言标注)分派。
const REHYPE_PLUGINS: PluggableList = [
rehypeSlug,
- [rehypeKatex, { strict: 'ignore', errorColor: 'var(--error-fg)' }],
+ [rehypeKatex, { strict: 'ignore', errorColor: 'inherit' }],
rehypeMathBlockMarker,
rehypeHighlight,
rehypeFencedCodeMarker,
diff --git a/apps/desktop/src/renderer/components/chat/remarkStrictInlineMath.ts b/apps/desktop/src/renderer/components/chat/remarkStrictInlineMath.ts
index 6abe263676..0c398bb3ca 100644
--- a/apps/desktop/src/renderer/components/chat/remarkStrictInlineMath.ts
+++ b/apps/desktop/src/renderer/components/chat/remarkStrictInlineMath.ts
@@ -9,6 +9,8 @@
* matcher 保持一致**(apps/mobile messageMarkdown.ts),两端语义对齐:
*
* - 内容首/尾是空白 → 降级(「$5 和 $」这类货币配对)。
+ * - 内容含换行 → 降级。mobile 的 inline matcher 只接受单行公式,跨行内容
+ * 是正文边界误配,不能交给 KaTeX 变成跨行排版或错误色文本。
* - 内容含反引号 → 降级(合法 LaTeX 无 backtick,必是跨 code span 误判)。
* - 闭合 $ 后紧跟数字 → 降级(「$5和$10」CJK 无空格形态)。
*
@@ -42,7 +44,7 @@ const remarkStrictInlineMath: Plugin<[], Root> = () => {
const inner = raw.replace(/^\$+/, '').replace(/\$+$/, '');
const nextChar = source[end] ?? '';
const loose =
- /^\s|\s$/.test(inner) || inner.includes('`') || /^\d/.test(nextChar);
+ /^\s|\s$/.test(inner) || inner.includes('\n') || inner.includes('`') || /^\d/.test(nextChar);
if (!loose) return;
const text: Text = { type: 'text', value: raw };
parent.children[index] = text;