Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | 914x 961x 273x 130x 130x 36x 289x 1142x 289x 3504x 3504x 2506x 2506x 3504x 3504x 3504x 9284x 3504x 2x 2x 121x 210x 117x 139x 3x 243x 284x 4x 121x 2156x 121x 121x 121x 191x 191x 198x 198x 198x 191x 3x 3x 3x 3x 3x 2182x 2182x 4583x 4539x 44x 40218x 9802x 235x 4583x 618x 618x 618x 618x 618x 583x 1166x 583x 583x 618x 618x 1x 772x 24204x 24204x 3x 3x 44452x 44452x 44452x 41218x 41218x 1003x 1003x 1003x 1001x 1001x 1001x 1000x 1000x 1000x 1000x 1999x 1999x 999x 999x 1000x 1000x 1000x 1000x 40218x 40218x 44452x 1609x 1609x 10025x 3609x 3609x 3609x 1019x 1019x 2590x 3609x 3609x 1245x 1245x 1245x 1245x 3x 3x 5x 3x 3x 3x 457x 457x 457x 457x 457x 457x 457x 52x 52x 52x 52x 3511x 3511x 3511x 13x 13x 13x 1135x 1609x 52x 52x 52x 109x 109x 109x 109x 109x 116x 116x 110x 6x 6x 109x 109x 7032x 3511x 18602x 3511x 7032x 5x 7027x 7027x 3511x 1152x 1119x 1119x 33x 33x 2198x 2198x 2198x | /**
* Markdown-to-Terminal Renderer
*
* Custom renderer that walks `marked` tokens and produces ANSI-styled
* terminal output using `chalk`. Replaces `marked-terminal` to eliminate
* its ~970KB dependency chain (cli-highlight, node-emoji, cli-table3,
* parse5) while giving us full control over table rendering.
*
* Table rendering delegates to the text-table module which uses
* OpenTUI-inspired column fitting algorithms and Unicode box-drawing
* borders.
*
* Pre-rendered ANSI escape codes embedded in markdown source are preserved
* — `string-width` correctly treats them as zero-width.
*
* ## Output mode resolution
*
* See {@link isPlainOutput} in `plain-detect.ts` for the full priority chain.
*/
import chalk from "chalk";
import { highlight as cliHighlight } from "cli-highlight";
import { marked, type Token, type Tokens } from "marked";
import stringWidth from "string-width";
import { COLORS, muted, terminalLink } from "./colors.js";
import { isPlainOutput, stripAnsi } from "./plain-detect.js";
import { type Alignment, renderTextTable } from "./text-table.js";
// Re-export isPlainOutput so existing importers don't break
export { isPlainOutput } from "./plain-detect.js";
// ──────────────────────────── Escape helpers ──────────────────────────
/**
* Escape a string for safe use inside a markdown table cell.
*
* Collapses newlines, escapes backslashes, pipes, and angle brackets.
* Angle brackets are backslash-escaped (`\<`/`\>`) so that user-supplied
* content (e.g. `Expected <string>`) is not parsed as an HTML tag by
* `marked`, which would silently drop the content via `renderHtmlToken`.
*/
export function escapeMarkdownCell(value: string): string {
return value
.replace(/\n/g, " ")
.replace(/\\/g, "\\\\")
.replace(/\|/g, "\\|")
.replace(/</g, "\\<")
.replace(/>/g, "\\>");
}
/**
* Escape CommonMark inline emphasis characters.
*
* Prevents `_`, `*`, `` ` ``, `[`, `]`, `<`, and `>` from being consumed
* by the parser. Angle brackets are backslash-escaped (`\<`/`\>`) so that
* user-supplied content (e.g. `Expected <string> got <number>`) is not
* silently dropped when `marked` parses the text as HTML tokens.
*/
export function escapeMarkdownInline(value: string): string {
return value
.replace(/\\/g, "\\\\")
.replace(/\*/g, "\\*")
.replace(/_/g, "\\_")
.replace(/`/g, "\\`")
.replace(/\[/g, "\\[")
.replace(/\]/g, "\\]")
.replace(/</g, "\\<")
.replace(/>/g, "\\>");
}
/**
* Wrap a string in a backtick code span, sanitising characters that
* would break the span or surrounding table structure.
*/
export function safeCodeSpan(value: string): string {
return `\`${value.replace(/`/g, "\u02CB").replace(/\|/g, "\u2502").replace(/\n/g, " ")}\``;
}
// ──────────────────────── Streaming table helpers ─────────────────────
/**
* Build a raw markdown table header row + separator from column names.
*
* Column names ending with `:` are right-aligned (the `:` is stripped).
*/
export function mdTableHeader(cols: readonly string[]): string {
const names = cols.map((c) => (c.endsWith(":") ? c.slice(0, -1) : c));
const seps = cols.map((c) => (c.endsWith(":") ? "---:" : "---"));
return `| ${names.join(" | ")} |\n| ${seps.join(" | ")} |`;
}
/**
* Build a streaming markdown table row. In plain mode emits raw markdown;
* in rendered mode applies inline styling and replaces `|` with `│`.
*/
export function mdRow(cells: readonly string[]): string {
// Both modes render through the same pipeline; plain mode strips ANSI.
// Literal pipes are replaced with box-drawing │ to prevent breaking the
// pipe-delimited table format.
const out = cells.map((c) =>
renderInlineMarkdown(c).replace(/\|/g, "\u2502")
);
return `| ${out.join(" | ")} |\n`;
}
/**
* Build a key-value markdown table section with an optional heading.
*
* Each entry is rendered as `| **Label** | value |`.
*/
export function mdKvTable(
rows: ReadonlyArray<readonly [string, string]>,
heading?: string
): string {
const lines: string[] = [];
if (heading) {
lines.push(`### ${heading}`);
lines.push("");
}
lines.push("| | |");
lines.push("|---|---|");
for (const [label, value] of rows) {
// Escape backslashes first, then replace pipes with a Unicode box character
// so that backslash-pipe sequences in values don't produce escaped pipes in
// the rendered output. Newlines are collapsed to spaces.
// Only replace structural characters that break table syntax.
// Content escaping (<>, \, _*`) is the caller's responsibility.
lines.push(
`| **${label}** | ${value.replace(/\n/g, " ").replace(/\|/g, "\u2502")} |`
);
}
return lines.join("\n");
}
/**
* Render a horizontal rule. Muted (dimmed) in TTY mode, plain in pipes.
*/
export function divider(width = 80): string {
const line = "\u2500".repeat(width);
return isPlainOutput() ? line : muted(line);
}
// ──────────────────────── Inline token rendering ─────────────────────
/**
* Semantic HTML color tags supported in markdown strings.
*
* Formatters can embed `<red>text</red>`, `<green>text</green>`, etc. in
* any markdown string and the custom renderer will apply the corresponding
* ANSI color. In plain (non-TTY) mode the tags are stripped, leaving only
* the inner text.
*
* Supported tags: red, green, yellow, blue, magenta, cyan, muted, bu (bold+underline)
*/
const COLOR_TAGS: Record<string, (text: string) => string> = {
red: (t) => chalk.hex(COLORS.red)(t),
green: (t) => chalk.hex(COLORS.green)(t),
yellow: (t) => chalk.hex(COLORS.yellow)(t),
blue: (t) => chalk.hex(COLORS.blue)(t),
magenta: (t) => chalk.hex(COLORS.magenta)(t),
cyan: (t) => chalk.hex(COLORS.cyan)(t),
muted: (t) => chalk.hex(COLORS.muted)(t),
bu: (t) => chalk.bold.underline(t),
};
/** Regex matching all supported color tag pairs — compiled once at module scope. */
const COLOR_TAG_RE = new RegExp(
`<(${Object.keys(COLOR_TAGS).join("|")})>([\\s\\S]*?)<\\/\\1>`,
"gi"
);
/**
* Wrap text in a semantic color tag for use in markdown strings.
*
* In TTY mode the tag is rendered as an ANSI color by the custom renderer.
* In plain mode the tag is stripped and only the inner text is emitted.
*
* @example
* colorTag("red", "ERROR") // → "<red>ERROR</red>"
* colorTag("green", "✓") // → "<green>✓</green>"
*/
export function colorTag(tag: keyof typeof COLOR_TAGS, text: string): string {
return `<${tag}>${text}</${tag}>`;
}
// Pre-compiled regexes for HTML color tag parsing (module-level for performance)
const RE_OPEN_TAG = /^<([a-z]+)>$/i;
const RE_CLOSE_TAG = /^<\/([a-z]+)>$/i;
const RE_SELF_TAG = /^<([a-z]+)>([\s\S]*?)<\/\1>$/i;
/**
* Strip color tags (`<red>…</red>`, `<green>…</green>`, etc.) from a string,
* leaving only the inner text.
*
* Used in plain output mode so that `colorTag()` values don't leak as literal
* HTML-like tags into piped / CI / redirected output.
*/
export function stripColorTags(text: string): string {
// Repeatedly replace all supported color tag pairs until none remain.
// The loop handles nested tags (uncommon but possible).
let result = text;
let prev: string;
do {
prev = result;
COLOR_TAG_RE.lastIndex = 0;
result = result.replace(COLOR_TAG_RE, "$2");
} while (result !== prev);
return result;
}
/**
* Render an inline HTML token as a color-tagged string.
*
* Handles self-contained `<tag>text</tag>` forms. Bare open/close
* tags are dropped (marked emits them as separate tokens; the
* self-contained form is produced by `colorTag()`).
*/
function renderHtmlToken(raw: string): string {
const trimmed = raw.trim();
Eif (RE_OPEN_TAG.test(trimmed) || RE_CLOSE_TAG.test(trimmed)) {
return "";
}
const m = RE_SELF_TAG.exec(trimmed);
if (m) {
const tagName = m[1];
const inner = m[2];
if (tagName !== undefined && inner !== undefined) {
const colorFn = COLOR_TAGS[tagName.toLowerCase()];
return colorFn ? colorFn(inner) : inner;
}
}
return "";
}
/**
* Syntax-highlight a code block. Falls back to uniform yellow if the
* language is unknown or highlighting fails.
*/
function highlightCode(code: string, language?: string): string {
try {
return cliHighlight(code, { language, ignoreIllegals: true });
} catch {
return chalk.hex(COLORS.yellow)(code);
}
}
/**
* Flatten a top-level token's inline content. Paragraphs and other block
* tokens that wrap inline tokens are unwrapped; bare inline tokens pass
* through as-is.
*/
function flattenInline(token: Token): Token[] {
Eif (
"tokens" in token &&
token.tokens &&
token.type !== "strong" &&
token.type !== "em" &&
token.type !== "link"
) {
return token.tokens;
}
return [token];
}
/**
* Render a code span token.
*
* Plain mode: raw text without padding so table column widths aren't inflated.
* TTY mode: styled with background color and padded spaces for visual weight.
*/
function renderCodespan(token: Tokens.Codespan): string {
if (isPlainOutput()) {
return token.text;
}
return chalk.bgHex(COLORS.codeBg).hex(COLORS.codeFg)(` ${token.text} `);
}
/**
* Render a single inline token to an ANSI string.
*/
function renderOneInline(token: Token): string {
switch (token.type) {
case "strong":
return chalk.bold(renderInline((token as Tokens.Strong).tokens));
case "em":
return chalk.italic(renderInline((token as Tokens.Em).tokens));
case "codespan":
return renderCodespan(token as Tokens.Codespan);
case "link": {
const link = token as Tokens.Link;
let linkText = renderInline(link.tokens);
let href = link.href ?? "";
// GFM autolinks absorb backslash-escapes literally (\_astro → \_astro).
// Detect autolinks via link.raw (starts with URL, not "[") and strip artifacts.
const raw = link.raw ?? "";
if (raw.startsWith("http://") || raw.startsWith("https://")) {
const stripEscapes = (s: string) =>
s.replace(/\\([_*`[\]<>\\])/g, "$1");
linkText = stripEscapes(linkText);
href = stripEscapes(href);
}
const styled = chalk.hex(COLORS.blue)(linkText);
return href ? terminalLink(styled, href) : styled;
}
case "del":
return chalk.dim.gray.strikethrough(
renderInline((token as Tokens.Del).tokens)
);
case "br":
return "\n";
case "escape":
return (token as Tokens.Escape).text;
case "text":
Iif ("tokens" in token && (token as Tokens.Text).tokens) {
return renderInline((token as Tokens.Text).tokens ?? []);
}
return (token as Tokens.Text).text;
case "html": {
const raw = (token as Tokens.HTML).raw ?? (token as Tokens.HTML).text;
return renderHtmlToken(raw);
}
default:
return (token as { raw?: string }).raw ?? "";
}
}
/**
* Render an array of inline tokens into an ANSI-styled string.
*
* Handles paired color tags (`<red>\u2026</red>`) that `marked` emits as
* separate `html` tokens (open, inner tokens, close). Buffers inner
* tokens until the matching close tag, then applies the color function.
*
* Also handles: strong, em, codespan, link, text, br, del, escape.
*/
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: paired color tag buffering
function renderInline(tokens: Token[]): string {
const parts: string[] = [];
let i = 0;
while (i < tokens.length) {
const token = tokens[i] as Token;
// Check for color tag open: <red>, <green>, etc.
if (token.type === "html") {
const raw = (
(token as Tokens.HTML).raw ?? (token as Tokens.HTML).text
).trim();
const openMatch = RE_OPEN_TAG.exec(raw);
if (openMatch) {
const tagName = (openMatch[1] ?? "").toLowerCase();
const colorFn = COLOR_TAGS[tagName];
if (colorFn) {
// Collect inner tokens until matching </tag>
const closeTag = `</${openMatch[1]}>`;
const inner: Token[] = [];
i += 1;
while (i < tokens.length) {
const t = tokens[i] as Token;
if (
t.type === "html" &&
((t as Tokens.HTML).raw ?? (t as Tokens.HTML).text)
.trim()
.toLowerCase() === closeTag.toLowerCase()
) {
i += 1; // consume close tag
break;
}
inner.push(t);
i += 1;
}
parts.push(colorFn(renderInline(inner)));
continue;
}
}
}
parts.push(renderOneInline(token));
i += 1;
}
return parts.join("");
}
// ──────────────────────── Block token rendering ──────────────────────
/**
* Render an array of block-level tokens into ANSI-styled terminal output.
*
* Handles: heading, paragraph, code, blockquote, list, table, hr, space.
*/
function renderBlocks(tokens: Token[]): string {
const parts: string[] = [];
for (const token of tokens) {
switch (token.type) {
case "heading": {
const t = token as Tokens.Heading;
const text = renderInline(t.tokens);
if (t.depth <= 2) {
// h1/h2 → bold cyan with colored divider bar for visual weight
parts.push(chalk.hex(COLORS.cyan).bold(text));
parts.push(
chalk.hex(COLORS.cyan)(
"\u2501".repeat(Math.min(stringWidth(text), 30))
)
);
} else {
// h3+ → bold cyan (less prominent, no divider)
parts.push(chalk.hex(COLORS.cyan).bold(text));
}
parts.push("");
break;
}
case "paragraph": {
const t = token as Tokens.Paragraph;
parts.push(renderInline(t.tokens));
parts.push("");
break;
}
case "code": {
const t = token as Tokens.Code;
const highlighted = highlightCode(t.text, t.lang ?? undefined);
const lines = highlighted.split("\n").map((l) => ` ${l}`);
parts.push(lines.join("\n"));
parts.push("");
break;
}
case "blockquote": {
const t = token as Tokens.Blockquote;
const inner = renderBlocks(t.tokens).trim();
const quoted = inner
.split("\n")
.map((l) => chalk.hex(COLORS.muted).italic(` ${l}`))
.join("\n");
parts.push(quoted);
parts.push("");
break;
}
case "list": {
const t = token as Tokens.List;
parts.push(renderList(t));
parts.push("");
break;
}
case "table": {
const t = token as Tokens.Table;
parts.push(renderTableToken(t));
break;
}
case "hr":
parts.push(muted("\u2500".repeat(40)));
parts.push("");
break;
case "space":
// Intentional blank line — skip
break;
default: {
// Unknown block type — emit raw text as fallback
const raw = (token as { raw?: string }).raw;
if (raw) {
parts.push(raw);
}
}
}
}
return parts.join("\n");
}
/**
* Render a list token (ordered or unordered) with proper indentation.
*/
function renderList(list: Tokens.List, depth = 0): string {
const indent = " ".repeat(depth);
const lines: string[] = [];
for (let i = 0; i < list.items.length; i++) {
const item = list.items[i];
Iif (!item) {
continue;
}
const start = Number(list.start ?? 1);
const bullet = list.ordered ? `${start + i}.` : "•";
const body = item.tokens
.map((t) => {
Iif (t.type === "list") {
return renderList(t as Tokens.List, depth + 1);
}
if (t.type === "text" && "tokens" in t && (t as Tokens.Text).tokens) {
return renderInline((t as Tokens.Text).tokens ?? []);
}
Iif ("tokens" in t && (t as Tokens.Generic).tokens) {
return renderInline(((t as Tokens.Generic).tokens as Token[]) ?? []);
}
return (t as { raw?: string }).raw ?? "";
})
.join("\n");
lines.push(`${indent}${bullet} ${body}`);
}
return lines.join("\n");
}
/**
* Render a markdown table token using the text-table renderer.
*
* Converts marked's `Tokens.Table` into headers + rows + alignments and
* delegates to `renderTextTable()` for column fitting and box drawing.
*/
function renderTableToken(table: Tokens.Table): string {
const headers = table.header.map((cell) => renderInline(cell.tokens));
const rows = table.rows.map((row) =>
row.map((cell) => renderInline(cell.tokens))
);
const alignments: Alignment[] = table.align.map((a) => {
if (a === "right") {
return "right";
}
Iif (a === "center") {
return "center";
}
return "left";
});
return renderTextTable(headers, rows, { alignments });
}
// ──────────────────────── Public API ─────────────────────────────────
/**
* Render a full markdown document as styled terminal output.
*
* In TTY mode: uses `marked.lexer()` to tokenize and a custom block/inline
* renderer for ANSI output. Tables are rendered with Unicode box-drawing
* borders via the text-table module.
*
* In plain mode: parses and renders the same way (preserving structural
* formatting — headings, aligned tables, blockquote indentation, lists),
* then strips ANSI codes. This produces human-readable output rather than
* raw CommonMark source.
*
* @param md - Markdown source text
* @returns Styled terminal string (TTY) or clean plain text (non-TTY / plain mode)
*/
export function renderMarkdown(md: string): string {
if (isPlainOutput()) {
// Parse and render to get structural formatting (headings, tables, lists),
// then strip ANSI codes. This produces human-readable output with aligned
// tables and proper heading emphasis, without ANSI escape sequences.
const tokens = marked.lexer(md);
return stripAnsi(renderBlocks(tokens)).trimEnd();
}
const tokens = marked.lexer(md);
return renderBlocks(tokens).trimEnd();
}
/**
* Render inline markdown (bold, code spans, emphasis, links) as styled
* terminal output, or return plain text when in plain mode.
*
* Both modes use the same `marked.lexer()` → `renderInline()` pipeline.
* In plain mode, ANSI codes are stripped from the result, producing
* clean text. This avoids maintaining a separate regex-based stripping
* function that would need to replicate the markdown parser's rules.
*
* @param md - Inline markdown text
* @returns Styled string (TTY) or plain text (non-TTY / plain mode)
*/
export function renderInlineMarkdown(md: string): string {
const tokens = marked.lexer(md);
const rendered = renderInline(tokens.flatMap(flattenInline));
return isPlainOutput() ? stripAnsi(rendered) : rendered;
}
|