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