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

markdownPipeline.test.ts

57 lines2.3 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/environment/thread)\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/environment/thread" },
24 children: [{ type: "text", value: "Open thread" }],
25 },
26 ],
27 });
28 expect(JSON.stringify(tree)).not.toContain("javascript:");
29});
30​
31it("keeps backslashes that CommonMark would read as escapes in prose Windows paths", () => {
32 const processor = unified()
33 .use(remarkParse)
34 .use(CHAT_MARKDOWN_REMARK_PLUGINS)
35 .use(remarkRehype, { allowDangerousHtml: true })
36 .use(CHAT_MARKDOWN_REHYPE_PLUGINS);
37 type Node = { type: string; value?: string; tagName?: string; children?: Node[] };
38 const text = (node: Node): string =>
39 node.type === "text"
40 ? (node.value ?? "")
41 : node.tagName === "code"
42 ? `<code>${(node.children ?? []).map(text).join("")}</code>`
43 : (node.children ?? []).map(text).join("");
44 const render = (markdown: string) => text(processor.runSync(processor.parse(markdown)) as Node);
45​
46 expect(
47 render(String.raw`Saved to C:\Users\me\.t3\_build and (C:\.scratch) and C:/work\.config.`),
48 ).toBe(String.raw`Saved to C:\Users\me\.t3\_build and (C:\.scratch) and C:/work\.config.`);
49 // Paths render as written, so one an agent already escaped keeps its doubled backslashes.
50 expect(render(String.raw`Escaped C:\\Users\\me`)).toBe(String.raw`Escaped C:\\Users\\me`);
51 expect(render(String.raw`See C:\x and \*this\* and Note:\*that\*.`)).toBe(
52 String.raw`See C:\x and *this* and Note:*that*.`,
53 );
54 expect(render(String.raw`${"`"}C:\me\.t3${"`"} and C:\me\.t3`)).toBe(
55 String.raw`<code>C:\me\.t3</code> and C:\me\.t3`,
56 );
57});