Open History to see the last commit
1import { chatMarkdownRehypePlugins, loadRehypeRaw } from "@t3tools/shared/markdownPipeline";
2import type { Root } from "mdast";
3import { renderToStaticMarkup } from "react-dom/server";
4import ReactMarkdown from "react-markdown";
5import remarkGfm from "remark-gfm";
6import type { Plugin } from "unified";
7import { describe, expect, it } from "vite-plus/test";
8
9import { remarkCodexDirectives } from "@t3tools/shared/codexMarkdownDirectives";
10import { remarkGithubAlerts } from "@t3tools/shared/markdownGithubAlerts";
11import { createIncrementalMarkdownPlugin } from "./markdown-incremental";
12import { remarkNormalizeListItemIndentation } from "@t3tools/shared/markdownListIndentation";
13
14const CHAT_MARKDOWN_REHYPE_PLUGINS = chatMarkdownRehypePlugins(await loadRehypeRaw());
15
16function render(source: string, incremental?: Plugin<[], Root>, parsedSources?: string[]) {
17 let tree: Root | undefined;
18 const observeParsing: Plugin<[], Root> = function () {
19 const original = this.parser;
20 if (original) {
21 this.parser = (text, file) => {
22 parsedSources?.push(text);
23 return original(text, file);
24 };
25 }
26 };
27 const capture: Plugin<[], Root> = () => (root) => {
28 tree = structuredClone(root);
29 };
30 const html = renderToStaticMarkup(
31 <ReactMarkdown
32 remarkPlugins={[
33 observeParsing,
34 capture,
35 remarkGfm,
36 remarkGithubAlerts,
37 remarkNormalizeListItemIndentation,
38 remarkCodexDirectives,
39 ...(incremental ? [incremental] : []),
40 ]}
41 rehypePlugins={CHAT_MARKDOWN_REHYPE_PLUGINS}
42 >
43 {source}
44 </ReactMarkdown>,
45 );
46 return { html, tree };
47}
48
49const prefix = "# Before\n\n```ts\nconst values = [1, 2];\n```\n\n";
50
51describe("incremental Markdown parsing", () => {
52 it("keeps the document prefix cached when list recovery parses contain fences", () => {
53 const source =
54 prefix +
55 "- first block\n\n ```ts\n const nested = 1;\n ```\n\n tail";
56 const incremental = createIncrementalMarkdownPlugin();
57 const parsedSources: string[] = [];
58 expect(render(source, incremental, parsedSources)).toEqual(render(source));
59 parsedSources.length = 0;
60 const next = source + " more";
61 expect(render(next, incremental, parsedSources)).toEqual(render(next));
62 expect(parsedSources).not.toContain(next);
63 expect(parsedSources.some((text) => text.startsWith("t3-markdown-inline-prefix:"))).toBe(true);
64 });
65
66 it.each([
67 "a\n===\n\nb\n---\n",
68 "- first\n\n continued\n\n- next\n",
69 "> quoted\n>\n> ```js\n> abc\n> ```\n\nend",
70 "<div>\nhello\n\n</div>\n\nend",
71 "[ref]\n\n[ref]: /later",
72 "a[^x]\n\n[^x]: note",
73 "a | b\n--|--\na | b\n",
74 "```\na\n```\n\nnext\n\n~~~\nb\n~~~\n\nmore",
75 "\n\n\tcode\n\nmore",
76 "text <https://example.com> *bold*",
77 "> [!NOTE]\n> alert\n\n- [ ] task",
78 "\uFEFFtext after a byte-order mark",
79 ])("preserves the parse tree, positions, and HTML while streaming %j", (tail) => {
80 const source = prefix + tail;
81 const incremental = createIncrementalMarkdownPlugin();
82 for (let end = 0; end <= source.length; end++) {
83 const text = source.slice(0, end);
84 expect(render(text, incremental), `prefix ${end}`).toEqual(render(text));
85 }
86 });
87
88 it.each(["\r\n", "\r"])("preserves partial %j line endings", (newline) => {
89 const source = (prefix + "next\n\n```\nlast\n```\n\nend").replaceAll("\n", newline);
90 const incremental = createIncrementalMarkdownPlugin();
91 for (let end = 0; end <= source.length; end++) {
92 const text = source.slice(0, end);
93 expect(render(text, incremental)).toEqual(render(text));
94 }
95 });
96
97 it("updates earlier references when definitions arrive after the cached prefix", () => {
98 const before = "[later] and footnote[^note]\n\n" + prefix;
99 const incremental = createIncrementalMarkdownPlugin();
100 for (const tail of ["text", "[later]: /target", "[later]: /target\n\n[^note]: a note"]) {
101 expect(render(before + tail, incremental)).toEqual(render(before + tail));
102 }
103 });
104
105 it("handles edits, replacements, and repeated renders without leaking transformed nodes", () => {
106 const incremental = createIncrementalMarkdownPlugin();
107 const documents = [
108 prefix + "- first\n - second",
109 prefix + "> [!NOTE]\n> transformed alert",
110 prefix + "plain text",
111 "replacement without fences",
112 prefix.replace("Before", "Edited") + "edited prefix",
113 prefix + "plain text",
114 prefix + "plain text",
115 ];
116 for (const document of documents) {
117 expect(render(document, incremental)).toEqual(render(document));
118 }
119 });
120
121 it("does not freeze unclosed, nested, indented, or mismatched fences", () => {
122 const prefixes = [
123 "```\nopen\n\n",
124 "````\n```\n\n",
125 "> ```\n> code\n> ```\n\n",
126 "- ```\n code\n ```\n\n",
127 " ```\n code\n ```\n\n",
128 "<script>\n```\ncode\n```\n\n",
129 ];
130 for (const start of prefixes) {
131 const incremental = createIncrementalMarkdownPlugin();
132 for (const tail of ["", "text", "\n```\n", "\n```\n\nnext"]) {
133 expect(render(start + tail, incremental)).toEqual(render(start + tail));
134 }
135 }
136 });
137});