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