Skip to content

Commit 0d4aee4

Browse files
committed
feat(web): add opt-in rendering for TeX equations
1 parent d720210 commit 0d4aee4

14 files changed

Lines changed: 881 additions & 6 deletions

File tree

‎apps/desktop/src/settings/DesktopClientSettings.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const clientSettings: ClientSettings = {
7272
pullRequestMergeMethodOverrides: {},
7373
timestampFormat: "24-hour",
7474
wordWrap: true,
75+
mathRenderingEnabled: false,
7576
};
7677

7778
const decodeClientSettingsJson = Schema.decodeEffect(Schema.fromJsonString(ClientSettingsSchema));

‎apps/web/package.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"jose": "catalog:",
5454
"jsonc-parser": "3.3.1",
5555
"jszip": "3.10.1",
56+
"katex": "^0.16.47",
5657
"lucide": "^0.564.0",
5758
"lucide-react": "^0.564.0",
5859
"mermaid": "^11.17.2",
@@ -86,6 +87,8 @@
8687
"babel-plugin-react-compiler": "1.0.0",
8788
"compression": "^1.8.1",
8889
"jsdom": "^30.0.1",
90+
"mdast-util-from-markdown": "^2.0.3",
91+
"micromark-util-types": "^2.0.2",
8992
"react-test-renderer": "19.2.6",
9093
"tailwindcss": "^4.0.0",
9194
"unified": "^11.0.5",

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

Lines changed: 277 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,33 @@
22

33
import { EnvironmentId, type AuthEnvironmentScope } from "@t3tools/contracts";
44
import { act, type ComponentProps, type ReactNode } from "react";
5+
import { createRoot } from "react-dom/client";
56
import { renderToStaticMarkup } from "react-dom/server";
67
import { create, type ReactTestRenderer } from "react-test-renderer";
78
import { describe, expect, it, vi } from "vite-plus/test";
89

910
import { getSyntaxHighlighterPromise } from "../lib/syntaxHighlighting";
11+
import {
12+
chatMarkdownClipboardPayload,
13+
serializeTableElementToCsv,
14+
serializeTableElementToMarkdown,
15+
} from "../markdown-clipboard";
1016
import { GitHubIcon } from "./Icons";
1117
import { Button } from "./ui/button";
1218
import { setMarkdownTaskChecked } from "./files/filePreviewMode";
1319

1420
vi.mock("@effect/atom-react", () => ({ useAtomValue: () => null }));
1521
vi.mock("../hooks/useTheme", () => ({ useTheme: () => ({ resolvedTheme: "dark" }) }));
22+
const settingsOverrides = vi.hoisted(() => ({}) as Record<string, unknown>);
1623
vi.mock("../hooks/useSettings", async (importOriginal) => {
1724
const actual = await importOriginal<typeof import("../hooks/useSettings")>();
18-
const settings = actual.getClientSettings();
25+
const defaults = actual.getClientSettings();
1926
return {
2027
...actual,
21-
useClientSettings: (select?: (value: typeof settings) => unknown) =>
22-
select ? select(settings) : settings,
28+
useClientSettings: (select?: (value: typeof defaults) => unknown) => {
29+
const settings = { ...defaults, ...settingsOverrides };
30+
return select ? select(settings) : settings;
31+
},
2332
};
2433
});
2534
vi.mock("./ui/tooltip", async () => {
@@ -1034,3 +1043,268 @@ describe("ChatMarkdown Windows file links", () => {
10341043
expect(html).not.toContain("chat-markdown-file-link");
10351044
});
10361045
});
1046+
1047+
describe("ChatMarkdown math", () => {
1048+
// A real assistant reply: `\(…\)` inline, including table cells, and `\[` / `\]` on their own lines.
1049+
const reply = [
1050+
"**Your revised \\(G\\) value is reproducible.** Starting from the printed mean free path:",
1051+
"",
1052+
"\\[",
1053+
"r_1=3.88781\\times10^{-5}\\frac{5772}{6000}",
1054+
"=3.74007322\\times10^{-5}\\ \\mathrm m.",
1055+
"\\]",
1056+
"",
1057+
"| Quantity | Calculated | Accepted |",
1058+
"|---|---:|---:|",
1059+
"| \\(G\\) | \\(6.74731\\times10^{-11}\\) | \\(6.67430\\times10^{-11}\\) |",
1060+
"",
1061+
"Your equations imply",
1062+
"",
1063+
"\\[",
1064+
"\\sigma\\propto\\frac{1}{r_1T^4},\\qquad r_1\\propto T",
1065+
"\\quad\\Rightarrow\\quad \\sigma\\propto T^{-5}.",
1066+
"\\]",
1067+
].join("\n");
1068+
1069+
async function renderMath(
1070+
text: string,
1071+
props: Partial<ComponentProps<typeof ChatMarkdown>> = {},
1072+
) {
1073+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
1074+
// KaTeX loads lazily; having the module already resolved lets its Suspense boundaries settle.
1075+
await import("./chat/KatexMath");
1076+
const container = document.createElement("div");
1077+
const root = createRoot(container);
1078+
await act(async () => {
1079+
root.render(<ChatMarkdown cwd="/tmp/project" text={text} {...props} />);
1080+
});
1081+
const tex = (selector: string) =>
1082+
[...container.querySelectorAll(selector)].map(
1083+
(node) => node.querySelector('annotation[encoding="application/x-tex"]')?.textContent,
1084+
);
1085+
return { container, tex, unmount: () => act(async () => root.unmount()) };
1086+
}
1087+
1088+
it("renders \\(…\\) and \\[…\\] as math when math rendering is enabled", async () => {
1089+
settingsOverrides.mathRenderingEnabled = true;
1090+
const view = await renderMath(reply);
1091+
try {
1092+
expect(view.tex(".katex-display")).toEqual([
1093+
"r_1=3.88781\\times10^{-5}\\frac{5772}{6000}\n=3.74007322\\times10^{-5}\\ \\mathrm m.",
1094+
"\\sigma\\propto\\frac{1}{r_1T^4},\\qquad r_1\\propto T\n\\quad\\Rightarrow\\quad \\sigma\\propto T^{-5}.",
1095+
]);
1096+
expect(view.tex("td .katex")).toEqual([
1097+
"G",
1098+
"6.74731\\times10^{-11}",
1099+
"6.67430\\times10^{-11}",
1100+
]);
1101+
expect(view.container.querySelector("strong .katex")).not.toBeNull();
1102+
} finally {
1103+
await view.unmount();
1104+
delete settingsOverrides.mathRenderingEnabled;
1105+
}
1106+
});
1107+
1108+
it.each([true, false])("renders math with parseRawHtml=%s", async (parseRawHtml) => {
1109+
settingsOverrides.mathRenderingEnabled = true;
1110+
const view = await renderMath("Inline \\(a_1\\) here.\n\n\\[\nb^2\n\\]", {
1111+
parseRawHtml,
1112+
lineBreaks: !parseRawHtml,
1113+
});
1114+
try {
1115+
expect(view.tex(".katex")).toEqual(["a_1", "b^2"]);
1116+
} finally {
1117+
await view.unmount();
1118+
delete settingsOverrides.mathRenderingEnabled;
1119+
}
1120+
});
1121+
1122+
it("keeps code, escapes, currency, and unfinished formulas as written", async () => {
1123+
settingsOverrides.mathRenderingEnabled = true;
1124+
const view = await renderMath(
1125+
[
1126+
"Costs $5 and $10, and `\\(x\\)` stays code, as does \\\\(y\\\\).",
1127+
"",
1128+
"```tex",
1129+
"\\[",
1130+
"z",
1131+
"\\]",
1132+
"```",
1133+
"",
1134+
"Still streaming \\(w and a [link](https://x.test/a_\\(b\\))",
1135+
].join("\n"),
1136+
);
1137+
try {
1138+
expect(view.container.querySelector(".katex")).toBeNull();
1139+
expect(view.container.querySelector("code")?.textContent).toBe("\\(x\\)");
1140+
expect(view.container.querySelector("pre code")?.textContent).toBe("\\[\nz\n\\]\n");
1141+
expect(view.container.textContent).toContain("Costs $5 and $10");
1142+
expect(view.container.querySelector("a")?.getAttribute("href")).toBe("https://x.test/a_(b)");
1143+
} finally {
1144+
await view.unmount();
1145+
delete settingsOverrides.mathRenderingEnabled;
1146+
}
1147+
});
1148+
1149+
it("shows the source of TeX that KaTeX rejects and copies formulas as TeX", async () => {
1150+
settingsOverrides.mathRenderingEnabled = true;
1151+
const view = await renderMath("Good \\(x^2\\), bad \\(\\frac{1\\).");
1152+
try {
1153+
expect(view.tex(".katex")).toEqual(["x^2"]);
1154+
const copies = [...view.container.querySelectorAll("[data-markdown-copy]")].map((node) =>
1155+
node.getAttribute("data-markdown-copy"),
1156+
);
1157+
expect(copies).toEqual(["\\(x^2\\)", "\\(\\frac{1\\)"]);
1158+
expect(view.container.querySelector("code")?.textContent).toBe("\\frac{1");
1159+
} finally {
1160+
await view.unmount();
1161+
delete settingsOverrides.mathRenderingEnabled;
1162+
}
1163+
});
1164+
1165+
describe("clipboard", () => {
1166+
const displayTex = "r_1=3.88781\\times10^{-5}\\frac{5772}{6000}\n=3.74\\ \\mathrm m.";
1167+
const source = `Intro \\(a_1\\) text.\n\n\\[\n${displayTex}\n\\]\n\nOutro.`;
1168+
1169+
async function copyFrom(select: (container: HTMLElement, range: Range) => void) {
1170+
settingsOverrides.mathRenderingEnabled = true;
1171+
const view = await renderMath(source);
1172+
const range = document.createRange();
1173+
select(view.container, range);
1174+
const selection = { rangeCount: 1, getRangeAt: () => range } as unknown as Selection;
1175+
const payload = chatMarkdownClipboardPayload(selection);
1176+
await view.unmount();
1177+
delete settingsOverrides.mathRenderingEnabled;
1178+
return payload;
1179+
}
1180+
1181+
it.each([
1182+
[
1183+
"the whole formula",
1184+
(c: HTMLElement, r: Range) =>
1185+
r.selectNode(c.querySelector(".katex-display")!.parentElement!),
1186+
],
1187+
[
1188+
"the formula's contents",
1189+
(c: HTMLElement, r: Range) =>
1190+
r.selectNodeContents(c.querySelector(".katex-display")!.parentElement!),
1191+
],
1192+
[
1193+
"its visible .katex-html",
1194+
(c: HTMLElement, r: Range) =>
1195+
r.selectNodeContents(c.querySelector(".katex-display .katex-html")!),
1196+
],
1197+
[
1198+
"one glyph run inside it",
1199+
(c: HTMLElement, r: Range) =>
1200+
r.selectNodeContents(c.querySelector(".katex-display .katex-html .base")!),
1201+
],
1202+
[
1203+
"an inline formula's .katex-html",
1204+
(c: HTMLElement, r: Range) => r.selectNodeContents(c.querySelector("p .katex-html")!),
1205+
],
1206+
])("copies the TeX when %s is selected", async (name, select) => {
1207+
const payload = await copyFrom(select);
1208+
expect(payload?.text).toBe(
1209+
name === "an inline formula's .katex-html" ? "\\(a_1\\)" : `\\[\n${displayTex}\n\\]`,
1210+
);
1211+
// The rich flavor is the same source, never KaTeX's hidden MathML or its glyph soup.
1212+
expect(payload?.html).not.toMatch(/katex|annotation|<math/);
1213+
expect(payload?.html).toContain("<code>");
1214+
});
1215+
1216+
it("keeps one coherent copy when a selection spans prose and formulas", async () => {
1217+
const payload = await copyFrom((container, range) => {
1218+
range.setStartBefore(container.querySelector("p")!);
1219+
range.setEndAfter(container.lastElementChild!);
1220+
});
1221+
expect(payload?.text).toBe(`Intro \\(a_1\\) text.\n\n\\[\n${displayTex}\n\\]\n\nOutro.`);
1222+
expect(payload?.html).not.toMatch(/katex|annotation|<math/);
1223+
expect(payload?.html).toContain("Outro.");
1224+
});
1225+
});
1226+
1227+
it("copies table math as TeX in CSV and Markdown while ordinary cells stay as before", async () => {
1228+
settingsOverrides.mathRenderingEnabled = true;
1229+
const view = await renderMath(
1230+
"| Quantity | Value |\n|---|---|\n| \\(G\\) | \\(6.7\\times10^{-11}\\) |\n| **bold**, plain | so \\(\\sigma\\) here |",
1231+
);
1232+
try {
1233+
const table = view.container.querySelector("table")!;
1234+
expect(serializeTableElementToCsv(table)).toBe(
1235+
'Quantity,Value\n\\(G\\),\\(6.7\\times10^{-11}\\)\n"bold, plain",so \\(\\sigma\\) here',
1236+
);
1237+
expect(serializeTableElementToMarkdown(table)).toContain(
1238+
"| \\(G\\) | \\(6.7\\times10^{-11}\\) |",
1239+
);
1240+
} finally {
1241+
await view.unmount();
1242+
delete settingsOverrides.mathRenderingEnabled;
1243+
}
1244+
});
1245+
1246+
it("keeps code that only looks like math as code while math rendering is on", async () => {
1247+
settingsOverrides.mathRenderingEnabled = true;
1248+
const view = await renderMath(
1249+
[
1250+
"```math-inline",
1251+
"a_1",
1252+
"```",
1253+
"",
1254+
"```math-display",
1255+
"b^2",
1256+
"```",
1257+
"",
1258+
'<code class="language-math-inline">c^3</code> and <span><code class="language-math-inline">d^4</code></span>',
1259+
"",
1260+
'<pre><code class="language-math-display">e^5</code></pre>',
1261+
"",
1262+
"Real \\(f\\).",
1263+
].join("\n"),
1264+
);
1265+
try {
1266+
expect(view.tex(".katex")).toEqual(["f"]);
1267+
const code = view.container.textContent ?? "";
1268+
for (const source of ["a_1", "b^2", "c^3", "d^4", "e^5"]) expect(code).toContain(source);
1269+
} finally {
1270+
await view.unmount();
1271+
delete settingsOverrides.mathRenderingEnabled;
1272+
}
1273+
});
1274+
1275+
it("points task checkboxes at their own source after math", async () => {
1276+
settingsOverrides.mathRenderingEnabled = true;
1277+
const text = "Do \\(a\\) first:\n\n\\[\nb\n\\]\n\n- [ ] one \\(c\\)\n- [x] two";
1278+
const view = await renderMath(text);
1279+
try {
1280+
const markers = [...view.container.querySelectorAll("li")].map((item) => {
1281+
const offset = Number(item.getAttribute("data-task-marker-offset"));
1282+
return text.slice(offset, offset + 3);
1283+
});
1284+
expect(markers).toEqual(["[ ]", "[x]"]);
1285+
} finally {
1286+
await view.unmount();
1287+
delete settingsOverrides.mathRenderingEnabled;
1288+
}
1289+
});
1290+
1291+
it("does not typeset math-classed raw HTML while math rendering is off", async () => {
1292+
const view = await renderMath('<code class="language-math-inline">x^2</code>');
1293+
try {
1294+
expect(view.container.querySelector(".katex")).toBeNull();
1295+
expect(view.container.querySelector("code")?.textContent).toBe("x^2");
1296+
} finally {
1297+
await view.unmount();
1298+
}
1299+
});
1300+
1301+
it("leaves the source as ordinary Markdown while math rendering is off", async () => {
1302+
const view = await renderMath(reply);
1303+
try {
1304+
expect(view.container.querySelector(".katex")).toBeNull();
1305+
expect(view.container.textContent).toContain("(G)");
1306+
} finally {
1307+
await view.unmount();
1308+
}
1309+
});
1310+
});

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,13 @@ import { parseAssistantCitationHref } from "@t3tools/shared/assistantCitations";
9090
import { parseComposerContextHref } from "@t3tools/shared/composerContextReferences";
9191
import { parseThreadLinkHref, THREAD_LINK_PROTOCOL } from "@t3tools/shared/threadLinks";
9292
import { AssistantCitationChip } from "./chat/AssistantCitationChip";
93+
import { MarkdownMath, isTexMath, useMathRendering } from "./chat/MarkdownMath";
9394
import { MarkdownThreadLink } from "./chat/MarkdownThreadLink";
9495
import remarkGfm from "remark-gfm";
9596
import type { Processor } from "unified";
9697
import { isWindowsAbsolutePath } from "@t3tools/shared/path";
9798
import { remarkGithubAlerts } from "../markdown-github-alerts";
99+
import { remarkTexMath } from "../markdown-math";
98100
import {
99101
artifactTemplateFromHastProperties,
100102
CODEX_ARTIFACT_TEMPLATE_HAST_PROPERTIES,
@@ -3404,9 +3406,13 @@ const CHAT_MARKDOWN_COMPONENTS = {
34043406
);
34053407
},
34063408
code: function MarkdownCode({ node, children, className, ...props }) {
3407-
const { cwd, imageBaseDir, inlineCodeFileLinkMetaByText, fileLinkChip } = use(
3409+
const { cwd, imageBaseDir, inlineCodeFileLinkMetaByText, fileLinkChip, text } = use(
34083410
ChatMarkdownRendererContext,
34093411
);
3412+
const mathRendering = useMathRendering();
3413+
if (mathRendering && isTexMath(node, text, false)) {
3414+
return <MarkdownMath tex={nodeToPlainText(children)} display={false} />;
3415+
}
34103416
if (node?.properties?.dataInlineCode != null) {
34113417
const codeText = nodeToPlainText(children);
34123418
const fileLinkMeta =
@@ -3565,10 +3571,14 @@ const CHAT_MARKDOWN_COMPONENTS = {
35653571
const { resolvedTheme, diffThemeName, expandMedia, isStreaming, onRunShellCommand, text } = use(
35663572
ChatMarkdownRendererContext,
35673573
);
3574+
const mathRendering = useMathRendering();
35683575
const codeBlock = extractCodeBlock(children);
35693576
if (!codeBlock) {
35703577
return <pre {...props}>{children}</pre>;
35713578
}
3579+
if (mathRendering && isTexMath(node, text, true)) {
3580+
return <MarkdownMath tex={codeBlock.code} display />;
3581+
}
35723582

35733583
const language = extractFenceLanguage(codeBlock.className);
35743584
const fenceTitle = extractFenceTitle(extractPreCodeMeta(node));
@@ -3643,17 +3653,22 @@ function ChatMarkdown({
36433653
localMediaPreview,
36443654
setLocalMediaPreview,
36453655
} = useChatMarkdownState({ text, ...props });
3656+
// Off by default; when on, only text that could hold `\(` or `\[` pays for the math syntax.
3657+
const parseMath = useMathRendering() && /\\[([]/.test(text);
3658+
// The incremental parser reuses a cached prefix and does not know about extra syntax.
36463659
const incrementalParsing =
36473660
props.isStreaming === true &&
36483661
extraRemarkPlugins.length === 0 &&
3662+
!parseMath &&
36493663
/(?:^|\n) {0,3}(?:`{3}|~{3})/.test(text);
36503664
const remarkPlugins = useMemo(
36513665
() => [
36523666
...(lineBreaks ? CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS : CHAT_MARKDOWN_REMARK_PLUGINS),
36533667
...extraRemarkPlugins,
3668+
...(parseMath ? [remarkTexMath] : []),
36543669
...(incrementalParsing ? [createIncrementalMarkdownPlugin()] : []),
36553670
],
3656-
[extraRemarkPlugins, incrementalParsing, lineBreaks],
3671+
[extraRemarkPlugins, incrementalParsing, lineBreaks, parseMath],
36573672
);
36583673

36593674
// react-markdown converts unparsed HTML nodes to text when skipHtml is false.

0 commit comments

Comments
 (0)