Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

markdownDetails.test.ts

253 lines9.6 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { splitMarkdownDetails } from "./markdownDetails";
4import {
5 markdownForNativeParsing,
6 markdownReferenceDefinitions,
7 withReferenceDefinitions,
8} from "./markdownDocument";
9import { parseEnrichedMarkdown as parseMarkdown } from "./parseEnrichedMarkdown.test-support";
10​
11type NativeNode = Awaited<ReturnType<typeof parseMarkdown>>;
12​
13/** Where the native parser sends each link and image in one rendered fragment of a reply. */
14async function referenceDestinations(reply: string, pick: number) {
15 const segment = splitMarkdownDetails(reply)[pick];
16 const fragment = segment?.kind === "details" ? segment.body : (segment?.markdown ?? "");
17 const urls: Array<string | undefined> = [];
18 const visit = (node: NativeNode) => {
19 if (node.type === "Link" || node.type === "Image") urls.push(node.attributes?.url);
20 node.children?.forEach(visit);
21 };
22 visit(
23 await parseMarkdown(
24 markdownForNativeParsing(
25 withReferenceDefinitions(fragment, markdownReferenceDefinitions(reply)),
26 ),
27 ),
28 );
29 return urls;
31​
32describe("splitMarkdownDetails", () => {
33 // regression: #28059 mobile chat showed every <details> fold open
34 it("splits a top-level fold into its summary and hidden body", () => {
35 const markdown =
36 "Short answer.\n\n<details>\n<summary>Long details</summary>\n\nA long paragraph.\n\n</details>\n\nAfter.";
37 expect(splitMarkdownDetails(markdown)).toEqual([
38 { kind: "markdown", markdown: "Short answer.\n\n", sourceOffset: 0 },
39 {
40 kind: "details",
41 summary: "Long details",
42 body: "\n\nA long paragraph.\n\n",
43 open: false,
44 sourceOffset: 15,
45 },
46 { kind: "markdown", markdown: "\n\nAfter.", sourceOffset: 87 },
47 ]);
48 });
49​
50 it("splits a fold after a leading byte-order mark at its source offsets", () => {
51 const markdown =
52 "\uFEFFShort answer.\n\n<details>\n<summary>Long details</summary>\n\nA long paragraph.\n\n</details>\n\nAfter.";
53 expect(splitMarkdownDetails(markdown)).toEqual([
54 { kind: "markdown", markdown: "\uFEFFShort answer.\n\n", sourceOffset: 0 },
55 {
56 kind: "details",
57 summary: "Long details",
58 body: "\n\nA long paragraph.\n\n",
59 open: false,
60 sourceOffset: 16,
61 },
62 { kind: "markdown", markdown: "\n\nAfter.", sourceOffset: 88 },
63 ]);
64 });
65​
66 it("keeps nested folds in the body and honours the open attribute", () => {
67 const markdown =
68 "<details open>\n<summary>Outer &amp; <code>more</code></summary>\n\n<details>\n<summary>Inner</summary>\n\nx\n\n</details>\n\n</details>";
69 const [fold] = splitMarkdownDetails(markdown);
70 expect(fold).toMatchObject({ kind: "details", summary: "Outer & more", open: true });
71 expect(fold?.kind === "details" && splitMarkdownDetails(fold.body)).toMatchObject([
72 { kind: "markdown" },
73 { kind: "details", summary: "Inner", body: "\n\nx\n\n" },
74 { kind: "markdown" },
75 ]);
76 });
77​
78 it("leaves folds in code and inline tags as markdown", () => {
79 for (const markdown of [
80 "```html\n<details>\n<summary>Code</summary>\n</details>\n```",
81 "Use `<details>` for folds.",
82 "Text <details><summary>inline</summary>body</details> more",
83 "<!-- <details> -->\n\nVisible answer.",
84 ]) {
85 expect(splitMarkdownDetails(markdown)).toEqual([
86 { kind: "markdown", markdown, sourceOffset: 0 },
87 ]);
88 }
89 });
90​
91 it("ignores a closing tag inside a code example in the fold", () => {
92 for (const [eol, openFence, closeFence] of [
93 ["\n", "```html", "```"],
94 ["\r\n", "```html", "```"],
95 ["\r", "~~~", "~~~"],
96 ] as const) {
97 const body = `${eol}${openFence}${eol}</details>${eol}${closeFence}${eol}`;
98 const trailing = `${eol}${eol}After.`;
99 const markdown = `<details>${eol}<summary>Example</summary>${body}</details>${trailing}`;
100 expect(splitMarkdownDetails(markdown)).toMatchObject([
101 { kind: "details", summary: "Example", body },
102 { kind: "markdown", markdown: trailing },
103 ]);
104 }
105 const fencedAfterEscaped = [
106 "<details>",
107 "<summary>Example</summary>",
108 "\\``",
109 "~~~",
110 "`",
111 "</details>",
112 "~~~",
113 "</details>",
114 "",
115 "After",
116 ].join("\n");
117 expect(splitMarkdownDetails(fencedAfterEscaped)).toMatchObject([
118 { kind: "details", summary: "Example", body: "\n\\``\n~~~\n`\n</details>\n~~~\n" },
119 { kind: "markdown", markdown: "\n\nAfter" },
120 ]);
121 });
122​
123 it("closes a fold at a closing tag that ends a paragraph", () => {
124 expect(
125 splitMarkdownDetails("<details>\n<summary>Plan</summary>\n\nDone </details>\n\nNext step"),
126 ).toMatchObject([
127 { kind: "details", summary: "Plan", body: "\n\nDone " },
128 { kind: "markdown", markdown: "\n\nNext step" },
129 ]);
130 });
131​
132 it("keeps same-line triple backticks inline in CR-delimited folds", () => {
133 const body = "\r```literal </details>```\r";
134 const trailing = "\r\rAfter.";
135 const markdown = `<details>\r<summary>Example</summary>${body}</details>${trailing}`;
136 expect(splitMarkdownDetails(markdown)).toMatchObject([
137 { kind: "details", summary: "Example", body },
138 { kind: "markdown", markdown: trailing },
139 ]);
140 });
141​
142 it("treats escaped backticks as literal when finding fold boundaries", () => {
143 for (const [line, trailing] of [
144 ["\\`", "\n\\`\n\nAfter"],
145 ["\\``</details>`", "\n\nAfter"],
146 ["\\\\`</details>`", "\n\nAfter"],
147 ['\\``<a title="`">', "\n`\nAfter"],
148 ]) {
149 const body = `\n${line}\n`;
150 expect(
151 splitMarkdownDetails(`<details>\n<summary>Example</summary>${body}</details>${trailing}`),
152 ).toMatchObject([
153 { kind: "details", summary: "Example", body },
154 { kind: "markdown", markdown: trailing },
155 ]);
156 }
157 });
158​
159 it("folds the rest of a reply whose closing tag has not streamed yet", () => {
160 expect(splitMarkdownDetails("<details>\n<summary>Plan</summary>\n\nStep one")).toEqual([
161 { kind: "details", summary: "Plan", body: "\n\nStep one", open: false, sourceOffset: 0 },
162 ]);
163 expect(
164 splitMarkdownDetails("<details>\n<summary>Plan</summary>\n\n```html\n</details>"),
165 ).toMatchObject([{ kind: "details", body: "\n\n```html\n</details>" }]);
166 });
167​
168 it("reads open only as an attribute name", () => {
169 for (const [attributes, open] of [
170 ['title="is open"', false],
171 ["open-on-load", false],
172 ["title = open", false],
173 ["data-label=hello\u00a0open", false],
174 ['title=foo"open"', false],
175 ['"open"', false],
176 ["=open", false],
177 ["open", true],
178 ['title=preview\tOPEN="false"', true],
179 ] as const) {
180 expect(
181 splitMarkdownDetails(
182 `<details ${attributes}>\n<summary>Example</summary>\nHidden\n</details>`,
183 ),
184 attributes,
185 ).toMatchObject([{ kind: "details", open }]);
186 }
187 });
188});
189​
190describe("withReferenceDefinitions", () => {
191 it.each([
192 [
193 "\uFEFF[docs]: https://example.com/guide\n\n[other]: https://example.com/other",
194 ["[docs]: https://example.com/guide", "[other]: https://example.com/other"],
195 ],
196 ["intro\n\n\uFEFF[docs]: https://example.com/guide", []],
197 ["\uFEFF\uFEFF[docs]: https://example.com/guide", []],
198 ])("extracts complete reference definitions after BOM in %j", (markdown, expected) => {
199 expect(markdownReferenceDefinitions(markdown)).toEqual(expected);
200 });
201​
202 it("resolves a fragment's references from the whole reply, its own definitions first", async () => {
203 const fold = "\n\n<details><summary>A</summary>a</details>\n\n";
204 for (const [reply, pick, expected] of [
205 // Into a fold, with an escaped label and a Windows destination kept as written.
206 [
207 "<details>\n<summary>A</summary>\n\n[config][c] ![B][a\\]b]\n\n</details>\n\n[c]: C:\\work\\.t3\\settings.json\n[a\\]b]: https://example.com/a",
208 0,
209 ["C:\\work\\.t3\\settings.json", "https://example.com/a"],
210 ],
211 // Out of a fold.
212 [
213 "See [d].\n\n<details>\n<summary>A</summary>\n\n[d]: https://example.com\n\n</details>",
214 0,
215 ["https://example.com"],
216 ],
217 // A quoted definition that wraps onto continuation lines.
218 [`See [d].${fold}> [d]:\n> https://example.com\n> "Title"`, 0, ["https://example.com"]],
219 // A definition the compact HTML block hides from the reply's parse, with a label that wraps.
220 [
221 "See [the\n doc].\n\n<details><summary>A</summary>a</details>\n[The Doc]: https://example.com",
222 0,
223 ["https://example.com"],
224 ],
225 // A literal ">" in a wrapped label is part of the label, not a quote marker.
226 [
227 `[part rest] and [part > rest]${fold}[part\n > rest]: https://example.com/a\n\n[part rest]: https://example.com/b`,
228 0,
229 ["https://example.com/b", "https://example.com/a"],
230 ],
231 // A fragment's own definition wins, however the parser equates the two labels.
232 [
233 "[ x ]: https://outside.example\n\n<details>\n<summary>S</summary>\n\nSee [x].\n\n[X]: https://inside.example\n\n</details>",
234 1,
235 ["https://inside.example"],
236 ],
237 [
238 "[SS]: https://outside.example\n\n<details>\n<summary>S</summary>\n\nSee [SS].\n\n[\u00df]: https://inside.example\n\n</details>",
239 1,
240 ["https://inside.example"],
241 ],
242 ] as const) {
243 expect(await referenceDestinations(reply, pick)).toEqual(expected);
244 }
245 });
246​
247 it("leaves a fragment with nothing to resolve as written", () => {
248 expect(withReferenceDefinitions("See [d].", [])).toBe("See [d].");
249 expect(withReferenceDefinitions("Plain text.", ["[d]: https://example.com"])).toBe(
250 "Plain text.",
251 );
252 });
253});