@@ -6,6 +6,11 @@ import * as c from "yoctocolors";
66 function renderInline(text: string): string {
77 let out = "";
88 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;
914 // Track whether the previous character in the *source* was consumed as part
1015 // of a bold closing **. This lets the italic branch distinguish "the * right
1116 // after **bold**" (safe to start italic) from "the second * of an unclosed **"
@@ -13,51 +18,60 @@ function renderInline(text: string): string {
1318 let prevWasBoldClose = false;
1419
1520 while (i < text.length) {
21+ const ch = text[i] as string;
22+
1623 // Inline code: `...`
17- if (text[i] === "`") {
24+ if (ch === "`") {
1825 const end = text.indexOf("`", i + 1);
1926 if (end !== -1) {
27+ if (i > segStart) out += text.slice(segStart, i);
2028 out += c.yellow(text.slice(i, end + 1));
2129 i = end + 1;
30+ segStart = i;
2231 prevWasBoldClose = false;
2332 continue;
2433 }
2534 }
2635
27- // Bold: **...** (opening ** must not be followed by a third *)
28- if (text.slice(i, i + 2) === "**" && text[i + 2] !== "*") {
29- const end = text.indexOf("**", i + 2);
30- if (end !== -1) {
31- out += c.bold(text.slice(i + 2, end));
32- i = end + 2;
33- prevWasBoldClose = true;
34- continue;
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+ }
3549 }
36- }
3750
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- ) {
47- const end = text.indexOf("*", i + 1);
48- if (end !== -1 && text[end - 1] !== "*") {
49- out += c.dim(text.slice(i + 1, end));
50- i = end + 1;
51- prevWasBoldClose = false;
52- continue;
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+ }
5365 }
5466 }
5567
56- out += text[i];
57- i++;
5868 prevWasBoldClose = false;
69+ i++;
5970 }
6071
72+ // Flush any remaining plain-text segment in one shot.
73+ if (segStart < text.length) out += text.slice(segStart);
74+
6175 return out;
6276 }
6377