Skip to content

Commit 5b357b3

Browse files
perf(web): chat Markdown parses about twice as fast with the same output (#27685)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 78fba05 commit 5b357b3

13 files changed

Lines changed: 552 additions & 23 deletions

‎apps/web/package.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
"katex": "^0.16.47",
6969
"lucide": "^0.564.0",
7070
"lucide-react": "^0.564.0",
71+
"mdast-util-from-markdown": "^2.0.3",
72+
"micromark-util-combine-extensions": "^2.0.1",
7173
"morphicons": "^1.7.1",
7274
"pdfjs-dist": "6.4.299",
7375
"pele": "^1.8.0",
@@ -105,6 +107,7 @@
105107
"@vitejs/plugin-react": "^6.1.2",
106108
"@vitest/browser-playwright": "5.0.5",
107109
"compression": "^1.8.2",
110+
"micromark-util-types": "catalog:",
108111
"oxc-transform-react": "~0.152.0",
109112
"playwright": "1.60.0",
110113
"tailwindcss": "^4.0.0",

‎apps/web/src/components/chat/markdownRenderer.test.tsx‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { renderToStaticMarkup } from "react-dom/server";
66
import ReactMarkdown from "react-markdown";
77
import { describe, expect, it } from "vite-plus/test";
88

9+
import { createIncrementalMarkdownPlugin } from "../../markdown-incremental";
910
import { createMarkdownRenderer } from "./markdownRenderer";
1011

1112
// Visible in the markup, so every URL attribute must pass through it.
@@ -26,6 +27,14 @@ describe("createMarkdownRenderer", () => {
2627
"| a | b |\n| - | - |\n| <br> | [x](#frag) |\n\n- [ ] task\n- [x] done",
2728
'```ts title="a.ts"\nconst a = 1;\n```\n\n> [!WARNING]\n> careful <a>bare</a>',
2829
"Footnote[^1] and ~~strike~~\n\n[^1]: the note",
30+
// Each GFM construct the parser skips when its text cannot match, next to one that can.
31+
"Mail [email protected], see www.example.com and HTTPS://example.com/a, not [x](https://y.z).",
32+
33+
"A table without pipes\n:--",
34+
"> | quoted |\n> | - |\n> | body |\n\n- [ ] open\n- [X] done",
35+
"Escaped www\\.example.com and me&#64;example.com",
36+
"\ufeffA byte order mark\n\n| a |\n| - |\n| b |",
37+
"> - | a |\r\n> | - |\r\n> | [email protected] |\r\n\n-\t[ ] tab\n\n ~~lazy~~",
2938
])("renders what react-markdown renders: %j", (source) => {
3039
const expected = renderToStaticMarkup(
3140
<ReactMarkdown
@@ -41,6 +50,33 @@ describe("createMarkdownRenderer", () => {
4150
expect(render(source, true)).toBe(element);
4251
});
4352

53+
it("renders what react-markdown renders while a reply streams through the incremental parser", () => {
54+
const reply =
55+
"```ts\nconst a = 1;\n```\n\n| a | b |\n| - | - |\n| [email protected] | www.x.io |\n\nDone ~~now~~.";
56+
// As ChatMarkdown streams: one incremental plugin for the reply, a fresh renderer per update.
57+
const incremental = createIncrementalMarkdownPlugin();
58+
for (let end = 1; end <= reply.length; end++) {
59+
const source = reply.slice(0, end);
60+
const streamed = createMarkdownRenderer({
61+
remarkPlugins: [...CHAT_MARKDOWN_REMARK_PLUGINS, incremental],
62+
rehypePlugins: CHAT_MARKDOWN_REHYPE_PLUGINS,
63+
components: {},
64+
urlTransform,
65+
})(source, false);
66+
expect(renderToStaticMarkup(streamed)).toBe(
67+
renderToStaticMarkup(
68+
<ReactMarkdown
69+
remarkPlugins={CHAT_MARKDOWN_REMARK_PLUGINS}
70+
rehypePlugins={CHAT_MARKDOWN_REHYPE_PLUGINS}
71+
urlTransform={urlTransform}
72+
>
73+
{source}
74+
</ReactMarkdown>,
75+
),
76+
);
77+
}
78+
});
79+
4480
it("keeps cached messages when one far larger than the cache is rendered", () => {
4581
const element = render("Kept *message*", true);
4682
render("x".repeat(600_000), true);

‎apps/web/src/components/chat/markdownRenderer.ts‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import remarkRehype from "remark-rehype";
88
import { type PluggableList, unified } from "unified";
99
import { VFile } from "vfile";
1010

11+
import { remarkParseWithoutUnusedSyntax } from "./markdownSyntaxGates";
12+
1113
export type MarkdownUrlTransform = (url: string, key: string, node: Element) => string;
1214

1315
interface MarkdownRendererOptions {
@@ -50,6 +52,7 @@ export function createMarkdownRenderer({
5052
}: MarkdownRendererOptions) {
5153
const processor = unified()
5254
.use(remarkParse)
55+
.use(remarkParseWithoutUnusedSyntax)
5356
.use(remarkPlugins)
5457
.use(remarkRehype, { allowDangerousHtml: true })
5558
.use(rehypePlugins)
@@ -59,23 +62,24 @@ export function createMarkdownRenderer({
5962
// Literal raw HTML that survived the rehype plugins, and URL attributes, as react-markdown treats them.
6063
const prepare = (node: Nodes) => {
6164
if (!("children" in node)) return;
62-
node.children.forEach((child, index) => {
65+
const { children } = node;
66+
for (let index = 0; index < children.length; index++) {
67+
const child = children[index]!;
6368
if (child.type === "raw") {
64-
node.children[index] = { type: "text", value: child.value };
65-
return;
69+
children[index] = { type: "text", value: child.value };
70+
continue;
6671
}
67-
if (child.type === "element") {
68-
for (const [key, tags] of Object.entries(urlAttributes)) {
69-
if (
70-
Object.hasOwn(child.properties, key) &&
71-
(tags === null || tags.includes(child.tagName))
72-
) {
73-
child.properties[key] = urlTransform(String(child.properties[key] || ""), key, child);
74-
}
72+
if (child.type !== "element") continue;
73+
const { properties } = child;
74+
for (const key in properties) {
75+
if (!Object.hasOwn(urlAttributes, key)) continue;
76+
const tags = urlAttributes[key];
77+
if (tags === null || tags?.includes(child.tagName)) {
78+
properties[key] = urlTransform(String(properties[key] || ""), key, child);
7579
}
7680
}
7781
prepare(child);
78-
});
82+
}
7983
};
8084

8185
return (text: string, cache: boolean): ReactElement => {
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
import type { Nodes as MdastNodes } from "mdast";
2+
import {
3+
type Extension as MdastExtension,
4+
fromMarkdown,
5+
type Options as FromMarkdownOptions,
6+
type Transform,
7+
} from "mdast-util-from-markdown";
8+
import { combineExtensions } from "micromark-util-combine-extensions";
9+
import type { Construct, Extension, TokenizeContext } from "micromark-util-types";
10+
import type { Processor } from "unified";
11+
12+
// A table needs a delimiter row: dashes with a `|` or `:`, after any `>` or indent.
13+
// One character class per pattern: overlapping ones backtrack quadratically on a run of dashes.
14+
const DELIMITER_LINE = /^[ \t>|:-]+$/gm;
15+
const isDelimiterRow = (line: string) => line.includes("-") && /[|:]/.test(line);
16+
const NEXT_LINE = /[^\r\n]*(?:\r\n?|\n)([^\r\n]*)/y;
17+
18+
function hasDelimiterRow(text: string) {
19+
for (const [line] of text.matchAll(DELIMITER_LINE)) if (isDelimiterRow(line)) return true;
20+
return false;
21+
}
22+
const PROTOCOL = /https?:\/\//iy;
23+
const WWW = /www\./iy;
24+
const EMAIL_ATEXT = /[\w+.-]*@/y;
25+
26+
const matchesAt = (pattern: RegExp, text: string, offset: number) => {
27+
pattern.lastIndex = offset;
28+
return pattern.test(text);
29+
};
30+
31+
interface Gate {
32+
readonly names: ReadonlyArray<string>;
33+
/** False when the construct cannot match anywhere in the text. */
34+
readonly inText: (text: string) => boolean;
35+
/** False when it cannot start at this offset, which keeps micromark from trying it there. */
36+
readonly at?: (context: TokenizeContext, text: string, offset: number) => boolean;
37+
}
38+
39+
// micromark tries these at every candidate character: email autolinks at every word, tables
40+
// at every line. That is most of what GFM adds to a parse, and most texts cannot match them.
41+
const GATES: ReadonlyArray<Gate> = [
42+
{
43+
names: ["emailAutolink"],
44+
inText: (text) => text.includes("@"),
45+
at: (_, text, offset) => matchesAt(EMAIL_ATEXT, text, offset),
46+
},
47+
{
48+
names: ["protocolAutolink"],
49+
inText: (text) => /https?:\/\//i.test(text),
50+
at: (_, text, offset) => matchesAt(PROTOCOL, text, offset),
51+
},
52+
{
53+
names: ["wwwAutolink"],
54+
inText: (text) => /www\./i.test(text),
55+
at: (_, text, offset) => matchesAt(WWW, text, offset),
56+
},
57+
{
58+
names: ["table"],
59+
inText: hasDelimiterRow,
60+
at: (context, text, offset) => {
61+
NEXT_LINE.lastIndex = offset;
62+
const nextLine = NEXT_LINE.exec(text)?.[1];
63+
return (nextLine !== undefined && hasDelimiterRow(nextLine)) || continuesTable(context);
64+
},
65+
},
66+
{ names: ["strikethrough"], inText: (text) => text.includes("~") },
67+
{
68+
names: ["gfmFootnoteCall", "gfmPotentialFootnoteCall", "gfmFootnoteDefinition"],
69+
inText: (text) => text.includes("[^"),
70+
},
71+
{ names: ["tasklistCheck"], inText: (text) => /\[[\sxX]\]/.test(text) },
72+
];
73+
74+
// A body row follows the head or another row. Mirrors `start` in micromark-extension-gfm-table's
75+
// lib/syntax.js: recheck it when that package changes.
76+
function continuesTable({ events }: TokenizeContext) {
77+
let index = events.length - 1;
78+
while (index > -1 && /^(?:lineEnding|linePrefix)$/.test(events[index]![1].type)) index--;
79+
// The table extension's token types are not in micromark's map unless its types are loaded.
80+
const tail: string | undefined = events[index]?.[1].type;
81+
return tail === "tableHead" || tail === "tableRow";
82+
}
83+
84+
let parsing = "";
85+
const gated = new WeakMap<Construct, Construct>();
86+
87+
function gatedConstruct(construct: Construct, gate: Gate): Construct {
88+
const at = gate.at;
89+
if (!at) return construct;
90+
let wrapped = gated.get(construct);
91+
if (!wrapped) {
92+
const { previous, tokenize } = construct;
93+
wrapped = {
94+
...construct,
95+
tokenize(effects, ok, nok) {
96+
return at(this, parsing, this.now().offset) ? tokenize.call(this, effects, ok, nok) : nok;
97+
},
98+
...(previous && {
99+
previous(code) {
100+
return previous.call(this, code) && at(this, parsing, this.now().offset);
101+
},
102+
}),
103+
};
104+
gated.set(construct, wrapped);
105+
}
106+
return wrapped;
107+
}
108+
109+
type ConstructMap = Record<string, Construct | Construct[] | undefined>;
110+
111+
function gateExtension(extension: Extension, absent: ReadonlySet<Gate>): Extension {
112+
const gateOf = (name: string | undefined) =>
113+
GATES.find((gate) => name && gate.names.includes(name));
114+
const result: Record<string, unknown> = {};
115+
for (const [field, value] of Object.entries(extension)) {
116+
if (field === "disable" || field === "attentionMarkers" || !value) {
117+
result[field] = value;
118+
continue;
119+
}
120+
const map: ConstructMap = {};
121+
for (const [code, entry] of Object.entries(value as ConstructMap)) {
122+
const kept = (Array.isArray(entry) ? entry : [entry]).flatMap((construct) => {
123+
const gate = construct && gateOf(construct.name);
124+
if (!construct || !gate) return construct ? [construct] : [];
125+
return absent.has(gate) ? [] : [gatedConstruct(construct, gate)];
126+
});
127+
if (kept.length > 0) map[code] = Array.isArray(entry) ? kept : kept[0];
128+
}
129+
result[field] = map;
130+
}
131+
return result as Extension;
132+
}
133+
134+
type MdastExtensions = NonNullable<FromMarkdownOptions["mdastExtensions"]>;
135+
136+
// Merged once, as mdast-util-from-markdown merges them on every call: lists in order, handlers by key.
137+
function mergeMdastExtensions(extensions: MdastExtensions): MdastExtension {
138+
const merged = {
139+
canContainEols: [] as string[],
140+
enter: {},
141+
exit: {},
142+
transforms: [] as Transform[],
143+
};
144+
for (const extension of extensions.flat()) {
145+
merged.canContainEols.push(...(extension.canContainEols ?? []));
146+
Object.assign(merged.enter, extension.enter);
147+
Object.assign(merged.exit, extension.exit);
148+
// GFM looks for autolink literals again in the decoded text outside links, so `www\.x.io`
149+
// links too. Most texts have URLs only in link destinations and code, where it finds nothing.
150+
for (const transform of extension.transforms ?? []) {
151+
merged.transforms.push(
152+
extension.enter?.literalAutolink
153+
? (tree) => (hasAutolinkText(tree) ? transform(tree) : undefined)
154+
: transform,
155+
);
156+
}
157+
}
158+
return merged;
159+
}
160+
161+
const AUTOLINK_TEXT = /@|https?:\/\/|www\./i;
162+
163+
function hasAutolinkText(node: MdastNodes): boolean {
164+
if (node.type === "text") return AUTOLINK_TEXT.test(node.value);
165+
if (node.type === "link" || node.type === "linkReference" || !("children" in node)) return false;
166+
return node.children.some(hasAutolinkText);
167+
}
168+
169+
/**
170+
* Parses as remark-parse does, without the syntax extension constructs that cannot match the
171+
* text. Same tree, in about half the time.
172+
*/
173+
function attachParserWithoutUnusedSyntax(this: Processor) {
174+
const data = this.data();
175+
const byMask = new Map<number, FromMarkdownOptions["extensions"]>();
176+
let mdastExtensions: MdastExtension[] | undefined;
177+
this.parser = (document) => {
178+
let mask = 0;
179+
GATES.forEach((gate, index) => {
180+
if (!gate.inText(document)) mask |= 1 << index;
181+
});
182+
let extensions = byMask.get(mask);
183+
if (!extensions) {
184+
const absent = new Set(GATES.filter((_, index) => mask & (1 << index)));
185+
extensions = [
186+
combineExtensions(
187+
(data.micromarkExtensions ?? []).map((extension) => gateExtension(extension, absent)),
188+
),
189+
];
190+
byMask.set(mask, extensions);
191+
}
192+
mdastExtensions ??= [mergeMdastExtensions(data.fromMarkdownExtensions ?? [])];
193+
const outer = parsing;
194+
// Gates read micromark offsets, which skip a leading byte order mark.
195+
parsing = document.charCodeAt(0) === 0xfeff ? document.slice(1) : document;
196+
try {
197+
return fromMarkdown(document, { ...data.settings, extensions, mdastExtensions });
198+
} finally {
199+
parsing = outer;
200+
}
201+
};
202+
}
203+
204+
export const remarkParseWithoutUnusedSyntax = attachParserWithoutUnusedSyntax;

‎apps/web/src/textDirection.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const LETTER = /\p{L}/u;
1010
* Count words so mixed prose follows its main language; break ties with the first word.
1111
*/
1212
export function textDirection(text: string): TextDirection | undefined {
13+
if (!RTL_LETTER.test(text)) return LETTER.test(text) ? "ltr" : undefined;
1314
let rtlWords = 0;
1415
let ltrWords = 0;
1516
let first: TextDirection | undefined;

‎packages/shared/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@
526526
"mdast-util-to-hast": "^13.2.1",
527527
"micromark-extension-directive": "catalog:",
528528
"micromark-util-character": "catalog:",
529+
"property-information": "^7.2.0",
529530
"rehype-raw": "catalog:",
530531
"rehype-sanitize": "catalog:",
531532
"remark-breaks": "catalog:",

‎packages/shared/src/markdownListIndentation.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ function attachListItemIndentationNormalizer(this: MarkdownParser) {
113113
if (!node.children) {
114114
return;
115115
}
116+
if (!node.children.some((child) => isSameLineOverIndentedCode(child, node, source))) {
117+
for (const child of node.children) visit(child, source);
118+
return;
119+
}
116120
node.children = node.children.flatMap((child) => {
117121
if (isSameLineOverIndentedCode(child, node, source)) {
118122
const recovered = blocksFromIndentedCode(child, this);

0 commit comments

Comments
 (0)