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

markdownPipeline.test.ts

88 lines3.5 KB

1import { expect, it } from "vite-plus/test";
2import { unified } from "unified";
3import remarkParse from "remark-parse";
4import remarkRehype from "remark-rehype";
5import { CHAT_MARKDOWN_REHYPE_PLUGINS, CHAT_MARKDOWN_REMARK_PLUGINS } from "./markdownPipeline.ts";
6​
7it("preserves in-app thread links through the shared Markdown sanitizer", () => {
8 const processor = unified()
9 .use(remarkParse)
10 .use(CHAT_MARKDOWN_REMARK_PLUGINS)
11 .use(remarkRehype, { allowDangerousHtml: true })
12 .use(CHAT_MARKDOWN_REHYPE_PLUGINS);
13 const tree = processor.runSync(
14 processor.parse(
15 '[Open thread](t3-thread://v1/thread)\n\n<t3-thread://v2/environment/thread:a%3A1>\n\n<a href="javascript:alert(1)">Unsafe</a>',
16 ),
17 );
18 expect(tree.children[0]).toMatchObject({
19 tagName: "p",
20 children: [
21 {
22 tagName: "a",
23 properties: { href: "t3-thread://v1/thread" },
24 children: [{ type: "text", value: "Open thread" }],
25 },
26 ],
27 });
28 expect(tree.children[2]).toMatchObject({
29 tagName: "p",
30 children: [
31 {
32 tagName: "a",
33 properties: { href: "t3-thread://v2/environment/thread:a%3A1" },
34 children: [{ type: "text", value: "t3-thread://v2/environment/thread:a%3A1" }],
35 },
36 ],
37 });
38 expect(JSON.stringify(tree)).not.toContain("javascript:");
39});
40​
41it("keeps backslashes that CommonMark would read as escapes in prose Windows paths", () => {
42 const processor = unified()
43 .use(remarkParse)
44 .use(CHAT_MARKDOWN_REMARK_PLUGINS)
45 .use(remarkRehype, { allowDangerousHtml: true })
46 .use(CHAT_MARKDOWN_REHYPE_PLUGINS);
47 type Node = { type: string; value?: string; tagName?: string; children?: Node[] };
48 const text = (node: Node): string =>
49 node.type === "text"
50 ? (node.value ?? "")
51 : node.tagName === "code"
52 ? `<code>${(node.children ?? []).map(text).join("")}</code>`
53 : (node.children ?? []).map(text).join("");
54 const render = (markdown: string) => text(processor.runSync(processor.parse(markdown)) as Node);
55​
56 expect(
57 render(String.raw`Saved to C:\Users\me\.t3\_build and (C:\.scratch) and C:/work\.config.`),
58 ).toBe(String.raw`Saved to C:\Users\me\.t3\_build and (C:\.scratch) and C:/work\.config.`);
59 expect(render(String.raw`Escaped C:\\Users\\me`)).toBe(String.raw`Escaped C:\\Users\\me`);
60 expect(render(String.raw`See C:\x and \*this\* and Note:\*that\*.`)).toBe(
61 String.raw`See C:\x and *this* and Note:*that*.`,
62 );
63 expect(render(String.raw`${"`"}C:\me\.t3${"`"} and C:\me\.t3`)).toBe(
64 String.raw`<code>C:\me\.t3</code> and C:\me\.t3`,
65 );
66});
67​
68it("keeps an image on its own line under a label but leaves inline images inline", () => {
69 const processor = unified()
70 .use(remarkParse)
71 .use(CHAT_MARKDOWN_REMARK_PLUGINS)
72 .use(remarkRehype, { allowDangerousHtml: true })
73 .use(CHAT_MARKDOWN_REHYPE_PLUGINS);
74 type Node = { type: string; value?: string; tagName?: string; children?: Node[] };
75 const shape = (node: Node): string =>
76 node.type === "text"
77 ? (node.value ?? "")
78 : node.tagName === "br" || node.tagName === "img"
79 ? `<${node.tagName}>`
80 : (node.children ?? []).map(shape).join("");
81 const render = (markdown: string) => shape(processor.runSync(processor.parse(markdown)) as Node);
82​
83 expect(
84 render("**Palette**\n![Palette](/tmp/palette.png)\n**Hero** (dark)\n![Hero](/tmp/hero.png)"),
85 ).toBe("Palette<br>\n<img><br>\nHero (dark)<br>\n<img>");
86 expect(render("Built [![CI](/ci.svg)](/ci)\nand wrapped")).toBe("Built <img>\nand wrapped");
87 expect(render("Hard-wrapped\nprose")).toBe("Hard-wrapped\nprose");
88});