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

markdownRenderer.test.tsx

85 lines3.5 KB

1import {
2 CHAT_MARKDOWN_REHYPE_PLUGINS,
3 CHAT_MARKDOWN_REMARK_PLUGINS,
4} from "@t3tools/shared/markdownPipeline";
5import { renderToStaticMarkup } from "react-dom/server";
6import ReactMarkdown from "react-markdown";
7import { describe, expect, it } from "vite-plus/test";
8​
9import { createIncrementalMarkdownPlugin } from "../../markdown-incremental";
10import { createMarkdownRenderer } from "./markdownRenderer";
11​
12// Visible in the markup, so every URL attribute must pass through it.
13const urlTransform = (url: string) => `${url}#seen`;
14​
15describe("createMarkdownRenderer", () => {
16 const render = createMarkdownRenderer({
17 remarkPlugins: CHAT_MARKDOWN_REMARK_PLUGINS,
18 rehypePlugins: CHAT_MARKDOWN_REHYPE_PLUGINS,
19 components: {},
20 urlTransform,
21 });
22​
23 it.each([
24 '# Title\n\nText with `code`, **bold**, [a link](https://example.com "t") and https://bare.example.com.',
25 "![shot](./shot.png) and [file](file:///C:/repo/a.ts) and [bad](javascript:alert(1))",
26 "<details><summary>More</summary>\n\nHidden <b>bold</b> <script>x()</script>\n\n</details>",
27 "| a | b |\n| - | - |\n| <br> | [x](#frag) |\n\n- [ ] task\n- [x] done",
28 '```ts title="a.ts"\nconst a = 1;\n```\n\n> [!WARNING]\n> careful <a>bare</a>',
29 "Footnote[^1] and ~~strike~~\n\n[^1]: the note",
30 // Each GFM construct the parser skips when its text cannot match, next to one that can.
31 "Mail me@example.com, see www.example.com and HTTPS://example.com/a, not [x](https://y.z).",
32 "a.b-c_d+e@x.io and @user\n\n`me@x.io` <me@x.io>",
33 "A table without pipes\n:--",
34 "> | quoted |\n> | - |\n> | body |\n\n- [ ] open\n- [X] done",
35 "Escaped www\\.example.com and me&#64;example.com",
36 "\ufeffA byte order mark\n\n| a |\n| - |\n| b |",
37 "> - | a |\r\n> | - |\r\n> | me@x.io |\r\n\n-\t[ ] tab\n\n ~~lazy~~",
38 ])("renders what react-markdown renders: %j", (source) => {
39 const expected = renderToStaticMarkup(
40 <ReactMarkdown
41 remarkPlugins={CHAT_MARKDOWN_REMARK_PLUGINS}
42 rehypePlugins={CHAT_MARKDOWN_REHYPE_PLUGINS}
43 urlTransform={urlTransform}
44 >
45 {source}
46 </ReactMarkdown>,
47 );
48 const element = render(source, true);
49 expect(renderToStaticMarkup(element)).toBe(expected);
50 expect(render(source, true)).toBe(element);
51 });
52​
53 it("renders what react-markdown renders while a reply streams through the incremental parser", () => {
54 const reply =
55 "```ts\nconst a = 1;\n```\n\n| a | b |\n| - | - |\n| me@x.io | www.x.io |\n\nDone ~~now~~.";
56 // As ChatMarkdown streams: one incremental plugin for the reply, a fresh renderer per update.
57 const incremental = createIncrementalMarkdownPlugin();
58 for (let end = 1; end <= reply.length; end++) {
59 const source = reply.slice(0, end);
60 const streamed = createMarkdownRenderer({
61 remarkPlugins: [...CHAT_MARKDOWN_REMARK_PLUGINS, incremental],
62 rehypePlugins: CHAT_MARKDOWN_REHYPE_PLUGINS,
63 components: {},
64 urlTransform,
65 })(source, false);
66 expect(renderToStaticMarkup(streamed)).toBe(
67 renderToStaticMarkup(
68 <ReactMarkdown
69 remarkPlugins={CHAT_MARKDOWN_REMARK_PLUGINS}
70 rehypePlugins={CHAT_MARKDOWN_REHYPE_PLUGINS}
71 urlTransform={urlTransform}
72 >
73 {source}
74 </ReactMarkdown>,
75 ),
76 );
77 }
78 });
79​
80 it("keeps cached messages when one far larger than the cache is rendered", () => {
81 const element = render("Kept *message*", true);
82 render("x".repeat(600_000), true);
83 expect(render("Kept *message*", true)).toBe(element);
84 });
85});