Skip to content

Commit 87d0562

Browse files
committed
fix(web): render LaTeX in chat messages
Render inline and display math with KaTeX while preserving code spans, fenced code blocks, and Markdown source offsets. Built with GPT Soul.
1 parent dd7bc14 commit 87d0562

9 files changed

Lines changed: 320 additions & 7 deletions

File tree

‎apps/web/package.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@
3838
"jose": "catalog:",
3939
"jsonc-parser": "3.3.1",
4040
"jszip": "3.10.1",
41+
"katex": "^0.16.47",
4142
"lexical": "^0.41.0",
4243
"lucide-react": "^0.564.0",
4344
"react": "19.2.6",
4445
"react-dom": "19.2.6",
4546
"react-markdown": "^10.1.0",
47+
"rehype-katex": "^7.0.1",
4648
"rehype-raw": "^7.0.0",
4749
"rehype-sanitize": "^6.0.0",
4850
"remark-breaks": "^4.0.0",
4951
"remark-gfm": "^4.0.1",
52+
"remark-math": "^6.0.0",
5053
"tailwind-merge": "^3.4.0",
5154
"zustand": "^5.0.11"
5255
},

‎apps/web/src/components/ChatMarkdown.test.tsx‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,77 @@ describe("ChatMarkdown streaming", () => {
205205
});
206206
});
207207

208+
describe("ChatMarkdown math", () => {
209+
it.each([true, false])(
210+
"renders inline and display LaTeX with accessible MathML when parseRawHtml=%s",
211+
(parseRawHtml) => {
212+
const html = renderToStaticMarkup(
213+
<ChatMarkdown
214+
cwd="/tmp/project"
215+
parseRawHtml={parseRawHtml}
216+
text={String.raw`Euler wrote $e^{i\pi} + 1 = 0$.
217+
218+
$$
219+
\int_0^1 x^2\,dx = \frac{1}{3}
220+
$$`}
221+
/>,
222+
);
223+
224+
expect(html).toContain("katex");
225+
expect(html).toContain("katex-display");
226+
expect(html).toContain("<math");
227+
expect(html).toContain("e^{i\\pi} + 1 = 0");
228+
},
229+
);
230+
231+
it.each([true, false])(
232+
"renders parenthesis and bracket LaTeX delimiters when parseRawHtml=%s",
233+
(parseRawHtml) => {
234+
const html = renderToStaticMarkup(
235+
<ChatMarkdown
236+
cwd="/tmp/project"
237+
parseRawHtml={parseRawHtml}
238+
text={String.raw`GenCast represents \(F\) using samples \(x_1,\ldots,x_M\).
239+
240+
\[
241+
\boxed{\widehat{\operatorname{CRPS}} = \frac{1}{M}\sum_{i=1}^{M}|x_i-y|}
242+
\]`}
243+
/>,
244+
);
245+
246+
expect(html).toContain("katex");
247+
expect(html).toContain("katex-display");
248+
expect(html).toContain("GenCast represents ");
249+
expect(html).toContain("x_1,\\ldots,x_M");
250+
expect(html).toContain("\\boxed{\\widehat{\\operatorname{CRPS}}");
251+
},
252+
);
253+
254+
it("does not interpret LaTeX delimiters inside code", () => {
255+
const markdown = [
256+
"Render \\(x^2\\), but keep `\\(inlineExample\\)` unchanged.",
257+
"",
258+
"```text",
259+
String.raw`\[displayExample\]`,
260+
"```",
261+
].join("\n");
262+
const html = renderToStaticMarkup(<ChatMarkdown cwd="/tmp/project" text={markdown} />);
263+
264+
expect(html).toContain("katex");
265+
expect(html).toContain("\\(inlineExample\\)");
266+
expect(html).toContain("\\[displayExample\\]");
267+
});
268+
269+
it("keeps a lone dollar amount as text", () => {
270+
const html = renderToStaticMarkup(
271+
<ChatMarkdown cwd="/tmp/project" text="The build costs $20." />,
272+
);
273+
274+
expect(html).toContain("$20");
275+
expect(html).not.toContain("katex");
276+
});
277+
});
278+
208279
describe("canUseMarkdownFileShellActions", () => {
209280
const environmentId = EnvironmentId.make("environment-1");
210281

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ import React, {
6767
import type { Components, Options as ReactMarkdownOptions } from "react-markdown";
6868
import ReactMarkdown from "react-markdown";
6969
import { defaultUrlTransform } from "react-markdown";
70+
import rehypeKatex from "rehype-katex";
7071
import rehypeRaw from "rehype-raw";
7172
import rehypeSanitize, { defaultSchema } from "rehype-sanitize";
7273
import remarkBreaks from "remark-breaks";
7374
import { parseAssistantCitationHref } from "@t3tools/shared/assistantCitations";
7475
import { AssistantCitationChip } from "./chat/AssistantCitationChip";
7576
import remarkGfm from "remark-gfm";
77+
import remarkMath from "remark-math";
7678
import { remarkGithubAlerts } from "../markdown-github-alerts";
7779
import {
7880
artifactTemplateFromHastProperties,
@@ -126,6 +128,7 @@ import {
126128
serializeTableElementToMarkdown,
127129
} from "../markdown-clipboard";
128130
import { remarkNormalizeListItemIndentation } from "../markdown-list-indentation";
131+
import { normalizeLatexDelimiters } from "../markdown-latex";
129132
import {
130133
extractMarkdownLinkHrefs,
131134
isWindowsDrivePathHref,
@@ -396,6 +399,7 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
396399

397400
const CHAT_MARKDOWN_REMARK_PLUGINS = [
398401
remarkGfm,
402+
remarkMath,
399403
remarkGithubAlerts,
400404
remarkNormalizeListItemIndentation,
401405
remarkCodexDirectives,
@@ -405,6 +409,7 @@ const CHAT_MARKDOWN_REMARK_PLUGINS = [
405409

406410
const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
407411
remarkGfm,
412+
remarkMath,
408413
remarkGithubAlerts,
409414
remarkNormalizeListItemIndentation,
410415
remarkCodexDirectives,
@@ -413,10 +418,15 @@ const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
413418
remarkNormalizeLinksAndTagInlineCode,
414419
] satisfies NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
415420

416-
const CHAT_MARKDOWN_REHYPE_PLUGINS = [
421+
const CHAT_MARKDOWN_REHYPE_PLUGINS = [rehypeKatex] satisfies NonNullable<
422+
ReactMarkdownOptions["rehypePlugins"]
423+
>;
424+
425+
const CHAT_MARKDOWN_RAW_HTML_REHYPE_PLUGINS = [
417426
rehypeRaw,
418427
rehypePreserveImageSourceMeta,
419428
[rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],
429+
rehypeKatex,
420430
] satisfies NonNullable<ReactMarkdownOptions["rehypePlugins"]>;
421431

422432
/** GitHub's own five alert kinds, in its colors: the glyph names the urgency, the title says it. */
@@ -2908,6 +2918,7 @@ function ChatMarkdown({
29082918
],
29092919
[extraRemarkPlugins, lineBreaks],
29102920
);
2921+
const normalizedText = useMemo(() => normalizeLatexDelimiters(text), [text]);
29112922

29122923
// react-markdown converts unparsed HTML nodes to text when skipHtml is false.
29132924
// Keep that behavior explicit because literal mode depends on escaping the
@@ -2923,12 +2934,14 @@ function ChatMarkdown({
29232934
<ChatMarkdownRendererContext value={componentState}>
29242935
<ReactMarkdown
29252936
remarkPlugins={remarkPlugins}
2926-
rehypePlugins={parseRawHtml ? CHAT_MARKDOWN_REHYPE_PLUGINS : undefined}
2937+
rehypePlugins={
2938+
parseRawHtml ? CHAT_MARKDOWN_RAW_HTML_REHYPE_PLUGINS : CHAT_MARKDOWN_REHYPE_PLUGINS
2939+
}
29272940
skipHtml={false}
29282941
components={CHAT_MARKDOWN_COMPONENTS}
29292942
urlTransform={markdownUrlTransform}
29302943
>
2931-
{text}
2944+
{normalizedText}
29322945
</ReactMarkdown>
29332946
</ChatMarkdownRendererContext>
29342947
{localMediaPreview ? (

‎apps/web/src/index.css‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,13 @@ code {
16681668
color: var(--contrast-muted-foreground);
16691669
}
16701670

1671+
.chat-markdown .katex-display {
1672+
margin: 0.65rem 0;
1673+
overflow-x: auto;
1674+
overflow-y: hidden;
1675+
padding-block: 0.15rem;
1676+
}
1677+
16711678
.chat-markdown section[data-footnotes] {
16721679
margin-top: 1.25rem;
16731680
border-top: 1px solid var(--contrast-border);

‎apps/web/src/main.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react";
22
import ReactDOM from "react-dom/client";
33
import { createHashHistory, createBrowserHistory } from "@tanstack/react-router";
44

5+
import "katex/dist/katex.min.css";
56
import "./index.css";
67

78
import { isElectron } from "./env";
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
import { normalizeLatexDelimiters } from "./markdown-latex";
3+
4+
describe("normalizeLatexDelimiters", () => {
5+
it("normalizes parenthesis and bracket delimiters without shifting source offsets", () => {
6+
const markdown = "Inline \\(x\\)\n\\[\ny\n\\]\n- [ ] task";
7+
const normalized = normalizeLatexDelimiters(markdown);
8+
9+
expect(normalized).toBe("Inline $$x$$\n$$\ny\n$$\n- [ ] task");
10+
expect(normalized).toHaveLength(markdown.length);
11+
});
12+
13+
it("leaves escaped delimiters and code unchanged", () => {
14+
const markdown = [
15+
String.raw`Literal \\(x\\) and \[math\].`,
16+
"Inline code: `\\(code\\)`.",
17+
"",
18+
"~~~text",
19+
String.raw`\[fenced\]`,
20+
"~~~",
21+
String.raw` \(indented\)`,
22+
].join("\n");
23+
24+
expect(normalizeLatexDelimiters(markdown)).toBe(
25+
markdown.replace(String.raw`\[math\]`, () => "$$math$$"),
26+
);
27+
});
28+
});

‎apps/web/src/markdown-latex.ts‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const LATEX_DELIMITERS = ["\\(", "\\)", "\\[", "\\]"] as const;
2+
3+
function isEscaped(value: string, index: number): boolean {
4+
let slashCount = 0;
5+
for (let cursor = index - 1; cursor >= 0 && value[cursor] === "\\"; cursor -= 1) {
6+
slashCount += 1;
7+
}
8+
return slashCount % 2 === 1;
9+
}
10+
11+
function startingFence(line: string): { marker: "`" | "~"; length: number } | null {
12+
const match = /^ {0,3}(`{3,}|~{3,})/.exec(line);
13+
const run = match?.[1];
14+
if (!run) return null;
15+
return { marker: run[0] as "`" | "~", length: run.length };
16+
}
17+
18+
function closesFence(line: string, fence: { marker: "`" | "~"; length: number }): boolean {
19+
const match = /^ {0,3}(`+|~+)[ \t]*$/.exec(line);
20+
const run = match?.[1];
21+
return Boolean(run && run[0] === fence.marker && run.length >= fence.length);
22+
}
23+
24+
/** Converts TeX delimiters before Markdown consumes their leading backslashes. */
25+
export function normalizeLatexDelimiters(markdown: string): string {
26+
if (!LATEX_DELIMITERS.some((delimiter) => markdown.includes(delimiter))) return markdown;
27+
28+
let fence: { marker: "`" | "~"; length: number } | null = null;
29+
let inlineCodeTicks = 0;
30+
31+
return markdown
32+
.split(/(\r?\n)/)
33+
.map((line) => {
34+
if (line === "\n" || line === "\r\n") return line;
35+
36+
if (fence) {
37+
if (closesFence(line, fence)) fence = null;
38+
return line;
39+
}
40+
41+
if (inlineCodeTicks === 0) {
42+
const openingFence = startingFence(line);
43+
if (openingFence) {
44+
fence = openingFence;
45+
return line;
46+
}
47+
if (/^(?: {4}|\t)/.test(line)) return line;
48+
}
49+
50+
let normalized = "";
51+
for (let index = 0; index < line.length; index += 1) {
52+
const character = line[index];
53+
54+
if (character === "`" && !isEscaped(line, index)) {
55+
let runLength = 1;
56+
while (line[index + runLength] === "`") runLength += 1;
57+
if (inlineCodeTicks === 0) inlineCodeTicks = runLength;
58+
else if (inlineCodeTicks === runLength) inlineCodeTicks = 0;
59+
normalized += "`".repeat(runLength);
60+
index += runLength - 1;
61+
continue;
62+
}
63+
64+
if (inlineCodeTicks === 0 && character === "\\" && !isEscaped(line, index)) {
65+
const delimiter = line[index + 1];
66+
if (delimiter === "(" || delimiter === ")" || delimiter === "[" || delimiter === "]") {
67+
normalized += "$$";
68+
index += 1;
69+
continue;
70+
}
71+
}
72+
73+
normalized += character;
74+
}
75+
return normalized;
76+
})
77+
.join("");
78+
}

‎docs/user/composer.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ File links refer to the environment's machine, including when you connect remote
103103
Previews use the original file, even outside the workspace. Moving or deleting it
104104
can break the preview, so save a copy if you need to keep it.
105105

106+
## Math in messages
107+
108+
Web and desktop render LaTeX in messages. Inline math accepts `$...$` or `\(...\)`. Display math
109+
accepts `$$...$$` or `\[...\]`:
110+
111+
```markdown
112+
Euler's identity is $e^{i\pi} + 1 = 0$.
113+
114+
$$
115+
\int_0^1 x^2\,dx = \frac{1}{3}
116+
$$
117+
```
118+
119+
Escape a dollar sign as `\$` when two prices or other dollar amounts in the same paragraph could
120+
look like inline math. Mobile currently displays the source text instead.
121+
106122
## Files outside the workspace
107123

108124
Follow an agent's file link to read a report or other file outside the workspace.

0 commit comments

Comments
 (0)