@@ -1,12 +1,6 @@
11 import { describe, test, expect } from "bun:test";
22 import type { ContainerNode, Node, TextNode } from "@cel-tui/types";
3-import {
4- parseInline,
5- tokenize,
6- Markdown,
7- type BlockToken,
8- type InlineSpan,
9-} from "./markdown";
3+import { Markdown } from "./markdown";
104
115 // ─── Helpers ────────────────────────────────────────────────────
126
@@ -28,299 +22,12 @@ function asContainer(node: Node): ContainerNode {
2822 return node as ContainerNode;
2923 }
3024
31-// ─── parseInline ────────────────────────────────────────────────
32-
33-describe("parseInline", () => {
34- test("plain text", () => {
35- expect(parseInline("hello world")).toEqual([
36- { type: "text", content: "hello world" },
37- ]);
38- });
39-
40- test("bold", () => {
41- expect(parseInline("some **bold** text")).toEqual([
42- { type: "text", content: "some " },
43- { type: "bold", content: "bold" },
44- { type: "text", content: " text" },
45- ]);
46- });
47-
48- test("italic", () => {
49- expect(parseInline("some *italic* text")).toEqual([
50- { type: "text", content: "some " },
51- { type: "italic", content: "italic" },
52- { type: "text", content: " text" },
53- ]);
54- });
55-
56- test("inline code", () => {
57- expect(parseInline("use `foo()` here")).toEqual([
58- { type: "text", content: "use " },
59- { type: "code", content: "foo()" },
60- { type: "text", content: " here" },
61- ]);
62- });
63-
64- test("link", () => {
65- expect(parseInline("see [docs](https://example.com) now")).toEqual([
66- { type: "text", content: "see " },
67- { type: "link", text: "docs", url: "https://example.com" },
68- { type: "text", content: " now" },
69- ]);
70- });
71-
72- test("mixed inline", () => {
73- const spans = parseInline("**bold** and *italic* and `code`");
74- expect(spans).toEqual([
75- { type: "bold", content: "bold" },
76- { type: "text", content: " and " },
77- { type: "italic", content: "italic" },
78- { type: "text", content: " and " },
79- { type: "code", content: "code" },
80- ]);
81- });
82-
83- test("unclosed bold is plain text", () => {
84- expect(parseInline("some **unclosed")).toEqual([
85- { type: "text", content: "some **unclosed" },
86- ]);
87- });
88-
89- test("unclosed italic is plain text", () => {
90- expect(parseInline("some *unclosed")).toEqual([
91- { type: "text", content: "some *unclosed" },
92- ]);
93- });
94-
95- test("unclosed code is plain text", () => {
96- expect(parseInline("some `unclosed")).toEqual([
97- { type: "text", content: "some `unclosed" },
98- ]);
99- });
100-
101- test("unclosed link is plain text", () => {
102- expect(parseInline("see [docs](no-close")).toEqual([
103- { type: "text", content: "see [docs](no-close" },
104- ]);
105- });
106-
107- test("empty string", () => {
108- expect(parseInline("")).toEqual([]);
109- });
110-
111- test("code has priority over bold", () => {
112- // The ` opens before ** can match
113- expect(parseInline("`**not bold**`")).toEqual([
114- { type: "code", content: "**not bold**" },
115- ]);
116- });
117-});
118-
119-// ─── tokenize ───────────────────────────────────────────────────
120-
121-describe("tokenize", () => {
122- test("heading levels", () => {
123- const tokens = tokenize("# H1\n## H2\n### H3");
124- expect(tokens).toEqual([
125- { type: "heading", depth: 1, spans: [{ type: "text", content: "H1" }] },
126- { type: "heading", depth: 2, spans: [{ type: "text", content: "H2" }] },
127- { type: "heading", depth: 3, spans: [{ type: "text", content: "H3" }] },
128- ]);
129- });
130-
131- test("heading with inline formatting", () => {
132- const tokens = tokenize("# Hello **world**");
133- expect(tokens[0]).toEqual({
134- type: "heading",
135- depth: 1,
136- spans: [
137- { type: "text", content: "Hello " },
138- { type: "bold", content: "world" },
139- ],
140- });
141- });
142-
143- test("paragraph", () => {
144- const tokens = tokenize("Hello world");
145- expect(tokens).toEqual([
146- { type: "paragraph", spans: [{ type: "text", content: "Hello world" }] },
147- ]);
148- });
149-
150- test("code block with lang", () => {
151- const tokens = tokenize("```js\nconst x = 1;\nconst y = 2;\n```");
152- expect(tokens).toEqual([
153- {
154- type: "code_block",
155- lang: "js",
156- content: "const x = 1;\nconst y = 2;",
157- },
158- ]);
159- });
160-
161- test("code block without lang", () => {
162- const tokens = tokenize("```\nsome code\n```");
163- expect(tokens).toEqual([
164- { type: "code_block", lang: "", content: "some code" },
165- ]);
166- });
167-
168- test("unclosed code block (streaming)", () => {
169- const tokens = tokenize("```js\nconst x = 1;");
170- expect(tokens).toEqual([
171- { type: "code_block", lang: "js", content: "const x = 1;" },
172- ]);
173- });
174-
175- test("unordered list", () => {
176- const tokens = tokenize("- first\n- second\n* third");
177- expect(tokens).toEqual([
178- {
179- type: "list_item",
180- ordered: false,
181- index: 0,
182- spans: [{ type: "text", content: "first" }],
183- },
184- {
185- type: "list_item",
186- ordered: false,
187- index: 0,
188- spans: [{ type: "text", content: "second" }],
189- },
190- {
191- type: "list_item",
192- ordered: false,
193- index: 0,
194- spans: [{ type: "text", content: "third" }],
195- },
196- ]);
197- });
198-
199- test("ordered list", () => {
200- const tokens = tokenize("1. first\n2. second");
201- expect(tokens).toEqual([
202- {
203- type: "list_item",
204- ordered: true,
205- index: 1,
206- spans: [{ type: "text", content: "first" }],
207- },
208- {
209- type: "list_item",
210- ordered: true,
211- index: 2,
212- spans: [{ type: "text", content: "second" }],
213- },
214- ]);
215- });
216-
217- test("blockquote", () => {
218- const tokens = tokenize("> some quote");
219- expect(tokens).toEqual([
220- {
221- type: "blockquote",
222- spans: [{ type: "text", content: "some quote" }],
223- },
224- ]);
225- });
226-
227- test("blockquote with inline formatting", () => {
228- const tokens = tokenize("> a **bold** quote");
229- expect(tokens[0]).toEqual({
230- type: "blockquote",
231- spans: [
232- { type: "text", content: "a " },
233- { type: "bold", content: "bold" },
234- { type: "text", content: " quote" },
235- ],
236- });
237- });
238-
239- test("horizontal rules", () => {
240- expect(tokenize("---")).toEqual([{ type: "hr" }]);
241- expect(tokenize("***")).toEqual([{ type: "hr" }]);
242- expect(tokenize("___")).toEqual([{ type: "hr" }]);
243- expect(tokenize("----")).toEqual([{ type: "hr" }]);
244- });
245-
246- test("blank lines", () => {
247- const tokens = tokenize("hello\n\nworld");
248- expect(tokens).toEqual([
249- { type: "paragraph", spans: [{ type: "text", content: "hello" }] },
250- { type: "blank" },
251- { type: "paragraph", spans: [{ type: "text", content: "world" }] },
252- ]);
253- });
254-
255- test("full document", () => {
256- const md = [
257- "# Title",
258- "",
259- "A paragraph with **bold**.",
260- "",
261- "```ts",
262- "const x = 1;",
263- "```",
264- "",
265- "- item one",
266- "- item two",
267- "",
268- "> a quote",
269- "",
270- "---",
271- ].join("\n");
272-
273- const tokens = tokenize(md);
274- const types = tokens.map((t) => t.type);
275- expect(types).toEqual([
276- "heading",
277- "blank",
278- "paragraph",
279- "blank",
280- "code_block",
281- "blank",
282- "list_item",
283- "list_item",
284- "blank",
285- "blockquote",
286- "blank",
287- "hr",
288- ]);
289- });
290-
291- test("empty content", () => {
292- expect(tokenize("")).toEqual([{ type: "blank" }]);
293- });
294-
295- test("code block preserves content verbatim", () => {
296- const tokens = tokenize("```\n indented\n**not bold**\n```");
297- const cb = tokens[0] as { type: "code_block"; content: string };
298- expect(cb.content).toBe(" indented\n**not bold**");
299- });
300-
301- test("list item with inline formatting", () => {
302- const tokens = tokenize("- some **bold** item");
303- expect(tokens[0]).toEqual({
304- type: "list_item",
305- ordered: false,
306- index: 0,
307- spans: [
308- { type: "text", content: "some " },
309- { type: "bold", content: "bold" },
310- { type: "text", content: " item" },
311- ],
312- });
313- });
314-});
315-
31625 // ─── Markdown component ─────────────────────────────────────────
31726
31827 describe("Markdown", () => {
31928 test("returns empty array for empty string", () => {
320- // Empty string tokenizes to one blank → one Text("")
32129 const nodes = Markdown("");
322- expect(nodes).toHaveLength(1);
323- expect(textContent(nodes[0]!)).toBe("");
30+ expect(nodes).toHaveLength(0);
32431 });
32532
32633 test("heading renders as styled Text", () => {