xonecas.io
70160
Ce

cel-tui / commit 618990c

feat(components): render Markdown inline styling via flexWrap

The Markdown component now renders **bold**, *italic*, `code`, and
[links](url) with proper styling in paragraphs, list items, and
blockquotes. Previously all inline formatting was stripped to plain
text.

Implementation: inline spans are split at word/whitespace boundaries
into individual Text nodes inside HStack({ flexWrap: "wrap" }).
Code and link spans are kept atomic (single node, no split).
Plain-text blocks without formatting stay as Text with wrap: "word"
for efficiency.

Theme additions: bold, italic, inlineCode, link — all customizable
via MarkdownTheme. Defaults: bold text, italic text, yellow code,
cyan underlined links.

Headings still strip inline formatting (short, single-line, low
priority). Updated TODO.md accordingly.

8 new tests covering: bold/italic/code/link paragraphs, mixed
formatting, list items with formatting, blockquotes with formatting,
and theme customization for inline code.

xonecas2026-04-04 618990cc2741b4cdcbc8137836a65935d889d774 parent edf13b5

3 files changed

TODO.md+1−1
@@ -30,7 +30,7 @@ Remaining work, known bugs, and planned improvements.
3030
3131 - ❌ Bracketed paste mode support
3232
33-- ❌ **Markdown inline styling** — The `Markdown` component strips inline formatting (`**bold**`, `*italic*`, `` `code` ``, `[links](url)`) to plain text. With `flexWrap: "wrap"` on HStack (now implemented), the component can split inline spans at word boundaries into individual `Text` nodes and wrap them in `HStack({ flexWrap: "wrap" })`. The framework handles line wrapping; the component handles word splitting.
33+- ❌ **Markdown heading inline styling** — Headings (`#`, `##`, `###`) still strip inline formatting to plain text. Since headings are short and single-line, this is low priority. Paragraphs, list items, and blockquotes now render inline formatting via wrapping HStack.
3434
3535 ## Future Enhancements
3636
packages/components/src/markdown.test.ts+114−6
@@ -49,15 +49,75 @@ describe("Markdown", () => {
4949 expect(h3.props.fgColor).toBe("green");
5050 });
5151
52- test("paragraph renders as wrapping Text", () => {
53- const nodes = Markdown("Hello **bold** world");
52+ test("plain paragraph renders as wrapping Text", () => {
53+ const nodes = Markdown("Hello world");
5454 expect(nodes).toHaveLength(1);
5555 const t = asText(nodes[0]!);
56- // Inline markers stripped, plain text content
57- expect(t.content).toBe("Hello bold world");
56+ expect(t.content).toBe("Hello world");
5857 expect(t.props.wrap).toBe("word");
5958 });
6059
60+ test("paragraph with bold renders as wrapping HStack", () => {
61+ const nodes = Markdown("Hello **bold** world");
62+ expect(nodes).toHaveLength(1);
63+ const hstack = asContainer(nodes[0]!);
64+ expect(hstack.type).toBe("hstack");
65+ expect(hstack.props.flexWrap).toBe("wrap");
66+ // Children: "Hello", " ", bold("bold"), " ", "world"
67+ expect(hstack.children).toHaveLength(5);
68+ expect(asText(hstack.children[0]!).content).toBe("Hello");
69+ expect(asText(hstack.children[1]!).content).toBe(" ");
70+ const boldNode = asText(hstack.children[2]!);
71+ expect(boldNode.content).toBe("bold");
72+ expect(boldNode.props.bold).toBe(true);
73+ expect(asText(hstack.children[3]!).content).toBe(" ");
74+ expect(asText(hstack.children[4]!).content).toBe("world");
75+ });
76+
77+ test("paragraph with italic renders styled nodes", () => {
78+ const nodes = Markdown("some *italic* text");
79+ const hstack = asContainer(nodes[0]!);
80+ expect(hstack.type).toBe("hstack");
81+ // "some", " ", italic("italic"), " ", "text"
82+ const italicNode = asText(hstack.children[2]!);
83+ expect(italicNode.content).toBe("italic");
84+ expect(italicNode.props.italic).toBe(true);
85+ });
86+
87+ test("paragraph with inline code keeps code span atomic", () => {
88+ const nodes = Markdown("use `foo bar` here");
89+ const hstack = asContainer(nodes[0]!);
90+ expect(hstack.type).toBe("hstack");
91+ // "use", " ", code("foo bar"), " ", "here"
92+ expect(hstack.children).toHaveLength(5);
93+ const codeNode = asText(hstack.children[2]!);
94+ expect(codeNode.content).toBe("foo bar");
95+ expect(codeNode.props.fgColor).toBe("yellow");
96+ });
97+
98+ test("paragraph with link keeps link text atomic", () => {
99+ const nodes = Markdown("see [the docs](https://example.com) here");
100+ const hstack = asContainer(nodes[0]!);
101+ // "see", " ", link("the docs"), " ", "here"
102+ expect(hstack.children).toHaveLength(5);
103+ const linkNode = asText(hstack.children[2]!);
104+ expect(linkNode.content).toBe("the docs");
105+ expect(linkNode.props.fgColor).toBe("cyan");
106+ expect(linkNode.props.underline).toBe(true);
107+ });
108+
109+ test("paragraph with mixed formatting", () => {
110+ const nodes = Markdown("Hello, **bold**!");
111+ const hstack = asContainer(nodes[0]!);
112+ // "Hello,", " ", bold("bold"), "!"
113+ expect(hstack.children).toHaveLength(4);
114+ expect(asText(hstack.children[0]!).content).toBe("Hello,");
115+ expect(asText(hstack.children[1]!).content).toBe(" ");
116+ expect(asText(hstack.children[2]!).props.bold).toBe(true);
117+ // No space before "!" — punctuation is adjacent
118+ expect(asText(hstack.children[3]!).content).toBe("!");
119+ });
120+
61121 test("code block renders as VStack with Text", () => {
62122 const nodes = Markdown("```js\nconst x = 1;\n```");
63123 expect(nodes).toHaveLength(1);
@@ -159,8 +219,16 @@ describe("Markdown", () => {
159219
160220 test("link text is used, url is stripped", () => {
161221 const nodes = Markdown("See [the docs](https://example.com) here.");
162- const t = asText(nodes[0]!);
163- expect(t.content).toBe("See the docs here.");
222+ const hstack = asContainer(nodes[0]!);
223+ expect(hstack.type).toBe("hstack");
224+ expect(hstack.props.flexWrap).toBe("wrap");
225+ // "See", " ", link("the docs"), " ", "here."
226+ expect(hstack.children).toHaveLength(5);
227+ expect(asText(hstack.children[0]!).content).toBe("See");
228+ const link = asText(hstack.children[2]!);
229+ expect(link.content).toBe("the docs");
230+ expect(link.props.fgColor).toBe("cyan");
231+ expect(asText(hstack.children[4]!).content).toBe("here.");
164232 });
165233
166234 test("streaming: unclosed code block renders as code", () => {
@@ -172,9 +240,49 @@ describe("Markdown", () => {
172240 });
173241
174242 test("streaming: unclosed bold appears as plain text", () => {
243+ // Unclosed ** is not parsed as bold — stays as text span
175244 const nodes = Markdown("hello **world");
176245 const t = asText(nodes[0]!);
177246 expect(t.content).toBe("hello **world");
247+ expect(t.props.wrap).toBe("word");
248+ });
249+
250+ test("list item with inline formatting", () => {
251+ const nodes = Markdown("- hello **bold** world");
252+ const row = asContainer(nodes[0]!);
253+ expect(row.type).toBe("hstack");
254+ const wrapper = asContainer(row.children[1]!);
255+ expect(wrapper.type).toBe("vstack");
256+ // Content is a wrapping HStack
257+ const content = asContainer(wrapper.children[0]!);
258+ expect(content.type).toBe("hstack");
259+ expect(content.props.flexWrap).toBe("wrap");
260+ // "hello", " ", bold("bold"), " ", "world"
261+ expect(content.children).toHaveLength(5);
262+ expect(asText(content.children[2]!).props.bold).toBe(true);
263+ });
264+
265+ test("blockquote with inline formatting", () => {
266+ const nodes = Markdown("> hello **bold** text");
267+ const row = asContainer(nodes[0]!);
268+ const wrapper = asContainer(row.children[1]!);
269+ const content = asContainer(wrapper.children[0]!);
270+ expect(content.type).toBe("hstack");
271+ expect(content.props.flexWrap).toBe("wrap");
272+ // blockquoteText style (italic) applied to the wrapping HStack
273+ expect(content.props.italic).toBe(true);
274+ // bold node inside inherits italic from parent, also has bold
275+ expect(asText(content.children[2]!).props.bold).toBe(true);
276+ });
277+
278+ test("inline code theme is customizable", () => {
279+ const nodes = Markdown("use `code` here", {
280+ theme: { inlineCode: { fgColor: "red", bgColor: "black" } },
281+ });
282+ const hstack = asContainer(nodes[0]!);
283+ const codeNode = asText(hstack.children[2]!);
284+ expect(codeNode.props.fgColor).toBe("red");
285+ expect(codeNode.props.bgColor).toBe("black");
178286 });
179287
180288 test("full document produces correct node sequence", () => {
packages/components/src/markdown.ts+100−14
@@ -35,6 +35,14 @@ export interface MarkdownTheme {
3535 blockquoteText?: StyleProps;
3636 /** Divider color and character for horizontal rules. */
3737 hr?: { fgColor?: Color; char?: string };
38+ /** Style for **bold** inline text. */
39+ bold?: StyleProps;
40+ /** Style for *italic* inline text. */
41+ italic?: StyleProps;
42+ /** Style for `inline code` text. */
43+ inlineCode?: StyleProps;
44+ /** Style for [link](url) text. */
45+ link?: StyleProps;
3846 }
3947
4048 const defaults: Required<MarkdownTheme> = {
@@ -47,6 +55,10 @@ const defaults: Required<MarkdownTheme> = {
4755 blockquoteBar: { fgColor: "green" },
4856 blockquoteText: { italic: true },
4957 hr: { fgColor: "brightBlack", char: "─" },
58+ bold: { bold: true },
59+ italic: { italic: true },
60+ inlineCode: { fgColor: "yellow" },
61+ link: { fgColor: "cyan", underline: true },
5062 };
5163
5264 function resolveTheme(theme?: MarkdownTheme): Required<MarkdownTheme> {
@@ -61,6 +73,10 @@ function resolveTheme(theme?: MarkdownTheme): Required<MarkdownTheme> {
6173 blockquoteBar: theme.blockquoteBar ?? defaults.blockquoteBar,
6274 blockquoteText: theme.blockquoteText ?? defaults.blockquoteText,
6375 hr: theme.hr ?? defaults.hr,
76+ bold: theme.bold ?? defaults.bold,
77+ italic: theme.italic ?? defaults.italic,
78+ inlineCode: theme.inlineCode ?? defaults.inlineCode,
79+ link: theme.link ?? defaults.link,
6480 };
6581 }
6682
@@ -71,6 +87,62 @@ function spansToText(spans: InlineSpan[]): string {
7187 return spans.map((s) => (s.type === "link" ? s.text : s.content)).join("");
7288 }
7389
90+/** Check whether spans contain any inline formatting. */
91+function hasFormatting(spans: InlineSpan[]): boolean {
92+ return spans.some((s) => s.type !== "text");
93+}
94+
95+/**
96+ * Convert inline spans into individual Text nodes for use inside a
97+ * wrapping HStack. Text/bold/italic spans are split at word/whitespace
98+ * boundaries (`/\S+|\s+/g`) so flexWrap can break between words.
99+ * Code and link spans are kept atomic (single node, no split).
100+ */
101+function spansToNodes(
102+ spans: InlineSpan[],
103+ theme: Required<MarkdownTheme>,
104+): Node[] {
105+ const nodes: Node[] = [];
106+
107+ for (const span of spans) {
108+ let style: StyleProps;
109+ let text: string;
110+
111+ switch (span.type) {
112+ case "text":
113+ style = {};
114+ text = span.content;
115+ break;
116+ case "bold":
117+ style = theme.bold;
118+ text = span.content;
119+ break;
120+ case "italic":
121+ style = theme.italic;
122+ text = span.content;
123+ break;
124+ case "code":
125+ // Code spans are atomic — don't split at word boundaries
126+ nodes.push(Text(span.content, theme.inlineCode));
127+ continue;
128+ case "link":
129+ // Links are atomic — keep link text as one unit
130+ nodes.push(Text(span.text, theme.link));
131+ continue;
132+ }
133+
134+ // Split at word/whitespace boundaries so flexWrap can break between words
135+ const pieces = text.match(/\S+|\s+/g);
136+ if (pieces) {
137+ for (const piece of pieces) {
138+ nodes.push(Text(piece, style));
139+ }
140+ }
141+ }
142+
143+ return nodes;
144+}
145+
74146 // ─── Component ──────────────────────────────────────────────────
75147
76148 /** Props for the {@link Markdown} component. */
@@ -92,9 +164,11 @@ export interface MarkdownProps {
92164 *
93165 * **Block-level styling** is fully supported: headings, code blocks,
94166 * lists, blockquotes, and horizontal rules are rendered with distinct
95- * styles. **Inline styling** (bold, italic, code, links within
96- * paragraphs) is not yet rendered — inline-formatted text appears
97- * as clean plain text with markdown markers stripped.
167+ * styles. **Inline styling** (bold, italic, code, links) is rendered
168+ * in paragraphs, list items, and blockquotes by splitting spans at
169+ * word boundaries into individual `Text` nodes inside a wrapping
170+ * `HStack({ flexWrap: "wrap" })`. Headings strip inline formatting
171+ * to plain text (they're typically short and single-line).
98172 *
99173 * **Streaming** works naturally: append chunks to the content string
100174 * and call `cel.render()`. The component re-tokenizes the full string
@@ -145,7 +219,13 @@ export function Markdown(content: string, props?: MarkdownProps): Node[] {
145219 }
146220
147221 case "paragraph":
148- nodes.push(Text(spansToText(token.spans), { wrap: "word" }));
222+ if (hasFormatting(token.spans)) {
223+ nodes.push(
224+ HStack({ flexWrap: "wrap" }, spansToNodes(token.spans, theme)),
225+ );
226+ } else {
227+ nodes.push(Text(spansToText(token.spans), { wrap: "word" }));
228+ }
149229 break;
150230
151231 case "code_block":
@@ -158,30 +238,36 @@ export function Markdown(content: string, props?: MarkdownProps): Node[] {
158238
159239 case "list_item": {
160240 const marker = token.ordered ? `${token.index}.` : "•";
241+ const liContent = hasFormatting(token.spans)
242+ ? HStack({ flexWrap: "wrap" }, spansToNodes(token.spans, theme))
243+ : Text(spansToText(token.spans), { wrap: "word" });
161244 nodes.push(
162245 HStack({}, [
163246 Text(`${marker} `, theme.listMarker),
164- VStack({ flex: 1 }, [
165- Text(spansToText(token.spans), { wrap: "word" }),
166- ]),
247+ VStack({ flex: 1 }, [liContent]),
167248 ]),
168249 );
169250 break;
170251 }
171252
172- case "blockquote":
253+ case "blockquote": {
254+ const bqContent = hasFormatting(token.spans)
255+ ? HStack(
256+ { flexWrap: "wrap", ...theme.blockquoteText },
257+ spansToNodes(token.spans, theme),
258+ )
259+ : Text(spansToText(token.spans), {
260+ wrap: "word",
261+ ...theme.blockquoteText,
262+ });
173263 nodes.push(
174264 HStack({}, [
175265 Text("│ ", theme.blockquoteBar),
176- VStack({ flex: 1 }, [
177- Text(spansToText(token.spans), {
178- wrap: "word",
179- ...theme.blockquoteText,
180- }),
181- ]),
266+ VStack({ flex: 1 }, [bqContent]),
182267 ]),
183268 );
184269 break;
270+ }
185271
186272 case "hr":
187273 nodes.push(Divider({ fgColor: theme.hr.fgColor, char: theme.hr.char }));