|
1
|
+ |
/**
|
|
2
|
+ |
* Markdown to ANSI for the coder transcript.
|
|
3
|
+ |
*
|
|
4
|
+ |
* Replies arrive as Markdown and used to be printed as source, so the reader
|
|
5
|
+ |
* saw `**ox-alpha**` rather than a bold name. This turns the source into
|
|
6
|
+ |
* styled, width-wrapped rows.
|
|
7
|
+ |
*
|
|
8
|
+ |
* Two properties of the caller shape every decision here:
|
|
9
|
+ |
*
|
|
10
|
+ |
* - The text arrives in chunks. Every frame re-renders the whole entry from
|
|
11
|
+ |
* whatever has arrived, so a half-arrived `**` or an unclosed fence is the
|
|
12
|
+ |
* normal case rather than an error. Unterminated markup renders as the
|
|
13
|
+ |
* literal characters that arrived, which means the row never loses a
|
|
14
|
+ |
* character and never flickers between two different readings of the same
|
|
15
|
+ |
* prefix.
|
|
16
|
+ |
* - Rows are laid out by the interface, which owns the gutter. Wrapping
|
|
17
|
+ |
* therefore works on visible width with ANSI ignored, and a wrapped list
|
|
18
|
+ |
* item continues under its text rather than under its bullet.
|
|
19
|
+ |
*
|
|
20
|
+ |
* There is no Markdown dependency. The subset below is what a reply uses, and
|
|
21
|
+ |
* a parser small enough to test directly is worth more here than a general one
|
|
22
|
+ |
* that would still need a streaming and a wrapping layer on top.
|
|
23
|
+ |
*/
|
|
24
|
+ |
|
|
25
|
+ |
const RESET = "\x1b[0m";
|
|
26
|
+ |
const BOLD = "\x1b[1m";
|
|
27
|
+ |
const DIM = "\x1b[2m";
|
|
28
|
+ |
const ITALIC = "\x1b[3m";
|
|
29
|
+ |
const CODE = "\x1b[36m";
|
|
30
|
+ |
const HEADING = "\x1b[1m\x1b[36m";
|
|
31
|
+ |
|
|
32
|
+ |
/** A run of text that shares one ANSI prefix. */
|
|
33
|
+ |
export interface StyledSpan {
|
|
34
|
+ |
readonly text: string;
|
|
35
|
+ |
readonly style: string;
|
|
36
|
+ |
}
|
|
37
|
+ |
|
|
38
|
+ |
/** Visible width, ignoring ANSI styling. */
|
|
39
|
+ |
export function visibleWidth(text: string): number {
|
|
40
|
+ |
return [...text.replace(/\x1b\[[0-9;]*m/g, "")].length;
|
|
41
|
+ |
}
|
|
42
|
+ |
|
|
43
|
+ |
/** Render one text block with a single style, wrapped like a paragraph. */
|
|
44
|
+ |
export function wrapStyled(text: string, width: number, style: string): ReadonlyArray<string> {
|
|
45
|
+ |
const rows: string[] = [];
|
|
46
|
+ |
for (const paragraph of text.split("\n")) {
|
|
47
|
+ |
if (paragraph.length === 0) {
|
|
48
|
+ |
rows.push("");
|
|
49
|
+ |
continue;
|
|
50
|
+ |
}
|
|
51
|
+ |
rows.push(...wrapSpans([{ text: paragraph, style }], width, "", ""));
|
|
52
|
+ |
}
|
|
53
|
+ |
return rows;
|
|
54
|
+ |
}
|
|
55
|
+ |
|
|
56
|
+ |
/** Render Markdown source as styled rows no wider than `width`. */
|
|
57
|
+ |
export function renderMarkdown(text: string, width: number): ReadonlyArray<string> {
|
|
58
|
+ |
const rows: string[] = [];
|
|
59
|
+ |
/** The fence marker that opened the current code block, if one is open. */
|
|
60
|
+ |
let fence: string | undefined;
|
|
61
|
+ |
|
|
62
|
+ |
for (const line of text.split("\n")) {
|
|
63
|
+ |
const fenced = /^\s*(```+|~~~+)/.exec(line);
|
|
64
|
+ |
|
|
65
|
+ |
if (fence !== undefined) {
|
|
66
|
+ |
// A fence closes only on its own marker, so a ``` inside a ~~~ block is
|
|
67
|
+ |
// content rather than a terminator.
|
|
68
|
+ |
if (fenced !== null && line.trim().startsWith(fence)) {
|
|
69
|
+ |
fence = undefined;
|
|
70
|
+ |
continue;
|
|
71
|
+ |
}
|
|
72
|
+ |
rows.push(...codeRows(line, width));
|
|
73
|
+ |
continue;
|
|
74
|
+ |
}
|
|
75
|
+ |
|
|
76
|
+ |
if (fenced !== null) {
|
|
77
|
+ |
fence = fenced[1] ?? "```";
|
|
78
|
+ |
continue;
|
|
79
|
+ |
}
|
|
80
|
+ |
|
|
81
|
+ |
rows.push(...blockRows(line, width));
|
|
82
|
+ |
}
|
|
83
|
+ |
|
|
84
|
+ |
return rows;
|
|
85
|
+ |
}
|
|
86
|
+ |
|
|
87
|
+ |
/** One non-fenced source line as one or more rendered rows. */
|
|
88
|
+ |
function blockRows(line: string, width: number): ReadonlyArray<string> {
|
|
89
|
+ |
if (line.trim().length === 0) return [""];
|
|
90
|
+ |
|
|
91
|
+ |
if (/^\s*([-*_])\s*(\1\s*){2,}$/.test(line)) {
|
|
92
|
+ |
return [`${DIM}${"─".repeat(Math.max(1, Math.min(width, 24)))}${RESET}`];
|
|
93
|
+ |
}
|
|
94
|
+ |
|
|
95
|
+ |
const heading = /^\s*(#{1,6})\s+(.*)$/.exec(line);
|
|
96
|
+ |
if (heading !== null) {
|
|
97
|
+ |
return wrapSpans(scan(heading[2] ?? "", HEADING), width, "", "");
|
|
98
|
+ |
}
|
|
99
|
+ |
|
|
100
|
+ |
const quote = /^\s*>\s?(.*)$/.exec(line);
|
|
101
|
+ |
if (quote !== null) {
|
|
102
|
+ |
const bar = `${DIM}│${RESET} `;
|
|
103
|
+ |
return wrapSpans(scan(quote[1] ?? "", DIM), width, bar, bar);
|
|
104
|
+ |
}
|
|
105
|
+ |
|
|
106
|
+ |
const bullet = /^(\s*)([-*+])\s+(.*)$/.exec(line);
|
|
107
|
+ |
if (bullet !== null) {
|
|
108
|
+ |
const indent = " ".repeat(bullet[1]?.length ?? 0);
|
|
109
|
+ |
return wrapSpans(scan(bullet[3] ?? "", ""), width, `${indent}${DIM}•${RESET} `, `${indent} `);
|
|
110
|
+ |
}
|
|
111
|
+ |
|
|
112
|
+ |
const numbered = /^(\s*)(\d{1,9}[.)])\s+(.*)$/.exec(line);
|
|
113
|
+ |
if (numbered !== null) {
|
|
114
|
+ |
const indent = " ".repeat(numbered[1]?.length ?? 0);
|
|
115
|
+ |
const marker = numbered[2] ?? "1.";
|
|
116
|
+ |
return wrapSpans(
|
|
117
|
+ |
scan(numbered[3] ?? "", ""),
|
|
118
|
+ |
width,
|
|
119
|
+ |
`${indent}${DIM}${marker}${RESET} `,
|
|
120
|
+ |
`${indent}${" ".repeat(marker.length + 1)}`,
|
|
121
|
+ |
);
|
|
122
|
+ |
}
|
|
123
|
+ |
|
|
124
|
+ |
return wrapSpans(scan(line, ""), width, "", "");
|
|
125
|
+ |
}
|
|
126
|
+ |
|
|
127
|
+ |
/** A line inside a fenced block: never wrapped by word, only hard-split. */
|
|
128
|
+ |
function codeRows(line: string, width: number): ReadonlyArray<string> {
|
|
129
|
+ |
const body = Math.max(4, width - 2);
|
|
130
|
+ |
const expanded = line.replace(/\t/g, " ");
|
|
131
|
+ |
const rows: string[] = [];
|
|
132
|
+ |
let rest = [...expanded];
|
|
133
|
+ |
do {
|
|
134
|
+ |
const piece = rest.slice(0, body).join("");
|
|
135
|
+ |
rows.push(`${DIM}│${RESET} ${CODE}${piece}${RESET}`);
|
|
136
|
+ |
rest = rest.slice(body);
|
|
137
|
+ |
} while (rest.length > 0);
|
|
138
|
+ |
return rows;
|
|
139
|
+ |
}
|
|
140
|
+ |
|
|
141
|
+ |
/**
|
|
142
|
+ |
* Split inline Markdown into styled spans.
|
|
143
|
+ |
*
|
|
144
|
+ |
* Every construct is matched by finding its terminator first. When the
|
|
145
|
+ |
* terminator has not arrived the opening characters are kept as literal text,
|
|
146
|
+ |
* which is what makes a half-streamed `**bold` read as `**bold` rather than
|
|
147
|
+ |
* swallowing the rest of the reply.
|
|
148
|
+ |
*/
|
|
149
|
+ |
function scan(text: string, style: string): ReadonlyArray<StyledSpan> {
|
|
150
|
+ |
const spans: StyledSpan[] = [];
|
|
151
|
+ |
let buffer = "";
|
|
152
|
+ |
|
|
153
|
+ |
const flush = () => {
|
|
154
|
+ |
if (buffer.length === 0) return;
|
|
155
|
+ |
spans.push({ text: buffer, style });
|
|
156
|
+ |
buffer = "";
|
|
157
|
+ |
};
|
|
158
|
+ |
|
|
159
|
+ |
let index = 0;
|
|
160
|
+ |
while (index < text.length) {
|
|
161
|
+ |
const char = text[index] ?? "";
|
|
162
|
+ |
const next = text[index + 1];
|
|
163
|
+ |
|
|
164
|
+ |
if (char === "\\" && next !== undefined && /[\\`*_~[\]()#+\-.!>]/.test(next)) {
|
|
165
|
+ |
buffer += next;
|
|
166
|
+ |
index += 2;
|
|
167
|
+ |
continue;
|
|
168
|
+ |
}
|
|
169
|
+ |
|
|
170
|
+ |
if (char === "`") {
|
|
171
|
+ |
const end = text.indexOf("`", index + 1);
|
|
172
|
+ |
if (end > index + 1) {
|
|
173
|
+ |
flush();
|
|
174
|
+ |
spans.push({ text: text.slice(index + 1, end), style: `${style}${CODE}` });
|
|
175
|
+ |
index = end + 1;
|
|
176
|
+ |
continue;
|
|
177
|
+ |
}
|
|
178
|
+ |
buffer += char;
|
|
179
|
+ |
index += 1;
|
|
180
|
+ |
continue;
|
|
181
|
+ |
}
|
|
182
|
+ |
|
|
183
|
+ |
const strong = text.startsWith("**", index)
|
|
184
|
+ |
? "**"
|
|
185
|
+ |
: text.startsWith("__", index)
|
|
186
|
+ |
? "__"
|
|
187
|
+ |
: undefined;
|
|
188
|
+ |
if (strong !== undefined) {
|
|
189
|
+ |
const end = text.indexOf(strong, index + 2);
|
|
190
|
+ |
if (end > index + 2) {
|
|
191
|
+ |
flush();
|
|
192
|
+ |
spans.push(...scan(text.slice(index + 2, end), `${style}${BOLD}`));
|
|
193
|
+ |
index = end + 2;
|
|
194
|
+ |
continue;
|
|
195
|
+ |
}
|
|
196
|
+ |
buffer += strong;
|
|
197
|
+ |
index += 2;
|
|
198
|
+ |
continue;
|
|
199
|
+ |
}
|
|
200
|
+ |
|
|
201
|
+ |
if ((char === "*" || char === "_") && opensEmphasis(text, index, char)) {
|
|
202
|
+ |
const end = closesEmphasis(text, index + 1, char);
|
|
203
|
+ |
if (end !== undefined) {
|
|
204
|
+ |
flush();
|
|
205
|
+ |
spans.push(...scan(text.slice(index + 1, end), `${style}${ITALIC}`));
|
|
206
|
+ |
index = end + 1;
|
|
207
|
+ |
continue;
|
|
208
|
+ |
}
|
|
209
|
+ |
}
|
|
210
|
+ |
|
|
211
|
+ |
buffer += char;
|
|
212
|
+ |
index += 1;
|
|
213
|
+ |
}
|
|
214
|
+ |
|
|
215
|
+ |
flush();
|
|
216
|
+ |
return spans;
|
|
217
|
+ |
}
|
|
218
|
+ |
|
|
219
|
+ |
const WORD = /[\p{L}\p{N}]/u;
|
|
220
|
+ |
|
|
221
|
+ |
/** An opener needs text after it, and `_` also needs a boundary before it. */
|
|
222
|
+ |
function opensEmphasis(text: string, index: number, marker: string): boolean {
|
|
223
|
+ |
const after = text[index + 1];
|
|
224
|
+ |
if (after === undefined || /\s/.test(after)) return false;
|
|
225
|
+ |
if (marker !== "_") return true;
|
|
226
|
+ |
const before = text[index - 1];
|
|
227
|
+ |
return before === undefined || !WORD.test(before);
|
|
228
|
+ |
}
|
|
229
|
+ |
|
|
230
|
+ |
/** The matching terminator, or undefined when it has not arrived yet. */
|
|
231
|
+ |
function closesEmphasis(text: string, from: number, marker: string): number | undefined {
|
|
232
|
+ |
for (let index = from; index < text.length; index += 1) {
|
|
233
|
+ |
if (text[index] !== marker) continue;
|
|
234
|
+ |
const before = text[index - 1];
|
|
235
|
+ |
if (before === undefined || /\s/.test(before)) continue;
|
|
236
|
+ |
if (marker === "_") {
|
|
237
|
+ |
const after = text[index + 1];
|
|
238
|
+ |
if (after !== undefined && WORD.test(after)) continue;
|
|
239
|
+ |
}
|
|
240
|
+ |
return index;
|
|
241
|
+ |
}
|
|
242
|
+ |
return undefined;
|
|
243
|
+ |
}
|
|
244
|
+ |
|
|
245
|
+ |
/**
|
|
246
|
+ |
* Greedy word wrap over styled spans.
|
|
247
|
+ |
*
|
|
248
|
+ |
* Wrapping happens on visible width so styling never shifts the right edge,
|
|
249
|
+ |
* and the continuation prefix is separate from the first one so a wrapped list
|
|
250
|
+ |
* item lines up under its own text.
|
|
251
|
+ |
*/
|
|
252
|
+ |
export function wrapSpans(
|
|
253
|
+ |
spans: ReadonlyArray<StyledSpan>,
|
|
254
|
+ |
width: number,
|
|
255
|
+ |
first: string,
|
|
256
|
+ |
continuation: string,
|
|
257
|
+ |
): ReadonlyArray<string> {
|
|
258
|
+ |
const rows: string[] = [];
|
|
259
|
+ |
let prefix = first;
|
|
260
|
+ |
let line: StyledSpan[] = [];
|
|
261
|
+ |
let used = 0;
|
|
262
|
+ |
/** A space seen between two words, carrying the style of the span it came from. */
|
|
263
|
+ |
let pendingSpace: string | undefined;
|
|
264
|
+ |
|
|
265
|
+ |
const room = () => Math.max(4, width - visibleWidth(prefix));
|
|
266
|
+ |
|
|
267
|
+ |
const emit = () => {
|
|
268
|
+ |
rows.push(prefix + merge(line).map(paint).join(""));
|
|
269
|
+ |
prefix = continuation;
|
|
270
|
+ |
line = [];
|
|
271
|
+ |
used = 0;
|
|
272
|
+ |
pendingSpace = undefined;
|
|
273
|
+ |
};
|
|
274
|
+ |
|
|
275
|
+ |
for (const span of spans) {
|
|
276
|
+ |
for (const piece of span.text.split(/(\s+)/)) {
|
|
277
|
+ |
if (piece.length === 0) continue;
|
|
278
|
+ |
if (/^\s+$/.test(piece)) {
|
|
279
|
+ |
if (used > 0) pendingSpace = span.style;
|
|
280
|
+ |
continue;
|
|
281
|
+ |
}
|
|
282
|
+ |
|
|
283
|
+ |
let word = [...piece];
|
|
284
|
+ |
while (word.length > 0) {
|
|
285
|
+ |
const gap = pendingSpace !== undefined && used > 0 ? 1 : 0;
|
|
286
|
+ |
const available = room() - used - gap;
|
|
287
|
+ |
|
|
288
|
+ |
if (word.length > available && used > 0) {
|
|
289
|
+ |
emit();
|
|
290
|
+ |
continue;
|
|
291
|
+ |
}
|
|
292
|
+ |
|
|
293
|
+ |
// A word wider than a whole row is split rather than dropped.
|
|
294
|
+ |
const take = word.length > room() ? room() : word.length;
|
|
295
|
+ |
if (pendingSpace !== undefined && used > 0) {
|
|
296
|
+ |
// The space keeps the style of the span it came from, so a styled
|
|
297
|
+ |
// run stays one escape sequence and a boundary space stays plain.
|
|
298
|
+ |
line.push({ text: " ", style: pendingSpace });
|
|
299
|
+ |
used += 1;
|
|
300
|
+ |
}
|
|
301
|
+ |
pendingSpace = undefined;
|
|
302
|
+ |
line.push({ text: word.slice(0, take).join(""), style: span.style });
|
|
303
|
+ |
used += take;
|
|
304
|
+ |
word = word.slice(take);
|
|
305
|
+ |
if (word.length > 0) emit();
|
|
306
|
+ |
}
|
|
307
|
+ |
}
|
|
308
|
+ |
}
|
|
309
|
+ |
|
|
310
|
+ |
emit();
|
|
311
|
+ |
return rows;
|
|
312
|
+ |
}
|
|
313
|
+ |
|
|
314
|
+ |
/** Join neighbours that share a style, so a styled run is one escape sequence. */
|
|
315
|
+ |
function merge(spans: ReadonlyArray<StyledSpan>): ReadonlyArray<StyledSpan> {
|
|
316
|
+ |
const out: StyledSpan[] = [];
|
|
317
|
+ |
for (const span of spans) {
|
|
318
|
+ |
const last = out.at(-1);
|
|
319
|
+ |
if (last !== undefined && last.style === span.style)
|
|
320
|
+ |
out[out.length - 1] = { text: last.text + span.text, style: last.style };
|
|
321
|
+ |
else out.push(span);
|
|
322
|
+ |
}
|
|
323
|
+ |
return out;
|
|
324
|
+ |
}
|
|
325
|
+ |
|
|
326
|
+ |
function paint(span: StyledSpan): string {
|
|
327
|
+ |
return span.style.length === 0 ? span.text : `${span.style}${span.text}${RESET}`;
|
|
328
|
+ |
}
|