@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 88 lines 3.63 KB
Raw
1 import {
2 CHAT_MARKDOWN_REHYPE_PLUGINS,
3 CHAT_MARKDOWN_REMARK_PLUGINS,
4 } from "@t3tools/shared/markdownPipeline";
5 import { renderToStaticMarkup } from "react-dom/server";
6 import ReactMarkdown from "react-markdown";
7 import { describe, expect, it } from "vite-plus/test";
8
9 import { createIncrementalMarkdownPlugin } from "../../markdown-incremental";
10 import { createMarkdownRenderer } from "./markdownRenderer";
11
12 // Visible in the markup, so every URL attribute must pass through it.
13 const urlTransform = (url: string) => `${url}#seen`;
14
15 describe("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);
49 expect(renderToStaticMarkup(element)).toBe(expected);
50 render.remember(source, element);
51 expect(render(source)).toBe(element);
52 });
53
54 it("renders what react-markdown renders while a reply streams through the incremental parser", () => {
55 const reply =
56 "```ts\nconst a = 1;\n```\n\n| a | b |\n| - | - |\n| me@x.io | www.x.io |\n\nDone ~~now~~.";
57 // As ChatMarkdown streams: one incremental plugin for the reply, a fresh renderer per update.
58 const incremental = createIncrementalMarkdownPlugin();
59 for (let end = 1; end <= reply.length; end++) {
60 const source = reply.slice(0, end);
61 const streamed = createMarkdownRenderer({
62 remarkPlugins: [...CHAT_MARKDOWN_REMARK_PLUGINS, incremental],
63 rehypePlugins: CHAT_MARKDOWN_REHYPE_PLUGINS,
64 components: {},
65 urlTransform,
66 })(source);
67 expect(renderToStaticMarkup(streamed)).toBe(
68 renderToStaticMarkup(
69 <ReactMarkdown
70 remarkPlugins={CHAT_MARKDOWN_REMARK_PLUGINS}
71 rehypePlugins={CHAT_MARKDOWN_REHYPE_PLUGINS}
72 urlTransform={urlTransform}
73 >
74 {source}
75 </ReactMarkdown>,
76 ),
77 );
78 }
79 });
80
81 it("keeps cached messages when one far larger than the cache is rendered", () => {
82 const element = render("Kept *message*");
83 render.remember("Kept *message*", element);
84 const large = "x".repeat(600_000);
85 render.remember(large, render(large));
86 expect(render("Kept *message*")).toBe(element);
87 });
88 });