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 multi-line TeX bracket math in one block (#541)
PI-Desktop5c00e40701eb
diff --git a/apps/desktop/src/components/Markdown.tsx b/apps/desktop/src/components/Markdown.tsxindex 02b837c09..8ef3b38c1 100644--- a/apps/desktop/src/components/Markdown.tsx+++ b/apps/desktop/src/components/Markdown.tsx@@ -869,12 +869,14 @@ function useBlocks(source: string): string[] {const Block = memo(function MarkdownBlock({raw,+ originalRaw,sourceOffset,renderDiagrams,workspaceRoot,baseDir,}: {raw: string;+ originalRaw: string;sourceOffset: number;renderDiagrams: boolean;workspaceRoot?: string | null;@@ -887,14 +889,16 @@ const Block = memo(function MarkdownBlock({}),[raw, renderDiagrams],);- const normalized = useMemo(() => normalizeLatexMathDelimiters(raw), [raw]);const remarkPlugins = useMemo(() => [...staticRemarkPlugins,- remarkLatexBracketDisplay(raw),+ // `originalRaw` still carries the TeX `\[ … \]` delimiters so the+ // bracket-display plugin can promote them to display math after+ // remark-math parses the pre-normalized `$$ … $$` form.+ remarkLatexBracketDisplay(originalRaw),remarkChatFileLinks(workspaceRoot, baseDir),],- [raw, workspaceRoot, baseDir],+ [originalRaw, workspaceRoot, baseDir],);const positionedRehypePlugins = useMemo(() => [...rehypePlugins!, [rehypeSourcePositions, { offset: sourceOffset }]] as Options["rehypePlugins"],@@ -907,7 +911,7 @@ const Block = memo(function MarkdownBlock({rehypePlugins={positionedRehypePlugins}components={markdownComponents}>- {normalized}+ {raw}</ReactMarkdown></MarkdownBlockContext.Provider>);@@ -924,17 +928,32 @@ export const Markdown = memo(function Markdown({baseDir?: string;}) {const workspaceRoot = useAppStore((s) => s.workspace?.path);- const blocks = useBlocks(source);+ // Normalize once at the source level: marked's block lexer runs on the raw+ // text and would otherwise split `\[ … \]` display math whose body puts a+ // lone `=`/`-` (setext underline) or `+`/`*` (list marker) on its own line,+ // stranding `\[` and `\]` in different blocks so the delimiters escape as+ // literal `[`/`]`. The normalizer both rewrites the delimiters to `$$` and+ // flattens newlines inside every paired region, keeping the whole formula+ // inside a single markdown block. The rewrite is length-preserving, so we+ // can still slice the original text at the same offsets for downstream+ // plugins that need the pre-normalized delimiters.+ const normalizedSource = useMemo(+ () => normalizeLatexMathDelimiters(source),+ [source],+ );+ const blocks = useBlocks(normalizedSource);let sourceOffset = 0;return (<MarkdownBaseDirContext.Provider value={baseDir ?? ""}>{blocks.map((raw, i) => {const start = sourceOffset;sourceOffset = start + raw.length;+ const originalRaw = source.slice(start, start + raw.length);return (<Blockkey={i}raw={raw}+ originalRaw={originalRaw}sourceOffset={start}renderDiagrams={renderDiagrams}workspaceRoot={workspaceRoot}diff --git a/apps/desktop/src/lib/latex-math.ts b/apps/desktop/src/lib/latex-math.tsindex a5c13b340..5eece851c 100644--- a/apps/desktop/src/lib/latex-math.ts+++ b/apps/desktop/src/lib/latex-math.ts@@ -56,6 +56,16 @@ export function normalizeLatexMathDelimiters(source: string): string {if (isDelimiter(index, math.close)) {output[math.open] = output[math.open + 1] = "$";output[index] = output[index + 1] = "$";+ // Marked lexes the raw source before remark-math sees it, so any+ // newline inside a `\[ … \]` block that is followed by a lone `=`,+ // `-`, `+` or `*` line collapses the display math into a setext+ // heading or a list item and splits `\[` from `\]` across blocks.+ // KaTeX treats intra-formula whitespace the same, so replacing the+ // paired region's newlines with spaces keeps the math in one block+ // without changing what KaTeX renders.+ for (let j = math.open + 2; j < index; j++) {+ if (output[j] === "\n") output[j] = " ";+ }math = null;i += 2;} else {diff --git a/apps/desktop/test/latex-math.test.mjs b/apps/desktop/test/latex-math.test.mjsindex 1472cad97..4c0fddd11 100644--- a/apps/desktop/test/latex-math.test.mjs+++ b/apps/desktop/test/latex-math.test.mjs@@ -15,7 +15,7 @@ test("normalizes TeX math delimiters without changing source length", () => {[String.raw`before \(a+b\) after`, "before $$a+b$$ after"],[String.raw`\[a+b-\]`, "$$\na+b\n$$"],+\]`, "$$ a+b $$"],]) {const normalized = normalizeLatexMathDelimiters(source);assert.equal(normalized, expected);diff --git a/apps/desktop/test/markdown-math-rendering.test.mjs b/apps/desktop/test/markdown-math-rendering.test.mjsindex 114ee4e37..1183b264b 100644--- a/apps/desktop/test/markdown-math-rendering.test.mjs+++ b/apps/desktop/test/markdown-math-rendering.test.mjs@@ -81,6 +81,33 @@ test("TeX bracket math renders through the production Markdown pipeline", async// Escaped and unmatched markers stay literal.assert.doesNotMatch(render("\\\\(escaped\\\\)"), /katex/);assert.doesNotMatch(render("text \\(unmatched"), /katex/);++ // Issue #541: display math whose body puts `=`, `-`, `+` or `*` on their+ // own lines used to be shredded by marked's block lexer (setext headings /+ // list markers), which left `\[` and `\]` in different blocks so the+ // delimiters escaped through as literal `[` / `]` and the equation body+ // rendered as prose. All four markers must now stay inside one math node.+ const setextEquals = render(+ "\\[\n\\mathbf{a}\n=\n\\frac{\\partial\\mathbf{u}}{\\partial t}\n\\]",+ );+ assert.match(setextEquals, /katex-display/);+ assert.doesNotMatch(setextEquals, /<h[1-6][ >]/);+ assert.doesNotMatch(setextEquals, /\\\[|\\\]/);++ const setextDash = render("\\[\nx\n-\ny\n\\]");+ assert.match(setextDash, /katex-display/);+ assert.doesNotMatch(setextDash, /<h[1-6][ >]/);++ const listPlus = render(+ "\\[\n\\boxed{\na\n+\nb\n+\nc\n}\n\\]",+ );+ assert.match(listPlus, /katex-display/);+ assert.doesNotMatch(listPlus, /<(ul|ol|li)[ >]/);+ assert.doesNotMatch(listPlus, /\\\[|\\\]/);++ const listStar = render("\\[\n*\na\n\\]");+ assert.match(listStar, /katex-display/);+ assert.doesNotMatch(listStar, /<(ul|ol|li)[ >]/);} finally {globalThis.document = originalDocument;await server.close();