xonecas.io
64875
Mc

mini-coder / commit 0bd31e8

fix(cli): remove markdown rendering from assistant output

xonecas2026-03-15 0bd31e8f52154a3caf4069ce9b5207de60adce17 parent c4d9057

5 files changed

src/cli/commands.ts+1−2
@@ -15,7 +15,6 @@ import {
1515 loadCustomCommands,
1616 } from "./custom-commands.ts";
1717 import { watchForCancel } from "./input.ts";
18-import { renderMarkdown } from "./markdown.ts";
1918 import { PREFIX, renderBanner, write, writeln } from "./output.ts";
2019
2120 // ─── Types ────────────────────────────────────────────────────────────────────
@@ -89,7 +88,7 @@ async function handleCustomCommand(
8988 cmd.model,
9089 abortController.signal,
9190 );
92- write(renderMarkdown(output.result));
91+ write(output.result);
9392 writeln();
9493
9594 return {
src/cli/markdown.test.ts+0−298
@@ -1,298 +0,0 @@
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- for (const [input, visible] of [
47- ["# Hello World", "Hello World"],
48- ["## Section", "Section"],
49- ] as const) {
50- test(`${input.slice(0, input.indexOf(" "))} strips its marker and keeps bold cyan styling`, () => {
51- const out = renderMarkdown(input);
52- expect(strip(out)).toBe(visible);
53- expect(hasBold(out)).toBe(true);
54- expect(hasCyan(out)).toBe(true);
55- });
56- }
57-
58- test("h3+ strips markers, stays bold, and drops cyan", () => {
59- const h3 = renderMarkdown("### Sub");
60- const h4 = renderMarkdown("#### Deep");
61- expect(strip(h3)).toBe("Sub");
62- expect(hasBold(h3)).toBe(true);
63- expect(hasCyan(h3)).toBe(false);
64- expect(strip(h4)).toBe("Deep");
65- expect(hasBold(h4)).toBe(true);
66- expect(hasCyan(h4)).toBe(false);
67- });
68-
69- test("multiple headings each rendered on own line", () => {
70- const out = strip(renderMarkdown("# H1\n## H2\n### H3"));
71- expect(out).toBe("H1\nH2\nH3");
72- });
73-});
74-
75-describe("renderMarkdown – horizontal rules", () => {
76- test("supported rule markers render a dim divider", () => {
77- for (const input of ["---", "===", "***", "----"]) {
78- const out = renderMarkdown(input);
79- expect(hasDim(out)).toBe(true);
80- expect(strip(out)).toMatch(/^─+$/);
81- }
82- });
83-
84- test("too-short markers are passed through as plain text", () => {
85- const out = strip(renderMarkdown("--"));
86- expect(out).toBe("--");
87- });
88-});
89-
90-describe("renderMarkdown – fenced code blocks", () => {
91- test("lines inside ``` fence are yellow", () => {
92- const out = renderMarkdown("```\ncode line\n```");
93- const lines = out.split("\n");
94- // line index 1 is "code line"
95- expect(hasYellow(lines[1] ?? "")).toBe(true);
96- });
97-
98- test("fence delimiter lines are dim", () => {
99- const out = renderMarkdown("```\ncode\n```");
100- const lines = out.split("\n");
101- expect(hasDim(lines[0] ?? "")).toBe(true);
102- expect(hasDim(lines[2] ?? "")).toBe(true);
103- });
104-
105- test("language hint on opening fence is preserved in stripped output", () => {
106- const out = strip(renderMarkdown("```ts\nconst x = 1;\n```"));
107- expect(out).toBe("```ts\nconst x = 1;\n```");
108- });
109-
110- test("inline markdown inside fenced block is NOT processed", () => {
111- const out = strip(renderMarkdown("```\n**not bold**\n```"));
112- expect(out).toContain("**not bold**");
113- });
114-
115- test("~~~ also opens and closes a fenced block", () => {
116- const out = renderMarkdown("~~~\ncode\n~~~");
117- const lines = out.split("\n");
118- expect(hasYellow(lines[1] ?? "")).toBe(true);
119- });
120-
121- test("two consecutive fenced blocks are both fenced correctly", () => {
122- const input = "```\nfirst\n```\n```\nsecond\n```";
123- const out = renderMarkdown(input);
124- const lines = out.split("\n");
125- // line 1 = "first" (inside first fence), line 4 = "second" (inside second fence)
126- expect(hasYellow(lines[1] ?? "")).toBe(true);
127- expect(hasYellow(lines[4] ?? "")).toBe(true);
128- // line 3 = "```" (opening second fence), should be dim not yellow
129- expect(hasDim(lines[3] ?? "")).toBe(true);
130- expect(hasYellow(lines[3] ?? "")).toBe(false);
131- });
132-
133- test("unclosed fence: lines after opening ``` are treated as code", () => {
134- const out = renderMarkdown("```\ncode\nnormal line");
135- const lines = out.split("\n");
136- expect(hasYellow(lines[1] ?? "")).toBe(true);
137- expect(hasYellow(lines[2] ?? "")).toBe(true);
138- });
139-});
140-
141-describe("renderMarkdown – blockquotes", () => {
142- test("> line renders with │ prefix and dim styling", () => {
143- const out = renderMarkdown("> quoted");
144- expect(strip(out)).toBe("│ quoted");
145- expect(hasDim(out)).toBe(true);
146- });
147-
148- test("> with no space also matches", () => {
149- const out = strip(renderMarkdown(">no space"));
150- expect(out).toBe("│ no space");
151- });
152-});
153-
154-describe("renderMarkdown – unordered lists", () => {
155- test("all supported unordered markers render the same bullet", () => {
156- for (const input of ["- item", "* item", "+ item"]) {
157- expect(strip(renderMarkdown(input))).toBe("· item");
158- }
159- });
160-
161- test("indented items preserve leading whitespace", () => {
162- const out = strip(renderMarkdown(" - nested"));
163- expect(out).toBe(" · nested");
164- });
165-
166- test("list item content still goes through inline processing", () => {
167- const out = strip(renderMarkdown("- **bold** item"));
168- expect(out).toBe("· bold item");
169- });
170-});
171-
172-describe("renderMarkdown – ordered lists", () => {
173- test("ordered list numbers stay visible while the marker is dimmed", () => {
174- const out = renderMarkdown("1. first");
175- expect(strip(out)).toBe("1. first");
176- expect(hasDim(out)).toBe(true);
177- });
178-
179- test("multi-item ordered lists preserve numbering across lines", () => {
180- const out = strip(renderMarkdown("1. alpha\n2. beta\n3. gamma"));
181- expect(out).toBe("1. alpha\n2. beta\n3. gamma");
182- });
183-
184- test("ordered list item content still goes through inline processing", () => {
185- const out = strip(renderMarkdown("1. **bold** text"));
186- expect(out).toBe("1. bold text");
187- });
188-});
189-
190-describe("renderMarkdown – plain text", () => {
191- test("plain text is passed through unchanged", () => {
192- const out = strip(renderMarkdown("hello world"));
193- expect(out).toBe("hello world");
194- });
195-
196- test("empty string produces empty string", () => {
197- expect(renderMarkdown("")).toBe("");
198- });
199-
200- test("empty line in multi-line input is preserved", () => {
201- const out = strip(renderMarkdown("line1\n\nline3"));
202- expect(out).toBe("line1\n\nline3");
203- });
204-});
205-
206-// ---------------------------------------------------------------------------
207-// renderMarkdown — inline spans (via plain text lines)
208-// ---------------------------------------------------------------------------
209-
210-describe("renderMarkdown – inline code", () => {
211- test("`code` renders yellow and preserves backticks in stripped text", () => {
212- const out = renderMarkdown("`code`");
213- expect(hasYellow(out)).toBe(true);
214- expect(strip(out)).toBe("`code`");
215- });
216-
217- test("unclosed backtick is passed through as plain text", () => {
218- const out = strip(renderMarkdown("hello `world"));
219- expect(out).toBe("hello `world");
220- });
221-
222- test("two separate inline code spans on one line both get highlighted", () => {
223- const out = renderMarkdown("`foo` and `bar`");
224- expect(strip(out)).toBe("`foo` and `bar`");
225- // Two yellow regions should appear
226- const yellowCount =
227- out.split(`${String.fromCharCode(0x1b)}[33m`).length - 1;
228- expect(yellowCount).toBe(2);
229- });
230-});
231-
232-describe("renderMarkdown – bold", () => {
233- test("**bold** renders bold styling and strips markers from plain text", () => {
234- const out = renderMarkdown("**bold**");
235- expect(hasBold(out)).toBe(true);
236- expect(strip(out)).toBe("bold");
237- });
238-
239- test("bold with surrounding text", () => {
240- const out = strip(renderMarkdown("before **bold** after"));
241- expect(out).toBe("before bold after");
242- });
243-
244- test("unclosed ** is passed through as plain text", () => {
245- const out = strip(renderMarkdown("**not closed"));
246- expect(out).toBe("**not closed");
247- });
248-});
249-
250-describe("renderMarkdown – italic", () => {
251- test("*italic* renders dim styling", () => {
252- const out = renderMarkdown("*italic*");
253- expect(hasDim(out)).toBe(true);
254- expect(strip(out)).toBe("italic");
255- });
256-
257- test("italic with surrounding text", () => {
258- const out = strip(renderMarkdown("before *italic* after"));
259- expect(out).toBe("before italic after");
260- });
261-
262- test("unclosed * is passed through as plain text", () => {
263- const out = strip(renderMarkdown("*not closed"));
264- expect(out).toBe("*not closed");
265- });
266-});
267-
268-describe("renderMarkdown – inline combinations", () => {
269- test("bold and italic on same line both render correctly", () => {
270- const out = strip(renderMarkdown("**bold** and *italic*"));
271- expect(out).toBe("bold and italic");
272- });
273-
274- test("inline code and bold on same line both render correctly", () => {
275- const out = strip(renderMarkdown("`code` and **bold**"));
276- expect(out).toBe("`code` and bold");
277- });
278-
279- test("heading with inline bold strips # and bold markers", () => {
280- const out = strip(renderMarkdown("## Section **key**"));
281- expect(out).toBe("Section key");
282- });
283-
284- test("italic immediately after bold (no space) renders both correctly", () => {
285- const out = strip(renderMarkdown("**bold***italic*"));
286- expect(out).toBe("bolditalic");
287- });
288-
289- test("bold immediately after italic (no space) renders both correctly", () => {
290- const out = strip(renderMarkdown("*italic***bold**"));
291- expect(out).toBe("italicbold");
292- });
293-
294- test("unclosed ** does not corrupt a following *italic* span", () => {
295- const out = strip(renderMarkdown("**foo *bar*"));
296- expect(out).toBe("**foo bar");
297- });
298-});
src/cli/markdown.ts+0−155
@@ -1,155 +0,0 @@
1-import * as c from "yoctocolors";
2-
3-// ─── Inline span renderer ─────────────────────────────────────────────────────
4-// Handles: `code`, **bold**, *italic* within a single line of text.
5-
6-function renderInline(text: string): string {
7- let out = "";
8- let i = 0;
9- // segStart marks the beginning of the current plain-text segment that has
10- // not yet been flushed to `out`. When we hit a recognised span we flush the
11- // accumulated plain text in one slice instead of appending character by
12- // character.
13- let segStart = 0;
14- // Track whether the previous character in the *source* was consumed as part
15- // of a bold closing **. This lets the italic branch distinguish "the * right
16- // after **bold**" (safe to start italic) from "the second * of an unclosed **"
17- // (must not start italic).
18- let prevWasBoldClose = false;
19-
20- while (i < text.length) {
21- const ch = text[i] as string;
22-
23- // Inline code: `...`
24- if (ch === "`") {
25- const end = text.indexOf("`", i + 1);
26- if (end !== -1) {
27- if (i > segStart) out += text.slice(segStart, i);
28- out += c.yellow(text.slice(i, end + 1));
29- i = end + 1;
30- segStart = i;
31- prevWasBoldClose = false;
32- continue;
33- }
34- }
35-
36- if (ch === "*") {
37- // Bold: **...** (opening ** must not be followed by a third *)
38- // Compare individual chars instead of allocating a 2-char slice.
39- if (text[i + 1] === "*" && text[i + 2] !== "*") {
40- const end = text.indexOf("**", i + 2);
41- if (end !== -1) {
42- if (i > segStart) out += text.slice(segStart, i);
43- out += c.bold(text.slice(i + 2, end));
44- i = end + 2;
45- segStart = i;
46- prevWasBoldClose = true;
47- continue;
48- }
49- }
50-
51- // Italic: *...* — opening * must not be part of ** (next char is *).
52- // We also require that the previous source character was NOT a * UNLESS
53- // we just closed a bold span (in which case the adjacent * is a legitimate
54- // italic opener, not the second star of a ** pair).
55- if (text[i + 1] !== "*" && (prevWasBoldClose || text[i - 1] !== "*")) {
56- const end = text.indexOf("*", i + 1);
57- if (end !== -1 && text[end - 1] !== "*") {
58- if (i > segStart) out += text.slice(segStart, i);
59- out += c.dim(text.slice(i + 1, end));
60- i = end + 1;
61- segStart = i;
62- prevWasBoldClose = false;
63- continue;
64- }
65- }
66- }
67-
68- prevWasBoldClose = false;
69- i++;
70- }
71-
72- // Flush any remaining plain-text segment in one shot.
73- if (segStart < text.length) out += text.slice(segStart);
74-
75- return out;
76-}
77-
78-// ─── Block renderer ───────────────────────────────────────────────────────────
79-
80-/**
81- * Render a single source line given the current fence state.
82- * Returns the rendered string and the updated fence state.
83- * Use this for incremental streaming — call once per complete line.
84- */
85-export function renderLine(
86- raw: string,
87- inFence: boolean,
88-): { output: string; inFence: boolean } {
89- // Fenced code block toggle (``` or ~~~)
90- if (/^(`{3,}|~{3,})/.test(raw)) {
91- return { output: c.dim(raw), inFence: !inFence };
92- }
93- if (inFence) {
94- return { output: c.yellow(raw), inFence: true };
95- }
96- // Horizontal rule: --- or *** or ===
97- if (/^(-{3,}|\*{3,}|={3,})\s*$/.test(raw)) {
98- return { output: c.dim("─".repeat(40)), inFence: false };
99- }
100- // Headings
101- const h3 = raw.match(/^(#{3,})\s+(.*)/);
102- if (h3) return { output: c.bold(renderInline(h3[2] ?? "")), inFence: false };
103- const h2 = raw.match(/^##\s+(.*)/);
104- if (h2)
105- return {
106- output: c.bold(c.cyan(renderInline(h2[1] ?? ""))),
107- inFence: false,
108- };
109- const h1 = raw.match(/^#\s+(.*)/);
110- if (h1)
111- return {
112- output: c.bold(c.cyan(renderInline(h1[1] ?? ""))),
113- inFence: false,
114- };
115- // Blockquote: > ...
116- const bq = raw.match(/^>\s?(.*)/);
117- if (bq)
118- return {
119- output: c.dim(`│ ${renderInline(bq[1] ?? "")}`),
120- inFence: false,
121- };
122- // Unordered list: - or * or +
123- const ul = raw.match(/^(\s*)[*\-+]\s+(.*)/);
124- if (ul) {
125- const indent = ul[1] ?? "";
126- return {
127- output: `${indent}${c.dim("·")} ${renderInline(ul[2] ?? "")}`,
128- inFence: false,
129- };
130- }
131- // Ordered list: 1. ...
132- const ol = raw.match(/^(\s*)(\d+)\.\s+(.*)/);
133- if (ol) {
134- const indent = ol[1] ?? "";
135- const num = ol[2] ?? "";
136- return {
137- output: `${indent}${c.dim(`${num}.`)} ${renderInline(ol[3] ?? "")}`,
138- inFence: false,
139- };
140- }
141- // Plain text (inline spans still applied)
142- return { output: renderInline(raw), inFence: false };
143-}
144-
145-export function renderMarkdown(text: string): string {
146- let inFence = false;
147- return text
148- .split("\n")
149- .map((raw) => {
150- const r = renderLine(raw, inFence);
151- inFence = r.inFence;
152- return r.output;
153- })
154- .join("\n");
155-}
src/cli/stream-render-content.ts+12−61
@@ -1,12 +1,8 @@
1-import { renderLine } from "./markdown.ts";
21 import { G, write, writeln } from "./output.ts";
32 import type { Spinner } from "./spinner.ts";
43
54 export class StreamRenderContent {
65 private inText = false;
7- private rawBuffer = "";
8- private streamedChars = 0;
9- private inFence = false;
106 private accumulatedText = "";
117 private accumulatedReasoning = "";
128
@@ -24,27 +20,29 @@ export class StreamRenderContent {
2420 return this.inText;
2521 }
2622
27- appendTextDelta(delta: string): void {
23+ appendTextDelta(delta: string | undefined): void {
24+ const text = delta ?? "";
25+ if (!text) return;
2826 if (!this.inText) {
2927 this.spinner.stop();
3028 write(`${G.reply} `);
3129 this.inText = true;
3230 }
33- this.rawBuffer += delta;
34- this.accumulatedText += delta;
35- this.flushCompleteLines();
36- this.streamPartialRemainder();
31+ this.accumulatedText += text;
32+ this.spinner.stop();
33+ write(text);
3734 }
3835
39- appendReasoningDelta(delta: string): string {
40- if (!delta) return "";
41- let appended = delta;
36+ appendReasoningDelta(delta: string | undefined): string {
37+ const text = delta ?? "";
38+ if (!text) return "";
39+ let appended = text;
4240 if (
4341 this.accumulatedReasoning.endsWith("**") &&
44- delta.startsWith("**") &&
42+ text.startsWith("**") &&
4543 !this.accumulatedReasoning.endsWith("\n")
4644 ) {
47- appended = `\n${delta}`;
45+ appended = `\n${text}`;
4846 }
4947 this.accumulatedReasoning += appended;
5048 return appended;
@@ -52,54 +50,7 @@ export class StreamRenderContent {
5250
5351 flushOpenContent(): void {
5452 if (!this.inText) return;
55- this.streamPartialRemainder();
5653 writeln();
5754 this.inText = false;
58- this.inFence = false;
59- this.rawBuffer = "";
60- this.streamedChars = 0;
61- }
62-
63- private renderSingleLine(raw: string): string {
64- const rendered = renderLine(raw, this.inFence);
65- this.inFence = rendered.inFence;
66- return rendered.output;
67- }
68-
69- private flushCompleteLines(): void {
70- let boundary = this.rawBuffer.indexOf("\n");
71- if (boundary === -1) return;
72- this.spinner.stop();
73-
74- while (boundary !== -1) {
75- const raw = this.rawBuffer.slice(0, boundary);
76- const rendered = this.renderSingleLine(raw);
77- const streamedForLine = Math.min(this.streamedChars, raw.length);
78-
79- if (streamedForLine > 0) {
80- if (streamedForLine < raw.length) {
81- this.writePartial(raw.slice(streamedForLine));
82- }
83- writeln();
84- } else {
85- writeln(rendered);
86- }
87-
88- this.rawBuffer = this.rawBuffer.slice(boundary + 1);
89- this.streamedChars = Math.max(0, this.streamedChars - raw.length);
90- boundary = this.rawBuffer.indexOf("\n");
91- }
92- }
93-
94- private streamPartialRemainder(): void {
95- if (this.rawBuffer.length <= this.streamedChars) return;
96- this.spinner.stop();
97- this.writePartial(this.rawBuffer.slice(this.streamedChars));
98- this.streamedChars = this.rawBuffer.length;
99- }
100-
101- private writePartial(text: string): void {
102- if (!text) return;
103- write(text);
10455 }
10556 }
src/cli/stream-render.test.ts+3−5
@@ -255,7 +255,7 @@ describe("renderTurn", () => {
255255 expect(hasAnsi(stdout, "[1m")).toBe(false);
256256 });
257257
258- test("preserves fenced-code state across streamed lines", async () => {
258+ test("leaves fenced markdown plain across streamed lines", async () => {
259259 captureStdout();
260260
261261 await renderTurn(
@@ -267,11 +267,9 @@ describe("renderTurn", () => {
267267 new Spinner(),
268268 );
269269
270- const lines = stdout.split("\n");
271270 expect(strip(stdout)).toBe("◆ ```ts\nconst emoji = '👩🏽‍💻';\n```\n\n");
272- expect(hasAnsi(lines[0] ?? "", "[2m")).toBe(true);
273- expect(hasAnsi(lines[1] ?? "", "[33m")).toBe(true);
274- expect(hasAnsi(lines[2] ?? "", "[2m")).toBe(true);
271+ expect(hasAnsi(stdout, "[2m")).toBe(false);
272+ expect(hasAnsi(stdout, "[33m")).toBe(false);
275273 });
276274 test("can hide reasoning output while still accumulating reasoning text", async () => {
277275 captureStdout();