xonecas.io
64875
Mc

mini-coder / commit a7effd0

fix(markdown): correct italic rendering adjacent to bold spans

Two bugs in renderInline where italic spans were silently dropped:

- **bold***italic*: the i-1 guard (text[i-1] !== '*') blocked italic
  from opening immediately after a closing **, because the source char
  behind the opener was the closing star of the bold span. Fixed with a
  prevWasBoldClose flag that exempts this specific case.

- *italic***bold**: the closing guard (text[end+1] !== '*') blocked the
  italic closer when a bold span opened immediately after, because
  text[end+1] was the first star of **. Fixed by switching to
  text[end-1] !== '*', which correctly checks the closer isn't the tail
  of a ** sequence without rejecting legitimate adjacent-span closes.

Also guards the bold branch against *** openers (text[i+2] !== '*') so
triple-star sequences don't get partially consumed as bold, leaving
stray * characters in the output.

Adds src/cli/markdown.test.ts with 48 tests covering all block and
inline elements, edge cases, and the fixed adjacency combinations.

xonecas2026-02-20 a7effd0b6dd833e810ef8e35aa30ef4ae41d124a parent 98579ba

2 files changed

src/cli/markdown.test.ts+346−0
@@ -0,0 +1,346 @@
1+import { describe, expect, test } from "bun:test";
2+import { renderMarkdown } from "./markdown.ts";
3+
4+// ---------------------------------------------------------------------------
5+// Helpers
6+// ---------------------------------------------------------------------------
7+
8+/** Strip all ANSI escape sequences, leaving only the visible text. */
9+function strip(s: string): string {
10+ // Use a dynamic RegExp so the ESC literal doesn't trigger the lint rule
11+ // for control characters in regex literals.
12+ const esc = String.fromCharCode(0x1b);
13+ return s.replace(new RegExp(`${esc}\\[[0-9;]*m`, "g"), "");
14+}
15+
16+/** Return true if the string contains the given ANSI SGR sequence (e.g. "[1m"). */
17+function hasAnsi(s: string, code: string): boolean {
18+ return s.includes(`${String.fromCharCode(0x1b)}${code}`);
19+}
20+
21+/** Return true if the string contains ANSI bold codes. */
22+function hasBold(s: string): boolean {
23+ return hasAnsi(s, "[1m");
24+}
25+
26+/** Return true if the string contains ANSI dim codes. */
27+function hasDim(s: string): boolean {
28+ return hasAnsi(s, "[2m");
29+}
30+
31+/** Return true if the string contains ANSI cyan codes. */
32+function hasCyan(s: string): boolean {
33+ return hasAnsi(s, "[36m");
34+}
35+
36+/** Return true if the string contains ANSI yellow codes. */
37+function hasYellow(s: string): boolean {
38+ return hasAnsi(s, "[33m");
39+}
40+
41+// ---------------------------------------------------------------------------
42+// renderMarkdown — block elements
43+// ---------------------------------------------------------------------------
44+
45+describe("renderMarkdown – headings", () => {
46+ test("h1 renders the heading text without the # prefix", () => {
47+ const out = renderMarkdown("# Hello World");
48+ expect(strip(out)).toBe("Hello World");
49+ });
50+
51+ test("h1 is bold and cyan", () => {
52+ const out = renderMarkdown("# Hello");
53+ expect(hasBold(out)).toBe(true);
54+ expect(hasCyan(out)).toBe(true);
55+ });
56+
57+ test("h2 renders the heading text without the ## prefix", () => {
58+ const out = renderMarkdown("## Section");
59+ expect(strip(out)).toBe("Section");
60+ });
61+
62+ test("h2 is bold and cyan", () => {
63+ const out = renderMarkdown("## Section");
64+ expect(hasBold(out)).toBe(true);
65+ expect(hasCyan(out)).toBe(true);
66+ });
67+
68+ test("h3 renders the heading text without the ### prefix", () => {
69+ const out = renderMarkdown("### Sub");
70+ expect(strip(out)).toBe("Sub");
71+ });
72+
73+ test("h3 is bold but NOT cyan (only bold)", () => {
74+ const out = renderMarkdown("### Sub");
75+ expect(hasBold(out)).toBe(true);
76+ expect(hasCyan(out)).toBe(false);
77+ });
78+
79+ test("h4+ is treated the same as h3 (#{3,} pattern)", () => {
80+ const h4 = strip(renderMarkdown("#### Deep"));
81+ const h3 = strip(renderMarkdown("### Deep"));
82+ expect(h4).toBe(h3);
83+ });
84+
85+ test("multiple headings each rendered on own line", () => {
86+ const out = strip(renderMarkdown("# H1\n## H2\n### H3"));
87+ expect(out).toBe("H1\nH2\nH3");
88+ });
89+});
90+
91+describe("renderMarkdown – horizontal rules", () => {
92+ test("--- renders as a dim rule", () => {
93+ const out = renderMarkdown("---");
94+ expect(hasDim(out)).toBe(true);
95+ expect(strip(out)).toMatch(/^─+$/);
96+ });
97+
98+ test("=== renders as a dim rule", () => {
99+ const out = renderMarkdown("===");
100+ expect(hasDim(out)).toBe(true);
101+ expect(strip(out)).toMatch(/^─+$/);
102+ });
103+
104+ test("*** renders as a dim rule", () => {
105+ const out = renderMarkdown("***");
106+ expect(hasDim(out)).toBe(true);
107+ expect(strip(out)).toMatch(/^─+$/);
108+ });
109+
110+ test("---- (4 dashes) is also a horizontal rule", () => {
111+ const out = strip(renderMarkdown("----"));
112+ expect(out).toMatch(/^─+$/);
113+ });
114+
115+ test("-- (2 dashes) is NOT a horizontal rule — passed through as plain text", () => {
116+ const out = strip(renderMarkdown("--"));
117+ expect(out).toBe("--");
118+ });
119+});
120+
121+describe("renderMarkdown – fenced code blocks", () => {
122+ test("lines inside ``` fence are yellow", () => {
123+ const out = renderMarkdown("```\ncode line\n```");
124+ const lines = out.split("\n");
125+ // line index 1 is "code line"
126+ expect(hasYellow(lines[1] ?? "")).toBe(true);
127+ });
128+
129+ test("fence delimiter lines are dim", () => {
130+ const out = renderMarkdown("```\ncode\n```");
131+ const lines = out.split("\n");
132+ expect(hasDim(lines[0] ?? "")).toBe(true);
133+ expect(hasDim(lines[2] ?? "")).toBe(true);
134+ });
135+
136+ test("language hint on opening fence is preserved in stripped output", () => {
137+ const out = strip(renderMarkdown("```ts\nconst x = 1;\n```"));
138+ expect(out).toBe("```ts\nconst x = 1;\n```");
139+ });
140+
141+ test("inline markdown inside fenced block is NOT processed", () => {
142+ const out = strip(renderMarkdown("```\n**not bold**\n```"));
143+ expect(out).toContain("**not bold**");
144+ });
145+
146+ test("~~~ also opens and closes a fenced block", () => {
147+ const out = renderMarkdown("~~~\ncode\n~~~");
148+ const lines = out.split("\n");
149+ expect(hasYellow(lines[1] ?? "")).toBe(true);
150+ });
151+
152+ test("two consecutive fenced blocks are both fenced correctly", () => {
153+ const input = "```\nfirst\n```\n```\nsecond\n```";
154+ const out = renderMarkdown(input);
155+ const lines = out.split("\n");
156+ // line 1 = "first" (inside first fence), line 4 = "second" (inside second fence)
157+ expect(hasYellow(lines[1] ?? "")).toBe(true);
158+ expect(hasYellow(lines[4] ?? "")).toBe(true);
159+ // line 3 = "```" (opening second fence), should be dim not yellow
160+ expect(hasDim(lines[3] ?? "")).toBe(true);
161+ expect(hasYellow(lines[3] ?? "")).toBe(false);
162+ });
163+
164+ test("unclosed fence: lines after opening ``` are treated as code", () => {
165+ const out = renderMarkdown("```\ncode\nnormal line");
166+ const lines = out.split("\n");
167+ expect(hasYellow(lines[1] ?? "")).toBe(true);
168+ expect(hasYellow(lines[2] ?? "")).toBe(true);
169+ });
170+});
171+
172+describe("renderMarkdown – blockquotes", () => {
173+ test("> line renders with │ prefix and dim styling", () => {
174+ const out = renderMarkdown("> quoted");
175+ expect(strip(out)).toBe("│ quoted");
176+ expect(hasDim(out)).toBe(true);
177+ });
178+
179+ test("> with no space also matches", () => {
180+ const out = strip(renderMarkdown(">no space"));
181+ expect(out).toBe("│ no space");
182+ });
183+});
184+
185+describe("renderMarkdown – unordered lists", () => {
186+ test("- item renders with · bullet", () => {
187+ const out = strip(renderMarkdown("- item"));
188+ expect(out).toBe("· item");
189+ });
190+
191+ test("* item renders with · bullet", () => {
192+ const out = strip(renderMarkdown("* item"));
193+ expect(out).toBe("· item");
194+ });
195+
196+ test("+ item renders with · bullet", () => {
197+ const out = strip(renderMarkdown("+ item"));
198+ expect(out).toBe("· item");
199+ });
200+
201+ test("indented - item preserves leading whitespace", () => {
202+ const out = strip(renderMarkdown(" - nested"));
203+ expect(out).toBe(" · nested");
204+ });
205+
206+ test("list item content goes through inline processing", () => {
207+ const out = strip(renderMarkdown("- **bold** item"));
208+ // bold markers stripped in plain text, content preserved
209+ expect(out).toBe("· bold item");
210+ });
211+});
212+
213+describe("renderMarkdown – ordered lists", () => {
214+ test("1. item renders number with dim styling", () => {
215+ const out = strip(renderMarkdown("1. first"));
216+ expect(out).toBe("1. first");
217+ });
218+
219+ test("multi-item ordered list", () => {
220+ const out = strip(renderMarkdown("1. alpha\n2. beta\n3. gamma"));
221+ expect(out).toBe("1. alpha\n2. beta\n3. gamma");
222+ });
223+
224+ test("ordered list item content goes through inline processing", () => {
225+ const out = strip(renderMarkdown("1. **bold** text"));
226+ expect(out).toBe("1. bold text");
227+ });
228+});
229+
230+describe("renderMarkdown – plain text", () => {
231+ test("plain text is passed through unchanged", () => {
232+ const out = strip(renderMarkdown("hello world"));
233+ expect(out).toBe("hello world");
234+ });
235+
236+ test("empty string produces empty string", () => {
237+ expect(renderMarkdown("")).toBe("");
238+ });
239+
240+ test("empty line in multi-line input is preserved", () => {
241+ const out = strip(renderMarkdown("line1\n\nline3"));
242+ expect(out).toBe("line1\n\nline3");
243+ });
244+});
245+
246+// ---------------------------------------------------------------------------
247+// renderMarkdown — inline spans (via plain text lines)
248+// ---------------------------------------------------------------------------
249+
250+describe("renderMarkdown – inline code", () => {
251+ test("`code` renders yellow and preserves backticks in stripped text", () => {
252+ const out = renderMarkdown("`code`");
253+ expect(hasYellow(out)).toBe(true);
254+ expect(strip(out)).toBe("`code`");
255+ });
256+
257+ test("unclosed backtick is passed through as plain text", () => {
258+ const out = strip(renderMarkdown("hello `world"));
259+ expect(out).toBe("hello `world");
260+ });
261+
262+ test("two separate inline code spans on one line both get highlighted", () => {
263+ const out = renderMarkdown("`foo` and `bar`");
264+ expect(strip(out)).toBe("`foo` and `bar`");
265+ // Two yellow regions should appear
266+ const yellowCount =
267+ out.split(`${String.fromCharCode(0x1b)}[33m`).length - 1;
268+ expect(yellowCount).toBe(2);
269+ });
270+});
271+
272+describe("renderMarkdown – bold", () => {
273+ test("**bold** renders bold styling and strips markers from plain text", () => {
274+ const out = renderMarkdown("**bold**");
275+ expect(hasBold(out)).toBe(true);
276+ expect(strip(out)).toBe("bold");
277+ });
278+
279+ test("bold with surrounding text", () => {
280+ const out = strip(renderMarkdown("before **bold** after"));
281+ expect(out).toBe("before bold after");
282+ });
283+
284+ test("unclosed ** is passed through as plain text", () => {
285+ const out = strip(renderMarkdown("**not closed"));
286+ expect(out).toBe("**not closed");
287+ });
288+});
289+
290+describe("renderMarkdown – italic", () => {
291+ test("*italic* renders dim styling", () => {
292+ const out = renderMarkdown("*italic*");
293+ expect(hasDim(out)).toBe(true);
294+ expect(strip(out)).toBe("italic");
295+ });
296+
297+ test("italic with surrounding text", () => {
298+ const out = strip(renderMarkdown("before *italic* after"));
299+ expect(out).toBe("before italic after");
300+ });
301+
302+ test("unclosed * is passed through as plain text", () => {
303+ const out = strip(renderMarkdown("*not closed"));
304+ expect(out).toBe("*not closed");
305+ });
306+});
307+
308+describe("renderMarkdown – inline combinations", () => {
309+ test("bold and italic on same line both render correctly", () => {
310+ const out = strip(renderMarkdown("**bold** and *italic*"));
311+ expect(out).toBe("bold and italic");
312+ });
313+
314+ test("inline code and bold on same line both render correctly", () => {
315+ const out = strip(renderMarkdown("`code` and **bold**"));
316+ expect(out).toBe("`code` and bold");
317+ });
318+
319+ test("heading with inline bold strips # and bold markers", () => {
320+ const out = strip(renderMarkdown("## Section **key**"));
321+ expect(out).toBe("Section key");
322+ });
323+
324+ test("italic immediately after bold (no space) renders both correctly", () => {
325+ const out = strip(renderMarkdown("**bold***italic*"));
326+ expect(out).toBe("bolditalic");
327+ });
328+
329+ test("bold immediately after italic (no space) renders both correctly", () => {
330+ const out = strip(renderMarkdown("*italic***bold**"));
331+ expect(out).toBe("italicbold");
332+ });
333+
334+ test("unclosed ** does not corrupt a following *italic* span", () => {
335+ const out = strip(renderMarkdown("**foo *bar*"));
336+ expect(out).toBe("**foo bar");
337+ });
338+
339+ test("***text*** does not corrupt surrounding text — stray * passed through as plain", () => {
340+ // Triple-star is ambiguous; the parser should not silently eat content.
341+ // Current behaviour: outer * passed through as plain, inner text bolded.
342+ const out = strip(renderMarkdown("***text***"));
343+ expect(out).toContain("text");
344+ expect(out).not.toContain("**");
345+ });
346+});
src/cli/markdown.ts+20−4
@@ -6,6 +6,11 @@ import * as c from "yoctocolors";
66 function renderInline(text: string): string {
77 let out = "";
88 let i = 0;
9+ // Track whether the previous character in the *source* was consumed as part
10+ // of a bold closing **. This lets the italic branch distinguish "the * right
11+ // after **bold**" (safe to start italic) from "the second * of an unclosed **"
12+ // (must not start italic).
13+ let prevWasBoldClose = false;
914
1015 while (i < text.length) {
1116 // Inline code: `...`
@@ -14,32 +19,43 @@ function renderInline(text: string): string {
1419 if (end !== -1) {
1520 out += c.yellow(text.slice(i, end + 1));
1621 i = end + 1;
22+ prevWasBoldClose = false;
1723 continue;
1824 }
1925 }
2026
21- // Bold: **...**
22- if (text.slice(i, i + 2) === "**") {
27+ // Bold: **...** (opening ** must not be followed by a third *)
28+ if (text.slice(i, i + 2) === "**" && text[i + 2] !== "*") {
2329 const end = text.indexOf("**", i + 2);
2430 if (end !== -1) {
2531 out += c.bold(text.slice(i + 2, end));
2632 i = end + 2;
33+ prevWasBoldClose = true;
2734 continue;
2835 }
2936 }
3037
31- // Italic: *...* (not preceded by another *)
32- if (text[i] === "*" && text[i - 1] !== "*" && text[i + 1] !== "*") {
38+ // Italic: *...* — opening * must not be part of ** (next char is *).
39+ // We also require that the previous source character was NOT a * UNLESS
40+ // we just closed a bold span (in which case the adjacent * is a legitimate
41+ // italic opener, not the second star of a ** pair).
42+ if (
43+ text[i] === "*" &&
44+ text[i + 1] !== "*" &&
45+ (prevWasBoldClose || text[i - 1] !== "*")
46+ ) {
3347 const end = text.indexOf("*", i + 1);
3448 if (end !== -1 && text[end - 1] !== "*") {
3549 out += c.dim(text.slice(i + 1, end));
3650 i = end + 1;
51+ prevWasBoldClose = false;
3752 continue;
3853 }
3954 }
4055
4156 out += text[i];
4257 i++;
58+ prevWasBoldClose = false;
4359 }
4460
4561 return out;