Skip to content

Commit 7aa5bc8

Browse files
committed
fix(web): render LaTeX equations in chat
1 parent 0b83045 commit 7aa5bc8

11 files changed

Lines changed: 639 additions & 4 deletions

‎apps/web/package.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,18 @@
4141
"jose": "catalog:",
4242
"jsonc-parser": "3.3.1",
4343
"jszip": "3.10.1",
44+
"katex": "^0.16.47",
4445
"lexical": "^0.41.0",
4546
"lucide-react": "^0.564.0",
47+
"micromark-util-character": "^2.1.1",
4648
"react": "19.2.6",
4749
"react-dom": "19.2.6",
4850
"react-markdown": "^10.1.0",
4951
"rehype-raw": "^7.0.0",
5052
"rehype-sanitize": "^6.0.0",
5153
"remark-breaks": "^4.0.0",
5254
"remark-gfm": "^4.0.1",
55+
"remark-math": "^6.0.0",
5356
"tailwind-merge": "^3.4.0",
5457
"zustand": "^5.0.11"
5558
},
@@ -70,7 +73,10 @@
7073
"@vitejs/plugin-react": "^6.0.0",
7174
"babel-plugin-react-compiler": "1.0.0",
7275
"compression": "^1.8.1",
76+
"micromark-extension-math": "^3.1.0",
77+
"micromark-util-types": "^2.0.2",
7378
"react-test-renderer": "19.2.6",
79+
"remark-parse": "^11.0.0",
7480
"tailwindcss": "^4.0.0",
7581
"unified": "^11.0.5",
7682
"vite": "catalog:",

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

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EnvironmentId } from "@t3tools/contracts";
22
import { act, type ComponentProps, type ReactNode } from "react";
3-
import { renderToStaticMarkup } from "react-dom/server";
3+
import { renderToReadableStream, renderToStaticMarkup } from "react-dom/server";
44
import { create, type ReactTestRenderer } from "react-test-renderer";
55
import { describe, expect, it, vi } from "vite-plus/test";
66

@@ -72,6 +72,80 @@ function codeButton(renderer: ReactTestRenderer, label: string) {
7272
return button.props as ComponentProps<typeof Button>;
7373
}
7474

75+
describe("ChatMarkdown math", () => {
76+
async function renderMath(text: string, parseRawHtml = true, isStreaming = false) {
77+
const stream = await renderToReadableStream(
78+
<ChatMarkdown
79+
cwd={undefined}
80+
text={text}
81+
parseRawHtml={parseRawHtml}
82+
lineBreaks={!parseRawHtml}
83+
isStreaming={isStreaming}
84+
/>,
85+
);
86+
await stream.allReady;
87+
return new Response(stream).text();
88+
}
89+
90+
it.each([true, false])(
91+
"typesets accessible inline and display math with raw HTML=%s",
92+
async (parseRawHtml) => {
93+
const html = await renderMath(
94+
String.raw`Fraction $\frac{1}{2}$ and root \(\sqrt{x}\).` +
95+
"\n\n" +
96+
String.raw`\[\begin{pmatrix}1 & 2 \\ 3 & 4\end{pmatrix}\]`,
97+
parseRawHtml,
98+
);
99+
expect(html).toContain("<mfrac>");
100+
expect(html).toContain("<msqrt>");
101+
expect(html).toContain("<mtable");
102+
expect(html).toContain('<annotation encoding="application/x-tex">\\frac{1}{2}</annotation>');
103+
expect(html).not.toContain("chat-markdown-codeblock");
104+
},
105+
);
106+
107+
it("keeps invalid math readable and renders the completed streamed formula", async () => {
108+
const partial = await renderMath("Before\n\n$$\n\\frac{1}{\n", true, true);
109+
expect(partial).toContain("Before");
110+
expect(partial).toContain("\\frac{1}{");
111+
const complete = await renderMath("Before\n\n$$\n\\frac{1}{2}\n$$\n\nAfter", true, true);
112+
expect(complete).toContain("<mfrac>");
113+
expect(complete).toContain("After");
114+
});
115+
116+
it("does not load external resources or execute HTML from a formula", async () => {
117+
const html = await renderMath(
118+
String.raw`$\includegraphics{https://example.com/tracker.png}$` +
119+
"\n\n" +
120+
String.raw`$\text{<img src=x onerror=alert(1)>}$`,
121+
);
122+
expect(html).not.toContain("<img");
123+
expect(html).toContain("&lt;img");
124+
expect(html).toContain("\\includegraphics");
125+
});
126+
127+
it("does not typeset a completed equation again when more text streams in", async () => {
128+
const katex = (await import("katex")).default;
129+
const render = vi.spyOn(katex, "renderToString");
130+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
131+
let renderer: ReactTestRenderer | undefined;
132+
try {
133+
await act(async () => {
134+
renderer = create(<ChatMarkdown cwd={undefined} text="$x^2$" isStreaming />);
135+
});
136+
expect(render).toHaveBeenCalledTimes(1);
137+
await act(async () => {
138+
renderer!.update(<ChatMarkdown cwd={undefined} text="$x^2$ and more text" isStreaming />);
139+
});
140+
expect(render).toHaveBeenCalledTimes(1);
141+
} finally {
142+
await act(async () => renderer?.unmount());
143+
render.mockRestore();
144+
vi.unstubAllGlobals();
145+
}
146+
});
147+
});
148+
75149
describe("ChatMarkdown context references", () => {
76150
it("renders text and image references through the chip renderer, with readable fallback", async () => {
77151
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import React, {
6262
type KeyboardEvent as ReactKeyboardEvent,
6363
type MouseEvent as ReactMouseEvent,
6464
isValidElement,
65+
lazy,
6566
use,
6667
useCallback,
6768
memo,
@@ -88,6 +89,7 @@ import { parseComposerContextHref } from "@t3tools/shared/composerContextReferen
8889
import { AssistantCitationChip } from "./chat/AssistantCitationChip";
8990
import remarkGfm from "remark-gfm";
9091
import { remarkGithubAlerts } from "../markdown-github-alerts";
92+
import { remarkChatMath } from "../markdown-math";
9193
import {
9294
artifactTemplateFromHastProperties,
9395
CODEX_ARTIFACT_TEMPLATE_HAST_PROPERTIES,
@@ -269,6 +271,7 @@ export function shouldUseMarkdownFileBrowserPrimaryAction(input: {
269271

270272
const EMPTY_MARKDOWN_SKILLS: ReadonlyArray<Pick<ServerProviderSkill, "name" | "displayName">> = [];
271273
const EMPTY_REMARK_PLUGINS: NonNullable<ReactMarkdownOptions["remarkPlugins"]> = [];
274+
const MarkdownMath = lazy(() => import("./chat/MarkdownMath"));
272275

273276
const ARTIFACT_TEMPLATE_ICON_BY_KIND = {
274277
document: FileTextIcon,
@@ -462,7 +465,15 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
462465
attributes: {
463466
...defaultSchema.attributes,
464467
"*": (defaultSchema.attributes?.["*"] ?? []).filter((attribute) => attribute !== "title"),
465-
code: [...(defaultSchema.attributes?.code ?? []), "dataCodeMeta", "dataInlineCode"],
468+
code: [
469+
...(defaultSchema.attributes?.code ?? []).filter(
470+
(attribute) => !Array.isArray(attribute) || attribute[0] !== "className",
471+
),
472+
["className", /^language-./, "math-inline", "math-display"],
473+
"dataCodeMeta",
474+
"dataInlineCode",
475+
"dataMathSource",
476+
],
466477
blockquote: [...(defaultSchema.attributes?.blockquote ?? []), "dataAlert"],
467478
div: [...(defaultSchema.attributes?.div ?? []), ...CODEX_ARTIFACT_TEMPLATE_HAST_PROPERTIES],
468479
a: [...(defaultSchema.attributes?.a ?? []), "dataPullRequestAutolink"],
@@ -482,6 +493,7 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
482493

483494
const CHAT_MARKDOWN_REMARK_PLUGINS = [
484495
remarkGfm,
496+
remarkChatMath,
485497
remarkGithubAlerts,
486498
remarkNormalizeListItemIndentation,
487499
remarkCodexDirectives,
@@ -491,6 +503,7 @@ const CHAT_MARKDOWN_REMARK_PLUGINS = [
491503

492504
const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
493505
remarkGfm,
506+
remarkChatMath,
494507
remarkGithubAlerts,
495508
remarkNormalizeListItemIndentation,
496509
remarkCodexDirectives,
@@ -3060,6 +3073,32 @@ const CHAT_MARKDOWN_COMPONENTS = {
30603073
const { cwd, imageBaseDir, inlineCodeFileLinkMetaByText, fileLinkChip } = use(
30613074
ChatMarkdownRendererContext,
30623075
);
3076+
const classes = className?.split(/\s+/);
3077+
const displayMath = classes?.includes("math-display") === true;
3078+
if (displayMath || classes?.includes("math-inline")) {
3079+
const math = nodeToPlainText(children);
3080+
const source =
3081+
typeof node?.properties.dataMathSource === "string"
3082+
? node.properties.dataMathSource
3083+
: displayMath
3084+
? `$$\n${math}\n$$`
3085+
: `$${math}$`;
3086+
const fallback = (
3087+
<code
3088+
className={displayMath ? "chat-markdown-math-display" : "chat-markdown-math-inline"}
3089+
data-markdown-copy={displayMath ? `\n\n${source}\n\n` : source}
3090+
>
3091+
{source}
3092+
</code>
3093+
);
3094+
return (
3095+
<RenderErrorBoundary resetKeys={[math]} fallback={fallback}>
3096+
<Suspense fallback={fallback}>
3097+
<MarkdownMath math={math} source={source} display={displayMath} />
3098+
</Suspense>
3099+
</RenderErrorBoundary>
3100+
);
3101+
}
30633102
if (node?.properties?.dataInlineCode != null) {
30643103
const codeText = nodeToPlainText(children);
30653104
const fileLinkMeta =
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import katex from "katex";
2+
import { memo, useMemo } from "react";
3+
import "katex/dist/katex.min.css";
4+
5+
export default memo(function MarkdownMath({
6+
math,
7+
source,
8+
display,
9+
}: {
10+
math: string;
11+
source: string;
12+
display: boolean;
13+
}) {
14+
const html = useMemo(() => {
15+
try {
16+
return katex.renderToString(math, {
17+
displayMode: display,
18+
output: "htmlAndMathml",
19+
trust: false,
20+
strict: "ignore",
21+
maxSize: 20,
22+
maxExpand: 1000,
23+
});
24+
} catch {
25+
// Partial streamed expressions and unsupported commands stay readable.
26+
return null;
27+
}
28+
}, [math, display]);
29+
30+
return (
31+
<span
32+
className={display ? "chat-markdown-math-display" : "chat-markdown-math-inline"}
33+
data-markdown-copy={display ? `\n\n${source}\n\n` : source}
34+
>
35+
{html === null ? <code>{source}</code> : <span dangerouslySetInnerHTML={{ __html: html }} />}
36+
</span>
37+
);
38+
});

‎apps/web/src/index.css‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,29 @@ code {
16651665
margin-bottom: 0;
16661666
}
16671667

1668+
.chat-markdown-math-display {
1669+
display: block;
1670+
max-width: 100%;
1671+
margin-block: 0.75rem;
1672+
overflow-x: auto;
1673+
overflow-y: hidden;
1674+
padding-block: 0.25rem;
1675+
}
1676+
1677+
.chat-markdown-math-display .katex-display {
1678+
margin: 0;
1679+
}
1680+
1681+
.chat-markdown-math-display .katex-display > .katex {
1682+
width: max-content;
1683+
min-width: 100%;
1684+
}
1685+
1686+
.chat-markdown .katex {
1687+
overflow-wrap: normal;
1688+
word-break: normal;
1689+
}
1690+
16681691
.chat-markdown p,
16691692
.chat-markdown ul,
16701693
.chat-markdown ol,

‎apps/web/src/markdown-clipboard.test.ts‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
22

3-
import { serializeRenderedMarkdownFragment } from "./markdown-clipboard";
3+
import {
4+
chatMarkdownClipboardPayload,
5+
serializeRenderedMarkdownFragment,
6+
} from "./markdown-clipboard";
47
import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
58
import {
69
collectAssistantCitations,
@@ -132,6 +135,40 @@ describe("serializeRenderedMarkdownFragment", () => {
132135
vi.unstubAllGlobals();
133136
});
134137

138+
it.each([String.raw`$\frac{1}{2}$`, "\\[\n\\frac{1}{2}\n\\]"])(
139+
"copies the LaTeX when the selection is entirely inside an equation: %s",
140+
(source) => {
141+
const math = Object.assign(new FakeElement("SPAN", [], { "data-markdown-copy": source }), {
142+
cloneNode: () => math,
143+
}).append(new FakeText("1212"));
144+
const fragment = new FakeElement("DIV");
145+
const container = Object.assign(fragment, {
146+
innerHTML: "",
147+
appendChild(child: FakeText) {
148+
fragment.append(child);
149+
},
150+
replaceChildren(child: FakeElement) {
151+
fragment.childNodes.splice(0, fragment.childNodes.length, child);
152+
},
153+
querySelectorAll: () => [],
154+
});
155+
vi.stubGlobal("document", { createElement: () => container });
156+
const selection = {
157+
rangeCount: 1,
158+
getRangeAt: () => ({
159+
collapsed: false,
160+
cloneContents: () => new FakeText("1"),
161+
commonAncestorContainer: {
162+
nodeType: ELEMENT_NODE,
163+
closest: (selector: string) => (selector === "pre" ? null : math),
164+
},
165+
}),
166+
} as unknown as Selection;
167+
168+
expect(chatMarkdownClipboardPayload(selection)?.text).toBe(source);
169+
},
170+
);
171+
135172
it("wraps inline code in backticks", () => {
136173
const paragraph = new FakeElement("P").append(
137174
new FakeText("run "),

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,12 @@ export function chatMarkdownClipboardPayload(
399399
}
400400
continue;
401401
}
402+
// A selection entirely inside KaTeX omits the wrapper that owns the TeX
403+
// source. Copy the equation once instead of its duplicated visual glyphs.
404+
const math = ancestorElement?.closest(
405+
".chat-markdown-math-inline, .chat-markdown-math-display",
406+
);
407+
if (math) container.replaceChildren(math.cloneNode(true));
402408
const text = serializeRenderedMarkdownFragment(container);
403409
if (!text) continue;
404410
texts.push(text);

0 commit comments

Comments
 (0)