Skip to content

Commit 52d45df

Browse files
committed
fix(web): leave verbatim math unchanged during percent repair
1 parent a379588 commit 52d45df

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

‎apps/web/src/components/chat/MarkdownMath.browser.test.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ it("typesets a display equation whose label has an unescaped percent", async ()
4141
const adjacent = "\\text{100%% yield}";
4242
const paired = "\\begin{aligned}x\\\\%\\end{aligned}";
4343
const commented = "\\begin{aligned}\nx &= 1 % +999\n\\\\ y &= \\text{2%}\n\\end{aligned}";
44+
const verbatim = "\\verb|100%|+\\text{2%}";
4445
await using screen = await render(
4546
<>
4647
<MarkdownMath tex={tex} display />
4748
<MarkdownMath tex={adjacent} display />
4849
<MarkdownMath tex={paired} display />
4950
<MarkdownMath tex={commented} display />
51+
<MarkdownMath tex={verbatim} display />
5052
</>,
5153
);
5254
await vi.waitFor(() => expect(screen.container.querySelector(".katex")).not.toBeNull());
@@ -60,5 +62,7 @@ it("typesets a display equation whose label has an unescaped percent", async ()
6062
expect(equations[2]?.querySelector(".katex")).not.toBeNull();
6163
expect(equations[3]?.querySelector(".katex")).toBeNull();
6264
expect(equations[3]?.textContent).toBe(`\\[\n${commented}\n\\]`);
65+
expect(equations[4]?.querySelector(".katex")).toBeNull();
66+
expect(equations[4]?.textContent).toBe(`\\[\n${verbatim}\n\\]`);
6367
expect(equations[0]?.getAttribute("data-markdown-copy")).toBe(`\\[\n${tex}\n\\]\n\n`);
6468
});

‎apps/web/src/components/chat/MathTypeset.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ function typeset(tex: string, display: boolean): string {
2828
} catch {
2929
// A percent before a newline may be an authored TeX comment; leave it alone.
3030
const lastNewline = Math.max(tex.lastIndexOf("\n"), tex.lastIndexOf("\r"));
31-
const repaired = tex.replace(/(\\*)%/g, (match, backslashes: string, offset: number) =>
32-
backslashes.length % 2 === 0 && offset > lastNewline ? `${backslashes}\\%` : match,
33-
);
31+
const repaired = tex.includes("\\verb")
32+
? tex
33+
: tex.replace(/(\\*)%/g, (match, backslashes: string, offset: number) =>
34+
backslashes.length % 2 === 0 && offset > lastNewline ? `${backslashes}\\%` : match,
35+
);
3436
if (repaired !== tex) {
3537
try {
3638
html = katex.renderToString(repaired, options);

0 commit comments

Comments
 (0)