Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(markdown): keep math classes through sanitize for TeX bracket display math
vastsa926688361932
diff --git a/apps/desktop/src/components/Markdown.tsx b/apps/desktop/src/components/Markdown.tsxindex 4466bf29e..02b837c09 100644--- a/apps/desktop/src/components/Markdown.tsx+++ b/apps/desktop/src/components/Markdown.tsx@@ -786,11 +786,15 @@ const markdownComponents: Components = {const staticRemarkPlugins = [remarkGfm, remarkMath];-// Extend the default schema only for the media elements rendered above.+// Extend the default schema only for the media elements rendered above, plus+// `remark-math`'s math classes on `<code>`: the default `language-*` allow list+// drops `math-display`, which leaves `rehype-katex` rendering TeX `\[ … \]`+// (single-line or mid-paragraph) as inline math instead of display math.const sanitizeSchema = {...defaultSchema,attributes: {...defaultSchema.attributes,+ code: [["className", /^language-./, "math-inline", "math-display"]],img: [...(defaultSchema.attributes?.img || []), "src", "alt", "title", "className"],audio: ["src", "controls", "preload", "className"],video: ["src", "controls", "preload", "className", "poster"],diff --git a/apps/desktop/test/markdown-math-rendering.test.mjs b/apps/desktop/test/markdown-math-rendering.test.mjsnew file mode 100644index 000000000..114ee4e37--- /dev/null+++ b/apps/desktop/test/markdown-math-rendering.test.mjs@@ -0,0 +1,88 @@+import assert from "node:assert/strict";+import test from "node:test";+import { createElement } from "react";+import { renderToStaticMarkup } from "react-dom/server";+import { createInstance } from "i18next";+import { I18nextProvider } from "react-i18next";+import { catalogs } from "@pi-desktop/i18n";+import { createServer } from "vite";+import { fileURLToPath } from "node:url";++/*+ * Issue #449: assistant replies that emit TeX `\[ … \]` display delimiters used+ * to reach the transcript as raw text. These cases render the real+ * `Markdown.tsx` through Vite SSR, so the whole production chain runs:+ * block splitting, `remark-math`, the TeX bracket normalizer, `rehype-raw`,+ * `rehype-sanitize` and `rehype-katex`.+ */+test("TeX bracket math renders through the production Markdown pipeline", async () => {+ const server = await createServer({+ root: fileURLToPath(new URL("..", import.meta.url)),+ configFile: false,+ server: { middlewareMode: true, hmr: false, ws: false },+ esbuild: { jsx: "automatic" },+ appType: "custom",+ optimizeDeps: { noDiscovery: true, include: [] },+ });+ const originalDocument = globalThis.document;+ try {+ // `useThemeMode` reads the theme off the document during render.+ globalThis.document = { documentElement: { dataset: {} } };+ const { Markdown } = await server.ssrLoadModule(+ "/src/components/Markdown.tsx",+ );+ const i18n = createInstance();+ await i18n.init({+ lng: "en",+ resources: { en: { translation: catalogs.en } },+ });+ const render = (source) =>+ renderToStaticMarkup(+ createElement(+ I18nextProvider,+ { i18n },+ createElement(Markdown, { source }),+ ),+ );+ const display = (html) => html.includes("katex-display");+ const inline = (html) =>+ html.includes('class="katex"') && !html.includes("katex-display");++ // The issue's exact replies: a display formula on its own lines.+ const issueMultiline = render("\\[\nD \\leftrightarrow H\n\\]");+ assert.match(issueMultiline, /katex-display/);+ assert.doesNotMatch(issueMultiline, /\\\[/);+ const issueText = render(+ "\\[\n\\text{Data-RSI} \\leftrightarrow \\text{Harness-RSI}\n\\]",+ );+ assert.match(issueText, /katex-display/);+ // KaTeX keeps the TeX source in its MathML annotation, so only the bracket+ // delimiters (which used to leak as visible text) are checked here.+ assert.doesNotMatch(issueText, /\\\[|\\\]/);++ // TeX brackets are display math in every position, not only as a block:+ // `rehype-sanitize` has to keep `remark-math`'s math class for this to hold.+ for (const source of [+ "\\[a+b\\]",+ "Formally \\[ a+b \\] holds.",+ "Here is the mapping:\n\\[\nD \\leftrightarrow H\n\\]",+ "prose in between\n\n\\[\nX \\leftrightarrow Y\n\\]",+ ]) {+ assert.equal(display(render(source)), true, `expected display math: ${source}`);+ }++ // Inline code never turns into math. Fenced blocks and mermaid fences are+ // exercised at DOM level instead: `HighlightedCode` reads a browser-only+ // external store, so a fenced block cannot be server-rendered at all.+ const codeSpan = render("run `\\(inline\\)` now");+ assert.match(codeSpan, /<code[^>]*>\\\(inline\\\)<\/code>/);+ assert.doesNotMatch(codeSpan, /katex/);++ // Escaped and unmatched markers stay literal.+ assert.doesNotMatch(render("\\\\(escaped\\\\)"), /katex/);+ assert.doesNotMatch(render("text \\(unmatched"), /katex/);+ } finally {+ globalThis.document = originalDocument;+ await server.close();+ }+});diff --git a/docs/spec/04-ux/08-component-spec.md b/docs/spec/04-ux/08-component-spec.mdindex cfd6b6fdc..655f823c2 100644--- a/docs/spec/04-ux/08-component-spec.md+++ b/docs/spec/04-ux/08-component-spec.md@@ -1683,8 +1683,9 @@ Renderer: `apps/desktop/src/components/Markdown.tsx` + `apps/desktop/src/lib/shior `\[…\]`). Raw HTML isparsed by `rehype-raw` and immediately constrained by the extended`rehype-sanitize` default schema; only the renderer-owned audio/video/source- additions are admitted. KaTeX's Vite-inlined WOFF2 fonts are allowed by the- renderer's `font-src 'self' data:` CSP directive.+ additions and the `math-inline`/`math-display` classes on `code` (which keep+ TeX `\[…\]` in display layout) are admitted. KaTeX's Vite-inlined WOFF2 fonts+ are allowed by the renderer's `font-src 'self' data:` CSP directive.- **Mermaid diagrams (D165)**: a completed `mermaid` fenced block in assistantanswer prose renders through the official Mermaid package. The dependency isdynamically imported only when a diagram approaches the viewport; Mermaid'sdiff --git a/docs/zh-CN/spec/04-ux/08-component-spec.md b/docs/zh-CN/spec/04-ux/08-component-spec.mdindex 5a03e052d..5d0b9c3ac 100644--- a/docs/zh-CN/spec/04-ux/08-component-spec.md+++ b/docs/zh-CN/spec/04-ux/08-component-spec.md@@ -1251,7 +1251,8 @@ Renderer: `apps/desktop/src/components/Markdown.tsx` + `apps/desktop/src/lib/s`remark-math` + `rehype-katex`(内联 `$…$` 或 `\(…\)`,显示 `$$…$$`或 `\[…\]`)。原始 HTML 是由 `rehype-raw` 解析并立即受扩展约束- `rehype-sanitize` 默认架构;仅渲染器拥有的 audio/video/source+ `rehype-sanitize` 默认架构;仅渲染器拥有的 audio/video/source,以及+ `code` 上的 `math-inline`/`math-display` 类(保证 TeX `\[…\]` 使用块级布局)允许添加。 KaTeX 的 Vite 内联 WOFF2 字体被允许渲染器的 `font-src 'self' data:` CSP 指令。- **美人鱼图(D165)**:助手中完成的 `mermaid` 围栏块