Open History to see the last commit
1import { toHtml } from "hast-util-to-html";
2import remarkGfm from "remark-gfm";
3import remarkParse from "remark-parse";
4import remarkRehype from "remark-rehype";
5import { type PluggableList, unified } from "unified";
6import { describe, expect, it } from "vite-plus/test";
7
8import { remarkCodexDirectives } from "./codexMarkdownDirectives.ts";
9import { remarkNormalizeListItemIndentation } from "./markdownListIndentation.ts";
10import { remarkEnableLeadingWhitespace } from "./markdownLeadingWhitespace.ts";
11
12const processor = unified()
13 .use(remarkParse)
14 .use([
15 remarkGfm,
16 remarkNormalizeListItemIndentation,
17 remarkCodexDirectives,
18 ] satisfies PluggableList)
19 .use(remarkRehype, { allowDangerousHtml: true });
20
21function renderMarkdown(markdown: string): string {
22 return toHtml(processor.runSync(processor.parse(markdown), markdown), {
23 allowDangerousHtml: true,
24 });
25}
26
27describe("remarkNormalizeListItemIndentation", () => {
28 it.each([
29 "- Read [the guide][guide].\n\n[guide]: https://example.com/guide",
30 '> [guide]: https://example.com/guide\n> "Guide"\n\n- Read [the guide][GUIDE].',
31 ">>[my\n> > guide]: https://example.com/guide\n\n- Read [the guide][my guide].",
32 ])("uses document reference definitions in recovered items: %j", (markdown) => {
33 expect(renderMarkdown(markdown)).toBe(renderMarkdown(markdown.replace("- ", "- ")));
34 expect(renderMarkdown(markdown)).toContain('<a href="https://example.com/guide"');
35 });
36
37 it.each([
38 ["- \tfoo\n bar", '<span data-leading-whitespace=""> </span>bar'],
39 ["- foo\n \tbar", "\nbar"],
40 ])("keeps continuation indentation aligned to tab stops: %j", (markdown, expected) => {
41 const userProcessor = processor().use(remarkEnableLeadingWhitespace);
42 const html = toHtml(userProcessor.runSync(userProcessor.parse(markdown), markdown));
43 expect(html).toContain(expected);
44 });
45
46 it("recovers tab-indented list text after a leading byte order mark", () => {
47 const markdown = "- \tfoo\n bar";
48 expect(renderMarkdown(`\uFEFF${markdown}`)).toBe(renderMarkdown(markdown));
49 });
50
51 it.each(["\u2028", "\u2029"])(
52 "preserves tabs after Unicode separators in inline code: %j",
53 (separator) => {
54 const code = `before${separator}\tafter`;
55 expect(renderMarkdown(`- \`${code}\``)).toBe(
56 `<ul>\n<li><code>${code}</code></li>\n</ul>`,
57 );
58 },
59 );
60
61 it("renders a leading directive in over-indented list content", () => {
62 expect(renderMarkdown('- :codex-file-citation{path="src/a.ts"}')).toBe(
63 '<ul>\n<li><a href="src/a.ts">a.ts</a></li>\n</ul>',
64 );
65 });
66
67 it("renders same-line over-indented list content as list text", () => {
68 const html = renderMarkdown(`why did you do this?
69
70- for (const step of rest.steps) {
71- if (step.request.body) {
72- step.request.body = "<redacted>";
73- }
74- }`);
75
76 expect(html).not.toContain("<pre>");
77 expect(html).toContain("<li>for (const step of rest.steps) {</li>");
78 expect(html).toContain("<li>if (step.request.body) {</li>");
79 expect(html).toContain('<li>step.request.body = "<redacted>";</li>');
80 });
81
82 it("preserves every recovered block separated by blank lines", () => {
83 const html = renderMarkdown(`- **first block**
84
85 [second block](https://example.com)`);
86
87 expect(html).toContain("<strong>first block</strong>");
88 expect(html).toContain('<a href="https://example.com">second block</a>');
89 expect(
90 renderMarkdown(
91 "- Read [the guide][guide].\n\n [guide]: /local\n\n[guide]: /document",
92 ),
93 ).toContain('<a href="/local">the guide</a>');
94 expect(renderMarkdown("- first\n\n ```\n \tcode\n ```")).toContain(
95 "<pre><code>\tcode\n</code></pre>",
96 );
97 });
98
99 it("recursively normalizes lists in recovered tail blocks", () => {
100 const html = renderMarkdown(`- first block
101
102 - nested block`);
103
104 expect(html).not.toContain("<pre>");
105 expect(html).toContain("<li>nested block</li>");
106 });
107
108 it("preserves fenced code blocks within list items", () => {
109 const html = renderMarkdown(`- \`\`\`ts
110 const value = 1;
111 \`\`\``);
112
113 expect(html).toContain('<pre><code class="language-ts">const value = 1;');
114 });
115
116 it("preserves indented code blocks that start below a list marker", () => {
117 const html = renderMarkdown(`-
118 const value = 1;`);
119
120 expect(html).toContain("<pre><code>const value = 1;");
121 });
122});