@oh-my-pi/pi-tui 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,655 @@
1
+ import { marked, type Token } from "marked";
2
+ import type { SymbolTheme } from "../symbols";
3
+ import type { Component } from "../tui";
4
+ import { applyBackgroundToLine, visibleWidth, wrapTextWithAnsi } from "../utils";
5
+
6
+ /**
7
+ * Default text styling for markdown content.
8
+ * Applied to all text unless overridden by markdown formatting.
9
+ */
10
+ export interface DefaultTextStyle {
11
+ /** Foreground color function */
12
+ color?: (text: string) => string;
13
+ /** Background color function */
14
+ bgColor?: (text: string) => string;
15
+ /** Bold text */
16
+ bold?: boolean;
17
+ /** Italic text */
18
+ italic?: boolean;
19
+ /** Strikethrough text */
20
+ strikethrough?: boolean;
21
+ /** Underline text */
22
+ underline?: boolean;
23
+ }
24
+
25
+ /**
26
+ * Theme functions for markdown elements.
27
+ * Each function takes text and returns styled text with ANSI codes.
28
+ */
29
+ export interface MarkdownTheme {
30
+ heading: (text: string) => string;
31
+ link: (text: string) => string;
32
+ linkUrl: (text: string) => string;
33
+ code: (text: string) => string;
34
+ codeBlock: (text: string) => string;
35
+ codeBlockBorder: (text: string) => string;
36
+ quote: (text: string) => string;
37
+ quoteBorder: (text: string) => string;
38
+ hr: (text: string) => string;
39
+ listBullet: (text: string) => string;
40
+ bold: (text: string) => string;
41
+ italic: (text: string) => string;
42
+ strikethrough: (text: string) => string;
43
+ underline: (text: string) => string;
44
+ highlightCode?: (code: string, lang?: string) => string[];
45
+ symbols: SymbolTheme;
46
+ }
47
+
48
+ export class Markdown implements Component {
49
+ private text: string;
50
+ private paddingX: number; // Left/right padding
51
+ private paddingY: number; // Top/bottom padding
52
+ private defaultTextStyle?: DefaultTextStyle;
53
+ private theme: MarkdownTheme;
54
+ private defaultStylePrefix?: string;
55
+
56
+ // Cache for rendered output
57
+ private cachedText?: string;
58
+ private cachedWidth?: number;
59
+ private cachedLines?: string[];
60
+
61
+ constructor(
62
+ text: string,
63
+ paddingX: number,
64
+ paddingY: number,
65
+ theme: MarkdownTheme,
66
+ defaultTextStyle?: DefaultTextStyle,
67
+ ) {
68
+ this.text = text;
69
+ this.paddingX = paddingX;
70
+ this.paddingY = paddingY;
71
+ this.theme = theme;
72
+ this.defaultTextStyle = defaultTextStyle;
73
+ }
74
+
75
+ setText(text: string): void {
76
+ this.text = text;
77
+ this.invalidate();
78
+ }
79
+
80
+ invalidate(): void {
81
+ this.cachedText = undefined;
82
+ this.cachedWidth = undefined;
83
+ this.cachedLines = undefined;
84
+ }
85
+
86
+ render(width: number): string[] {
87
+ // Check cache
88
+ if (this.cachedLines && this.cachedText === this.text && this.cachedWidth === width) {
89
+ return this.cachedLines;
90
+ }
91
+
92
+ // Calculate available width for content (subtract horizontal padding)
93
+ const contentWidth = Math.max(1, width - this.paddingX * 2);
94
+
95
+ // Don't render anything if there's no actual text
96
+ if (!this.text || this.text.trim() === "") {
97
+ const result: string[] = [];
98
+ // Update cache
99
+ this.cachedText = this.text;
100
+ this.cachedWidth = width;
101
+ this.cachedLines = result;
102
+ return result;
103
+ }
104
+
105
+ // Replace tabs with 3 spaces for consistent rendering
106
+ const normalizedText = this.text.replace(/\t/g, " ");
107
+
108
+ // Parse markdown to HTML-like tokens
109
+ const tokens = marked.lexer(normalizedText);
110
+
111
+ // Convert tokens to styled terminal output
112
+ const renderedLines: string[] = [];
113
+
114
+ for (let i = 0; i < tokens.length; i++) {
115
+ const token = tokens[i];
116
+ const nextToken = tokens[i + 1];
117
+ const tokenLines = this.renderToken(token, contentWidth, nextToken?.type);
118
+ renderedLines.push(...tokenLines);
119
+ }
120
+
121
+ // Wrap lines (NO padding, NO background yet)
122
+ const wrappedLines: string[] = [];
123
+ for (const line of renderedLines) {
124
+ wrappedLines.push(...wrapTextWithAnsi(line, contentWidth));
125
+ }
126
+
127
+ // Add margins and background to each wrapped line
128
+ const leftMargin = " ".repeat(this.paddingX);
129
+ const rightMargin = " ".repeat(this.paddingX);
130
+ const bgFn = this.defaultTextStyle?.bgColor;
131
+ const contentLines: string[] = [];
132
+
133
+ for (const line of wrappedLines) {
134
+ const lineWithMargins = leftMargin + line + rightMargin;
135
+
136
+ if (bgFn) {
137
+ contentLines.push(applyBackgroundToLine(lineWithMargins, width, bgFn));
138
+ } else {
139
+ // No background - just pad to width
140
+ const visibleLen = visibleWidth(lineWithMargins);
141
+ const paddingNeeded = Math.max(0, width - visibleLen);
142
+ contentLines.push(lineWithMargins + " ".repeat(paddingNeeded));
143
+ }
144
+ }
145
+
146
+ // Add top/bottom padding (empty lines)
147
+ const emptyLine = " ".repeat(width);
148
+ const emptyLines: string[] = [];
149
+ for (let i = 0; i < this.paddingY; i++) {
150
+ const line = bgFn ? applyBackgroundToLine(emptyLine, width, bgFn) : emptyLine;
151
+ emptyLines.push(line);
152
+ }
153
+
154
+ // Combine top padding, content, and bottom padding
155
+ const result = [...emptyLines, ...contentLines, ...emptyLines];
156
+
157
+ // Update cache
158
+ this.cachedText = this.text;
159
+ this.cachedWidth = width;
160
+ this.cachedLines = result;
161
+
162
+ return result.length > 0 ? result : [""];
163
+ }
164
+
165
+ /**
166
+ * Apply default text style to a string.
167
+ * This is the base styling applied to all text content.
168
+ * NOTE: Background color is NOT applied here - it's applied at the padding stage
169
+ * to ensure it extends to the full line width.
170
+ */
171
+ private applyDefaultStyle(text: string): string {
172
+ if (!this.defaultTextStyle) {
173
+ return text;
174
+ }
175
+
176
+ let styled = text;
177
+
178
+ // Apply foreground color (NOT background - that's applied at padding stage)
179
+ if (this.defaultTextStyle.color) {
180
+ styled = this.defaultTextStyle.color(styled);
181
+ }
182
+
183
+ // Apply text decorations using this.theme
184
+ if (this.defaultTextStyle.bold) {
185
+ styled = this.theme.bold(styled);
186
+ }
187
+ if (this.defaultTextStyle.italic) {
188
+ styled = this.theme.italic(styled);
189
+ }
190
+ if (this.defaultTextStyle.strikethrough) {
191
+ styled = this.theme.strikethrough(styled);
192
+ }
193
+ if (this.defaultTextStyle.underline) {
194
+ styled = this.theme.underline(styled);
195
+ }
196
+
197
+ return styled;
198
+ }
199
+
200
+ private getDefaultStylePrefix(): string {
201
+ if (!this.defaultTextStyle) {
202
+ return "";
203
+ }
204
+
205
+ if (this.defaultStylePrefix !== undefined) {
206
+ return this.defaultStylePrefix;
207
+ }
208
+
209
+ const sentinel = "\u0000";
210
+ let styled = sentinel;
211
+
212
+ if (this.defaultTextStyle.color) {
213
+ styled = this.defaultTextStyle.color(styled);
214
+ }
215
+
216
+ if (this.defaultTextStyle.bold) {
217
+ styled = this.theme.bold(styled);
218
+ }
219
+ if (this.defaultTextStyle.italic) {
220
+ styled = this.theme.italic(styled);
221
+ }
222
+ if (this.defaultTextStyle.strikethrough) {
223
+ styled = this.theme.strikethrough(styled);
224
+ }
225
+ if (this.defaultTextStyle.underline) {
226
+ styled = this.theme.underline(styled);
227
+ }
228
+
229
+ const sentinelIndex = styled.indexOf(sentinel);
230
+ this.defaultStylePrefix = sentinelIndex >= 0 ? styled.slice(0, sentinelIndex) : "";
231
+ return this.defaultStylePrefix;
232
+ }
233
+
234
+ private renderToken(token: Token, width: number, nextTokenType?: string): string[] {
235
+ const lines: string[] = [];
236
+
237
+ switch (token.type) {
238
+ case "heading": {
239
+ const headingLevel = token.depth;
240
+ const headingPrefix = `${"#".repeat(headingLevel)} `;
241
+ const headingText = this.renderInlineTokens(token.tokens || []);
242
+ let styledHeading: string;
243
+ if (headingLevel === 1) {
244
+ styledHeading = this.theme.heading(this.theme.bold(this.theme.underline(headingText)));
245
+ } else if (headingLevel === 2) {
246
+ styledHeading = this.theme.heading(this.theme.bold(headingText));
247
+ } else {
248
+ styledHeading = this.theme.heading(this.theme.bold(headingPrefix + headingText));
249
+ }
250
+ lines.push(styledHeading);
251
+ if (nextTokenType !== "space") {
252
+ lines.push(""); // Add spacing after headings (unless space token follows)
253
+ }
254
+ break;
255
+ }
256
+
257
+ case "paragraph": {
258
+ const paragraphText = this.renderInlineTokens(token.tokens || []);
259
+ lines.push(paragraphText);
260
+ // Don't add spacing if next token is space or list
261
+ if (nextTokenType && nextTokenType !== "list" && nextTokenType !== "space") {
262
+ lines.push("");
263
+ }
264
+ break;
265
+ }
266
+
267
+ case "code": {
268
+ lines.push(this.theme.codeBlockBorder(`\`\`\`${token.lang || ""}`));
269
+ if (this.theme.highlightCode) {
270
+ const highlightedLines = this.theme.highlightCode(token.text, token.lang);
271
+ for (const hlLine of highlightedLines) {
272
+ lines.push(` ${hlLine}`);
273
+ }
274
+ } else {
275
+ // Split code by newlines and style each line
276
+ const codeLines = token.text.split("\n");
277
+ for (const codeLine of codeLines) {
278
+ lines.push(` ${this.theme.codeBlock(codeLine)}`);
279
+ }
280
+ }
281
+ lines.push(this.theme.codeBlockBorder("```"));
282
+ if (nextTokenType !== "space") {
283
+ lines.push(""); // Add spacing after code blocks (unless space token follows)
284
+ }
285
+ break;
286
+ }
287
+
288
+ case "list": {
289
+ const listLines = this.renderList(token as any, 0);
290
+ lines.push(...listLines);
291
+ // Don't add spacing after lists if a space token follows
292
+ // (the space token will handle it)
293
+ break;
294
+ }
295
+
296
+ case "table": {
297
+ const tableLines = this.renderTable(token as any, width);
298
+ lines.push(...tableLines);
299
+ break;
300
+ }
301
+
302
+ case "blockquote": {
303
+ const quoteText = this.renderInlineTokens(token.tokens || []);
304
+ const quoteLines = quoteText.split("\n");
305
+ for (const quoteLine of quoteLines) {
306
+ lines.push(
307
+ this.theme.quoteBorder(`${this.theme.symbols.quoteBorder} `) +
308
+ this.theme.quote(this.theme.italic(quoteLine)),
309
+ );
310
+ }
311
+ if (nextTokenType !== "space") {
312
+ lines.push(""); // Add spacing after blockquotes (unless space token follows)
313
+ }
314
+ break;
315
+ }
316
+
317
+ case "hr":
318
+ lines.push(this.theme.hr(this.theme.symbols.hrChar.repeat(Math.min(width, 80))));
319
+ if (nextTokenType !== "space") {
320
+ lines.push(""); // Add spacing after horizontal rules (unless space token follows)
321
+ }
322
+ break;
323
+
324
+ case "html":
325
+ // Render HTML as plain text (escaped for terminal)
326
+ if ("raw" in token && typeof token.raw === "string") {
327
+ lines.push(this.applyDefaultStyle(token.raw.trim()));
328
+ }
329
+ break;
330
+
331
+ case "space":
332
+ // Space tokens represent blank lines in markdown
333
+ lines.push("");
334
+ break;
335
+
336
+ default:
337
+ // Handle any other token types as plain text
338
+ if ("text" in token && typeof token.text === "string") {
339
+ lines.push(token.text);
340
+ }
341
+ }
342
+
343
+ return lines;
344
+ }
345
+
346
+ private renderInlineTokens(tokens: Token[]): string {
347
+ let result = "";
348
+
349
+ for (const token of tokens) {
350
+ switch (token.type) {
351
+ case "text":
352
+ // Text tokens in list items can have nested tokens for inline formatting
353
+ if (token.tokens && token.tokens.length > 0) {
354
+ result += this.renderInlineTokens(token.tokens);
355
+ } else {
356
+ // Apply default style to plain text
357
+ result += this.applyDefaultStyle(token.text);
358
+ }
359
+ break;
360
+
361
+ case "strong": {
362
+ // Apply bold, then reapply default style after
363
+ const boldContent = this.renderInlineTokens(token.tokens || []);
364
+ result += this.theme.bold(boldContent) + this.getDefaultStylePrefix();
365
+ break;
366
+ }
367
+
368
+ case "em": {
369
+ // Apply italic, then reapply default style after
370
+ const italicContent = this.renderInlineTokens(token.tokens || []);
371
+ result += this.theme.italic(italicContent) + this.getDefaultStylePrefix();
372
+ break;
373
+ }
374
+
375
+ case "codespan":
376
+ // Apply code styling without backticks
377
+ result += this.theme.code(token.text) + this.getDefaultStylePrefix();
378
+ break;
379
+
380
+ case "link": {
381
+ const linkText = this.renderInlineTokens(token.tokens || []);
382
+ // If link text matches href, only show the link once
383
+ // Compare raw text (token.text) not styled text (linkText) since linkText has ANSI codes
384
+ if (token.text === token.href) {
385
+ result += this.theme.link(this.theme.underline(linkText)) + this.getDefaultStylePrefix();
386
+ } else {
387
+ result +=
388
+ this.theme.link(this.theme.underline(linkText)) +
389
+ this.theme.linkUrl(` (${token.href})`) +
390
+ this.getDefaultStylePrefix();
391
+ }
392
+ break;
393
+ }
394
+
395
+ case "br":
396
+ result += "\n";
397
+ break;
398
+
399
+ case "del": {
400
+ const delContent = this.renderInlineTokens(token.tokens || []);
401
+ result += this.theme.strikethrough(delContent) + this.getDefaultStylePrefix();
402
+ break;
403
+ }
404
+
405
+ case "html":
406
+ // Render inline HTML as plain text
407
+ if ("raw" in token && typeof token.raw === "string") {
408
+ result += this.applyDefaultStyle(token.raw);
409
+ }
410
+ break;
411
+
412
+ default:
413
+ // Handle any other inline token types as plain text
414
+ if ("text" in token && typeof token.text === "string") {
415
+ result += this.applyDefaultStyle(token.text);
416
+ }
417
+ }
418
+ }
419
+
420
+ return result;
421
+ }
422
+
423
+ /**
424
+ * Render a list with proper nesting support
425
+ */
426
+ private renderList(token: Token & { items: any[]; ordered: boolean }, depth: number): string[] {
427
+ const lines: string[] = [];
428
+ const indent = " ".repeat(depth);
429
+
430
+ for (let i = 0; i < token.items.length; i++) {
431
+ const item = token.items[i];
432
+ const bullet = token.ordered ? `${i + 1}. ` : "- ";
433
+
434
+ // Process item tokens to handle nested lists
435
+ const itemLines = this.renderListItem(item.tokens || [], depth);
436
+
437
+ if (itemLines.length > 0) {
438
+ // First line - check if it's a nested list
439
+ // A nested list will start with indent (spaces) followed by cyan bullet
440
+ const firstLine = itemLines[0];
441
+ const isNestedList = /^\s+\x1b\[36m[-\d]/.test(firstLine); // starts with spaces + cyan + bullet char
442
+
443
+ if (isNestedList) {
444
+ // This is a nested list, just add it as-is (already has full indent)
445
+ lines.push(firstLine);
446
+ } else {
447
+ // Regular text content - add indent and bullet
448
+ lines.push(indent + this.theme.listBullet(bullet) + firstLine);
449
+ }
450
+
451
+ // Rest of the lines
452
+ for (let j = 1; j < itemLines.length; j++) {
453
+ const line = itemLines[j];
454
+ const isNestedListLine = /^\s+\x1b\[36m[-\d]/.test(line); // starts with spaces + cyan + bullet char
455
+
456
+ if (isNestedListLine) {
457
+ // Nested list line - already has full indent
458
+ lines.push(line);
459
+ } else {
460
+ // Regular content - add parent indent + 2 spaces for continuation
461
+ lines.push(`${indent} ${line}`);
462
+ }
463
+ }
464
+ } else {
465
+ lines.push(indent + this.theme.listBullet(bullet));
466
+ }
467
+ }
468
+
469
+ return lines;
470
+ }
471
+
472
+ /**
473
+ * Render list item tokens, handling nested lists
474
+ * Returns lines WITHOUT the parent indent (renderList will add it)
475
+ */
476
+ private renderListItem(tokens: Token[], parentDepth: number): string[] {
477
+ const lines: string[] = [];
478
+
479
+ for (const token of tokens) {
480
+ if (token.type === "list") {
481
+ // Nested list - render with one additional indent level
482
+ // These lines will have their own indent, so we just add them as-is
483
+ const nestedLines = this.renderList(token as any, parentDepth + 1);
484
+ lines.push(...nestedLines);
485
+ } else if (token.type === "text") {
486
+ // Text content (may have inline tokens)
487
+ const text =
488
+ token.tokens && token.tokens.length > 0 ? this.renderInlineTokens(token.tokens) : token.text || "";
489
+ lines.push(text);
490
+ } else if (token.type === "paragraph") {
491
+ // Paragraph in list item
492
+ const text = this.renderInlineTokens(token.tokens || []);
493
+ lines.push(text);
494
+ } else if (token.type === "code") {
495
+ // Code block in list item
496
+ lines.push(this.theme.codeBlockBorder(`\`\`\`${token.lang || ""}`));
497
+ if (this.theme.highlightCode) {
498
+ const highlightedLines = this.theme.highlightCode(token.text, token.lang);
499
+ for (const hlLine of highlightedLines) {
500
+ lines.push(` ${hlLine}`);
501
+ }
502
+ } else {
503
+ const codeLines = token.text.split("\n");
504
+ for (const codeLine of codeLines) {
505
+ lines.push(` ${this.theme.codeBlock(codeLine)}`);
506
+ }
507
+ }
508
+ lines.push(this.theme.codeBlockBorder("```"));
509
+ } else {
510
+ // Other token types - try to render as inline
511
+ const text = this.renderInlineTokens([token]);
512
+ if (text) {
513
+ lines.push(text);
514
+ }
515
+ }
516
+ }
517
+
518
+ return lines;
519
+ }
520
+
521
+ /**
522
+ * Wrap a table cell to fit into a column.
523
+ *
524
+ * Delegates to wrapTextWithAnsi() so ANSI codes + long tokens are handled
525
+ * consistently with the rest of the renderer.
526
+ */
527
+ private wrapCellText(text: string, maxWidth: number): string[] {
528
+ return wrapTextWithAnsi(text, Math.max(1, maxWidth));
529
+ }
530
+
531
+ /**
532
+ * Render a table with width-aware cell wrapping.
533
+ * Cells that don't fit are wrapped to multiple lines.
534
+ */
535
+ private renderTable(
536
+ token: Token & { header: any[]; rows: any[][]; raw?: string },
537
+ availableWidth: number,
538
+ ): string[] {
539
+ const lines: string[] = [];
540
+ const numCols = token.header.length;
541
+
542
+ if (numCols === 0) {
543
+ return lines;
544
+ }
545
+
546
+ // Calculate border overhead: "│ " + (n-1) * " │ " + " │"
547
+ // = 2 + (n-1) * 3 + 2 = 3n + 1
548
+ const borderOverhead = 3 * numCols + 1;
549
+
550
+ // Minimum width for a bordered table with at least 1 char per column.
551
+ const minTableWidth = borderOverhead + numCols;
552
+ if (availableWidth < minTableWidth) {
553
+ // Too narrow to render a stable table. Fall back to raw markdown.
554
+ const fallbackLines = token.raw ? wrapTextWithAnsi(token.raw, availableWidth) : [];
555
+ fallbackLines.push("");
556
+ return fallbackLines;
557
+ }
558
+
559
+ // Calculate natural column widths (what each column needs without constraints)
560
+ const naturalWidths: number[] = [];
561
+ for (let i = 0; i < numCols; i++) {
562
+ const headerText = this.renderInlineTokens(token.header[i].tokens || []);
563
+ naturalWidths[i] = visibleWidth(headerText);
564
+ }
565
+ for (const row of token.rows) {
566
+ for (let i = 0; i < row.length; i++) {
567
+ const cellText = this.renderInlineTokens(row[i].tokens || []);
568
+ naturalWidths[i] = Math.max(naturalWidths[i] || 0, visibleWidth(cellText));
569
+ }
570
+ }
571
+
572
+ // Calculate column widths that fit within available width
573
+ const totalNaturalWidth = naturalWidths.reduce((a, b) => a + b, 0) + borderOverhead;
574
+ let columnWidths: number[];
575
+
576
+ if (totalNaturalWidth <= availableWidth) {
577
+ // Everything fits naturally
578
+ columnWidths = naturalWidths;
579
+ } else {
580
+ // Need to shrink columns to fit
581
+ const availableForCells = availableWidth - borderOverhead;
582
+ if (availableForCells <= numCols) {
583
+ // Extremely narrow - give each column at least 1 char
584
+ columnWidths = naturalWidths.map(() => Math.max(1, Math.floor(availableForCells / numCols)));
585
+ } else {
586
+ // Distribute space proportionally based on natural widths
587
+ const totalNatural = naturalWidths.reduce((a, b) => a + b, 0);
588
+ columnWidths = naturalWidths.map((w) => {
589
+ const proportion = w / totalNatural;
590
+ return Math.max(1, Math.floor(proportion * availableForCells));
591
+ });
592
+
593
+ // Adjust for rounding errors - distribute remaining space
594
+ const allocated = columnWidths.reduce((a, b) => a + b, 0);
595
+ let remaining = availableForCells - allocated;
596
+ for (let i = 0; remaining > 0 && i < numCols; i++) {
597
+ columnWidths[i]++;
598
+ remaining--;
599
+ }
600
+ }
601
+ }
602
+
603
+ const t = this.theme.symbols.table;
604
+ const h = t.horizontal;
605
+ const v = t.vertical;
606
+
607
+ // Render top border
608
+ const topBorderCells = columnWidths.map((w) => h.repeat(w));
609
+ lines.push(`${t.topLeft}${h}${topBorderCells.join(`${h}${t.teeDown}${h}`)}${h}${t.topRight}`);
610
+
611
+ // Render header with wrapping
612
+ const headerCellLines: string[][] = token.header.map((cell, i) => {
613
+ const text = this.renderInlineTokens(cell.tokens || []);
614
+ return this.wrapCellText(text, columnWidths[i]);
615
+ });
616
+ const headerLineCount = Math.max(...headerCellLines.map((c) => c.length));
617
+
618
+ for (let lineIdx = 0; lineIdx < headerLineCount; lineIdx++) {
619
+ const rowParts = headerCellLines.map((cellLines, colIdx) => {
620
+ const text = cellLines[lineIdx] || "";
621
+ const padded = text + " ".repeat(Math.max(0, columnWidths[colIdx] - visibleWidth(text)));
622
+ return this.theme.bold(padded);
623
+ });
624
+ lines.push(`${v} ${rowParts.join(` ${v} `)} ${v}`);
625
+ }
626
+
627
+ // Render separator
628
+ const separatorCells = columnWidths.map((w) => h.repeat(w));
629
+ lines.push(`${t.teeRight}${h}${separatorCells.join(`${h}${t.cross}${h}`)}${h}${t.teeLeft}`);
630
+
631
+ // Render rows with wrapping
632
+ for (const row of token.rows) {
633
+ const rowCellLines: string[][] = row.map((cell, i) => {
634
+ const text = this.renderInlineTokens(cell.tokens || []);
635
+ return this.wrapCellText(text, columnWidths[i]);
636
+ });
637
+ const rowLineCount = Math.max(...rowCellLines.map((c) => c.length));
638
+
639
+ for (let lineIdx = 0; lineIdx < rowLineCount; lineIdx++) {
640
+ const rowParts = rowCellLines.map((cellLines, colIdx) => {
641
+ const text = cellLines[lineIdx] || "";
642
+ return text + " ".repeat(Math.max(0, columnWidths[colIdx] - visibleWidth(text)));
643
+ });
644
+ lines.push(`${v} ${rowParts.join(` ${v} `)} ${v}`);
645
+ }
646
+ }
647
+
648
+ // Render bottom border
649
+ const bottomBorderCells = columnWidths.map((w) => h.repeat(w));
650
+ lines.push(`${t.bottomLeft}${h}${bottomBorderCells.join(`${h}${t.teeUp}${h}`)}${h}${t.bottomRight}`);
651
+
652
+ lines.push(""); // Add spacing after table
653
+ return lines;
654
+ }
655
+ }