@f5xc-salesdemos/pi-tui 14.0.2
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.
- package/CHANGELOG.md +682 -0
- package/README.md +704 -0
- package/package.json +71 -0
- package/src/autocomplete.ts +787 -0
- package/src/bracketed-paste.ts +47 -0
- package/src/components/box.ts +144 -0
- package/src/components/cancellable-loader.ts +40 -0
- package/src/components/editor.ts +2563 -0
- package/src/components/image.ts +90 -0
- package/src/components/input.ts +439 -0
- package/src/components/loader.ts +67 -0
- package/src/components/markdown.ts +914 -0
- package/src/components/select-list.ts +249 -0
- package/src/components/settings-list.ts +195 -0
- package/src/components/spacer.ts +28 -0
- package/src/components/tab-bar.ts +175 -0
- package/src/components/text.ts +110 -0
- package/src/components/truncated-text.ts +61 -0
- package/src/editor-component.ts +71 -0
- package/src/fuzzy.ts +143 -0
- package/src/index.ts +39 -0
- package/src/keybindings.ts +279 -0
- package/src/keys.ts +404 -0
- package/src/kill-ring.ts +46 -0
- package/src/stdin-buffer.ts +385 -0
- package/src/symbols.ts +24 -0
- package/src/terminal-capabilities.ts +525 -0
- package/src/terminal.ts +630 -0
- package/src/ttyid.ts +66 -0
- package/src/tui.ts +1328 -0
- package/src/utils.ts +301 -0
|
@@ -0,0 +1,914 @@
|
|
|
1
|
+
import { marked, type Token, type Tokens } from "marked";
|
|
2
|
+
import type { SymbolTheme } from "../symbols";
|
|
3
|
+
import { TERMINAL } from "../terminal-capabilities";
|
|
4
|
+
import type { Component } from "../tui";
|
|
5
|
+
import { applyBackgroundToLine, padding, replaceTabs, visibleWidth, wrapTextWithAnsi } from "../utils";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Default text styling for markdown content.
|
|
9
|
+
* Applied to all text unless overridden by markdown formatting.
|
|
10
|
+
*/
|
|
11
|
+
export interface DefaultTextStyle {
|
|
12
|
+
/** Foreground color function */
|
|
13
|
+
color?: (text: string) => string;
|
|
14
|
+
/** Background color function */
|
|
15
|
+
bgColor?: (text: string) => string;
|
|
16
|
+
/** Bold text */
|
|
17
|
+
bold?: boolean;
|
|
18
|
+
/** Italic text */
|
|
19
|
+
italic?: boolean;
|
|
20
|
+
/** Strikethrough text */
|
|
21
|
+
strikethrough?: boolean;
|
|
22
|
+
/** Underline text */
|
|
23
|
+
underline?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Theme functions for markdown elements.
|
|
28
|
+
* Each function takes text and returns styled text with ANSI codes.
|
|
29
|
+
*/
|
|
30
|
+
export interface MarkdownTheme {
|
|
31
|
+
heading: (text: string) => string;
|
|
32
|
+
link: (text: string) => string;
|
|
33
|
+
linkUrl: (text: string) => string;
|
|
34
|
+
code: (text: string) => string;
|
|
35
|
+
codeBlock: (text: string) => string;
|
|
36
|
+
codeBlockBorder: (text: string) => string;
|
|
37
|
+
quote: (text: string) => string;
|
|
38
|
+
quoteBorder: (text: string) => string;
|
|
39
|
+
hr: (text: string) => string;
|
|
40
|
+
listBullet: (text: string) => string;
|
|
41
|
+
bold: (text: string) => string;
|
|
42
|
+
italic: (text: string) => string;
|
|
43
|
+
strikethrough: (text: string) => string;
|
|
44
|
+
underline: (text: string) => string;
|
|
45
|
+
highlightCode?: (code: string, lang?: string) => string[];
|
|
46
|
+
/**
|
|
47
|
+
* Lookup a pre-rendered mermaid ASCII rendering by source hash.
|
|
48
|
+
* Hash is computed as `Bun.hash.xxHash64(source.trim())`.
|
|
49
|
+
* Return null to fall back to fenced code rendering.
|
|
50
|
+
*/
|
|
51
|
+
getMermaidAscii?: (sourceHash: bigint) => string | null;
|
|
52
|
+
symbols: SymbolTheme;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface InlineStyleContext {
|
|
56
|
+
applyText: (text: string) => string;
|
|
57
|
+
stylePrefix: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type ListToken = Token & { items: Array<{ tokens?: Token[] }>; ordered: boolean; start?: number };
|
|
61
|
+
type TableCellToken = { tokens?: Token[] };
|
|
62
|
+
type TableToken = Token & { header: TableCellToken[]; rows: TableCellToken[][]; raw?: string };
|
|
63
|
+
|
|
64
|
+
function formatHyperlink(text: string, target: string): string {
|
|
65
|
+
if (!TERMINAL.hyperlinks || !target) {
|
|
66
|
+
return text;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const safeTarget = target.replaceAll("\x1b", "").replaceAll("\x07", "");
|
|
70
|
+
if (!safeTarget) {
|
|
71
|
+
return text;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return `\x1b]8;;${safeTarget}\x07${text}\x1b]8;;\x07`;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export class Markdown implements Component {
|
|
78
|
+
#text: string;
|
|
79
|
+
#paddingX: number; // Left/right padding
|
|
80
|
+
#paddingY: number; // Top/bottom padding
|
|
81
|
+
#defaultTextStyle?: DefaultTextStyle;
|
|
82
|
+
#theme: MarkdownTheme;
|
|
83
|
+
#defaultStylePrefix?: string;
|
|
84
|
+
/** Number of spaces used to indent code block content. */
|
|
85
|
+
#codeBlockIndent: number;
|
|
86
|
+
|
|
87
|
+
// Cache for rendered output
|
|
88
|
+
#cachedText?: string;
|
|
89
|
+
#cachedWidth?: number;
|
|
90
|
+
#cachedLines?: string[];
|
|
91
|
+
|
|
92
|
+
constructor(
|
|
93
|
+
text: string,
|
|
94
|
+
paddingX: number,
|
|
95
|
+
paddingY: number,
|
|
96
|
+
theme: MarkdownTheme,
|
|
97
|
+
defaultTextStyle?: DefaultTextStyle,
|
|
98
|
+
codeBlockIndent: number = 2,
|
|
99
|
+
) {
|
|
100
|
+
this.#text = text;
|
|
101
|
+
this.#paddingX = paddingX;
|
|
102
|
+
this.#paddingY = paddingY;
|
|
103
|
+
this.#theme = theme;
|
|
104
|
+
this.#defaultTextStyle = defaultTextStyle;
|
|
105
|
+
this.#codeBlockIndent = Math.max(0, Math.floor(codeBlockIndent));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
setText(text: string): void {
|
|
109
|
+
this.#text = text;
|
|
110
|
+
this.invalidate();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
invalidate(): void {
|
|
114
|
+
this.#cachedText = undefined;
|
|
115
|
+
this.#cachedWidth = undefined;
|
|
116
|
+
this.#cachedLines = undefined;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
render(width: number): string[] {
|
|
120
|
+
// Check cache
|
|
121
|
+
if (this.#cachedLines && this.#cachedText === this.#text && this.#cachedWidth === width) {
|
|
122
|
+
return this.#cachedLines;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Calculate available width for content (subtract horizontal padding)
|
|
126
|
+
const contentWidth = Math.max(1, width - this.#paddingX * 2);
|
|
127
|
+
|
|
128
|
+
// Don't render anything if there's no actual text
|
|
129
|
+
if (!this.#text || this.#text.trim() === "") {
|
|
130
|
+
const result: string[] = [];
|
|
131
|
+
// Update cache
|
|
132
|
+
this.#cachedText = this.#text;
|
|
133
|
+
this.#cachedWidth = width;
|
|
134
|
+
this.#cachedLines = result;
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Replace tabs with 3 spaces for consistent rendering
|
|
139
|
+
const normalizedText = replaceTabs(this.#text);
|
|
140
|
+
|
|
141
|
+
// Parse markdown to HTML-like tokens
|
|
142
|
+
const tokens = marked.lexer(normalizedText);
|
|
143
|
+
|
|
144
|
+
// Convert tokens to styled terminal output
|
|
145
|
+
const renderedLines: string[] = [];
|
|
146
|
+
|
|
147
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
148
|
+
const token = tokens[i];
|
|
149
|
+
const nextToken = tokens[i + 1];
|
|
150
|
+
const tokenLines = this.#renderToken(token, contentWidth, nextToken?.type);
|
|
151
|
+
renderedLines.push(...tokenLines);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Wrap lines (NO padding, NO background yet)
|
|
155
|
+
const wrappedLines: string[] = [];
|
|
156
|
+
for (const line of renderedLines) {
|
|
157
|
+
// Skip wrapping for image protocol lines (would corrupt escape sequences)
|
|
158
|
+
if (TERMINAL.isImageLine(line)) {
|
|
159
|
+
wrappedLines.push(line);
|
|
160
|
+
} else {
|
|
161
|
+
wrappedLines.push(...wrapTextWithAnsi(line, contentWidth));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Add margins and background to each wrapped line
|
|
166
|
+
const leftMargin = padding(this.#paddingX);
|
|
167
|
+
const rightMargin = padding(this.#paddingX);
|
|
168
|
+
const bgFn = this.#defaultTextStyle?.bgColor;
|
|
169
|
+
const contentLines: string[] = [];
|
|
170
|
+
|
|
171
|
+
for (const line of wrappedLines) {
|
|
172
|
+
// Image lines must be output raw - no margins or background
|
|
173
|
+
if (TERMINAL.isImageLine(line)) {
|
|
174
|
+
contentLines.push(line);
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const lineWithMargins = leftMargin + line + rightMargin;
|
|
179
|
+
|
|
180
|
+
if (bgFn) {
|
|
181
|
+
contentLines.push(applyBackgroundToLine(lineWithMargins, width, bgFn));
|
|
182
|
+
} else {
|
|
183
|
+
// No background - just pad to width
|
|
184
|
+
const visibleLen = visibleWidth(lineWithMargins);
|
|
185
|
+
const paddingNeeded = Math.max(0, width - visibleLen);
|
|
186
|
+
contentLines.push(lineWithMargins + padding(paddingNeeded));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Add top/bottom padding (empty lines)
|
|
191
|
+
const emptyLine = padding(width);
|
|
192
|
+
const emptyLines: string[] = [];
|
|
193
|
+
for (let i = 0; i < this.#paddingY; i++) {
|
|
194
|
+
const line = bgFn ? applyBackgroundToLine(emptyLine, width, bgFn) : emptyLine;
|
|
195
|
+
emptyLines.push(line);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Combine top padding, content, and bottom padding
|
|
199
|
+
const result = [...emptyLines, ...contentLines, ...emptyLines];
|
|
200
|
+
|
|
201
|
+
// Update cache
|
|
202
|
+
this.#cachedText = this.#text;
|
|
203
|
+
this.#cachedWidth = width;
|
|
204
|
+
this.#cachedLines = result;
|
|
205
|
+
|
|
206
|
+
return result.length > 0 ? result : [""];
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Apply default text style to a string.
|
|
211
|
+
* This is the base styling applied to all text content.
|
|
212
|
+
* NOTE: Background color is NOT applied here - it's applied at the padding stage
|
|
213
|
+
* to ensure it extends to the full line width.
|
|
214
|
+
*/
|
|
215
|
+
#applyDefaultStyle(text: string): string {
|
|
216
|
+
if (!this.#defaultTextStyle) {
|
|
217
|
+
return text;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
let styled = text;
|
|
221
|
+
|
|
222
|
+
// Apply foreground color (NOT background - that's applied at padding stage)
|
|
223
|
+
if (this.#defaultTextStyle.color) {
|
|
224
|
+
styled = this.#defaultTextStyle.color(styled);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Apply text decorations using this.#theme
|
|
228
|
+
if (this.#defaultTextStyle.bold) {
|
|
229
|
+
styled = this.#theme.bold(styled);
|
|
230
|
+
}
|
|
231
|
+
if (this.#defaultTextStyle.italic) {
|
|
232
|
+
styled = this.#theme.italic(styled);
|
|
233
|
+
}
|
|
234
|
+
if (this.#defaultTextStyle.strikethrough) {
|
|
235
|
+
styled = this.#theme.strikethrough(styled);
|
|
236
|
+
}
|
|
237
|
+
if (this.#defaultTextStyle.underline) {
|
|
238
|
+
styled = this.#theme.underline(styled);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return styled;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
#getDefaultStylePrefix(): string {
|
|
245
|
+
if (!this.#defaultTextStyle) {
|
|
246
|
+
return "";
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (this.#defaultStylePrefix !== undefined) {
|
|
250
|
+
return this.#defaultStylePrefix;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const sentinel = "\u0000";
|
|
254
|
+
let styled = sentinel;
|
|
255
|
+
|
|
256
|
+
if (this.#defaultTextStyle.color) {
|
|
257
|
+
styled = this.#defaultTextStyle.color(styled);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (this.#defaultTextStyle.bold) {
|
|
261
|
+
styled = this.#theme.bold(styled);
|
|
262
|
+
}
|
|
263
|
+
if (this.#defaultTextStyle.italic) {
|
|
264
|
+
styled = this.#theme.italic(styled);
|
|
265
|
+
}
|
|
266
|
+
if (this.#defaultTextStyle.strikethrough) {
|
|
267
|
+
styled = this.#theme.strikethrough(styled);
|
|
268
|
+
}
|
|
269
|
+
if (this.#defaultTextStyle.underline) {
|
|
270
|
+
styled = this.#theme.underline(styled);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const sentinelIndex = styled.indexOf(sentinel);
|
|
274
|
+
this.#defaultStylePrefix = sentinelIndex >= 0 ? styled.slice(0, sentinelIndex) : "";
|
|
275
|
+
return this.#defaultStylePrefix;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
#getStylePrefix(styleFn: (text: string) => string): string {
|
|
279
|
+
const sentinel = "\u0000";
|
|
280
|
+
const styled = styleFn(sentinel);
|
|
281
|
+
const sentinelIndex = styled.indexOf(sentinel);
|
|
282
|
+
return sentinelIndex >= 0 ? styled.slice(0, sentinelIndex) : "";
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
#getDefaultInlineStyleContext(): InlineStyleContext {
|
|
286
|
+
return {
|
|
287
|
+
applyText: (text: string) => this.#applyDefaultStyle(text),
|
|
288
|
+
stylePrefix: this.#getDefaultStylePrefix(),
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
#renderToken(token: Token, width: number, nextTokenType?: string, styleContext?: InlineStyleContext): string[] {
|
|
293
|
+
const lines: string[] = [];
|
|
294
|
+
|
|
295
|
+
switch (token.type) {
|
|
296
|
+
case "heading": {
|
|
297
|
+
const headingLevel = token.depth;
|
|
298
|
+
const headingPrefix = `${"#".repeat(headingLevel)} `;
|
|
299
|
+
const headingText = this.#renderInlineTokens(token.tokens || [], styleContext);
|
|
300
|
+
let styledHeading: string;
|
|
301
|
+
if (headingLevel === 1) {
|
|
302
|
+
styledHeading = this.#theme.heading(this.#theme.bold(this.#theme.underline(headingText)));
|
|
303
|
+
} else if (headingLevel === 2) {
|
|
304
|
+
styledHeading = this.#theme.heading(this.#theme.bold(headingText));
|
|
305
|
+
} else {
|
|
306
|
+
styledHeading = this.#theme.heading(this.#theme.bold(headingPrefix + headingText));
|
|
307
|
+
}
|
|
308
|
+
lines.push(styledHeading);
|
|
309
|
+
if (nextTokenType && nextTokenType !== "space") {
|
|
310
|
+
lines.push(""); // Add spacing after headings (unless space token follows)
|
|
311
|
+
}
|
|
312
|
+
break;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
case "paragraph": {
|
|
316
|
+
const paragraphText = this.#renderInlineTokens(token.tokens || [], styleContext);
|
|
317
|
+
lines.push(paragraphText);
|
|
318
|
+
// Don't add spacing if next token is space or list
|
|
319
|
+
if (nextTokenType && nextTokenType !== "list" && nextTokenType !== "space") {
|
|
320
|
+
lines.push("");
|
|
321
|
+
}
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
case "code": {
|
|
326
|
+
// Handle mermaid diagrams with ASCII rendering when available
|
|
327
|
+
if (token.lang === "mermaid" && this.#theme.getMermaidAscii) {
|
|
328
|
+
const hash = Bun.hash.xxHash64(token.text.trim());
|
|
329
|
+
const ascii = this.#theme.getMermaidAscii(hash);
|
|
330
|
+
|
|
331
|
+
if (ascii) {
|
|
332
|
+
for (const asciiLine of Bun.stripANSI(ascii).split("\n")) {
|
|
333
|
+
lines.push(asciiLine);
|
|
334
|
+
}
|
|
335
|
+
if (nextTokenType && nextTokenType !== "space") {
|
|
336
|
+
lines.push("");
|
|
337
|
+
}
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const codeIndent = padding(this.#codeBlockIndent);
|
|
343
|
+
lines.push(this.#theme.codeBlockBorder(`\`\`\`${token.lang || ""}`));
|
|
344
|
+
if (this.#theme.highlightCode) {
|
|
345
|
+
const highlightedLines = this.#theme.highlightCode(token.text, token.lang);
|
|
346
|
+
for (const hlLine of highlightedLines) {
|
|
347
|
+
lines.push(`${codeIndent}${hlLine}`);
|
|
348
|
+
}
|
|
349
|
+
} else {
|
|
350
|
+
// Split code by newlines and style each line
|
|
351
|
+
const codeLines = token.text.split("\n");
|
|
352
|
+
for (const codeLine of codeLines) {
|
|
353
|
+
lines.push(`${codeIndent}${this.#theme.codeBlock(codeLine)}`);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
lines.push(this.#theme.codeBlockBorder("```"));
|
|
357
|
+
if (nextTokenType && nextTokenType !== "space") {
|
|
358
|
+
lines.push(""); // Add spacing after code blocks (unless space token follows)
|
|
359
|
+
}
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
case "list": {
|
|
364
|
+
const listLines = this.#renderList(token as ListToken, 0, styleContext);
|
|
365
|
+
lines.push(...listLines);
|
|
366
|
+
// Don't add spacing after lists if a space token follows
|
|
367
|
+
// (the space token will handle it)
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
case "table": {
|
|
372
|
+
const tableLines = this.#renderTable(token as TableToken, width, nextTokenType, styleContext);
|
|
373
|
+
lines.push(...tableLines);
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
case "blockquote": {
|
|
378
|
+
const quoteStyle = (text: string) => this.#theme.quote(this.#theme.italic(text));
|
|
379
|
+
const quoteStylePrefix = this.#getStylePrefix(quoteStyle);
|
|
380
|
+
const applyQuoteStyle = (line: string): string => {
|
|
381
|
+
if (!quoteStylePrefix) {
|
|
382
|
+
return quoteStyle(line);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const lineWithReappliedStyle = line.replace(/\x1b\[0m/g, `\x1b[0m${quoteStylePrefix}`);
|
|
386
|
+
return quoteStyle(lineWithReappliedStyle);
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
// Blockquotes contain block-level tokens (paragraph, list, code, etc.), so render
|
|
390
|
+
// children recursively and keep default message styling out of nested content.
|
|
391
|
+
const quoteInlineStyleContext: InlineStyleContext = {
|
|
392
|
+
applyText: (text: string) => text,
|
|
393
|
+
stylePrefix: "",
|
|
394
|
+
};
|
|
395
|
+
const quoteContentWidth = Math.max(1, width - 2);
|
|
396
|
+
const quoteTokens = token.tokens || [];
|
|
397
|
+
const renderedQuoteLines: string[] = [];
|
|
398
|
+
|
|
399
|
+
for (let i = 0; i < quoteTokens.length; i++) {
|
|
400
|
+
const quoteToken = quoteTokens[i];
|
|
401
|
+
const nextQuoteToken = quoteTokens[i + 1];
|
|
402
|
+
renderedQuoteLines.push(
|
|
403
|
+
...this.#renderToken(quoteToken, quoteContentWidth, nextQuoteToken?.type, quoteInlineStyleContext),
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
while (renderedQuoteLines.length > 0 && renderedQuoteLines[renderedQuoteLines.length - 1] === "") {
|
|
408
|
+
renderedQuoteLines.pop();
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
for (const quoteLine of renderedQuoteLines) {
|
|
412
|
+
const styledLine = applyQuoteStyle(quoteLine);
|
|
413
|
+
const wrappedLines = wrapTextWithAnsi(styledLine, quoteContentWidth);
|
|
414
|
+
for (const wrappedLine of wrappedLines) {
|
|
415
|
+
lines.push(this.#theme.quoteBorder(`${this.#theme.symbols.quoteBorder} `) + wrappedLine);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (nextTokenType && nextTokenType !== "space") {
|
|
419
|
+
lines.push(""); // Add spacing after blockquotes (unless space token follows)
|
|
420
|
+
}
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
case "hr":
|
|
425
|
+
lines.push(this.#theme.hr(this.#theme.symbols.hrChar.repeat(Math.min(width, 80))));
|
|
426
|
+
if (nextTokenType && nextTokenType !== "space") {
|
|
427
|
+
lines.push(""); // Add spacing after horizontal rules (unless space token follows)
|
|
428
|
+
}
|
|
429
|
+
break;
|
|
430
|
+
|
|
431
|
+
case "html":
|
|
432
|
+
// Render HTML as plain text (escaped for terminal)
|
|
433
|
+
if ("raw" in token && typeof token.raw === "string") {
|
|
434
|
+
lines.push(this.#applyDefaultStyle(token.raw.trim()));
|
|
435
|
+
}
|
|
436
|
+
break;
|
|
437
|
+
|
|
438
|
+
case "space":
|
|
439
|
+
// Space tokens represent blank lines in markdown
|
|
440
|
+
lines.push("");
|
|
441
|
+
break;
|
|
442
|
+
|
|
443
|
+
default:
|
|
444
|
+
// Handle any other token types as plain text
|
|
445
|
+
if ("text" in token && typeof token.text === "string") {
|
|
446
|
+
lines.push(token.text);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
return lines;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
#renderInlineTokens(tokens: Token[], styleContext?: InlineStyleContext): string {
|
|
454
|
+
let result = "";
|
|
455
|
+
const resolvedStyleContext = styleContext ?? this.#getDefaultInlineStyleContext();
|
|
456
|
+
const { applyText, stylePrefix } = resolvedStyleContext;
|
|
457
|
+
const applyTextWithNewlines = (text: string): string => {
|
|
458
|
+
const segments: string[] = text.split("\n");
|
|
459
|
+
return segments.map((segment: string) => applyText(segment)).join("\n");
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
for (const token of tokens) {
|
|
463
|
+
switch (token.type) {
|
|
464
|
+
case "text":
|
|
465
|
+
// Text tokens in list items can have nested tokens for inline formatting
|
|
466
|
+
if (token.tokens && token.tokens.length > 0) {
|
|
467
|
+
result += this.#renderInlineTokens(token.tokens, resolvedStyleContext);
|
|
468
|
+
} else {
|
|
469
|
+
result += applyTextWithNewlines(token.text);
|
|
470
|
+
}
|
|
471
|
+
break;
|
|
472
|
+
|
|
473
|
+
case "paragraph":
|
|
474
|
+
// Paragraph tokens contain nested inline tokens
|
|
475
|
+
result += this.#renderInlineTokens(token.tokens || [], resolvedStyleContext);
|
|
476
|
+
break;
|
|
477
|
+
|
|
478
|
+
case "strong": {
|
|
479
|
+
const boldContent = this.#renderInlineTokens(token.tokens || [], resolvedStyleContext);
|
|
480
|
+
result += this.#theme.bold(boldContent) + stylePrefix;
|
|
481
|
+
break;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
case "em": {
|
|
485
|
+
const italicContent = this.#renderInlineTokens(token.tokens || [], resolvedStyleContext);
|
|
486
|
+
result += this.#theme.italic(italicContent) + stylePrefix;
|
|
487
|
+
break;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
case "codespan":
|
|
491
|
+
result += this.#theme.code(token.text) + stylePrefix;
|
|
492
|
+
break;
|
|
493
|
+
|
|
494
|
+
case "link": {
|
|
495
|
+
const linkText = this.#renderInlineTokens(token.tokens || [], resolvedStyleContext);
|
|
496
|
+
const styledLinkText = this.#theme.link(this.#theme.underline(linkText));
|
|
497
|
+
const clickableLinkText = formatHyperlink(styledLinkText, token.href);
|
|
498
|
+
// If link text matches href, only show the link once
|
|
499
|
+
// Compare raw text (token.text) not styled text (linkText) since linkText has ANSI codes
|
|
500
|
+
// For mailto: links, strip the prefix before comparing (autolinked emails have
|
|
501
|
+
// text="foo@bar.com" but href="mailto:foo@bar.com")
|
|
502
|
+
const hrefForComparison = token.href.startsWith("mailto:") ? token.href.slice(7) : token.href;
|
|
503
|
+
if (token.text === token.href || token.text === hrefForComparison)
|
|
504
|
+
result += clickableLinkText + stylePrefix;
|
|
505
|
+
else {
|
|
506
|
+
const styledLinkUrl = this.#theme.linkUrl(` (${token.href})`);
|
|
507
|
+
result += clickableLinkText + formatHyperlink(styledLinkUrl, token.href) + stylePrefix;
|
|
508
|
+
}
|
|
509
|
+
break;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
case "br":
|
|
513
|
+
result += "\n";
|
|
514
|
+
break;
|
|
515
|
+
|
|
516
|
+
case "del": {
|
|
517
|
+
const delContent = this.#renderInlineTokens(token.tokens || [], resolvedStyleContext);
|
|
518
|
+
result += this.#theme.strikethrough(delContent) + stylePrefix;
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
case "html":
|
|
523
|
+
// Render inline HTML as plain text
|
|
524
|
+
if ("raw" in token && typeof token.raw === "string") {
|
|
525
|
+
result += applyTextWithNewlines(token.raw);
|
|
526
|
+
}
|
|
527
|
+
break;
|
|
528
|
+
|
|
529
|
+
default:
|
|
530
|
+
// Handle any other inline token types as plain text
|
|
531
|
+
if ("text" in token && typeof token.text === "string") {
|
|
532
|
+
result += applyTextWithNewlines(token.text);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
return result;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Render a list with proper nesting support
|
|
542
|
+
*/
|
|
543
|
+
#renderList(token: ListToken, depth: number, styleContext?: InlineStyleContext): string[] {
|
|
544
|
+
const lines: string[] = [];
|
|
545
|
+
const indent = " ".repeat(depth);
|
|
546
|
+
// Use the list's start property (defaults to 1 for ordered lists)
|
|
547
|
+
const startNumber = token.start ?? 1;
|
|
548
|
+
|
|
549
|
+
for (let i = 0; i < token.items.length; i++) {
|
|
550
|
+
const item = token.items[i];
|
|
551
|
+
const bullet = token.ordered ? `${startNumber + i}. ` : "- ";
|
|
552
|
+
|
|
553
|
+
// Process item tokens to handle nested lists
|
|
554
|
+
const itemLines = this.#renderListItem(item.tokens || [], depth, styleContext);
|
|
555
|
+
|
|
556
|
+
if (itemLines.length > 0) {
|
|
557
|
+
// First line - check if it's a nested list
|
|
558
|
+
// A nested list will start with indent (spaces) followed by cyan bullet
|
|
559
|
+
const firstLine = itemLines[0];
|
|
560
|
+
const isNestedList = /^\s+\x1b\[36m[-\d]/.test(firstLine); // starts with spaces + cyan + bullet char
|
|
561
|
+
|
|
562
|
+
if (isNestedList) {
|
|
563
|
+
// This is a nested list, just add it as-is (already has full indent)
|
|
564
|
+
lines.push(firstLine);
|
|
565
|
+
} else {
|
|
566
|
+
// Regular text content - add indent and bullet
|
|
567
|
+
lines.push(indent + this.#theme.listBullet(bullet) + firstLine);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// Rest of the lines
|
|
571
|
+
for (let j = 1; j < itemLines.length; j++) {
|
|
572
|
+
const line = itemLines[j];
|
|
573
|
+
const isNestedListLine = /^\s+\x1b\[36m[-\d]/.test(line); // starts with spaces + cyan + bullet char
|
|
574
|
+
|
|
575
|
+
if (isNestedListLine) {
|
|
576
|
+
// Nested list line - already has full indent
|
|
577
|
+
lines.push(line);
|
|
578
|
+
} else {
|
|
579
|
+
// Regular content - add parent indent + 2 spaces for continuation
|
|
580
|
+
lines.push(`${indent} ${line}`);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
} else {
|
|
584
|
+
lines.push(indent + this.#theme.listBullet(bullet));
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
return lines;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* Render list item tokens, handling nested lists
|
|
593
|
+
* Returns lines WITHOUT the parent indent (renderList will add it)
|
|
594
|
+
*/
|
|
595
|
+
#renderListItem(tokens: Token[], parentDepth: number, styleContext?: InlineStyleContext): string[] {
|
|
596
|
+
const lines: string[] = [];
|
|
597
|
+
|
|
598
|
+
for (const token of tokens) {
|
|
599
|
+
if (token.type === "list") {
|
|
600
|
+
// Nested list - render with one additional indent level
|
|
601
|
+
// These lines will have their own indent, so we just add them as-is
|
|
602
|
+
const nestedLines = this.#renderList(token as ListToken, parentDepth + 1, styleContext);
|
|
603
|
+
lines.push(...nestedLines);
|
|
604
|
+
} else if (token.type === "text") {
|
|
605
|
+
// Text content (may have inline tokens)
|
|
606
|
+
const text =
|
|
607
|
+
token.tokens && token.tokens.length > 0
|
|
608
|
+
? this.#renderInlineTokens(token.tokens, styleContext)
|
|
609
|
+
: token.text || "";
|
|
610
|
+
lines.push(text);
|
|
611
|
+
} else if (token.type === "paragraph") {
|
|
612
|
+
// Paragraph in list item
|
|
613
|
+
const text = this.#renderInlineTokens(token.tokens || [], styleContext);
|
|
614
|
+
lines.push(text);
|
|
615
|
+
} else if (token.type === "code") {
|
|
616
|
+
// Code block in list item
|
|
617
|
+
const codeIndent = padding(this.#codeBlockIndent);
|
|
618
|
+
lines.push(this.#theme.codeBlockBorder(`\`\`\`${token.lang || ""}`));
|
|
619
|
+
if (this.#theme.highlightCode) {
|
|
620
|
+
const highlightedLines = this.#theme.highlightCode(token.text, token.lang);
|
|
621
|
+
for (const hlLine of highlightedLines) {
|
|
622
|
+
lines.push(`${codeIndent}${hlLine}`);
|
|
623
|
+
}
|
|
624
|
+
} else {
|
|
625
|
+
const codeLines = token.text.split("\n");
|
|
626
|
+
for (const codeLine of codeLines) {
|
|
627
|
+
lines.push(`${codeIndent}${this.#theme.codeBlock(codeLine)}`);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
lines.push(this.#theme.codeBlockBorder("```"));
|
|
631
|
+
} else {
|
|
632
|
+
// Other token types - try to render as inline
|
|
633
|
+
const text = this.#renderInlineTokens([token], styleContext);
|
|
634
|
+
if (text) {
|
|
635
|
+
lines.push(text);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
return lines;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Get the visible width of the longest word in a string.
|
|
645
|
+
*/
|
|
646
|
+
#getLongestWordWidth(text: string, maxWidth?: number): number {
|
|
647
|
+
const words = text.split(/\s+/).filter(word => word.length > 0);
|
|
648
|
+
let longest = 0;
|
|
649
|
+
for (const word of words) {
|
|
650
|
+
longest = Math.max(longest, visibleWidth(word));
|
|
651
|
+
}
|
|
652
|
+
if (maxWidth === undefined) {
|
|
653
|
+
return longest;
|
|
654
|
+
}
|
|
655
|
+
return Math.min(longest, maxWidth);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* Wrap a table cell to fit into a column.
|
|
660
|
+
*
|
|
661
|
+
* Delegates to wrapTextWithAnsi() so ANSI codes + long tokens are handled
|
|
662
|
+
* consistently with the rest of the renderer.
|
|
663
|
+
*/
|
|
664
|
+
#wrapCellText(text: string, maxWidth: number): string[] {
|
|
665
|
+
return wrapTextWithAnsi(text, Math.max(1, maxWidth));
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* Render a table with width-aware cell wrapping.
|
|
670
|
+
* Cells that don't fit are wrapped to multiple lines.
|
|
671
|
+
*/
|
|
672
|
+
#renderTable(
|
|
673
|
+
token: TableToken,
|
|
674
|
+
availableWidth: number,
|
|
675
|
+
nextTokenType?: string,
|
|
676
|
+
styleContext?: InlineStyleContext,
|
|
677
|
+
): string[] {
|
|
678
|
+
const lines: string[] = [];
|
|
679
|
+
const numCols = token.header.length;
|
|
680
|
+
|
|
681
|
+
if (numCols === 0) {
|
|
682
|
+
return lines;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
// Calculate border overhead: "│ " + (n-1) * " │ " + " │"
|
|
686
|
+
// = 2 + (n-1) * 3 + 2 = 3n + 1
|
|
687
|
+
const borderOverhead = 3 * numCols + 1;
|
|
688
|
+
const availableForCells = availableWidth - borderOverhead;
|
|
689
|
+
if (availableForCells < numCols) {
|
|
690
|
+
// Too narrow to render a stable table. Fall back to raw markdown.
|
|
691
|
+
const fallbackLines = token.raw ? wrapTextWithAnsi(token.raw, availableWidth) : [];
|
|
692
|
+
if (nextTokenType && nextTokenType !== "space") {
|
|
693
|
+
fallbackLines.push("");
|
|
694
|
+
}
|
|
695
|
+
return fallbackLines;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
const maxUnbrokenWordWidth = 30;
|
|
699
|
+
|
|
700
|
+
// Calculate natural column widths (what each column needs without constraints)
|
|
701
|
+
const naturalWidths: number[] = [];
|
|
702
|
+
const minWordWidths: number[] = [];
|
|
703
|
+
for (let i = 0; i < numCols; i++) {
|
|
704
|
+
const headerText = this.#renderInlineTokens(token.header[i].tokens || [], styleContext);
|
|
705
|
+
naturalWidths[i] = visibleWidth(headerText);
|
|
706
|
+
minWordWidths[i] = Math.max(1, this.#getLongestWordWidth(headerText, maxUnbrokenWordWidth));
|
|
707
|
+
}
|
|
708
|
+
for (const row of token.rows) {
|
|
709
|
+
for (let i = 0; i < row.length; i++) {
|
|
710
|
+
const cellText = this.#renderInlineTokens(row[i].tokens || [], styleContext);
|
|
711
|
+
naturalWidths[i] = Math.max(naturalWidths[i] || 0, visibleWidth(cellText));
|
|
712
|
+
minWordWidths[i] = Math.max(
|
|
713
|
+
minWordWidths[i] || 1,
|
|
714
|
+
this.#getLongestWordWidth(cellText, maxUnbrokenWordWidth),
|
|
715
|
+
);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
let minColumnWidths = minWordWidths;
|
|
720
|
+
let minCellsWidth = minColumnWidths.reduce((a, b) => a + b, 0);
|
|
721
|
+
|
|
722
|
+
if (minCellsWidth > availableForCells) {
|
|
723
|
+
minColumnWidths = new Array(numCols).fill(1);
|
|
724
|
+
const remaining = availableForCells - numCols;
|
|
725
|
+
|
|
726
|
+
if (remaining > 0) {
|
|
727
|
+
const totalWeight = minWordWidths.reduce((total, width) => total + Math.max(0, width - 1), 0);
|
|
728
|
+
const growth = minWordWidths.map(width => {
|
|
729
|
+
const weight = Math.max(0, width - 1);
|
|
730
|
+
return totalWeight > 0 ? Math.floor((weight / totalWeight) * remaining) : 0;
|
|
731
|
+
});
|
|
732
|
+
|
|
733
|
+
for (let i = 0; i < numCols; i++) {
|
|
734
|
+
minColumnWidths[i] += growth[i] ?? 0;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
const allocated = growth.reduce((total, width) => total + width, 0);
|
|
738
|
+
let leftover = remaining - allocated;
|
|
739
|
+
for (let i = 0; leftover > 0 && i < numCols; i++) {
|
|
740
|
+
minColumnWidths[i]++;
|
|
741
|
+
leftover--;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
minCellsWidth = minColumnWidths.reduce((a, b) => a + b, 0);
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// Calculate column widths that fit within available width
|
|
749
|
+
const totalNaturalWidth = naturalWidths.reduce((a, b) => a + b, 0) + borderOverhead;
|
|
750
|
+
let columnWidths: number[];
|
|
751
|
+
|
|
752
|
+
if (totalNaturalWidth <= availableWidth) {
|
|
753
|
+
// Everything fits naturally
|
|
754
|
+
columnWidths = naturalWidths.map((width, index) => Math.max(width, minColumnWidths[index]));
|
|
755
|
+
} else {
|
|
756
|
+
// Need to shrink columns to fit
|
|
757
|
+
const totalGrowPotential = naturalWidths.reduce((total, width, index) => {
|
|
758
|
+
return total + Math.max(0, width - minColumnWidths[index]);
|
|
759
|
+
}, 0);
|
|
760
|
+
const extraWidth = Math.max(0, availableForCells - minCellsWidth);
|
|
761
|
+
columnWidths = minColumnWidths.map((minWidth, index) => {
|
|
762
|
+
const naturalWidth = naturalWidths[index];
|
|
763
|
+
const minWidthDelta = Math.max(0, naturalWidth - minWidth);
|
|
764
|
+
let grow = 0;
|
|
765
|
+
if (totalGrowPotential > 0) {
|
|
766
|
+
grow = Math.floor((minWidthDelta / totalGrowPotential) * extraWidth);
|
|
767
|
+
}
|
|
768
|
+
return minWidth + grow;
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
// Adjust for rounding errors - distribute remaining space
|
|
772
|
+
const allocated = columnWidths.reduce((a, b) => a + b, 0);
|
|
773
|
+
let remaining = availableForCells - allocated;
|
|
774
|
+
while (remaining > 0) {
|
|
775
|
+
let grew = false;
|
|
776
|
+
for (let i = 0; i < numCols && remaining > 0; i++) {
|
|
777
|
+
if (columnWidths[i] < naturalWidths[i]) {
|
|
778
|
+
columnWidths[i]++;
|
|
779
|
+
remaining--;
|
|
780
|
+
grew = true;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
if (!grew) {
|
|
784
|
+
break;
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
const t = this.#theme.symbols.table;
|
|
790
|
+
const h = t.horizontal;
|
|
791
|
+
const v = t.vertical;
|
|
792
|
+
|
|
793
|
+
// Render top border
|
|
794
|
+
const topBorderCells = columnWidths.map(w => h.repeat(w));
|
|
795
|
+
lines.push(`${t.topLeft}${h}${topBorderCells.join(`${h}${t.teeDown}${h}`)}${h}${t.topRight}`);
|
|
796
|
+
|
|
797
|
+
// Render header with wrapping
|
|
798
|
+
const headerCellLines: string[][] = token.header.map((cell, i) => {
|
|
799
|
+
const text = this.#renderInlineTokens(cell.tokens || [], styleContext);
|
|
800
|
+
return this.#wrapCellText(text, columnWidths[i]);
|
|
801
|
+
});
|
|
802
|
+
const headerLineCount = Math.max(...headerCellLines.map(c => c.length));
|
|
803
|
+
|
|
804
|
+
for (let lineIdx = 0; lineIdx < headerLineCount; lineIdx++) {
|
|
805
|
+
const rowParts = headerCellLines.map((cellLines, colIdx) => {
|
|
806
|
+
const text = cellLines[lineIdx] || "";
|
|
807
|
+
const padded = text + padding(Math.max(0, columnWidths[colIdx] - visibleWidth(text)));
|
|
808
|
+
return this.#theme.bold(padded);
|
|
809
|
+
});
|
|
810
|
+
lines.push(`${v} ${rowParts.join(` ${v} `)} ${v}`);
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
// Render separator
|
|
814
|
+
const separatorCells = columnWidths.map(w => h.repeat(w));
|
|
815
|
+
const separatorLine = `${t.teeRight}${h}${separatorCells.join(`${h}${t.cross}${h}`)}${h}${t.teeLeft}`;
|
|
816
|
+
lines.push(separatorLine);
|
|
817
|
+
|
|
818
|
+
// Render rows with wrapping
|
|
819
|
+
for (let rowIndex = 0; rowIndex < token.rows.length; rowIndex++) {
|
|
820
|
+
const row = token.rows[rowIndex];
|
|
821
|
+
const rowCellLines: string[][] = row.map((cell, i) => {
|
|
822
|
+
const text = this.#renderInlineTokens(cell.tokens || [], styleContext);
|
|
823
|
+
return this.#wrapCellText(text, columnWidths[i]);
|
|
824
|
+
});
|
|
825
|
+
const rowLineCount = Math.max(...rowCellLines.map(c => c.length));
|
|
826
|
+
|
|
827
|
+
for (let lineIdx = 0; lineIdx < rowLineCount; lineIdx++) {
|
|
828
|
+
const rowParts = rowCellLines.map((cellLines, colIdx) => {
|
|
829
|
+
const text = cellLines[lineIdx] || "";
|
|
830
|
+
return text + padding(Math.max(0, columnWidths[colIdx] - visibleWidth(text)));
|
|
831
|
+
});
|
|
832
|
+
lines.push(`${v} ${rowParts.join(` ${v} `)} ${v}`);
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
if (rowIndex < token.rows.length - 1) {
|
|
836
|
+
lines.push(separatorLine);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
// Render bottom border
|
|
841
|
+
const bottomBorderCells = columnWidths.map(w => h.repeat(w));
|
|
842
|
+
lines.push(`${t.bottomLeft}${h}${bottomBorderCells.join(`${h}${t.teeUp}${h}`)}${h}${t.bottomRight}`);
|
|
843
|
+
|
|
844
|
+
if (nextTokenType && nextTokenType !== "space") {
|
|
845
|
+
lines.push(""); // Add spacing after table
|
|
846
|
+
}
|
|
847
|
+
return lines;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
/**
|
|
852
|
+
* Render inline markdown (bold, italic, code, links, strikethrough) to a styled string.
|
|
853
|
+
* Unlike the full Markdown component, this produces a single line with no block-level elements.
|
|
854
|
+
*/
|
|
855
|
+
export function renderInlineMarkdown(text: string, mdTheme: MarkdownTheme, baseColor?: (t: string) => string): string {
|
|
856
|
+
const tokens = marked.lexer(text);
|
|
857
|
+
const applyText = baseColor ?? ((t: string) => t);
|
|
858
|
+
let result = "";
|
|
859
|
+
for (const token of tokens) {
|
|
860
|
+
if (token.type === "paragraph" && token.tokens) {
|
|
861
|
+
result += renderInlineTokens(token.tokens, mdTheme, applyText);
|
|
862
|
+
} else if (token.type === "list") {
|
|
863
|
+
result += token.items
|
|
864
|
+
.map((item: Tokens.ListItem, index: number) => {
|
|
865
|
+
const prefix = token.ordered ? `${(token.start || 1) + index}. ` : "• ";
|
|
866
|
+
const content = item.tokens ? renderInlineTokens(item.tokens, mdTheme, applyText) : applyText(item.text);
|
|
867
|
+
return `${applyText(prefix)}${content}`;
|
|
868
|
+
})
|
|
869
|
+
.join(applyText(" "));
|
|
870
|
+
} else if ("text" in token && typeof token.text === "string") {
|
|
871
|
+
result += applyText(token.text);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
return result;
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
function renderInlineTokens(tokens: Token[], mdTheme: MarkdownTheme, applyText: (t: string) => string): string {
|
|
878
|
+
let result = "";
|
|
879
|
+
const styleReset = applyText("");
|
|
880
|
+
for (const token of tokens) {
|
|
881
|
+
switch (token.type) {
|
|
882
|
+
case "text":
|
|
883
|
+
if (token.tokens && token.tokens.length > 0) {
|
|
884
|
+
result += renderInlineTokens(token.tokens, mdTheme, applyText);
|
|
885
|
+
} else {
|
|
886
|
+
result += applyText(token.text);
|
|
887
|
+
}
|
|
888
|
+
break;
|
|
889
|
+
case "strong":
|
|
890
|
+
result += mdTheme.bold(renderInlineTokens(token.tokens || [], mdTheme, applyText)) + styleReset;
|
|
891
|
+
break;
|
|
892
|
+
case "em":
|
|
893
|
+
result += mdTheme.italic(renderInlineTokens(token.tokens || [], mdTheme, applyText)) + styleReset;
|
|
894
|
+
break;
|
|
895
|
+
case "codespan":
|
|
896
|
+
result += mdTheme.code(token.text) + styleReset;
|
|
897
|
+
break;
|
|
898
|
+
case "del":
|
|
899
|
+
result += mdTheme.strikethrough(renderInlineTokens(token.tokens || [], mdTheme, applyText)) + styleReset;
|
|
900
|
+
break;
|
|
901
|
+
case "link": {
|
|
902
|
+
const linkText = renderInlineTokens(token.tokens || [], mdTheme, applyText);
|
|
903
|
+
result += mdTheme.link(mdTheme.underline(linkText)) + styleReset;
|
|
904
|
+
break;
|
|
905
|
+
}
|
|
906
|
+
default:
|
|
907
|
+
if ("text" in token && typeof token.text === "string") {
|
|
908
|
+
result += applyText(token.text);
|
|
909
|
+
}
|
|
910
|
+
break;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
return result;
|
|
914
|
+
}
|