Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { LARGE_MARKDOWN_CHUNK_MAX_CHARS, splitMarkdown } from "./markdownChunks.ts";
4
5const splitLargeMarkdown = (text: string, targetChars?: number) =>
6 splitMarkdown(text, targetChars).chunks;
7
8const starts = (text: string) =>
9 splitLargeMarkdown(text, 1).map((chunk) => chunk.text.split("\n")[0]);
10
11describe("splitMarkdown", () => {
12 it("cuts only where a new top-level block starts, and keeps every byte", () => {
13 const text = [
14 "# Title",
15 "Intro paragraph",
16 "",
17 "- item one",
18 "",
19 " continued item",
20 "- item two",
21 "",
22 "2. ordered",
23 "",
24 "```md",
25 "# not a heading",
26 "",
27 "Not a paragraph",
28 "```",
29 "",
30 "> quote",
31 "",
32 "Closing paragraph",
33 "## Next",
34 "---",
35 "",
36 "| a | b |",
37 ].join("\n");
38 const chunks = splitLargeMarkdown(text, 1);
39 expect(chunks.map((chunk) => chunk.text).join("\n")).toBe(text);
40 expect(chunks.reduce((sum, chunk) => sum + chunk.lines, 0)).toBe(text.split("\n").length);
41 expect(starts(text)).toEqual([
42 "# Title",
43 "```md",
44 "> quote",
45 "Closing paragraph",
46 "## Next",
47 "| a | b |",
48 ]);
49 });
50
51 it("keeps a fence open until a matching closer", () => {
52 const text = ["````", "```", "", "# inside", "````", "", "after"].join("\n");
53 expect(starts(text)).toEqual(["````", "after"]);
54 });
55
56 it("keeps HTML blocks and display math open across blank lines", () => {
57 const text = [
58 "<!--",
59 "",
60 "# hidden",
61 "-->",
62 "",
63 "$$",
64 "",
65 "Inside math",
66 "$$",
67 "",
68 "<pre>",
69 "",
70 "Preformatted",
71 "</pre>",
72 "",
73 "<!-- one line -->",
74 "",
75 "<div>",
76 "# literal",
77 "</div>",
78 "",
79 "<details>",
80 "<summary>Answer</summary>",
81 "",
82 "Inside details",
83 "",
84 "</details>",
85 "",
86 "<table><tr><td>",
87 "",
88 "```ts",
89 "inside a cell",
90 "```",
91 "",
92 "</td></tr></table>",
93 "",
94 '<ol start="10">',
95 "",
96 "<li>one</li>",
97 "",
98 "<li>two</li>",
99 "",
100 "</ol>",
101 "",
102 "```html",
103 "<details>",
104 "```",
105 "",
106 "after",
107 ].join("\n");
108 expect(starts(text)).toEqual(["<!--", "```html", "after"]);
109 const multiline = [
110 "<details",
111 ' class="faq"><!-- </details> -->',
112 "",
113 "Inside details",
114 "",
115 "</details>",
116 "",
117 "after",
118 ].join("\n");
119 expect(starts(multiline)).toEqual(["<details", "after"]);
120 const unclosed = ["<details>", "", "x".repeat(32 * 1024), "", "after"].join("\n");
121 expect(starts(unclosed)).toEqual(["<details>", "after"]);
122 const head = ["<details>", "", "a".repeat(8 * 1024), "", ""].join("\n");
123 const longLine = "x".repeat(LARGE_MARKDOWN_CHUNK_MAX_CHARS - head.length);
124 expect(starts(head + longLine)).toEqual(["<details>"]);
125 expect(starts(`${head}${longLine}x`)).toEqual(["<details>", `${longLine}x`]);
126 expect(starts(`${head}\`\`\`json\n${longLine}\n\`\`\`\n\n</details>`)).toEqual([
127 "<details>",
128 "```json",
129 ]);
130 });
131
132 it("treats an autolink line as a paragraph, so a following fence still opens", () => {
133 const text = ["<https://example.com>", "```md", "", "# literal", "```", "", "after"].join("\n");
134 expect(starts(text)).toEqual(["<https://example.com>", "after"]);
135 });
136
137 it("waits for the target size before cutting", () => {
138 const text = ["# a", "", "b", "", "# c", "", "d"].join("\n");
139 expect(splitLargeMarkdown(text, 6).map((chunk) => chunk.text)).toEqual([
140 "# a\n\nb\n",
141 "# c\n\nd",
142 ]);
143 });
144
145 it("keeps leading frontmatter in the first piece", () => {
146 const text = ["---", "title: x", "", "# not a heading", "---", "", "# Heading"].join("\n");
147 expect(starts(text)).toEqual(["---", "# Heading"]);
148 });
149
150 it.each([
151 ["one space", " # References"],
152 ["two spaces", " ### References"],
153 ["three spaces", " ###### References"],
154 ])("collects definitions after headings indented by %s without cutting there", (_, heading) => {
155 const intro = "[Release notes][release]\n";
156 const definition = "[release]: /releases";
157 const appendix = ["## Appendix", heading, definition].join("\n");
158 const result = splitMarkdown(`${intro}\n${appendix}`, 1);
159
160 expect(result.definitions).toBe(definition);
161 expect(result.chunks.map((chunk) => chunk.text)).toEqual([intro, appendix]);
162 });
163
164 it("collects link definitions outside code so every piece can resolve them", () => {
165 const text = [
166 "[![badge][b]][site]",
167 "",
168 "```md",
169 "[fake]: /not-a-definition",
170 "```",
171 "",
172 "Paragraph",
173 "[inline]: /continues-the-paragraph",
174 "",
175 "[site]: https://example.com",
176 "[b]: https://example.com/badge.svg 'Badge'",
177 "[^note]: a footnote",
178 "",
179 '[x]: /url "title" is paragraph text',
180 "",
181 "# Heading",
182 "[h]: /after-heading",
183 "",
184 "---",
185 "[t]: /after-break",
186 "",
187 "===",
188 "[p]: /paragraph-text",
189 "",
190 "```sh",
191 "npm install",
192 "```",
193 "[fence]: /after-fence",
194 "~~~",
195 "~~~",
196 "Text after a fence",
197 "[q]: /continues-the-paragraph",
198 "",
199 "$$",
200 "x",
201 "$$ trailing",
202 "[math]: /inside-math",
203 "",
204 "<!-->",
205 "```",
206 "-->",
207 "```",
208 "[comment]: /after-a-closed-comment",
209 "```",
210 "[code]: /inside-a-fence-after-a-comment",
211 "```",
212 "",
213 "- ```md",
214 " ~~~",
215 " ~~~",
216 " [list]: /inside-a-list-fence",
217 " ```",
218 ].join("\n");
219 expect(splitMarkdown(text).definitions).toBe(
220 "[site]: https://example.com\n[b]: https://example.com/badge.svg 'Badge'\n[h]: /after-heading\n[t]: /after-break\n[fence]: /after-fence\n[comment]: /after-a-closed-comment",
221 );
222 const frontmatter = ["---", "title: x", "...", "[fm]: /after-frontmatter", "", "# A"];
223 expect(splitMarkdown(frontmatter.join("\n")).definitions).toBe("[fm]: /after-frontmatter");
224 const unclosed = ["---", ...Array<string>(1000).fill("text"), "[u]: /paragraph-text"];
225 expect(splitMarkdown(unclosed.join("\n")).definitions).toBe("");
226 });
227});