@@ -0,0 +1,122 @@
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+
10+ while (i < text.length) {
11+ // Inline code: `...`
12+ if (text[i] === "`") {
13+ const end = text.indexOf("`", i + 1);
14+ if (end !== -1) {
15+ out += c.yellow(text.slice(i, end + 1));
16+ i = end + 1;
17+ continue;
18+ }
19+ }
20+
21+ // Bold: **...**
22+ if (text.slice(i, i + 2) === "**") {
23+ const end = text.indexOf("**", i + 2);
24+ if (end !== -1) {
25+ out += c.bold(text.slice(i + 2, end));
26+ i = end + 2;
27+ continue;
28+ }
29+ }
30+
31+ // Italic: *...* (not preceded by another *)
32+ if (text[i] === "*" && text[i - 1] !== "*" && text[i + 1] !== "*") {
33+ const end = text.indexOf("*", i + 1);
34+ if (end !== -1 && text[end - 1] !== "*") {
35+ out += c.dim(text.slice(i + 1, end));
36+ i = end + 1;
37+ continue;
38+ }
39+ }
40+
41+ out += text[i];
42+ i++;
43+ }
44+
45+ return out;
46+}
47+
48+// ─── Block renderer ───────────────────────────────────────────────────────────
49+
50+export function renderMarkdown(text: string): string {
51+ const lines = text.split("\n");
52+ const out: string[] = [];
53+ let inFence = false;
54+
55+ for (const raw of lines) {
56+ // Fenced code block toggle (``` or ~~~)
57+ if (/^(`{3,}|~{3,})/.test(raw)) {
58+ inFence = !inFence;
59+ // Show the fence line dimmed (language hint visible but subdued)
60+ out.push(c.dim(raw));
61+ continue;
62+ }
63+
64+ // Inside fenced block — yellow, no inline processing
65+ if (inFence) {
66+ out.push(c.yellow(raw));
67+ continue;
68+ }
69+
70+ // Horizontal rule: --- or *** or ===
71+ if (/^(-{3,}|\*{3,}|={3,})\s*$/.test(raw)) {
72+ out.push(c.dim("─".repeat(40)));
73+ continue;
74+ }
75+
76+ // Headings
77+ const h3 = raw.match(/^(#{3,})\s+(.*)/);
78+ if (h3) {
79+ out.push(c.bold(renderInline(h3[2] ?? "")));
80+ continue;
81+ }
82+ const h2 = raw.match(/^##\s+(.*)/);
83+ if (h2) {
84+ out.push(c.bold(c.cyan(renderInline(h2[1] ?? ""))));
85+ continue;
86+ }
87+ const h1 = raw.match(/^#\s+(.*)/);
88+ if (h1) {
89+ out.push(c.bold(c.cyan(renderInline(h1[1] ?? ""))));
90+ continue;
91+ }
92+
93+ // Blockquote: > ...
94+ const bq = raw.match(/^>\s?(.*)/);
95+ if (bq) {
96+ out.push(c.dim(`│ ${renderInline(bq[1] ?? "")}`));
97+ continue;
98+ }
99+
100+ // Unordered list: - or * or +
101+ const ul = raw.match(/^(\s*)[*\-+]\s+(.*)/);
102+ if (ul) {
103+ const indent = ul[1] ?? "";
104+ out.push(`${indent}${c.dim("·")} ${renderInline(ul[2] ?? "")}`);
105+ continue;
106+ }
107+
108+ // Ordered list: 1. ...
109+ const ol = raw.match(/^(\s*)(\d+)\.\s+(.*)/);
110+ if (ol) {
111+ const indent = ol[1] ?? "";
112+ const num = ol[2] ?? "";
113+ out.push(`${indent}${c.dim(`${num}.`)} ${renderInline(ol[3] ?? "")}`);
114+ continue;
115+ }
116+
117+ // Plain text (inline spans still applied)
118+ out.push(renderInline(raw));
119+ }
120+
121+ return out.join("\n");
122+}