Open History to see the last commit
1import { expect, it } from "vite-plus/test";
2import { unified } from "unified";
3import rehypeRaw from "rehype-raw";
4import remarkParse from "remark-parse";
5import remarkRehype from "remark-rehype";
6import { CHAT_MARKDOWN_REHYPE_PLUGINS, CHAT_MARKDOWN_REMARK_PLUGINS } from "./markdownPipeline.ts";
7
8it("preserves in-app thread links through the shared Markdown sanitizer", () => {
9 const processor = unified()
10 .use(remarkParse)
11 .use(CHAT_MARKDOWN_REMARK_PLUGINS)
12 .use(remarkRehype, { allowDangerousHtml: true })
13 .use(CHAT_MARKDOWN_REHYPE_PLUGINS);
14 const tree = processor.runSync(
15 processor.parse(
16 '[Open thread](t3-thread://v1/thread)\n\n<t3-thread://v2/environment/thread:a%3A1>\n\n<a href="javascript:alert(1)">Unsafe</a>',
17 ),
18 );
19 expect(tree.children[0]).toMatchObject({
20 tagName: "p",
21 children: [
22 {
23 tagName: "a",
24 properties: { href: "t3-thread://v1/thread" },
25 children: [{ type: "text", value: "Open thread" }],
26 },
27 ],
28 });
29 expect(tree.children[2]).toMatchObject({
30 tagName: "p",
31 children: [
32 {
33 tagName: "a",
34 properties: { href: "t3-thread://v2/environment/thread:a%3A1" },
35 children: [{ type: "text", value: "t3-thread://v2/environment/thread:a%3A1" }],
36 },
37 ],
38 });
39 expect(JSON.stringify(tree)).not.toContain("javascript:");
40});
41
42it("builds the tree rehype-raw builds for markdown without HTML", () => {
43 type Node = {
44 type: string;
45 tagName?: string;
46 value?: string;
47 position?: unknown;
48 data?: unknown;
49 children?: Node[];
50 };
51 // What the round trip changes that renders the same; see `needsHtmlParser`.
52 const comparable = (node: Node): Node => {
53 const { position, children, data, ...rest } = node;
54 const result: Node =
55 node.type === "text" || node.type === "root" ? rest : { ...rest, position, data };
56 if (!children) return result;
57 const merged: Node[] = [];
58 for (const child of children.map(comparable)) {
59 const last = merged.at(-1);
60 if (child.type === "text" && last?.type === "text") last.value += child.value ?? "";
61 else merged.push(child);
62 }
63 // Blank text inside a table moves in front of it; the JSX runtime drops it in tables.
64 const inTable = /^(?:table|thead|tbody|tfoot|tr)$/.test(node.tagName ?? "");
65 result.children = merged.filter(
66 (child, index) =>
67 child.type !== "text" ||
68 child.value?.trim() ||
69 !(inTable || merged[index + 1]?.tagName === "table"),
70 );
71 return result;
72 };
73 const tree = (rehypePlugins: typeof CHAT_MARKDOWN_REHYPE_PLUGINS, markdown: string) => {
74 const processor = unified()
75 .use(remarkParse)
76 .use(CHAT_MARKDOWN_REMARK_PLUGINS)
77 .use(remarkRehype, { allowDangerousHtml: true })
78 .use(rehypePlugins);
79 return comparable(processor.runSync(processor.parse(markdown)) as Node);
80 };
81 const [first, , ...rest] = CHAT_MARKDOWN_REHYPE_PLUGINS;
82 const withRaw = [first!, rehypeRaw, ...rest];
83 for (const markdown of [
84 "- [ ] open\n- [x] done\n\nNote[^1]\n\n[^1]: The note.",
85 "| a | b |\n| :- | -: |\n| `c` |  |\n\n```ts title=x\nconst a = 1;\n```",
86 // The HTML parser moves the footnote's link out of the label's link.
87 "[note[^x]](https://example.com)\n\n[^x]: text",
88 "**a** **b**\r\n> - | a |\r\n> | - |\r\n> | b |\r\n\n-\tx\n\n y",
89 ]) {
90 expect(tree(CHAT_MARKDOWN_REHYPE_PLUGINS, markdown)).toEqual(tree(withRaw, markdown));
91 }
92});
93
94it("keeps backslashes that CommonMark would read as escapes in prose Windows paths", () => {
95 const processor = unified()
96 .use(remarkParse)
97 .use(CHAT_MARKDOWN_REMARK_PLUGINS)
98 .use(remarkRehype, { allowDangerousHtml: true })
99 .use(CHAT_MARKDOWN_REHYPE_PLUGINS);
100 type Node = { type: string; value?: string; tagName?: string; children?: Node[] };
101 const text = (node: Node): string =>
102 node.type === "text"
103 ? (node.value ?? "")
104 : node.tagName === "code"
105 ? `<code>${(node.children ?? []).map(text).join("")}</code>`
106 : (node.children ?? []).map(text).join("");
107 const render = (markdown: string) => text(processor.runSync(processor.parse(markdown)) as Node);
108
109 expect(
110 render(String.raw`Saved to C:\Users\me\.t3\_build and (C:\.scratch) and C:/work\.config.`),
111 ).toBe(String.raw`Saved to C:\Users\me\.t3\_build and (C:\.scratch) and C:/work\.config.`);
112 expect(render(String.raw`Escaped C:\\Users\\me`)).toBe(String.raw`Escaped C:\\Users\\me`);
113 expect(render(String.raw`See C:\x and \*this\* and Note:\*that\*.`)).toBe(
114 String.raw`See C:\x and *this* and Note:*that*.`,
115 );
116 expect(render(String.raw`${"`"}C:\me\.t3${"`"} and C:\me\.t3`)).toBe(
117 String.raw`<code>C:\me\.t3</code> and C:\me\.t3`,
118 );
119});
120
121it("keeps an image on its own line under a label but leaves inline images inline", () => {
122 const processor = unified()
123 .use(remarkParse)
124 .use(CHAT_MARKDOWN_REMARK_PLUGINS)
125 .use(remarkRehype, { allowDangerousHtml: true })
126 .use(CHAT_MARKDOWN_REHYPE_PLUGINS);
127 type Node = { type: string; value?: string; tagName?: string; children?: Node[] };
128 const shape = (node: Node): string =>
129 node.type === "text"
130 ? (node.value ?? "")
131 : node.tagName === "br" || node.tagName === "img"
132 ? `<${node.tagName}>`
133 : (node.children ?? []).map(shape).join("");
134 const render = (markdown: string) => shape(processor.runSync(processor.parse(markdown)) as Node);
135
136 expect(
137 render("**Palette**\n\n**Hero** (dark)\n"),
138 ).toBe("Palette<br>\n<img><br>\nHero (dark)<br>\n<img>");
139 expect(render("Built [](/ci)\nand wrapped")).toBe("Built <img>\nand wrapped");
140 expect(render("Hard-wrapped\nprose")).toBe("Hard-wrapped\nprose");
141});