The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 12:00 UTC
perf(web): chat Markdown parses about twice as fast with the same output (#27685)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 30, 2026 at 09:39 UTC
5b357b3b894e8255ebd3049c8ef826b93c20c9e4
13 files changed
+552
-23
apps/web/package.json
+3
@@ -68,6 +68,8 @@
68
"katex": "^0.16.47",
69
"lucide": "^0.564.0",
70
"lucide-react": "^0.564.0",
71
+ "mdast-util-from-markdown": "^2.0.3",
72
+ "micromark-util-combine-extensions": "^2.0.1",
73
"morphicons": "^1.7.1",
74
"pdfjs-dist": "6.4.299",
75
"pele": "^1.8.0",
@@ -105,6 +107,7 @@
107
"@vitejs/plugin-react": "^6.1.2",
108
"@vitest/browser-playwright": "5.0.5",
109
"compression": "^1.8.2",
110
+ "micromark-util-types": "catalog:",
111
"oxc-transform-react": "~0.152.0",
112
"playwright": "1.60.0",
113
"tailwindcss": "^4.0.0",
apps/web/src/components/chat/markdownRenderer.test.tsx
+36
@@ -6,6 +6,7 @@ import { renderToStaticMarkup } from "react-dom/server";
6
import ReactMarkdown from "react-markdown";
7
import { describe, expect, it } from "vite-plus/test";
8
9
+import { createIncrementalMarkdownPlugin } from "../../markdown-incremental";
10
import { createMarkdownRenderer } from "./markdownRenderer";
11
12
// Visible in the markup, so every URL attribute must pass through it.
@@ -26,6 +27,14 @@ describe("createMarkdownRenderer", () => {
27
"| a | b |\n| - | - |\n| <br> | [x](#frag) |\n\n- [ ] task\n- [x] done",
28
'```ts title="a.ts"\nconst a = 1;\n```\n\n> [!WARNING]\n> careful <a>bare</a>',
29
"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 me@example.com, see www.example.com and HTTPS://example.com/a, not [x](https://y.z).",
32
+ "a.b-c_d+e@x.io and @user\n\n`me@x.io` <me@x.io>",
33
+ "A table without pipes\n:--",
34
+ "> | quoted |\n> | - |\n> | body |\n\n- [ ] open\n- [X] done",
35
+ "Escaped www\\.example.com and me@example.com",
36
+ "\ufeffA byte order mark\n\n| a |\n| - |\n| b |",
37
+ "> - | a |\r\n> | - |\r\n> | me@x.io |\r\n\n-\t[ ] tab\n\n ~~lazy~~",
38
])("renders what react-markdown renders: %j", (source) => {
39
const expected = renderToStaticMarkup(
40
<ReactMarkdown
@@ -41,6 +50,33 @@ describe("createMarkdownRenderer", () => {
50
expect(render(source, true)).toBe(element);
51
});
52
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| me@x.io | 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
+
80
it("keeps cached messages when one far larger than the cache is rendered", () => {
81
const element = render("Kept *message*", true);
82
render("x".repeat(600_000), true);
apps/web/src/components/chat/markdownRenderer.ts
+16
-12
@@ -8,6 +8,8 @@ import remarkRehype from "remark-rehype";
8
import { type PluggableList, unified } from "unified";
9
import { VFile } from "vfile";
10
11
+import { remarkParseWithoutUnusedSyntax } from "./markdownSyntaxGates";
12
+
13
export type MarkdownUrlTransform = (url: string, key: string, node: Element) => string;
14
15
interface MarkdownRendererOptions {
@@ -50,6 +52,7 @@ export function createMarkdownRenderer({
52
}: MarkdownRendererOptions) {
53
const processor = unified()
54
.use(remarkParse)
55
+ .use(remarkParseWithoutUnusedSyntax)
56
.use(remarkPlugins)
57
.use(remarkRehype, { allowDangerousHtml: true })
58
.use(rehypePlugins)
@@ -59,23 +62,24 @@ export function createMarkdownRenderer({
62
// Literal raw HTML that survived the rehype plugins, and URL attributes, as react-markdown treats them.
63
const prepare = (node: Nodes) => {
64
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]!;
68
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;
71
}
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);
79
}
80
}
81
prepare(child);
78
- });
82
+ }
83
};
84
85
return (text: string, cache: boolean): ReactElement => {
apps/web/src/components/chat/markdownSyntaxGates.ts
new
+204
@@ -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
+1
@@ -10,6 +10,7 @@ const LETTER = /\p{L}/u;
10
* Count words so mixed prose follows its main language; break ties with the first word.
11
*/
12
export function textDirection(text: string): TextDirection | undefined {
13
+ if (!RTL_LETTER.test(text)) return LETTER.test(text) ? "ltr" : undefined;
14
let rtlWords = 0;
15
let ltrWords = 0;
16
let first: TextDirection | undefined;
packages/shared/package.json
+1
@@ -526,6 +526,7 @@
526
"mdast-util-to-hast": "^13.2.1",
527
"micromark-extension-directive": "catalog:",
528
"micromark-util-character": "catalog:",
529
+ "property-information": "^7.2.0",
530
"rehype-raw": "catalog:",
531
"rehype-sanitize": "catalog:",
532
"remark-breaks": "catalog:",
packages/shared/src/markdownListIndentation.ts
+4
@@ -113,6 +113,10 @@ function attachListItemIndentationNormalizer(this: MarkdownParser) {
113
if (!node.children) {
114
return;
115
}
116
+ if (!node.children.some((child) => isSameLineOverIndentedCode(child, node, source))) {
117
+ for (const child of node.children) visit(child, source);
118
+ return;
119
+ }
120
node.children = node.children.flatMap((child) => {
121
if (isSameLineOverIndentedCode(child, node, source)) {
122
const recovered = blocksFromIndentedCode(child, this);
packages/shared/src/markdownPipeline.test.ts
+53
@@ -1,5 +1,6 @@
1
import { expect, it } from "vite-plus/test";
2
import { unified } from "unified";
3
+import rehypeRaw from "rehype-raw";
4
import remarkParse from "remark-parse";
5
import remarkRehype from "remark-rehype";
6
import { CHAT_MARKDOWN_REHYPE_PLUGINS, CHAT_MARKDOWN_REMARK_PLUGINS } from "./markdownPipeline.ts";
@@ -38,6 +39,58 @@ it("preserves in-app thread links through the shared Markdown sanitizer", () =>
39
expect(JSON.stringify(tree)).not.toContain("javascript:");
40
});
41
42
+it("builds the tree rehype-raw builds for markdown without HTML", () => {
43
+ type Node = {
44
+ type: string;
45
+ tagName?: string;
46
+ value?: string;
47
+ position?: unknown;
48
+ data?: unknown;
49
+ children?: Node[];
50
+ };
51
+ // What the round trip changes that renders the same; see `needsHtmlParser`.
52
+ const comparable = (node: Node): Node => {
53
+ const { position, children, data, ...rest } = node;
54
+ const result: Node =
55
+ node.type === "text" || node.type === "root" ? rest : { ...rest, position, data };
56
+ if (!children) return result;
57
+ const merged: Node[] = [];
58
+ for (const child of children.map(comparable)) {
59
+ const last = merged.at(-1);
60
+ if (child.type === "text" && last?.type === "text") last.value += child.value ?? "";
61
+ else merged.push(child);
62
+ }
63
+ // Blank text inside a table moves in front of it; the JSX runtime drops it in tables.
64
+ const inTable = /^(?:table|thead|tbody|tfoot|tr)$/.test(node.tagName ?? "");
65
+ result.children = merged.filter(
66
+ (child, index) =>
67
+ child.type !== "text" ||
68
+ child.value?.trim() ||
69
+ !(inTable || merged[index + 1]?.tagName === "table"),
70
+ );
71
+ return result;
72
+ };
73
+ const tree = (rehypePlugins: typeof CHAT_MARKDOWN_REHYPE_PLUGINS, markdown: string) => {
74
+ const processor = unified()
75
+ .use(remarkParse)
76
+ .use(CHAT_MARKDOWN_REMARK_PLUGINS)
77
+ .use(remarkRehype, { allowDangerousHtml: true })
78
+ .use(rehypePlugins);
79
+ return comparable(processor.runSync(processor.parse(markdown)) as Node);
80
+ };
81
+ const [first, , ...rest] = CHAT_MARKDOWN_REHYPE_PLUGINS;
82
+ const withRaw = [first!, rehypeRaw, ...rest];
83
+ for (const markdown of [
84
+ "- [ ] open\n- [x] done\n\nNote[^1]\n\n[^1]: The note.",
85
+ "| a | b |\n| :- | -: |\n| `c` |  |\n\n```ts title=x\nconst a = 1;\n```",
86
+ // The HTML parser moves the footnote's link out of the label's link.
87
+ "[note[^x]](https://example.com)\n\n[^x]: text",
88
+ "**a** **b**\r\n> - | a |\r\n> | - |\r\n> | b |\r\n\n-\tx\n\n y",
89
+ ]) {
90
+ expect(tree(CHAT_MARKDOWN_REHYPE_PLUGINS, markdown)).toEqual(tree(withRaw, markdown));
91
+ }
92
+});
93
+
94
it("keeps backslashes that CommonMark would read as escapes in prose Windows paths", () => {
95
const processor = unified()
96
.use(remarkParse)
packages/shared/src/markdownPipeline.ts
+74
-2
@@ -1,8 +1,9 @@
1
import type { Paragraph, PhrasingContent, Root, RootContent } from "mdast";
2
import { remarkKeepWindowsPaths } from "./markdownWindowsPaths.ts";
3
-import { type PluggableList, unified } from "unified";
3
+import { type PluggableList, type Processor, unified } from "unified";
4
import remarkParse from "remark-parse";
5
import remarkRehype from "remark-rehype";
6
+import { find, html } from "property-information";
7
import rehypeRaw from "rehype-raw";
8
import rehypeSanitize, { defaultSchema } from "rehype-sanitize";
9
import remarkBreaks from "remark-breaks";
@@ -185,9 +186,79 @@ export const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS: PluggableList = [
186
remarkNormalizeLinksAndTagInlineCode,
187
];
188
189
+// Start tags an HTML parser closes an open `<p>` for.
190
+const CLOSES_PARAGRAPH = new Set(
191
+ "address article aside blockquote center details dialog dir div dl fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hgroup hr li listing main menu nav ol p plaintext pre search section summary table ul xmp dd dt".split(
192
+ " ",
193
+ ),
194
+);
195
+const HEADING = /^h[1-6]$/;
196
+
197
+interface HtmlScope {
198
+ readonly anchor: boolean;
199
+ readonly paragraph: boolean;
200
+ readonly heading: boolean;
201
+}
202
+
203
+/**
204
+ * Whether the tree needs the HTML parser: raw HTML, or nesting it repairs (a footnote call in a
205
+ * link label nests `<a>`). Otherwise it only rewrites properties as hast-util-to-parse5 does and
206
+ * drops `data`, both repeated here, and moves blank text that renders the same.
207
+ */
208
+function needsHtmlParser(node: MarkdownImageHastNode, scope: HtmlScope): boolean {
209
+ if (node.type === "raw") return true;
210
+ let inner = scope;
211
+ if (node.type === "element" && node.tagName) {
212
+ const { tagName } = node;
213
+ const heading = HEADING.test(tagName);
214
+ if (
215
+ (tagName === "a" && scope.anchor) ||
216
+ (scope.paragraph && CLOSES_PARAGRAPH.has(tagName)) ||
217
+ (heading && scope.heading)
218
+ ) {
219
+ return true;
220
+ }
221
+ inner = {
222
+ anchor: scope.anchor || tagName === "a",
223
+ paragraph: scope.paragraph || tagName === "p",
224
+ heading: scope.heading || heading,
225
+ };
226
+ const properties = node.properties ?? {};
227
+ for (const key of Object.keys(properties)) {
228
+ const value = properties[key];
229
+ const info = find(html, key);
230
+ if (
231
+ value === false ||
232
+ value === null ||
233
+ value === undefined ||
234
+ Number.isNaN(value) ||
235
+ (!value && info.boolean)
236
+ ) {
237
+ delete properties[key];
238
+ } else if (value === true && !info.boolean && !info.overloadedBoolean) {
239
+ properties[key] = "";
240
+ }
241
+ }
242
+ delete (node as { data?: unknown }).data;
243
+ }
244
+ return (node.children ?? []).some((child) => needsHtmlParser(child, inner));
245
+}
246
+
247
+const TOP_SCOPE: HtmlScope = { anchor: false, paragraph: false, heading: false };
248
+
249
+/** rehype-raw reparses the whole tree through an HTML parser, which most trees can skip. */
250
+function rehypeRawWhenPresent(this: Processor) {
251
+ const transform = rehypeRaw.call(this) as unknown as (
252
+ tree: MarkdownImageHastNode,
253
+ file: unknown,
254
+ ) => unknown;
255
+ return (tree: MarkdownImageHastNode, file: unknown) =>
256
+ needsHtmlParser(tree, TOP_SCOPE) ? transform(tree, file) : undefined;
257
+}
258
+
259
export const CHAT_MARKDOWN_REHYPE_PLUGINS: PluggableList = [
260
rehypePreserveBareAnchorPlaceholders,
190
- rehypeRaw,
261
+ rehypeRawWhenPresent,
262
rehypePreserveImageSourceMeta,
263
[rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],
264
];
@@ -209,6 +280,7 @@ const startsLine = (node: PhrasingContent | undefined) =>
280
281
function breakLinesAroundImages(paragraph: Paragraph) {
282
const children = paragraph.children;
283
+ if (!children.some(isImageNode)) return;
284
const ownsLine = (index: number) =>
285
isImageNode(children[index]) &&
286
endsLine(children[index - 1]) &&
patches/mdast-util-to-hast@13.2.1.patch
new
+44
@@ -0,0 +1,44 @@
1
+diff --git a/lib/state.js b/lib/state.js
2
+index 0d9c9df..1279783 100644
3
+--- a/lib/state.js
4
++++ b/lib/state.js
5
+@@ -176,7 +176,6 @@
6
+ */
7
+
8
+ import structuredClone from '@ungap/structured-clone'
9
+-import {visit} from 'unist-util-visit'
10
+ import {position} from 'unist-util-position'
11
+ import {handlers as defaultHandlers} from './handlers/index.js'
12
+
13
+@@ -223,7 +222,16 @@ export function createState(tree, options) {
14
+ wrap
15
+ }
16
+
17
+- visit(tree, function (node) {
18
++ // A plain walk in the same order: `visit` costs more than the rest of this setup.
19
++ collectDefinitions(tree)
20
++
21
++ return state
22
++
23
++ /**
24
++ * @param {MdastNodes} node
25
++ * @returns {undefined}
26
++ */
27
++ function collectDefinitions(node) {
28
+ if (node.type === 'definition' || node.type === 'footnoteDefinition') {
29
+ const map = node.type === 'definition' ? definitionById : footnoteById
30
+ const id = String(node.identifier).toUpperCase()
31
+@@ -235,9 +243,11 @@ export function createState(tree, options) {
32
+ map.set(id, node)
33
+ }
34
+ }
35
+- })
36
+
37
+- return state
38
++ if ('children' in node) {
39
++ for (const child of node.children) collectDefinitions(child)
40
++ }
41
++ }
42
+
43
+ /**
44
+ * Transform an mdast node into a hast node.
patches/micromark@4.0.2.patch
new
+87
@@ -0,0 +1,87 @@
1
+diff --git a/dev/lib/parse.js b/dev/lib/parse.js
2
+index ffb0f88..c63f0a3 100644
3
+--- a/dev/lib/parse.js
4
++++ b/dev/lib/parse.js
5
+@@ -1,6 +1,7 @@
6
+ /**
7
+ * @import {
8
+ * Create,
9
++ * Extension,
10
+ * FullNormalizedExtension,
11
+ * InitialConstruct,
12
+ * ParseContext,
13
+@@ -16,6 +17,11 @@ import {string, text} from './initialize/text.js'
14
+ import * as defaultConstructs from './constructs.js'
15
+ import {createTokenizer} from './create-tokenizer.js'
16
+
17
++/** @type {WeakMap<ReadonlyArray<Extension>, FullNormalizedExtension>} */
18
++const combined = new WeakMap()
19
++/** @type {Array<Extension>} */
20
++const noExtensions = []
21
++
22
+ /**
23
+ * @param {ParseOptions | null | undefined} [options]
24
+ * Configuration (optional).
25
+@@ -24,9 +30,16 @@ import {createTokenizer} from './create-tokenizer.js'
26
+ */
27
+ export function parse(options) {
28
+ const settings = options || {}
29
+- const constructs = /** @type {FullNormalizedExtension} */ (
30
+- combineExtensions([defaultConstructs, ...(settings.extensions || [])])
31
+- )
32
++ const extensions = settings.extensions || noExtensions
33
++ // Parsers are made per document; their constructs only depend on the extensions, which must
34
++ // not change once parsed with (unified fills them while attaching plugins, before parsing).
35
++ let constructs = combined.get(extensions)
36
++ if (!constructs) {
37
++ constructs = /** @type {FullNormalizedExtension} */ (
38
++ combineExtensions([defaultConstructs, ...extensions])
39
++ )
40
++ combined.set(extensions, constructs)
41
++ }
42
+
43
+ /** @type {ParseContext} */
44
+ const parser = {
45
+diff --git a/lib/parse.js b/lib/parse.js
46
+index 4682319..11062f2 100644
47
+--- a/lib/parse.js
48
++++ b/lib/parse.js
49
+@@ -1,6 +1,7 @@
50
+ /**
51
+ * @import {
52
+ * Create,
53
++ * Extension,
54
+ * FullNormalizedExtension,
55
+ * InitialConstruct,
56
+ * ParseContext,
57
+@@ -16,6 +17,11 @@ import { string, text } from './initialize/text.js';
58
+ import * as defaultConstructs from './constructs.js';
59
+ import { createTokenizer } from './create-tokenizer.js';
60
+
61
++/** @type {WeakMap<ReadonlyArray<Extension>, FullNormalizedExtension>} */
62
++const combined = new WeakMap();
63
++/** @type {Array<Extension>} */
64
++const noExtensions = [];
65
++
66
+ /**
67
+ * @param {ParseOptions | null | undefined} [options]
68
+ * Configuration (optional).
69
+@@ -24,8 +30,16 @@ import { createTokenizer } from './create-tokenizer.js';
70
+ */
71
+ export function parse(options) {
72
+ const settings = options || {};
73
+- const constructs = /** @type {FullNormalizedExtension} */
74
+- combineExtensions([defaultConstructs, ...(settings.extensions || [])]);
75
++ const extensions = settings.extensions || noExtensions;
76
++ // Parsers are made per document; their constructs only depend on the extensions, which must
77
++ // not change once parsed with (unified fills them while attaching plugins, before parsing).
78
++ let constructs = combined.get(extensions);
79
++ if (!constructs) {
80
++ constructs = /** @type {FullNormalizedExtension} */ (
81
++ combineExtensions([defaultConstructs, ...extensions])
82
++ );
83
++ combined.set(extensions, constructs);
84
++ }
85
+
86
+ /** @type {ParseContext} */
87
+ const parser = {
pnpm-lock.yaml
+25
-9
@@ -268,6 +268,8 @@ overrides:
268
react-native-image-viewing: 0.2.2
269
mdast-util-math: 3.0.0
270
micromark-extension-llm-math: 3.1.0
271
+ micromark: 4.0.2
272
+ mdast-util-to-hast: 13.2.1
273
alchemy: 2.0.0-beta.84
274
expo-audio: 58.0.9
275
expo-network: 58.0.3
@@ -379,8 +381,10 @@ patchedDependencies:
381
expo-video@58.0.11: e80ec3ef8c4af50a607beea1c0917e71ba679844efdb702b45d17a5f03cf466c
382
expo-widgets@58.0.20: 8bf49067f93742855e15f29559ac75c8b50230b47069060993c9ea01dac11171
383
mdast-util-math@3.0.0: afe6b2c89e1eaddfd3c90cf2723f12c9a17d513e65beff51653781486013251c
384
+ mdast-util-to-hast@13.2.1: 57ea46c73aea3c876ab8d351fe0482ddd081dcb4a0610576e399dbe5eb59d2b1
385
micromark-extension-gfm-table@2.1.1: 9ad23a530e45d9f83cba1da8654fe16d5af8b59d5c73d5c7a7c91fd25b9be1d8
386
micromark-extension-llm-math@3.1.0: 7beb6ac4ba52166286593a494531aace81547e55fdddecf892824c25bd84d15a
387
+ micromark@4.0.2: cb79edde7eca83bbcaaafb6ceb7b044b81b987314cde4910823b15257ecada67
388
node-pty@1.2.0-beta.15: f1e680afec85f650699ea756f91f469844ec4c976f4f168492a45c6b3f67d651
389
react-native-gesture-handler@3.3.0: abe20e1d1c23549297cbbc818e14fe479b41455930d9663135d6b40fe359b626
390
react-native-image-viewing@0.2.2: 419f35ee91eac88ced9438058281db5eb09d9c799d8974c1172926068c9448cf
@@ -1171,6 +1175,12 @@ importers:
1175
lucide-react:
1176
specifier: ^0.564.0
1177
version: 0.564.0(react@19.3.0)
1178
+ mdast-util-from-markdown:
1179
+ specifier: ^2.0.3
1180
+ version: 2.0.3(supports-color@8.1.1)
1181
+ micromark-util-combine-extensions:
1182
+ specifier: ^2.0.1
1183
+ version: 2.0.1
1184
morphicons:
1185
specifier: ^1.7.1
1186
version: 1.7.1(react-native-svg@15.16.0(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)
@@ -1277,6 +1287,9 @@ importers:
1287
compression:
1288
specifier: ^1.8.2
1289
version: 1.8.2(supports-color@8.1.1)
1290
+ micromark-util-types:
1291
+ specifier: 'catalog:'
1292
+ version: 2.0.2
1293
oxc-transform-react:
1294
specifier: ~0.152.0
1295
version: 0.152.0
@@ -2000,14 +2013,17 @@ importers:
2013
specifier: 'catalog:'
2014
version: 3.1.0(supports-color@8.1.1)
2015
mdast-util-to-hast:
2003
- specifier: ^13.2.1
2004
- version: 13.2.1
2016
+ specifier: 13.2.1
2017
+ version: 13.2.1(patch_hash=57ea46c73aea3c876ab8d351fe0482ddd081dcb4a0610576e399dbe5eb59d2b1)
2018
micromark-extension-directive:
2019
specifier: 'catalog:'
2020
version: 4.0.0
2021
micromark-util-character:
2022
specifier: 'catalog:'
2023
version: 2.1.1
2024
+ property-information:
2025
+ specifier: ^7.2.0
2026
+ version: 7.2.0
2027
rehype-raw:
2028
specifier: 'catalog:'
2029
version: 7.0.0
@@ -19129,7 +19145,7 @@ snapshots:
19145
hast-util-from-parse5: 8.0.3
19146
hast-util-to-parse5: 8.0.1
19147
html-void-elements: 3.0.0
19132
- mdast-util-to-hast: 13.2.1
19148
+ mdast-util-to-hast: 13.2.1(patch_hash=57ea46c73aea3c876ab8d351fe0482ddd081dcb4a0610576e399dbe5eb59d2b1)
19149
parse5: 7.3.0
19150
unist-util-position: 5.0.0
19151
unist-util-visit: 5.1.0
@@ -19151,7 +19167,7 @@ snapshots:
19167
comma-separated-tokens: 2.0.3
19168
hast-util-whitespace: 3.0.0
19169
html-void-elements: 3.0.0
19154
- mdast-util-to-hast: 13.2.1
19170
+ mdast-util-to-hast: 13.2.1(patch_hash=57ea46c73aea3c876ab8d351fe0482ddd081dcb4a0610576e399dbe5eb59d2b1)
19171
property-information: 7.2.0
19172
space-separated-tokens: 2.0.2
19173
stringify-entities: 4.0.4
@@ -19718,7 +19734,7 @@ snapshots:
19734
decode-named-character-reference: 1.3.0
19735
devlop: 1.1.0
19736
mdast-util-to-string: 4.0.0
19721
- micromark: 4.0.2(supports-color@8.1.1)
19737
+ micromark: 4.0.2(patch_hash=cb79edde7eca83bbcaaafb6ceb7b044b81b987314cde4910823b15257ecada67)(supports-color@8.1.1)
19738
micromark-util-decode-numeric-character-reference: 2.0.2
19739
micromark-util-decode-string: 2.0.1
19740
micromark-util-normalize-identifier: 2.0.1
@@ -19846,7 +19862,7 @@ snapshots:
19862
'@types/mdast': 4.0.4
19863
unist-util-is: 6.0.1
19864
19849
- mdast-util-to-hast@13.2.1:
19865
+ mdast-util-to-hast@13.2.1(patch_hash=57ea46c73aea3c876ab8d351fe0482ddd081dcb4a0610576e399dbe5eb59d2b1):
19866
dependencies:
19867
'@types/hast': 3.0.5
19868
'@types/mdast': 4.0.4
@@ -20247,7 +20263,7 @@ snapshots:
20263
20264
micromark-util-types@2.0.2: {}
20265
20250
- micromark@4.0.2(supports-color@8.1.1):
20266
+ micromark@4.0.2(patch_hash=cb79edde7eca83bbcaaafb6ceb7b044b81b987314cde4910823b15257ecada67)(supports-color@8.1.1):
20267
dependencies:
20268
'@types/debug': 4.1.13
20269
debug: 4.4.3(supports-color@8.1.1)
@@ -21160,7 +21176,7 @@ snapshots:
21176
devlop: 1.1.0
21177
hast-util-to-jsx-runtime: 2.3.6(supports-color@8.1.1)
21178
html-url-attributes: 3.0.1
21163
- mdast-util-to-hast: 13.2.1
21179
+ mdast-util-to-hast: 13.2.1(patch_hash=57ea46c73aea3c876ab8d351fe0482ddd081dcb4a0610576e399dbe5eb59d2b1)
21180
react: 19.3.0
21181
remark-parse: 11.0.0(supports-color@8.1.1)
21182
remark-rehype: 11.1.2
@@ -21455,7 +21471,7 @@ snapshots:
21471
dependencies:
21472
'@types/hast': 3.0.5
21473
'@types/mdast': 4.0.4
21458
- mdast-util-to-hast: 13.2.1
21474
+ mdast-util-to-hast: 13.2.1(patch_hash=57ea46c73aea3c876ab8d351fe0482ddd081dcb4a0610576e399dbe5eb59d2b1)
21475
unified: 11.0.5
21476
vfile: 6.0.3
21477
pnpm-workspace.yaml
+4
@@ -234,6 +234,8 @@ overrides:
234
react-native-image-viewing: 0.2.2
235
mdast-util-math: 3.0.0
236
micromark-extension-llm-math: 3.1.0
237
+ micromark: 4.0.2
238
+ mdast-util-to-hast: 13.2.1
239
alchemy: "catalog:"
240
expo-audio: 58.0.9
241
expo-network: 58.0.3
@@ -357,6 +359,8 @@ patchedDependencies:
359
mdast-util-math@3.0.0: patches/mdast-util-math@3.0.0.patch
360
micromark-extension-llm-math@3.1.0: patches/micromark-extension-llm-math@3.1.0.patch
361
micromark-extension-gfm-table@2.1.1: patches/micromark-extension-gfm-table@2.1.1.patch
362
+ micromark@4.0.2: patches/micromark@4.0.2.patch
363
+ mdast-util-to-hast@13.2.1: patches/mdast-util-to-hast@13.2.1.patch
364
alchemy@2.0.0-beta.84: patches/alchemy@2.0.0-beta.84.patch
365
expo-audio@58.0.9: patches/expo-audio@58.0.9.patch
366
expo-video@58.0.11: patches/expo-video@58.0.11.patch