@promptctl/rich-js 0.23.0 → 0.24.1
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/THIRD-PARTY-NOTICES +29 -1
- package/dist/core/animation.d.ts +78 -0
- package/dist/core/animation.js +107 -0
- package/dist/core/cell-widths.d.ts +9 -0
- package/dist/core/cell-widths.js +140 -0
- package/dist/core/cells.d.ts +17 -11
- package/dist/core/cells.js +100 -25
- package/dist/core/clock.d.ts +9 -1
- package/dist/core/clock.js +22 -2
- package/dist/core/color.d.ts +10 -0
- package/dist/core/color.js +36 -1
- package/dist/core/console.js +5 -5
- package/dist/core/easing.d.ts +138 -7
- package/dist/core/easing.js +355 -17
- package/dist/core/json.js +2 -1
- package/dist/core/measure.js +2 -2
- package/dist/core/place.js +2 -1
- package/dist/core/pretty.js +2 -2
- package/dist/core/protocol.d.ts +73 -19
- package/dist/core/protocol.js +56 -15
- package/dist/core/render.js +2 -1
- package/dist/core/segment.d.ts +28 -1
- package/dist/core/segment.js +60 -44
- package/dist/core/strip.d.ts +107 -29
- package/dist/core/strip.js +242 -131
- package/dist/core/style.d.ts +18 -1
- package/dist/core/style.js +49 -9
- package/dist/core/text.js +26 -15
- package/dist/core/timeline.d.ts +84 -0
- package/dist/core/timeline.js +144 -0
- package/dist/core/tween.d.ts +49 -0
- package/dist/core/tween.js +69 -0
- package/dist/host/app.js +4 -6
- package/dist/host/index.d.ts +1 -1
- package/dist/host/input.d.ts +37 -13
- package/dist/host/input.js +146 -56
- package/dist/host/terminal-host.d.ts +16 -0
- package/dist/host/terminal-host.js +1 -0
- package/dist/index.d.ts +16 -11
- package/dist/index.js +15 -5
- package/dist/node/terminal-host.d.ts +8 -1
- package/dist/node/terminal-host.js +2 -0
- package/dist/renderables/align.d.ts +3 -2
- package/dist/renderables/align.js +3 -0
- package/dist/renderables/columns.d.ts +3 -2
- package/dist/renderables/columns.js +7 -2
- package/dist/renderables/constrain.d.ts +3 -2
- package/dist/renderables/constrain.js +5 -1
- package/dist/renderables/effect.d.ts +83 -30
- package/dist/renderables/effect.js +155 -57
- package/dist/renderables/effects.d.ts +88 -39
- package/dist/renderables/effects.js +444 -105
- package/dist/renderables/embed.d.ts +10 -0
- package/dist/renderables/embed.js +18 -9
- package/dist/renderables/flexStrip.d.ts +3 -2
- package/dist/renderables/flexStrip.js +6 -1
- package/dist/renderables/frames.d.ts +116 -0
- package/dist/renderables/frames.js +133 -0
- package/dist/renderables/group.d.ts +3 -2
- package/dist/renderables/group.js +5 -2
- package/dist/renderables/html-entities.d.ts +8 -0
- package/dist/renderables/html-entities.js +386 -0
- package/dist/renderables/layout.d.ts +3 -2
- package/dist/renderables/layout.js +15 -2
- package/dist/renderables/list-view.d.ts +3 -2
- package/dist/renderables/list-view.js +12 -5
- package/dist/renderables/live.d.ts +23 -7
- package/dist/renderables/live.js +69 -35
- package/dist/renderables/markdown-parse.d.ts +109 -0
- package/dist/renderables/markdown-parse.js +1421 -0
- package/dist/renderables/markdown-url.d.ts +19 -0
- package/dist/renderables/markdown-url.js +314 -0
- package/dist/renderables/markdown.d.ts +7 -6
- package/dist/renderables/markdown.js +363 -499
- package/dist/renderables/padding.d.ts +8 -20
- package/dist/renderables/padding.js +32 -50
- package/dist/renderables/panel.d.ts +26 -26
- package/dist/renderables/panel.js +67 -103
- package/dist/renderables/progress.d.ts +5 -3
- package/dist/renderables/progress.js +14 -3
- package/dist/renderables/progressBar.js +4 -4
- package/dist/renderables/python-space.d.ts +13 -0
- package/dist/renderables/python-space.js +24 -0
- package/dist/renderables/spinner.d.ts +73 -27
- package/dist/renderables/spinner.js +110 -51
- package/dist/renderables/status.d.ts +8 -3
- package/dist/renderables/status.js +8 -5
- package/dist/renderables/syntax.js +2 -1
- package/dist/renderables/table.d.ts +19 -11
- package/dist/renderables/table.js +140 -156
- package/dist/renderables/traceback.d.ts +1 -1
- package/dist/renderables/traceback.js +6 -5
- package/dist/renderables/tree.d.ts +6 -5
- package/dist/renderables/tree.js +11 -7
- package/dist/renderables/viewport.d.ts +13 -5
- package/dist/renderables/viewport.js +41 -20
- package/dist/template-bindings/animation-funcs.d.ts +37 -0
- package/dist/template-bindings/animation-funcs.js +83 -0
- package/dist/template-bindings/color-funcs.d.ts +13 -0
- package/dist/template-bindings/color-funcs.js +1 -1
- package/dist/template-bindings/helpers.d.ts +6 -0
- package/dist/template-bindings/helpers.js +12 -3
- package/dist/template-bindings/index.d.ts +6 -2
- package/dist/template-bindings/index.js +8 -3
- package/dist/themes/ramp.d.ts +18 -3
- package/dist/themes/ramp.js +23 -2
- package/dist/widgets/focus-manager.d.ts +21 -14
- package/dist/widgets/focus-manager.js +28 -29
- package/dist/widgets/index.d.ts +1 -1
- package/dist/widgets/static-item.d.ts +4 -3
- package/dist/widgets/static-item.js +9 -7
- package/dist/widgets/text-input.d.ts +28 -18
- package/dist/widgets/text-input.js +144 -101
- package/dist/widgets/widget-app.d.ts +19 -3
- package/dist/widgets/widget-app.js +39 -21
- package/dist/widgets/widget-base.js +0 -3
- package/package.json +2 -5
|
@@ -1,494 +1,298 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Markdown — renders Markdown content to the terminal.
|
|
3
|
-
* Uses built-in parsing (no external dependency).
|
|
4
3
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
4
|
+
* `markdown-parse.ts` reads the source, once, when a `Markdown` is made; this
|
|
5
|
+
* module draws what it read. A blockquote and a list item are drawn beside a
|
|
6
|
+
* gutter — the quote's bar, the item's marker then its hang — and a table as
|
|
7
|
+
* Rich draws one. Where CommonMark leaves the drawing open, Rich's is
|
|
8
|
+
* followed: HTML, a block of it or a tag inline, is drawn as Rich draws it,
|
|
9
|
+
* not at all.
|
|
10
10
|
*/
|
|
11
|
-
import {
|
|
11
|
+
import { cellCount, cellLen } from "../core/cells.js";
|
|
12
12
|
import { Segment } from "../core/segment.js";
|
|
13
13
|
import { Style } from "../core/style.js";
|
|
14
14
|
import { RichText, resolveStyle } from "../core/text.js";
|
|
15
15
|
import { Rule } from "./rule.js";
|
|
16
|
-
import {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const ITEM_MARKER = /^(?:[*+-]|(\d{1,9})\.)/;
|
|
24
|
-
const LEAD = /^[ \t]*/;
|
|
25
|
-
const HARD_BREAK = /(?: {2,}|\\)$/;
|
|
26
|
-
const BLOCK_STARTS = [HEADING, FENCE, QUOTE, RULE];
|
|
27
|
-
/** Indentation that makes a line indented code when it opens a block, as CommonMark counts it. */
|
|
28
|
-
const CODE_INDENT = 4;
|
|
29
|
-
/** The tab stop code is drawn with, as Rich's `Syntax` draws it. */
|
|
30
|
-
const CODE_TAB_SIZE = parseTabSize(8);
|
|
31
|
-
const BLANK_LINE = { text: "", margin: 0, lazy: false };
|
|
32
|
-
/** The column `ws` ends at when it starts at column `from`, a tab reaching the next stop of 4 as CommonMark counts it. */
|
|
33
|
-
function advance(from, ws) {
|
|
34
|
-
return [...ws].reduce((col, ch) => (ch === "\t" ? col + 4 - (col % 4) : col + 1), from);
|
|
35
|
-
}
|
|
36
|
-
/** The column `line`'s leading whitespace ends at. */
|
|
37
|
-
function leadEnd(line) {
|
|
38
|
-
return advance(0, LEAD.exec(line.text)[0]);
|
|
39
|
-
}
|
|
40
|
-
/** How far `line` is indented inside its container. */
|
|
41
|
-
function indentOf(line) {
|
|
42
|
-
return Math.max(0, leadEnd(line) - line.margin);
|
|
43
|
-
}
|
|
44
|
-
/** `line` from its first character that is not leading whitespace. */
|
|
45
|
-
function body(line) {
|
|
46
|
-
return line.text.slice(LEAD.exec(line.text)[0].length);
|
|
47
|
-
}
|
|
48
|
-
function isBlank(line) {
|
|
49
|
-
return line.text.trim() === "";
|
|
50
|
-
}
|
|
51
|
-
/** Whether `line` sits less than a code indent into its container and its text matches `re`. */
|
|
52
|
-
function startsWith(line, re) {
|
|
53
|
-
return indentOf(line) < CODE_INDENT && re.test(body(line));
|
|
54
|
-
}
|
|
16
|
+
import { Padding } from "./padding.js";
|
|
17
|
+
import { pyRstrip } from "./python-space.js";
|
|
18
|
+
import { Syntax } from "./syntax.js";
|
|
19
|
+
import { Table } from "./table.js";
|
|
20
|
+
import { parseMarkdown } from "./markdown-parse.js";
|
|
21
|
+
import { SIMPLE } from "../core/box.js";
|
|
22
|
+
import { drawable, getStyle, groundedStyle, render, stackedHeight, withCellWidth } from "../core/protocol.js";
|
|
55
23
|
/**
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
24
|
+
* Every inline in `inlines` holding none, in document order, and each
|
|
25
|
+
* container's `leave` after its last, carrying the frame `open` made for it
|
|
26
|
+
* as `walk` reached it. A loop over a stack, not recursion: emphasis nests as
|
|
27
|
+
* deep as its source does, and Rich, walking markdown-it's flat token stream,
|
|
28
|
+
* draws ten thousand levels of it.
|
|
59
29
|
*/
|
|
60
|
-
function
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
for (
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const spacing = LEAD.exec(after)[0];
|
|
79
|
-
const rest = after.slice(spacing.length);
|
|
80
|
-
if (spacing === "" && rest !== "")
|
|
81
|
-
return undefined;
|
|
82
|
-
const markerEnd = leadEnd(line) + m[0].length;
|
|
83
|
-
const spaced = advance(markerEnd, spacing) - markerEnd;
|
|
84
|
-
const content = rest === "" || spaced > CODE_INDENT ? markerEnd + 1 : markerEnd + spaced;
|
|
85
|
-
const lead = line.text.slice(0, line.text.length - text.length);
|
|
86
|
-
return {
|
|
87
|
-
bullet: m[1] === undefined ? m[0] : undefined,
|
|
88
|
-
start: m[1] === undefined ? 0 : parseInt(m[1], 10),
|
|
89
|
-
content,
|
|
90
|
-
first: rest === "" ? undefined : { text: lead + " ".repeat(m[0].length) + after, margin: content, lazy: false },
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
/** Whether two item starts belong to one list: the same bullet character, or both numbered. */
|
|
94
|
-
function sameList(a, b) {
|
|
95
|
-
return a.bullet === b.bullet;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* A `>` line as a line of its quote: the marker blanked, and the quote's
|
|
99
|
-
* content starting past it and the one column of space that may follow it —
|
|
100
|
-
* one column of a tab, when a tab follows it.
|
|
101
|
-
*/
|
|
102
|
-
function quoted(line) {
|
|
103
|
-
const at = line.text.length - body(line).length;
|
|
104
|
-
const spaced = /[ \t]/.test(line.text[at + 1] ?? "");
|
|
105
|
-
return { text: `${line.text.slice(0, at)} ${line.text.slice(at + 1)}`, margin: leadEnd(line) + 1 + (spaced ? 1 : 0), lazy: false };
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Whether `line` opens a block of its own — a heading, a fence, a quote, a
|
|
109
|
-
* rule or a list item. A line that does not is text, and under an open
|
|
110
|
-
* paragraph it continues that paragraph.
|
|
111
|
-
*/
|
|
112
|
-
function opensBlock(line) {
|
|
113
|
-
return BLOCK_STARTS.some((re) => startsWith(line, re)) || itemStart(line) !== undefined;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Whether `line` ends a paragraph: `opensBlock`, read as CommonMark reads it
|
|
117
|
-
* under a paragraph. Indented code cannot interrupt one; a list item does only
|
|
118
|
-
* when it has text and, numbered, counts from 1, so prose wrapped onto "2024.
|
|
119
|
-
* That year" stays prose; and a line of `=` or `-` is the paragraph's heading
|
|
120
|
-
* underline.
|
|
121
|
-
*/
|
|
122
|
-
function endsParagraph(line) {
|
|
123
|
-
if (line.lazy)
|
|
124
|
-
return false;
|
|
125
|
-
if (underline(line))
|
|
126
|
-
return true;
|
|
127
|
-
const item = itemStart(line);
|
|
128
|
-
return item ? item.first !== undefined && (item.bullet !== undefined || item.start === 1) : opensBlock(line);
|
|
129
|
-
}
|
|
130
|
-
/** The setext underline `line` is — a line of `=` or `-` under a paragraph — if it is one. A lazy line never is. */
|
|
131
|
-
function underline(line) {
|
|
132
|
-
return line && !line.lazy && indentOf(line) < CODE_INDENT ? SETEXT.exec(body(line)) : null;
|
|
133
|
-
}
|
|
134
|
-
/** Whether the last block of `tokens`, followed into the containers it closes, is a paragraph still open. */
|
|
135
|
-
function endsInParagraph(tokens) {
|
|
136
|
-
const last = tokens[tokens.length - 1];
|
|
137
|
-
switch (last?.type) {
|
|
138
|
-
case "paragraph":
|
|
139
|
-
return true;
|
|
140
|
-
case "list_item":
|
|
141
|
-
case "blockquote":
|
|
142
|
-
return endsInParagraph(last.children);
|
|
143
|
-
default:
|
|
144
|
-
return false;
|
|
30
|
+
function* walk(inlines, open) {
|
|
31
|
+
// The containers open here, innermost last; `outside` is the level outside them all.
|
|
32
|
+
const outside = { inlines, next: 0 };
|
|
33
|
+
const stack = [];
|
|
34
|
+
for (;;) {
|
|
35
|
+
const top = stack[stack.length - 1] ?? outside;
|
|
36
|
+
const inline = top.inlines[top.next++];
|
|
37
|
+
if (inline === undefined) {
|
|
38
|
+
const left = stack.pop();
|
|
39
|
+
if (left === undefined)
|
|
40
|
+
return;
|
|
41
|
+
yield { kind: "leave", frame: left.frame };
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if ("children" in inline)
|
|
45
|
+
stack.push({ inlines: inline.children, next: 0, frame: open(inline) });
|
|
46
|
+
else
|
|
47
|
+
yield { kind: "leaf", inline };
|
|
145
48
|
}
|
|
146
49
|
}
|
|
147
|
-
/**
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
* text, which continues the paragraph open at the end of the container's
|
|
151
|
-
* `inner` lines and opens no block of its own. Outside the container a line
|
|
152
|
-
* is read without the paragraph under it, so any list item — whatever it
|
|
153
|
-
* counts from — ends the container rather than continuing it. A line after a
|
|
154
|
-
* lazy continuation finds that paragraph still open, so `afterLazy` answers
|
|
155
|
-
* without reading `inner` again: a container's lazy lines cost one read of
|
|
156
|
-
* it, not one each.
|
|
157
|
-
*/
|
|
158
|
-
function continuesLazily(inner, line, afterLazy) {
|
|
159
|
-
return !isBlank(line) && !opensBlock(line) && (afterLazy || endsInParagraph(tokenize(inner)));
|
|
50
|
+
/** The styles Rich enters for a link with hyperlinks on, outermost first. */
|
|
51
|
+
function linkStyles(url) {
|
|
52
|
+
return [new Style({ link: url }), "markdown.link_url"];
|
|
160
53
|
}
|
|
161
54
|
/**
|
|
162
|
-
*
|
|
163
|
-
*
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
55
|
+
* Rich's one inline HTML tag that draws: `<kbd>` exactly as written pushes
|
|
56
|
+
* `entry()` onto `stack`, and `</kbd>` pops the top of it — whatever is
|
|
57
|
+
* there, as Rich's style stack pops, so a close that crosses an emphasis
|
|
58
|
+
* ends that emphasis's style and the emphasis ends the key's. Every other tag
|
|
59
|
+
* draws nothing. `stack` is one block's, or one table cell's: a key left open
|
|
60
|
+
* ends with it, and a close with no key open in it pops nothing. Rich's one
|
|
61
|
+
* stack spans the document, so there a key left open styles every block after
|
|
62
|
+
* it, a block's own close pops the key's entry and leaves the block's style
|
|
63
|
+
* behind for the rest, and a stray close pops whatever is left or, with
|
|
64
|
+
* nothing left, raises. An HTML parser closes an inline element where its
|
|
65
|
+
* block ends, and that is the reading kept here.
|
|
167
66
|
*/
|
|
168
|
-
function
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
67
|
+
function kbdTags(stack, entry) {
|
|
68
|
+
let open = 0;
|
|
69
|
+
return (tag) => {
|
|
70
|
+
if (tag === "<kbd>") {
|
|
71
|
+
open++;
|
|
72
|
+
stack.push(entry());
|
|
73
|
+
}
|
|
74
|
+
else if (tag === "</kbd>" && open > 0) {
|
|
75
|
+
open--;
|
|
76
|
+
stack.pop();
|
|
77
|
+
}
|
|
78
|
+
};
|
|
180
79
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
* next one: it is empty.
|
|
187
|
-
*/
|
|
188
|
-
function itemLines(item, lines, start) {
|
|
189
|
-
const inner = item.first === undefined ? [] : [item.first];
|
|
190
|
-
let lazy = false;
|
|
191
|
-
let i = start + 1;
|
|
192
|
-
for (; i < lines.length; i++) {
|
|
193
|
-
const line = lines[i];
|
|
194
|
-
const blank = isBlank(line);
|
|
195
|
-
if (blank && inner.length === 0)
|
|
196
|
-
break;
|
|
197
|
-
const held = blank || leadEnd(line) >= item.content;
|
|
198
|
-
lazy = !held && continuesLazily(inner, line, lazy);
|
|
199
|
-
if (!held && !lazy)
|
|
200
|
-
break;
|
|
201
|
-
inner.push(held ? { text: line.text, margin: item.content, lazy: false } : { ...line, lazy: true });
|
|
202
|
-
}
|
|
203
|
-
for (; inner.length > 0 && isBlank(inner[inner.length - 1]); i--)
|
|
204
|
-
inner.pop();
|
|
205
|
-
return { inner, next: i };
|
|
80
|
+
const KBD_STYLE = "markdown.kbd";
|
|
81
|
+
/** The style a quote draws its bar in and lays under everything it holds. */
|
|
82
|
+
const QUOTE_STYLE = "markdown.block_quote";
|
|
83
|
+
function headingStyle(options, level) {
|
|
84
|
+
return getStyle(options, `markdown.h${level}`);
|
|
206
85
|
}
|
|
207
86
|
/**
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
*
|
|
87
|
+
* `inlines` appended to `result` as Rich draws each: emphasis, code and
|
|
88
|
+
* `<kbd>` in their styles, other inline HTML as nothing, an image as nothing
|
|
89
|
+
* where it sits — `drawImage` draws it, ahead of the block — and text as it is. A link's text
|
|
90
|
+
* is the link; or, without hyperlinks, nothing is, and its text is written
|
|
91
|
+
* plain with the URL after it, for a reader who cannot click it.
|
|
212
92
|
*/
|
|
213
|
-
function
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
const
|
|
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
|
-
continue;
|
|
260
|
-
}
|
|
261
|
-
// Heading
|
|
262
|
-
const headingMatch = HEADING.exec(body(line));
|
|
263
|
-
if (headingMatch) {
|
|
264
|
-
tokens.push({ type: "heading", level: headingMatch[1].length, text: headingMatch[2] });
|
|
265
|
-
i++;
|
|
266
|
-
continue;
|
|
267
|
-
}
|
|
268
|
-
// Horizontal rule
|
|
269
|
-
if (startsWith(line, RULE)) {
|
|
270
|
-
tokens.push({ type: "hr" });
|
|
271
|
-
i++;
|
|
272
|
-
continue;
|
|
93
|
+
function appendInline(result, inlines, settings, options) {
|
|
94
|
+
// Rich's style stack: each open construct's style laid over the styles it
|
|
95
|
+
// sits in, so every piece of text takes one span, the style on top, and a
|
|
96
|
+
// nest ten thousand deep costs one combination a level.
|
|
97
|
+
const stack = [Style.null()];
|
|
98
|
+
// Inside a link drawn without hyperlinks, whose text is styled whole as it closes, nothing is styled.
|
|
99
|
+
let plain = false;
|
|
100
|
+
// The style on top with `styles` laid over it.
|
|
101
|
+
const over = (styles) => styles.reduce((under, style) => under.add(resolveStyle(options, style)), stack[stack.length - 1]);
|
|
102
|
+
const kbd = kbdTags(stack, () => over([KBD_STYLE]));
|
|
103
|
+
// The text from `start` to the end in the style on top, with `styles` over it.
|
|
104
|
+
const paint = (start, styles = []) => {
|
|
105
|
+
if (!plain)
|
|
106
|
+
result.stylize(over(styles), start);
|
|
107
|
+
};
|
|
108
|
+
const write = (text, styles) => {
|
|
109
|
+
const start = result.length;
|
|
110
|
+
result.append(text);
|
|
111
|
+
paint(start, styles);
|
|
112
|
+
};
|
|
113
|
+
const enter = (styles) => {
|
|
114
|
+
stack.push(over(styles));
|
|
115
|
+
return () => stack.pop();
|
|
116
|
+
};
|
|
117
|
+
// A link to `url` opened here: its text is the link, or without hyperlinks
|
|
118
|
+
// is written plain, in `markdown.link`, with the URL after it when it closes.
|
|
119
|
+
const openLink = (url) => {
|
|
120
|
+
if (settings.hyperlinks)
|
|
121
|
+
return enter(linkStyles(url));
|
|
122
|
+
const [start, outer] = [result.length, plain];
|
|
123
|
+
plain = true;
|
|
124
|
+
return () => {
|
|
125
|
+
plain = outer;
|
|
126
|
+
paint(start, ["markdown.link"]);
|
|
127
|
+
write(" (");
|
|
128
|
+
write(url, ["markdown.link_url"]);
|
|
129
|
+
write(")");
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
// Each construct's frame is what closing it does.
|
|
133
|
+
const open = (container) => {
|
|
134
|
+
switch (container.kind) {
|
|
135
|
+
case "emphasis":
|
|
136
|
+
return enter([`markdown.${container.tag}`]);
|
|
137
|
+
case "link":
|
|
138
|
+
return openLink(container.url);
|
|
273
139
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
for (i++; i < lines.length && !startsWith(lines[i], FENCE); i++)
|
|
279
|
-
codeLines.push(dropColumns(lines[i], indent));
|
|
280
|
-
i++; // skip closing ```
|
|
281
|
-
tokens.push({ type: "code_block", code: codeLines.join("\n") });
|
|
140
|
+
};
|
|
141
|
+
for (const step of walk(inlines, open)) {
|
|
142
|
+
if (step.kind === "leave") {
|
|
143
|
+
step.frame();
|
|
282
144
|
continue;
|
|
283
145
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
146
|
+
const inline = step.inline;
|
|
147
|
+
switch (inline.kind) {
|
|
148
|
+
case "text":
|
|
149
|
+
write(inline.text);
|
|
150
|
+
break;
|
|
151
|
+
// Rich writes a soft break as a space and a hard one as a newline.
|
|
152
|
+
case "softbreak":
|
|
153
|
+
write(" ");
|
|
154
|
+
break;
|
|
155
|
+
case "hardbreak":
|
|
156
|
+
write("\n");
|
|
157
|
+
break;
|
|
158
|
+
case "code":
|
|
159
|
+
write(inline.code, [groundedStyle(options, resolveStyle(options, settings.inlineCodeStyle))]);
|
|
160
|
+
break;
|
|
161
|
+
case "html":
|
|
162
|
+
kbd(inline.content);
|
|
163
|
+
break;
|
|
164
|
+
case "image":
|
|
165
|
+
break;
|
|
166
|
+
case "autolink": {
|
|
167
|
+
const close = openLink(inline.url);
|
|
168
|
+
write(inline.text);
|
|
169
|
+
close();
|
|
170
|
+
break;
|
|
296
171
|
}
|
|
297
|
-
tokens.push({ type: "blockquote", children: tokenize(inner) });
|
|
298
|
-
continue;
|
|
299
|
-
}
|
|
300
|
-
// List: its items, each Markdown of its own, as a quote is
|
|
301
|
-
const item = itemStart(line);
|
|
302
|
-
if (item) {
|
|
303
|
-
const { tokens: items, next } = list(item, lines, i);
|
|
304
|
-
tokens.push(...items);
|
|
305
|
-
i = next;
|
|
306
|
-
continue;
|
|
307
172
|
}
|
|
308
|
-
// Paragraph, or a setext heading when a line of `=` or `-` underlines it
|
|
309
|
-
const { text, next } = continuation(lines, i);
|
|
310
|
-
const setext = underline(lines[next]);
|
|
311
|
-
tokens.push(setext
|
|
312
|
-
? { type: "heading", level: setext[1].startsWith("=") ? 1 : 2, text }
|
|
313
|
-
: { type: "paragraph", text });
|
|
314
|
-
i = setext ? next + 1 : next;
|
|
315
173
|
}
|
|
316
|
-
return tokens;
|
|
317
|
-
}
|
|
318
|
-
const TICKS = /`+/y;
|
|
319
|
-
const STRONG = /\*\*(.+?)\*\*/y;
|
|
320
|
-
const EMPHASIS = /\*(.+?)\*/y;
|
|
321
|
-
const ANGLED_DESTINATION = /\(\s*<([^<>\n]*)>/y;
|
|
322
|
-
const LINK_TAIL = /\s*(?:"[^"]*"|'[^']*'|\([^()]*\))?\s*\)/y;
|
|
323
|
-
/** `re`, a sticky pattern, matched exactly at `i` in `text`. */
|
|
324
|
-
function matchAt(re, text, i) {
|
|
325
|
-
re.lastIndex = i;
|
|
326
|
-
return re.exec(text);
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* Each `[` in `text` that a `]` closes, to the index of that `]`: brackets
|
|
330
|
-
* nest, and a backslash escapes the character after it. One pass, so a run of
|
|
331
|
-
* text full of brackets that close nothing costs no more than one without.
|
|
332
|
-
*/
|
|
333
|
-
function bracketPairs(text) {
|
|
334
|
-
const pairs = new Map();
|
|
335
|
-
const open = [];
|
|
336
|
-
for (let i = 0; i < text.length; i++) {
|
|
337
|
-
if (text[i] === "\\")
|
|
338
|
-
i++;
|
|
339
|
-
else if (text[i] === "[")
|
|
340
|
-
open.push(i);
|
|
341
|
-
else if (text[i] === "]" && open.length > 0)
|
|
342
|
-
pairs.set(open.pop(), i);
|
|
343
|
-
}
|
|
344
|
-
return pairs;
|
|
345
174
|
}
|
|
175
|
+
const EXIT = { kind: "exit", newLine: true };
|
|
346
176
|
/**
|
|
347
|
-
* The
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
177
|
+
* The steps in a block's text. An image is an element, Rich's `ImageItem`,
|
|
178
|
+
* which sets no `new_line`. Text with no inlines is one too: markdown-it
|
|
179
|
+
* leaves an empty inline token unflattened, and Rich exits it as an element
|
|
180
|
+
* it draws nothing for.
|
|
351
181
|
*/
|
|
352
|
-
function
|
|
353
|
-
if (
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
else if (text[i] === "(")
|
|
371
|
-
depth++;
|
|
372
|
-
else if (text[i] === ")")
|
|
373
|
-
depth--;
|
|
374
|
-
}
|
|
375
|
-
url = text.slice(from, i);
|
|
376
|
-
}
|
|
377
|
-
const tail = matchAt(LINK_TAIL, text, i);
|
|
378
|
-
return tail ? { url, end: i + tail[0].length } : undefined;
|
|
379
|
-
}
|
|
380
|
-
/** The inline construct opening at `i` in `text`, if one does; `brackets` is `text`'s `bracketPairs`. */
|
|
381
|
-
function inlineAt(text, i, brackets) {
|
|
382
|
-
switch (text[i]) {
|
|
383
|
-
case "`": {
|
|
384
|
-
const ticks = matchAt(TICKS, text, i)[0];
|
|
385
|
-
const close = new RegExp(`(?<!\`)${ticks}(?!\`)`, "g");
|
|
386
|
-
close.lastIndex = i + ticks.length;
|
|
387
|
-
const found = close.exec(text);
|
|
388
|
-
return found
|
|
389
|
-
? { kind: "code", code: text.slice(i + ticks.length, found.index), end: found.index + ticks.length }
|
|
390
|
-
: { kind: "literal", text: ticks, end: i + ticks.length };
|
|
391
|
-
}
|
|
392
|
-
case "*": {
|
|
393
|
-
const strong = matchAt(STRONG, text, i);
|
|
394
|
-
const emphasis = strong ?? matchAt(EMPHASIS, text, i);
|
|
395
|
-
return emphasis
|
|
396
|
-
? { kind: "emphasis", style: strong ? "bold" : "italic", inner: emphasis[1], end: i + emphasis[0].length }
|
|
397
|
-
: undefined;
|
|
182
|
+
function* inlineSteps(inlines, within, settings) {
|
|
183
|
+
if (inlines.length === 0)
|
|
184
|
+
yield EXIT;
|
|
185
|
+
// Rich's style stack: what each open construct entered, innermost last, one entry each as Rich pushes one.
|
|
186
|
+
const stack = [within];
|
|
187
|
+
const kbd = kbdTags(stack, () => [KBD_STYLE]);
|
|
188
|
+
// Each construct's frame is how many entries it pushed.
|
|
189
|
+
const open = (container) => {
|
|
190
|
+
switch (container.kind) {
|
|
191
|
+
case "emphasis":
|
|
192
|
+
stack.push([`markdown.${container.tag}`]);
|
|
193
|
+
return 1;
|
|
194
|
+
case "link": {
|
|
195
|
+
// Without hyperlinks Rich enters no style for a link.
|
|
196
|
+
const entered = settings.hyperlinks ? [linkStyles(container.url)] : [];
|
|
197
|
+
stack.push(...entered);
|
|
198
|
+
return entered.length;
|
|
199
|
+
}
|
|
398
200
|
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
return image
|
|
409
|
-
? { kind: "image", alt: label, src: destination.url, end: destination.end }
|
|
410
|
-
: { kind: "link", inner: label, url: destination.url, end: destination.end };
|
|
201
|
+
};
|
|
202
|
+
for (const step of walk(inlines, open)) {
|
|
203
|
+
if (step.kind === "leave")
|
|
204
|
+
stack.length -= step.frame;
|
|
205
|
+
else if (step.inline.kind === "html")
|
|
206
|
+
kbd(step.inline.content);
|
|
207
|
+
else if (step.inline.kind === "image") {
|
|
208
|
+
yield { kind: "image", image: { alt: step.inline.alt, src: step.inline.src, styles: stack.flat() } };
|
|
209
|
+
yield { kind: "exit", newLine: false };
|
|
411
210
|
}
|
|
412
|
-
default:
|
|
413
|
-
return undefined;
|
|
414
211
|
}
|
|
415
212
|
}
|
|
416
213
|
/**
|
|
417
|
-
*
|
|
418
|
-
*
|
|
419
|
-
* draws a nested construct — the inner style on top.
|
|
214
|
+
* The step Rich takes exiting `token`'s element: every element sets
|
|
215
|
+
* `new_line` but a rule, which draws its own blank row after it.
|
|
420
216
|
*/
|
|
421
|
-
function
|
|
422
|
-
|
|
423
|
-
appendInline(result, text, settings, options);
|
|
424
|
-
return result;
|
|
217
|
+
function exitStep(token) {
|
|
218
|
+
return { kind: "exit", newLine: token.type !== "hr" };
|
|
425
219
|
}
|
|
426
220
|
/**
|
|
427
|
-
*
|
|
428
|
-
*
|
|
429
|
-
*
|
|
221
|
+
* The steps inside `token`, up to and not including its own exit: Rich
|
|
222
|
+
* checks `new_line` ahead of drawing a block before it exits it. A table's
|
|
223
|
+
* cells are elements of their own, the header's first.
|
|
430
224
|
*/
|
|
431
|
-
function
|
|
432
|
-
const
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
if (!found) {
|
|
437
|
-
i++;
|
|
438
|
-
continue;
|
|
225
|
+
function* stepsInside(token, within, options, settings) {
|
|
226
|
+
const children = function* (blocks, inner) {
|
|
227
|
+
for (const child of blocks) {
|
|
228
|
+
yield* stepsInside(child, inner, options, settings);
|
|
229
|
+
yield exitStep(child);
|
|
439
230
|
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
// As Rich draws it: without hyperlinks the URL is written out, and it
|
|
453
|
-
// is still the link, for a terminal that can click it.
|
|
454
|
-
const link = new RichText("", { style: new Style({ link: found.url }) });
|
|
455
|
-
if (settings.hyperlinks) {
|
|
456
|
-
result.append(link.append(styled(found.inner, "markdown.link_url", settings, options)));
|
|
457
|
-
}
|
|
458
|
-
else {
|
|
459
|
-
result
|
|
460
|
-
.append(styled(found.inner, "markdown.link", settings, options))
|
|
461
|
-
.append(" (")
|
|
462
|
-
.append(link.append(found.url, "markdown.link_url"))
|
|
463
|
-
.append(")");
|
|
464
|
-
}
|
|
465
|
-
break;
|
|
231
|
+
};
|
|
232
|
+
switch (token.type) {
|
|
233
|
+
case "paragraph":
|
|
234
|
+
yield* inlineSteps(token.text, within, settings);
|
|
235
|
+
break;
|
|
236
|
+
case "heading":
|
|
237
|
+
yield* inlineSteps(token.text, [...within, headingStyle(options, token.level)], settings);
|
|
238
|
+
break;
|
|
239
|
+
case "list":
|
|
240
|
+
for (const item of token.items) {
|
|
241
|
+
yield* children(item, within);
|
|
242
|
+
yield EXIT;
|
|
466
243
|
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
244
|
+
break;
|
|
245
|
+
case "blockquote":
|
|
246
|
+
yield* children(token.children, [...within, QUOTE_STYLE]);
|
|
247
|
+
break;
|
|
248
|
+
case "table":
|
|
249
|
+
for (const cell of [...token.columns.map(({ header }) => header), ...token.rows.flat()]) {
|
|
250
|
+
yield* inlineSteps(cell, within, settings);
|
|
251
|
+
yield EXIT;
|
|
474
252
|
}
|
|
475
|
-
|
|
476
|
-
|
|
253
|
+
break;
|
|
254
|
+
case "code_block":
|
|
255
|
+
case "html_block":
|
|
256
|
+
case "hr":
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* The steps of the whole document. Rich draws a block at the top of it as it
|
|
262
|
+
* exits, so a block is drawn where its exit is.
|
|
263
|
+
*/
|
|
264
|
+
function* documentSteps(tokens, options, settings) {
|
|
265
|
+
for (const token of tokens) {
|
|
266
|
+
yield* stepsInside(token, [], options, settings);
|
|
267
|
+
yield { kind: "block", token };
|
|
268
|
+
yield exitStep(token);
|
|
477
269
|
}
|
|
478
|
-
result.append(text.slice(plain));
|
|
479
270
|
}
|
|
480
271
|
/**
|
|
481
|
-
*
|
|
272
|
+
* An image drawn as Rich's `ImageItem` draws one: a picture glyph, then the
|
|
273
|
+
* alt text in the styles it sits in — or, with none, the image's file name,
|
|
274
|
+
* unstyled — linked to the link those styles carry or else to the image, then a
|
|
275
|
+
* space, at the full width, ending no row — so whatever is drawn next carries
|
|
276
|
+
* on after it.
|
|
277
|
+
*/
|
|
278
|
+
function drawImage(image, options, settings) {
|
|
279
|
+
// Rich reads the link off its style stack, so a link a crossed `</kbd>` left open still holds.
|
|
280
|
+
const style = image.styles.reduce((under, over) => under.add(resolveStyle(options, over)), Style.null());
|
|
281
|
+
const title = image.alt ? new RichText(image.alt).stylize(style) : new RichText(image.src.replace(/\/+$/, "").split("/").pop() ?? "");
|
|
282
|
+
if (settings.hyperlinks)
|
|
283
|
+
title.stylize(new Style({ link: style.link || image.src || undefined }));
|
|
284
|
+
return render(new RichText("", { end: "" }).append(drawable(options, "🌆 ", "")).append(title).append(" "), options);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* What `content` renders in what is left of the width beside a gutter, each of
|
|
482
288
|
* its rows ended. The first row's gutter is `first` and every later row's is
|
|
483
289
|
* `rest`, so a list item's text hangs clear of its bullet and a quote's bar
|
|
484
290
|
* runs its full height. A block with no gutter passes two empty segments:
|
|
485
|
-
* every block is laid out the same way.
|
|
486
|
-
* column
|
|
487
|
-
* swallows what it introduces.
|
|
291
|
+
* every block is laid out the same way. A gutter that leaves its content no
|
|
292
|
+
* column draws nothing beside it, since that content draws no rows.
|
|
488
293
|
*/
|
|
489
|
-
function* guttered(
|
|
490
|
-
const
|
|
491
|
-
const rows = Segment.splitLines(Segment.applyStyle([...draw({ ...options, maxWidth: width })], style));
|
|
294
|
+
function* guttered(content, options, first, rest) {
|
|
295
|
+
const rows = Segment.splitLines(render(content, { ...options, maxWidth: options.maxWidth - cellLen(first.text) }));
|
|
492
296
|
for (const [row, line] of rows.entries()) {
|
|
493
297
|
yield row === 0 ? first : rest;
|
|
494
298
|
yield* line;
|
|
@@ -502,66 +306,111 @@ function* guttered(draw, options, first, rest, style) {
|
|
|
502
306
|
* and a heading's padding is not underlined.
|
|
503
307
|
*/
|
|
504
308
|
function inline(text, settings, justify, style) {
|
|
505
|
-
return
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
309
|
+
return {
|
|
310
|
+
*render(options) {
|
|
311
|
+
const body = new RichText("", { end: "", style });
|
|
312
|
+
appendInline(body, text, settings, options);
|
|
313
|
+
// `append` lays the body's base style down as the first span.
|
|
314
|
+
yield* render(new RichText("", { end: "", justify }).append(body), options);
|
|
315
|
+
yield Segment.line();
|
|
316
|
+
},
|
|
511
317
|
};
|
|
512
318
|
}
|
|
513
319
|
const NO_GUTTER = new Segment("");
|
|
320
|
+
/**
|
|
321
|
+
* `tokens` drawn as Rich's `render_lines` draws a quote's or an item's
|
|
322
|
+
* content: in `style`, every row padded to the width in it and ended.
|
|
323
|
+
*/
|
|
324
|
+
function* renderLines(tokens, options, settings, style) {
|
|
325
|
+
for (const line of Segment.splitLines(Segment.applyStyle(renderTokens(tokens, options, settings), style))) {
|
|
326
|
+
yield* Segment.adjustLineLength(line, options.maxWidth, style);
|
|
327
|
+
yield Segment.line();
|
|
328
|
+
}
|
|
329
|
+
}
|
|
514
330
|
/**
|
|
515
331
|
* Each block in `tokens`, every row it draws ended, body text placed by
|
|
516
332
|
* `settings.justify`. A heading keeps its own placement whatever `settings`
|
|
517
|
-
* or `options` say — Rich's
|
|
518
|
-
* style stops where its
|
|
333
|
+
* or `options` say — Rich's `Heading.LEVEL_ALIGN`: an h1 centred, every other
|
|
334
|
+
* level left, each row padded to the width — and its style stops where its
|
|
335
|
+
* text does.
|
|
519
336
|
*/
|
|
520
337
|
function* renderTokens(tokens, options, settings) {
|
|
521
|
-
for (const token of tokens)
|
|
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
|
-
|
|
338
|
+
for (const token of tokens)
|
|
339
|
+
yield* renderBlock(token, options, settings);
|
|
340
|
+
}
|
|
341
|
+
function* renderBlock(token, options, settings) {
|
|
342
|
+
switch (token.type) {
|
|
343
|
+
case "heading": {
|
|
344
|
+
const draw = inline(token.text, settings, token.level === 1 ? "center" : "left", headingStyle(options, token.level));
|
|
345
|
+
yield* guttered(draw, options, NO_GUTTER, NO_GUTTER);
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
case "paragraph": {
|
|
349
|
+
yield* guttered(inline(token.text, settings, settings.justify), options, NO_GUTTER, NO_GUTTER);
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
case "code_block": {
|
|
353
|
+
// Rich's CodeBlock: the code, its trailing whitespace dropped, handed to
|
|
354
|
+
// `Syntax` word-wrapped inside a padding of one cell all round — every
|
|
355
|
+
// cell of the block, padding and all, in the code's style.
|
|
356
|
+
const style = groundedStyle(options, getStyle(options, "markdown.code"));
|
|
357
|
+
const syntax = new Syntax(pyRstrip(token.code), "text", { wordWrap: true });
|
|
358
|
+
yield* render(new Padding(syntax, 1, { style }), options);
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
case "hr": {
|
|
362
|
+
// Rich's HorizontalRule: the rule, drawn in `-` rather than Rule's own
|
|
363
|
+
// glyph, then a blank row of its own.
|
|
364
|
+
const rule = new Rule(undefined, { characters: "-", style: "markdown.hr" });
|
|
365
|
+
yield* render(rule, options);
|
|
366
|
+
yield Segment.line();
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
369
|
+
case "list": {
|
|
370
|
+
// Rich's ListItem: a bullet, or the number without its `.` or `)`,
|
|
371
|
+
// right-aligned in a column sized for the number one past the last
|
|
372
|
+
// item's — Rich's `last_number` — and the hang under it in the marker's
|
|
373
|
+
// style.
|
|
374
|
+
const { start } = token;
|
|
375
|
+
const markerStyle = getStyle(options, start === undefined ? "markdown.item.bullet" : "markdown.item.number");
|
|
376
|
+
const width = start === undefined ? 3 : String(start + token.items.length).length + 2;
|
|
377
|
+
const hang = new Segment(" ".repeat(width), markerStyle);
|
|
378
|
+
for (const [n, item] of token.items.entries()) {
|
|
379
|
+
const marker = start === undefined ? drawable(options, " • ", " * ") : `${String(start + n).padStart(width - 1)} `;
|
|
380
|
+
const body = { render: (inner) => renderLines(item, inner, settings, getStyle(options, "markdown.item")) };
|
|
381
|
+
yield* guttered(body, options, new Segment(marker, markerStyle), hang);
|
|
560
382
|
}
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
case "blockquote": {
|
|
386
|
+
// Rich's BlockQuote: the bar and everything the quote holds in the one
|
|
387
|
+
// style, the content laid out four cells short of the width beside a
|
|
388
|
+
// bar of two, so every row of a quote ends two cells short.
|
|
389
|
+
const style = getStyle(options, QUOTE_STYLE);
|
|
390
|
+
const bar = new Segment(drawable(options, "▌ ", "| "), style);
|
|
391
|
+
const body = { render: (inner) => renderLines(token.children, inner, settings, style) };
|
|
392
|
+
yield* guttered(body, { ...options, maxWidth: cellCount(options.maxWidth - 2) }, bar, bar);
|
|
393
|
+
break;
|
|
394
|
+
}
|
|
395
|
+
case "table": {
|
|
396
|
+
// Rich's TableElement: each cell inline Markdown placed by its
|
|
397
|
+
// column's alignment, the header's in `markdown.table.header` on top.
|
|
398
|
+
const cell = (text, justify) => {
|
|
399
|
+
const content = new RichText("", { justify });
|
|
400
|
+
appendInline(content, text, settings, options);
|
|
401
|
+
return content;
|
|
402
|
+
};
|
|
403
|
+
const table = new Table({ box: SIMPLE, padEdge: false, style: "markdown.table.border", showEdge: true, collapsePadding: true });
|
|
404
|
+
for (const { header, align } of token.columns)
|
|
405
|
+
table.addColumn(cell(header, align).stylize("markdown.table.header"));
|
|
406
|
+
for (const row of token.rows)
|
|
407
|
+
table.addRow(...row.map((text, n) => cell(text, token.columns[n].align)));
|
|
408
|
+
yield* render(table, options);
|
|
409
|
+
break;
|
|
564
410
|
}
|
|
411
|
+
case "html_block":
|
|
412
|
+
// Rich draws nothing for it.
|
|
413
|
+
break;
|
|
565
414
|
}
|
|
566
415
|
}
|
|
567
416
|
export class Markdown {
|
|
@@ -569,19 +418,34 @@ export class Markdown {
|
|
|
569
418
|
inlineCodeStyle;
|
|
570
419
|
hyperlinks;
|
|
571
420
|
justify;
|
|
421
|
+
// Read once, as Rich's Markdown parses its markup when it is made: nothing a
|
|
422
|
+
// render is asked for changes how the source reads, and a Live redraws it
|
|
423
|
+
// every frame.
|
|
424
|
+
parsed;
|
|
572
425
|
constructor(markdown, options) {
|
|
573
426
|
this.markdown = markdown;
|
|
427
|
+
this.parsed = parseMarkdown(markdown);
|
|
574
428
|
this.inlineCodeStyle = options?.inlineCodeStyle ?? "markdown.code";
|
|
575
429
|
this.hyperlinks = options?.hyperlinks !== false;
|
|
576
430
|
this.justify = options?.justify ?? "left";
|
|
577
431
|
}
|
|
578
432
|
*render(rawOptions) {
|
|
579
|
-
//
|
|
580
|
-
const options = { ...rawOptions, height: stackedHeight(rawOptions.height) };
|
|
581
|
-
|
|
582
|
-
|
|
433
|
+
// The width parsed once, for every block below, each one of a stack.
|
|
434
|
+
const options = { ...withCellWidth(rawOptions), height: stackedHeight(rawOptions.height) };
|
|
435
|
+
// Rich's `new_line`: set by each element as it exits, read before anything is drawn.
|
|
436
|
+
let newLine = false;
|
|
437
|
+
for (const step of documentSteps(this.parsed, options, this)) {
|
|
438
|
+
if (step.kind === "exit") {
|
|
439
|
+
newLine = step.newLine;
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
if (newLine)
|
|
443
|
+
yield Segment.line();
|
|
444
|
+
yield* step.kind === "image" ? drawImage(step.image, options, this) : renderBlock(step.token, options, this);
|
|
445
|
+
}
|
|
583
446
|
}
|
|
584
447
|
measure(options) {
|
|
585
|
-
|
|
448
|
+
// Rich's Markdown has no `__rich_measure__`, so Rich measures it as nothing up to the width.
|
|
449
|
+
return { minimum: 0, maximum: options.maxWidth };
|
|
586
450
|
}
|
|
587
451
|
}
|