Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { 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 "after",
80 ].join("\n");
81 expect(starts(text)).toEqual(["<!--", "after"]);
82 });
83
84 it("treats an autolink line as a paragraph, so a following fence still opens", () => {
85 const text = ["<https://example.com>", "```md", "", "# literal", "```", "", "after"].join("\n");
86 expect(starts(text)).toEqual(["<https://example.com>", "after"]);
87 });
88
89 it("waits for the target size before cutting", () => {
90 const text = ["# a", "", "b", "", "# c", "", "d"].join("\n");
91 expect(splitLargeMarkdown(text, 6).map((chunk) => chunk.text)).toEqual([
92 "# a\n\nb\n",
93 "# c\n\nd",
94 ]);
95 });
96
97 it("keeps leading frontmatter in the first piece", () => {
98 const text = ["---", "title: x", "", "# not a heading", "---", "", "# Heading"].join("\n");
99 expect(starts(text)).toEqual(["---", "# Heading"]);
100 });
101
102 it("collects link definitions outside code so every piece can resolve them", () => {
103 const text = [
104 "[![badge][b]][site]",
105 "",
106 "```md",
107 "[fake]: /not-a-definition",
108 "```",
109 "",
110 "Paragraph",
111 "[inline]: /continues-the-paragraph",
112 "",
113 "[site]: https://example.com",
114 "[b]: https://example.com/badge.svg 'Badge'",
115 "[^note]: a footnote",
116 "",
117 '[x]: /url "title" is paragraph text',
118 "",
119 "# Heading",
120 "[h]: /after-heading",
121 "",
122 "---",
123 "[t]: /after-break",
124 "",
125 "===",
126 "[p]: /paragraph-text",
127 ].join("\n");
128 expect(splitMarkdown(text).definitions).toBe(
129 "[site]: https://example.com\n[b]: https://example.com/badge.svg 'Badge'\n[h]: /after-heading\n[t]: /after-break",
130 );
131 });
132});