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

markdownListIndentation.test.ts

69 lines2.3 KB

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 { remarkNormalizeListItemIndentation } from "./markdownListIndentation.ts";
9​
10const processor = unified()
11 .use(remarkParse)
12 .use([remarkGfm, remarkNormalizeListItemIndentation] satisfies PluggableList)
13 .use(remarkRehype, { allowDangerousHtml: true });
14​
15function renderMarkdown(markdown: string): string {
16 return toHtml(processor.runSync(processor.parse(markdown), markdown), {
17 allowDangerousHtml: true,
18 });
20​
21describe("remarkNormalizeListItemIndentation", () => {
22 it("renders same-line over-indented list content as list text", () => {
23 const html = renderMarkdown(`why did you do this?
24​
25- for (const step of rest.steps) {
26- if (step.request.body) {
27- step.request.body = "<redacted>";
28- }
29- }`);
30​
31 expect(html).not.toContain("<pre>");
32 expect(html).toContain("<li>for (const step of rest.steps) {</li>");
33 expect(html).toContain("<li>if (step.request.body) {</li>");
34 expect(html).toContain('<li>step.request.body = "<redacted>";</li>');
35 });
36​
37 it("preserves every recovered block separated by blank lines", () => {
38 const html = renderMarkdown(`- **first block**
39​
40 [second block](https://example.com)`);
41​
42 expect(html).toContain("<strong>first block</strong>");
43 expect(html).toContain('<a href="https://example.com">second block</a>');
44 });
45​
46 it("recursively normalizes lists in recovered tail blocks", () => {
47 const html = renderMarkdown(`- first block
48​
49 - nested block`);
50​
51 expect(html).not.toContain("<pre>");
52 expect(html).toContain("<li>nested block</li>");
53 });
54​
55 it("preserves fenced code blocks within list items", () => {
56 const html = renderMarkdown(`- \`\`\`ts
57 const value = 1;
58 \`\`\``);
59​
60 expect(html).toContain('<pre><code class="language-ts">const value = 1;');
61 });
62​
63 it("preserves indented code blocks that start below a list marker", () => {
64 const html = renderMarkdown(`-
65 const value = 1;`);
66​
67 expect(html).toContain("<pre><code>const value = 1;");
68 });
69});