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("collects link definitions outside code so every piece can resolve them", () => {
151 const text = [
152 "[![badge][b]][site]",
153 "",
154 "```md",
155 "[fake]: /not-a-definition",
156 "```",
157 "",
158 "Paragraph",
159 "[inline]: /continues-the-paragraph",
160 "",
161 "[site]: https://example.com",
162 "[b]: https://example.com/badge.svg 'Badge'",
163 "[^note]: a footnote",
164 "",
165 '[x]: /url "title" is paragraph text',
166 "",
167 "# Heading",
168 "[h]: /after-heading",
169 "",
170 "---",
171 "[t]: /after-break",
172 "",
173 "===",
174 "[p]: /paragraph-text",
175 "",
176 "```sh",
177 "npm install",
178 "```",
179 "[fence]: /after-fence",
180 "~~~",
181 "~~~",
182 "Text after a fence",
183 "[q]: /continues-the-paragraph",
184 "",
185 "$$",
186 "x",
187 "$$ trailing",
188 "[math]: /inside-math",
189 "",
190 "<!-->",
191 "```",
192 "-->",
193 "```",
194 "[comment]: /after-a-closed-comment",
195 "```",
196 "[code]: /inside-a-fence-after-a-comment",
197 "```",
198 "",
199 "- ```md",
200 " ~~~",
201 " ~~~",
202 " [list]: /inside-a-list-fence",
203 " ```",
204 ].join("\n");
205 expect(splitMarkdown(text).definitions).toBe(
206 "[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",
207 );
208 });
209});