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

markdownHtml.test.ts

97 lines4.6 KB

1import { describe, expect, it } from "vite-plus/test";
2import { markdownHtmlContent, markdownWithHtml, parseMarkdownDocument } from "./markdownDocument";
3import { markdownImageSources } from "./markdownImageSources";
4​
5describe("HTML in native Markdown", () => {
6 it("preserves screenshot URLs and captions from GitHub HTML image embeds", () => {
7 expect(
8 markdownHtmlContent(
9 '<img width="900" alt="Before > after [settings]" src="https://example.com/before.png?a=1&amp;b=2" />',
10 ),
11 ).toBe("![Before > after \\[settings\\]](<https://example.com/before.png?a=1\\&b=2>)");
12 expect(
13 markdownHtmlContent(
14 "<a href='https://example.com'><img src='https://example.com/banner.svg' alt='Review' /></a>",
15 ),
16 ).toBe("[![Review](<https://example.com/banner.svg>)](<https://example.com>)");
17 });
18​
19 it("decodes HTML attributes once and preserves entity-shaped query values through Markdown", () => {
20 const markdown =
21 '<img alt="&amp;lt;" src="https://example.com/shot.png?label=&amp;lt;&amp;sig=a%26b">';
22 expect(markdownWithHtml(markdown)).toBe(
23 "![\\&lt;](<https://example.com/shot.png?label=\\&lt;\\&sig=a%26b>)",
24 );
25 expect(markdownImageSources(markdown)).toEqual([
26 "https://example.com/shot.png?label=&lt;&sig=a%26b",
27 ]);
28 const paragraph = parseMarkdownDocument(markdown).children[0];
29 expect(paragraph?.type).toBe("paragraph");
30 if (paragraph?.type === "paragraph")
31 expect(paragraph.children[0]).toMatchObject({ type: "image", alt: "&lt;" });
32 });
33​
34 it("ignores code delimiters in HTML attributes and comments when collecting screenshots", () => {
35 const markdown =
36 '<details data-example="`">\n<summary>Proof</summary>\n<img src="https://example.com/shot.png" alt="Screenshot">\n<!-- ` -->\n</details>';
37 expect(markdownHtmlContent(markdown)).toContain(
38 "![Screenshot](<https://example.com/shot.png>)",
39 );
40 expect(markdownImageSources(markdown)).toEqual(["https://example.com/shot.png"]);
41 expect(markdownWithHtml(markdown)).not.toContain("<img");
42 });
43​
44 it("removes bot metadata and details wrappers while keeping the content", () => {
45 const markdown =
46 "<!-- generated bot metadata -->\n<details>\n<summary>Walkthrough</summary>\n\n## Changes\n\nText\n\n</details>";
47 const result = markdownWithHtml(markdown);
48 expect(result).not.toContain("<!--");
49 expect(result).not.toContain("<details>");
50 expect(result).toContain("**Walkthrough**");
51 expect(result).toContain("## Changes");
52 expect(parseMarkdownDocument(markdown).children.some((node) => node.type === "html")).toBe(
53 false,
54 );
55 });
56​
57 it("collects HTML and Markdown screenshots while leaving inline and fenced code literal", () => {
58 const markdown =
59 '```html\n<img src="literal.png">\n```\n\nUse `<img src="inline.png">` here.\n\n<img src="one.png">\n\n![Two](two.png)\n\n<img src="one.png">';
60 expect(markdownImageSources(markdown)).toEqual(["one.png", "two.png"]);
61 const result = markdownWithHtml(markdown);
62 expect(result).toContain('```html\n<img src="literal.png">\n```');
63 expect(result).toContain('`<img src="inline.png">`');
64 });
65​
66 it("links HTML anchors in a table cell without breaking the row at <br>", () => {
67 const document = parseMarkdownDocument(
68 "| Status | Preview URL |\n| - | - |\n| Done | <a href='https://commit.example.com'>Commit</a><br><a href='https://branch.example.com'>Branch</a> |",
69 );
70 const table = document.children[0];
71 expect(table?.type).toBe("table");
72 if (table?.type !== "table") return;
73 expect(table.children).toHaveLength(2);
74 expect(table.children[1]?.children[1]?.children).toMatchObject([
75 { type: "link", url: "https://commit.example.com", children: [{ value: "Commit" }] },
76 { type: "text", value: " " },
77 { type: "link", url: "https://branch.example.com", children: [{ value: "Branch" }] },
78 ]);
79 });
80​
81 it("keeps a table row intact when an anchor href contains a pipe", () => {
82 const document = parseMarkdownDocument(
83 "| Preview | Logs |\n| - | - |\n| <a href='https://example.com/?a=1&#124;2'>Open</a> | done |",
84 );
85 const table = document.children[0];
86 expect(table?.type).toBe("table");
87 if (table?.type !== "table") return;
88 expect(table.children[1]?.children).toMatchObject([
89 { children: [{ type: "link", url: "https://example.com/?a=1%7C2" }] },
90 { children: [{ value: "done" }] },
91 ]);
92 });
93​
94 it("keeps anchor text unlinked for unsafe hrefs", () => {
95 expect(markdownWithHtml('Run <a href="javascript:alert(1)">this</a> now')).toBe("Run this now");
96 });
97});