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

main
tsx 104 lines 4.11 KB
Raw
1 import {
2 CHAT_MARKDOWN_REHYPE_PLUGINS,
3 CHAT_MARKDOWN_REMARK_PLUGINS,
4 remarkAssistantInsights,
5 } from "@t3tools/shared/markdownPipeline";
6 import { renderToStaticMarkup } from "react-dom/server";
7 import ReactMarkdown from "react-markdown";
8 import remarkMath from "remark-math";
9 import type { PluggableList } from "unified";
10 import { describe, expect, it } from "vite-plus/test";
11
12 import { splitMarkdownBlocks } from "./markdownStreamBlocks";
13 import { rehypeHeadingIds } from "./markdownFragments";
14
15 const MATH_PLUGINS: PluggableList = [[remarkMath, { singleDollarTextMath: true }]];
16
17 // Whitespace between top-level blocks collapses in the rendered message.
18 const renderMarkdown = (source: string, math = false) =>
19 renderToStaticMarkup(
20 <ReactMarkdown
21 remarkPlugins={[
22 remarkAssistantInsights,
23 ...(math ? MATH_PLUGINS : []),
24 ...CHAT_MARKDOWN_REMARK_PLUGINS,
25 ]}
26 rehypePlugins={[...CHAT_MARKDOWN_REHYPE_PLUGINS, rehypeHeadingIds]}
27 >
28 {source}
29 </ReactMarkdown>,
30 )
31 .replace(/>\s+</g, "><")
32 .trim();
33
34 const html = (source: string) => renderMarkdown(source);
35
36 const punctuatedHeadings = ["Step 1: Install", "Done:", "apps/web", "Fix (web)", "What's next?"];
37
38 describe("splitMarkdownBlocks", () => {
39 it.each([
40 "# Title\n\nFirst paragraph with `code` and **bold**.\n\nSecond [link](https://example.com).",
41 "## Step 1: Install\n\n## Step 1: Install\n\n[second](#step-1-install-1)",
42 "## /home/a/b.ts\n\n## home/a/b.ts",
43 "## see https://x.com/a\n\n## see httpsxcoma",
44 "## Done:\n\n## Done",
45 "## `Setup`\n\n## Setup",
46 "## a ` x ` b\n\n## a x b",
47 "# Setup\n\n# $$ Setup $$\n\n[second](#setup-1)",
48 "# Setup\n\n# Setup\n\n[Second setup](#setup-1)",
49 "\ufeff# Setup\n\n# Setup\n\n[second](#setup-1)",
50 "> Setup\n> ===\n\n> Setup\n> ===",
51 "# onetwo\n\none\ntwo\n===",
52 "# Setup\n\n# &#83;etup\n\n[second](#setup-1)",
53 "# Setup\n\npara\n\n- a\n\n ```\n b\nx\n\nSetup\n===\n",
54 "Setup\n===\n\n> ## **Setup**\n\n- # [Setup](https://example.com)\n\n```sh\n# Setup\n```",
55 "- one\n- two\n\n- loose three\n\n continued\n\nAfter the list.",
56 "1. first\n\n2. second\n\nText\n\n3) restarted",
57 "```ts\nconst a = 1;\n\n\nconst b = 2;\n```\n\nafter\n\n~~~\nx\n\n~~~~\n\ny\n~~~\n\nend",
58 "> quote\n\n> another quote\n\n> [!NOTE]\n> alert",
59 "| a | b |\n| - | - |\n| 1 | 2 |\n\nAfter the table.",
60 "para\n\n indented code\n\nnext",
61 "Setext\n===\n\nnext\n---\n\n- [ ] task\n- [x] done",
62 "- a\n\n ```\n x\n\n```\n\nb",
63 "- a\n\n ```\n x\n\ny\n\n ```\n\nb",
64 "```a``` is inline.\n\nprose\n\n```\none\n\ntwo\n```\n\nafter",
65 "intro\n\n★ Insight ─────\nfirst\n\n```\n─────\n```\n\n- point\n─────\n\nafter\n\n`★ Insight ─────`\nsecond\n`─────`\n\nend",
66 ])("renders each block as the whole text renders it, at every streamed prefix: %j", (text) => {
67 for (let end = 0; end <= text.length; end++) {
68 const source = text.slice(0, end);
69 const blocks = splitMarkdownBlocks(source) ?? [source];
70 expect(blocks.join("")).toBe(source);
71 expect(blocks.map(html).join("")).toBe(html(source));
72 expect(blocks.map((block) => renderMarkdown(block, true)).join("")).toBe(
73 renderMarkdown(source, true),
74 );
75 }
76 });
77
78 it.each(punctuatedHeadings)("keeps splitting headings with prose punctuation: %s", (heading) => {
79 expect(splitMarkdownBlocks(`## ${heading}\n\nNext paragraph.`)).toEqual([
80 `## ${heading}\n\n`,
81 "Next paragraph.",
82 ]);
83 });
84
85 it("splits only between closed blocks", () => {
86 expect(splitMarkdownBlocks("a\n\n```\nb\n\nc\n```\n\n- d\n\n- e\n\nf")).toEqual([
87 "a\n\n",
88 "```\nb\n\nc\n```\n\n- d\n\n- e\n\n",
89 "f",
90 ]);
91 expect(splitMarkdownBlocks("a\n\n---\n\nb")).toEqual(["a\n\n", "---\n\n", "b"]);
92 });
93
94 it.each([
95 "[ref]\n\n[ref]: /later",
96 "a[^x]\n\n[^x]: note",
97 "<details>\n\nbody\n\n</details>",
98 ":::note\n\ncontent\n\n:::",
99 "$$\na\n\nb\n$$",
100 "a\r\n\r\nb",
101 ])("keeps text with document-wide syntax whole: %j", (text) => {
102 expect(splitMarkdownBlocks(text)).toBeNull();
103 });
104 });