@promptctl/rich-js 0.24.0 → 0.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/README.md +7 -0
  2. package/THIRD-PARTY-NOTICES +29 -1
  3. package/dist/core/anchor.d.ts +5 -0
  4. package/dist/core/anchor.js +11 -4
  5. package/dist/core/animation.d.ts +243 -0
  6. package/dist/core/animation.js +375 -0
  7. package/dist/core/ansi.js +2 -2
  8. package/dist/core/box.d.ts +7 -7
  9. package/dist/core/cell-widths.d.ts +9 -0
  10. package/dist/core/cell-widths.js +140 -0
  11. package/dist/core/cells.d.ts +40 -24
  12. package/dist/core/cells.js +151 -66
  13. package/dist/core/clock.d.ts +2 -2
  14. package/dist/core/clock.js +6 -4
  15. package/dist/core/color.d.ts +27 -10
  16. package/dist/core/color.js +21 -29
  17. package/dist/core/console.d.ts +28 -17
  18. package/dist/core/console.js +48 -43
  19. package/dist/core/easing.d.ts +140 -10
  20. package/dist/core/easing.js +328 -50
  21. package/dist/core/emoji.d.ts +1 -1
  22. package/dist/core/emoji.js +1 -2
  23. package/dist/core/export-html.js +2 -1
  24. package/dist/core/export-lines.js +3 -3
  25. package/dist/core/highlighter.d.ts +9 -8
  26. package/dist/core/highlighter.js +3 -2
  27. package/dist/core/json.js +3 -2
  28. package/dist/core/markup.js +16 -12
  29. package/dist/core/measure.js +3 -3
  30. package/dist/core/memo.d.ts +11 -2
  31. package/dist/core/memo.js +16 -0
  32. package/dist/core/oklch.js +17 -6
  33. package/dist/core/osc8.d.ts +1 -1
  34. package/dist/core/osc8.js +4 -5
  35. package/dist/core/paint.d.ts +2 -1
  36. package/dist/core/paint.js +4 -3
  37. package/dist/core/pattern.d.ts +21 -0
  38. package/dist/core/pattern.js +21 -0
  39. package/dist/core/place.js +2 -1
  40. package/dist/core/pretty.js +11 -12
  41. package/dist/core/protocol.d.ts +136 -21
  42. package/dist/core/protocol.js +203 -16
  43. package/dist/core/render.d.ts +15 -2
  44. package/dist/core/render.js +27 -6
  45. package/dist/core/segment.d.ts +31 -6
  46. package/dist/core/segment.js +68 -53
  47. package/dist/core/spinnerData.d.ts +1 -1
  48. package/dist/core/state.d.ts +53 -0
  49. package/dist/core/state.js +82 -0
  50. package/dist/core/strip.d.ts +11 -13
  51. package/dist/core/strip.js +126 -113
  52. package/dist/core/style.d.ts +47 -14
  53. package/dist/core/style.js +74 -34
  54. package/dist/core/text.d.ts +5 -8
  55. package/dist/core/text.js +133 -133
  56. package/dist/core/timeline.d.ts +109 -0
  57. package/dist/core/timeline.js +180 -0
  58. package/dist/core/tween.d.ts +49 -0
  59. package/dist/core/tween.js +71 -0
  60. package/dist/core/view.d.ts +92 -0
  61. package/dist/core/view.js +83 -0
  62. package/dist/host/app.d.ts +49 -21
  63. package/dist/host/app.js +72 -38
  64. package/dist/host/input.js +3 -4
  65. package/dist/index.d.ts +30 -15
  66. package/dist/index.js +20 -6
  67. package/dist/node/traceback.js +4 -3
  68. package/dist/renderables/change.d.ts +42 -0
  69. package/dist/renderables/change.js +112 -0
  70. package/dist/renderables/columns.d.ts +1 -1
  71. package/dist/renderables/columns.js +3 -3
  72. package/dist/renderables/constrain.js +2 -1
  73. package/dist/renderables/effect.d.ts +155 -92
  74. package/dist/renderables/effect.js +203 -136
  75. package/dist/renderables/effects.d.ts +137 -24
  76. package/dist/renderables/effects.js +276 -113
  77. package/dist/renderables/embed.d.ts +1 -1
  78. package/dist/renderables/embed.js +18 -9
  79. package/dist/renderables/flexStrip.js +2 -1
  80. package/dist/renderables/frames.d.ts +45 -43
  81. package/dist/renderables/frames.js +57 -45
  82. package/dist/renderables/glyph-effect.d.ts +158 -0
  83. package/dist/renderables/glyph-effect.js +237 -0
  84. package/dist/renderables/glyph-effects.d.ts +147 -0
  85. package/dist/renderables/glyph-effects.js +283 -0
  86. package/dist/renderables/group.d.ts +1 -1
  87. package/dist/renderables/group.js +2 -2
  88. package/dist/renderables/html-entities.d.ts +8 -0
  89. package/dist/renderables/html-entities.js +386 -0
  90. package/dist/renderables/layout.d.ts +10 -9
  91. package/dist/renderables/layout.js +42 -35
  92. package/dist/renderables/lifecycle.d.ts +77 -0
  93. package/dist/renderables/lifecycle.js +132 -0
  94. package/dist/renderables/list-view.d.ts +2 -2
  95. package/dist/renderables/list-view.js +11 -8
  96. package/dist/renderables/live.d.ts +36 -21
  97. package/dist/renderables/live.js +89 -42
  98. package/dist/renderables/markdown-parse.d.ts +109 -0
  99. package/dist/renderables/markdown-parse.js +1427 -0
  100. package/dist/renderables/markdown-url.d.ts +19 -0
  101. package/dist/renderables/markdown-url.js +314 -0
  102. package/dist/renderables/markdown.d.ts +5 -7
  103. package/dist/renderables/markdown.js +356 -683
  104. package/dist/renderables/padding.d.ts +7 -20
  105. package/dist/renderables/padding.js +29 -50
  106. package/dist/renderables/panel.d.ts +30 -32
  107. package/dist/renderables/panel.js +65 -109
  108. package/dist/renderables/progress.d.ts +83 -32
  109. package/dist/renderables/progress.js +186 -90
  110. package/dist/renderables/progressBar.d.ts +22 -12
  111. package/dist/renderables/progressBar.js +33 -10
  112. package/dist/renderables/prompt.js +3 -2
  113. package/dist/renderables/python-space.d.ts +13 -0
  114. package/dist/renderables/python-space.js +25 -0
  115. package/dist/renderables/scroll.d.ts +50 -0
  116. package/dist/renderables/scroll.js +106 -0
  117. package/dist/renderables/spinner.d.ts +57 -24
  118. package/dist/renderables/spinner.js +101 -49
  119. package/dist/renderables/status.js +1 -1
  120. package/dist/renderables/syntax.d.ts +10 -9
  121. package/dist/renderables/syntax.js +5 -3
  122. package/dist/renderables/table.d.ts +48 -46
  123. package/dist/renderables/table.js +170 -208
  124. package/dist/renderables/traceback.d.ts +3 -3
  125. package/dist/renderables/traceback.js +12 -10
  126. package/dist/renderables/tree.d.ts +28 -25
  127. package/dist/renderables/tree.js +108 -58
  128. package/dist/renderables/viewport.d.ts +77 -38
  129. package/dist/renderables/viewport.js +156 -72
  130. package/dist/template-bindings/animation-funcs.d.ts +78 -9
  131. package/dist/template-bindings/animation-funcs.js +132 -14
  132. package/dist/template-bindings/index.d.ts +5 -5
  133. package/dist/template-bindings/index.js +7 -6
  134. package/dist/themes/ramp.d.ts +62 -4
  135. package/dist/themes/ramp.js +82 -4
  136. package/dist/themes/registry.js +5 -7
  137. package/dist/widgets/button.js +3 -2
  138. package/dist/widgets/checkbox.js +4 -3
  139. package/dist/widgets/dropdown.d.ts +3 -1
  140. package/dist/widgets/dropdown.js +20 -15
  141. package/dist/widgets/event-router.js +4 -2
  142. package/dist/widgets/focus-manager.d.ts +23 -14
  143. package/dist/widgets/focus-manager.js +34 -33
  144. package/dist/widgets/hit.d.ts +6 -0
  145. package/dist/widgets/hit.js +8 -1
  146. package/dist/widgets/index.d.ts +1 -1
  147. package/dist/widgets/observable.d.ts +23 -0
  148. package/dist/widgets/observable.js +43 -0
  149. package/dist/widgets/slider.d.ts +1 -1
  150. package/dist/widgets/slider.js +12 -3
  151. package/dist/widgets/static-item.d.ts +1 -1
  152. package/dist/widgets/static-item.js +6 -7
  153. package/dist/widgets/text-input.d.ts +53 -47
  154. package/dist/widgets/text-input.js +456 -399
  155. package/dist/widgets/themed-widget.js +2 -1
  156. package/dist/widgets/toggle.js +4 -3
  157. package/dist/widgets/types.d.ts +10 -4
  158. package/dist/widgets/types.js +8 -2
  159. package/dist/widgets/widget-app.d.ts +32 -6
  160. package/dist/widgets/widget-app.js +74 -27
  161. package/dist/widgets/widget-base.d.ts +2 -2
  162. package/dist/widgets/widget-base.js +29 -21
  163. package/package.json +9 -5
@@ -1,666 +1,298 @@
1
1
  /**
2
2
  * Markdown — renders Markdown content to the terminal.
3
- * Uses built-in parsing (no external dependency).
4
3
  *
5
- * Block structure is read as CommonMark reads it: a blockquote and a list
6
- * item are both containers, their lines tokenized as Markdown of their own
7
- * once the container's marker or indentation is cut off, and drawn beside a
8
- * gutter — the quote's bar, the item's marker then its hang. A table, which
9
- * CommonMark has none of, and an HTML block are read as markdown-it — Rich's
10
- * parser — reads them. Where CommonMark leaves the drawing open, Rich's is
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
11
8
  * followed: HTML, a block of it or a tag inline, is drawn as Rich draws it,
12
9
  * not at all.
13
10
  */
14
- import { cellLen, expandTabs, parseTabSize } from "../core/cells.js";
11
+ import { cellCount, cellLen } from "../core/cells.js";
15
12
  import { Segment } from "../core/segment.js";
16
13
  import { Style } from "../core/style.js";
17
14
  import { RichText, resolveStyle } from "../core/text.js";
18
15
  import { Rule } from "./rule.js";
16
+ import { Padding } from "./padding.js";
17
+ import { pyRstrip } from "./python-space.js";
18
+ import { Syntax } from "./syntax.js";
19
19
  import { Table } from "./table.js";
20
+ import { parseMarkdown } from "./markdown-parse.js";
20
21
  import { SIMPLE } from "../core/box.js";
21
- import { drawable, getStyle, groundedStyle, stackedHeight } from "../core/protocol.js";
22
- const HEADING = /^(#{1,6})\s+(.+)$/;
23
- /** A thematic break: three or more of one of `*`, `-` or `_`, spaces and tabs between them allowed. */
24
- const RULE = /^(?:(?:\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,})$/;
25
- const SETEXT = /^(=+|-+)[ \t]*$/;
26
- const FENCE = /^```(\w*)/;
27
- const QUOTE = /^>/;
28
- const ITEM_MARKER = /^(?:[*+-]|(\d{1,9})\.)/;
29
- const LEAD = /^[ \t]*/;
30
- const HARD_BREAK = /(?: {2,}|\\)$/;
31
- const BLOCK_STARTS = [HEADING, FENCE, QUOTE, RULE];
32
- /** A line that may be a table's delimiter row, screened as markdown-it screens one: pipes, dashes, colons and space only, and not a dash then a space, which opens a list item. */
33
- const DELIMITER_ROW = /^(?!-[ \t])[-:|][-:| \t]+$/;
34
- const DELIMITER_CELL = /^:?-+:?$/;
22
+ import { drawable, getStyle, groundedStyle, render, stackedHeight, withCellWidth } from "../core/protocol.js";
35
23
  /**
36
- * How many empty cells a table's short rows may be filled out with before the
37
- * table ends, as markdown-it caps them: a header of many columns over many
38
- * one-cell rows would otherwise grow a few kilobytes of source into millions of cells.
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.
39
29
  */
40
- const MAX_AUTOCOMPLETED_CELLS = 0x10000;
41
- /** Indentation that makes a line indented code when it opens a block, as CommonMark counts it. */
42
- const CODE_INDENT = 4;
43
- /** The tab stop code is drawn with, as Rich's `Syntax` draws it. */
44
- const CODE_TAB_SIZE = parseTabSize(8);
45
- const BLANK_LINE = { text: "", margin: 0, lazy: false };
46
- /** The block-level element names an HTML block of the sixth kind opens with, as markdown-it-py lists them. */
47
- const HTML_BLOCK_NAMES = "address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|" +
48
- "fieldset|figcaption|figure|footer|form|frame|frameset|h1|h2|h3|h4|h5|h6|head|header|hr|html|iframe|legend|li|link|" +
49
- "main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|search|section|summary|table|tbody|td|tfoot|th|thead|" +
50
- "title|tr|track|ul";
51
- // The tag grammar of markdown-it-py's common/html_re, shared by the HTML block a lone tag opens and the inline HTML it reads in text.
52
- const HTML_ATTRIBUTE = String.raw `(?:\s+[a-zA-Z_:][a-zA-Z0-9:._-]*(?:\s*=\s*(?:[^"'=<>\`\x00-\x20]+|'[^']*'|"[^"]*"))?)`;
53
- const HTML_OPEN_TAG = String.raw `<[A-Za-z][A-Za-z0-9-]*${HTML_ATTRIBUTE}*\s*\/?>`;
54
- const HTML_CLOSE_TAG = String.raw `<\/[A-Za-z][A-Za-z0-9-]*\s*>`;
55
- const HTML_BLOCKS = [
56
- { start: /^<(?:script|pre|style|textarea)(?=\s|>|$)/i, end: /<\/(?:script|pre|style|textarea)>/i, interrupts: true },
57
- { start: /^<!--/, end: /-->/, interrupts: true },
58
- { start: /^<\?/, end: /\?>/, interrupts: true },
59
- // Uppercase only, as markdown-it-py has it, where markdown-it in JS and CommonMark 0.31 take `<!doctype` too.
60
- { start: /^<![A-Z]/, end: />/, interrupts: true },
61
- { start: /^<!\[CDATA\[/, end: /\]\]>/, interrupts: true },
62
- { start: new RegExp(String.raw `^<\/?(?:${HTML_BLOCK_NAMES})(?=\s|\/?>|$)`, "i"), end: "blank", interrupts: true },
63
- {
64
- start: new RegExp(String.raw `^(?:${HTML_OPEN_TAG}|${HTML_CLOSE_TAG})\s*$`),
65
- end: "blank",
66
- interrupts: false,
67
- },
68
- ];
69
- /** The column `ws` ends at when it starts at column `from`, a tab reaching the next stop of 4 as CommonMark counts it. */
70
- function advance(from, ws) {
71
- return [...ws].reduce((col, ch) => (ch === "\t" ? col + 4 - (col % 4) : col + 1), from);
72
- }
73
- /** The column `line`'s leading whitespace ends at. */
74
- function leadEnd(line) {
75
- return advance(0, LEAD.exec(line.text)[0]);
76
- }
77
- /** How far `line` is indented inside its container. */
78
- function indentOf(line) {
79
- return Math.max(0, leadEnd(line) - line.margin);
80
- }
81
- /** `line` from its first character that is not leading whitespace. */
82
- function body(line) {
83
- return line.text.slice(LEAD.exec(line.text)[0].length);
84
- }
85
- function isBlank(line) {
86
- return line.text.trim() === "";
87
- }
88
- /** Whether `line` sits less than a code indent into its container and its text matches `re`. */
89
- function startsWith(line, re) {
90
- return indentOf(line) < CODE_INDENT && re.test(body(line));
91
- }
92
- /**
93
- * `line` from `n` columns into its container on: a block's own indentation
94
- * cut off, the rest as written. A tab straddling the cut leaves the columns
95
- * past it as spaces.
96
- */
97
- function dropColumns(line, n) {
98
- const to = line.margin + n;
99
- let col = 0;
100
- let i = 0;
101
- for (; i < line.text.length && col < to && (line.text[i] === " " || line.text[i] === "\t"); i++) {
102
- const next = advance(col, line.text[i]);
103
- if (next > to)
104
- return " ".repeat(next - to) + line.text.slice(i + 1);
105
- col = next;
106
- }
107
- return line.text.slice(i);
108
- }
109
- function itemStart(line) {
110
- const text = body(line);
111
- const m = ITEM_MARKER.exec(text);
112
- if (!m || indentOf(line) >= CODE_INDENT || RULE.test(text))
113
- return undefined;
114
- const after = text.slice(m[0].length);
115
- const spacing = LEAD.exec(after)[0];
116
- const rest = after.slice(spacing.length);
117
- if (spacing === "" && rest !== "")
118
- return undefined;
119
- const markerEnd = leadEnd(line) + m[0].length;
120
- const spaced = advance(markerEnd, spacing) - markerEnd;
121
- const content = rest === "" || spaced > CODE_INDENT ? markerEnd + 1 : markerEnd + spaced;
122
- const lead = line.text.slice(0, line.text.length - text.length);
123
- return {
124
- bullet: m[1] === undefined ? m[0] : undefined,
125
- start: m[1] === undefined ? 0 : parseInt(m[1], 10),
126
- content,
127
- first: rest === "" ? undefined : { text: lead + " ".repeat(m[0].length) + after, margin: content, lazy: false },
128
- };
129
- }
130
- /** Whether `line` may be a table's row: a line of its container's own, neither lazy paragraph text nor indented code. */
131
- function tableLine(line) {
132
- return !line.lazy && indentOf(line) < CODE_INDENT;
133
- }
134
- /**
135
- * A table row's cells, as markdown-it splits one: at every `|` no backslash escapes,
136
- * a pipe at either end of the row closing it rather than opening an empty
137
- * cell, and each cell trimmed. An escaped `\|` is a `|` in its cell, inside a
138
- * code span too.
139
- */
140
- function rowCells(row) {
141
- const cells = [""];
142
- for (const ch of row.trim()) {
143
- const last = cells.length - 1;
144
- if (ch !== "|")
145
- cells[last] += ch;
146
- else if (cells[last].endsWith("\\"))
147
- cells[last] = `${cells[last].slice(0, -1)}|`;
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) });
148
46
  else
149
- cells.push("");
47
+ yield { kind: "leaf", inline };
150
48
  }
151
- const from = cells[0] === "" ? 1 : 0;
152
- const to = cells.length > from && cells[cells.length - 1] === "" ? -1 : undefined;
153
- return cells.slice(from, to).map((cell) => cell.trim());
154
- }
155
- /** The alignment of each column `line` declares, if it is a table's delimiter row: a run of dashes per column, a colon on the side its cells sit. */
156
- function alignments(line) {
157
- const text = body(line);
158
- if (!tableLine(line) || !DELIMITER_ROW.test(text))
159
- return undefined;
160
- const cells = rowCells(text);
161
- if (!cells.every((cell) => DELIMITER_CELL.test(cell)))
162
- return undefined;
163
- return cells.map((cell) => (cell.endsWith(":") ? (cell.startsWith(":") ? "center" : "right") : cell.startsWith(":") ? "left" : undefined));
164
- }
165
- /**
166
- * The columns of the table opening at `start`, if one does, as markdown-it
167
- * reads one: a header row with a pipe in it, then a delimiter row naming as
168
- * many columns.
169
- */
170
- function tableAt(lines, start) {
171
- const head = lines[start];
172
- const delimiter = lines[start + 1];
173
- if (delimiter === undefined || !tableLine(head) || !body(head).includes("|"))
174
- return undefined;
175
- const aligns = alignments(delimiter);
176
- if (!aligns)
177
- return undefined;
178
- const headers = rowCells(body(head));
179
- return headers.length === aligns.length ? headers.map((header, n) => ({ header, align: aligns[n] })) : undefined;
180
49
  }
181
- /** Whether two item starts belong to one list: the same bullet character, or both numbered. */
182
- function sameList(a, b) {
183
- return a.bullet === b.bullet;
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"];
184
53
  }
185
54
  /**
186
- * A `>` line as a line of its quote: the marker blanked, and the quote's
187
- * content starting past it and the one column of space that may follow it —
188
- * one column of a tab, when a tab follows it.
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.
189
66
  */
190
- function quoted(line) {
191
- const at = line.text.length - body(line).length;
192
- const spaced = /[ \t]/.test(line.text[at + 1] ?? "");
193
- return { text: `${line.text.slice(0, at)} ${line.text.slice(at + 1)}`, margin: leadEnd(line) + 1 + (spaced ? 1 : 0), lazy: false };
194
- }
195
- /** The kind of HTML block `line` opens, if it opens one: the first kind whose opening it matches, as markdown-it tries them in order. */
196
- function htmlBlockAt(line) {
197
- return HTML_BLOCKS.find((kind) => startsWith(line, kind.start));
198
- }
199
- /**
200
- * Whether `line` opens a block of its own that may cut short the block before
201
- * it — a heading, a fence, a quote, a rule, a list item or an HTML block of
202
- * any kind but a lone tag. A line that does not is text, and under an open
203
- * paragraph it continues that paragraph.
204
- */
205
- function opensBlock(line) {
206
- return BLOCK_STARTS.some((re) => startsWith(line, re)) || itemStart(line) !== undefined || htmlBlockAt(line)?.interrupts === true;
207
- }
208
- /**
209
- * Whether `line` ends a paragraph: `opensBlock`, read as CommonMark reads it
210
- * under a paragraph. Indented code cannot interrupt one; a list item does only
211
- * when it has text and, numbered, counts from 1, so prose wrapped onto "2024.
212
- * That year" stays prose; and a line of `=` or `-` is the paragraph's heading
213
- * underline.
214
- */
215
- function endsParagraph(line) {
216
- if (line.lazy)
217
- return false;
218
- if (underline(line))
219
- return true;
220
- const item = itemStart(line);
221
- return item ? item.first !== undefined && (item.bullet !== undefined || item.start === 1) : opensBlock(line);
222
- }
223
- /** The setext underline `line` is — a line of `=` or `-` under a paragraph — if it is one. A lazy line never is. */
224
- function underline(line) {
225
- return line && !line.lazy && indentOf(line) < CODE_INDENT ? SETEXT.exec(body(line)) : null;
226
- }
227
- /** Whether the last block of `tokens`, followed into the containers it closes, is a paragraph still open. */
228
- function endsInParagraph(tokens) {
229
- const last = tokens[tokens.length - 1];
230
- switch (last?.type) {
231
- case "paragraph":
232
- return true;
233
- case "list_item":
234
- case "blockquote":
235
- return endsInParagraph(last.children);
236
- default:
237
- return false;
238
- }
239
- }
240
- /**
241
- * Whether `line`, short of a container's marker or indentation, still
242
- * belongs to it as a lazy continuation: CommonMark's paragraph continuation
243
- * text, which continues the paragraph open at the end of the container's
244
- * `inner` lines and opens no block of its own. Outside the container a line
245
- * is read without the paragraph under it, so any list item — whatever it
246
- * counts from — ends the container rather than continuing it. A line after a
247
- * lazy continuation finds that paragraph still open, so `afterLazy` answers
248
- * without reading `inner` again: a container's lazy lines cost one read of
249
- * it, not one each.
250
- */
251
- function continuesLazily(inner, line, afterLazy) {
252
- return !isBlank(line) && !opensBlock(line) && (afterLazy || endsInParagraph(tokenize(inner)));
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
+ };
253
79
  }
254
- /**
255
- * The text of the paragraph begun at `start`, every line after it up to a
256
- * blank, a line that ends a paragraph or a table's header row, and the index of the line that ended
257
- * it. Soft-broken lines are joined by one space, as a Markdown renderer
258
- * reflows them, with their own indentation dropped; a line ending in two
259
- * spaces or a backslash breaks hard, and its break is kept.
260
- */
261
- function continuation(lines, start) {
262
- const parts = [lines[start].text];
263
- let i = start + 1;
264
- for (; i < lines.length && !isBlank(lines[i]) && !endsParagraph(lines[i]) && !tableAt(lines, i); i++)
265
- parts.push(lines[i].text);
266
- const text = parts
267
- .map((part, n) => {
268
- const words = (n === parts.length - 1 ? part : part.replace(HARD_BREAK, "")).trim();
269
- return n === 0 ? words : (HARD_BREAK.test(parts[n - 1]) ? "\n" : " ") + words;
270
- })
271
- .join("");
272
- return { text, 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}`);
273
85
  }
274
86
  /**
275
- * The lines of the list item opened at `start` — every line after it that is
276
- * blank, indented to the content column, or a lazy continuation — and the
277
- * index of the line that ended it. Blank lines trailing the item are left to
278
- * whatever follows it, and an item that opens with a blank line ends at the
279
- * next one: it is empty.
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.
280
92
  */
281
- function itemLines(item, lines, start) {
282
- const inner = item.first === undefined ? [] : [item.first];
283
- let lazy = false;
284
- let i = start + 1;
285
- for (; i < lines.length; i++) {
286
- const line = lines[i];
287
- const blank = isBlank(line);
288
- if (blank && inner.length === 0)
289
- break;
290
- const held = blank || leadEnd(line) >= item.content;
291
- lazy = !held && continuesLazily(inner, line, lazy);
292
- if (!held && !lazy)
293
- break;
294
- inner.push(held ? { text: line.text, margin: item.content, lazy: false } : { ...line, lazy: true });
295
- }
296
- for (; inner.length > 0 && isBlank(inner[inner.length - 1]); i--)
297
- inner.pop();
298
- return { inner, next: i };
299
- }
300
- /**
301
- * The list opened at `start`: each item as a token holding its blocks, and
302
- * between items the blank lines that separated them in the source. Numbers
303
- * count on from the first item's, as Rich draws them, right-aligned to the
304
- * widest so every item hangs at one column.
305
- */
306
- function list(first, lines, start) {
307
- const items = [];
308
- let i = start;
309
- let gap = 0;
310
- for (let item = first; item && sameList(first, item); item = i < lines.length ? itemStart(lines[i]) : undefined) {
311
- const { inner, next } = itemLines(item, lines, i);
312
- items.push({ gap, children: tokenize(inner.length === 0 ? [BLANK_LINE] : inner) });
313
- let after = next;
314
- while (after < lines.length && isBlank(lines[after]))
315
- after++;
316
- gap = after - next;
317
- i = after;
318
- }
319
- // The blank lines after the last item belong to whatever follows the list.
320
- const next = i - gap;
321
- const width = String(first.start + items.length - 1).length + 1;
322
- const tokens = items.flatMap(({ gap: blanks, children }, n) => [
323
- ...Array.from({ length: blanks }, () => ({ type: "blank" })),
324
- {
325
- type: "list_item",
326
- marker: first.bullet === undefined ? { kind: "number", label: `${first.start + n}.`.padStart(width) } : { kind: "bullet" },
327
- children,
328
- },
329
- ]);
330
- return { tokens, next };
331
- }
332
- function tokenize(lines) {
333
- const tokens = [];
334
- let i = 0;
335
- while (i < lines.length) {
336
- const line = lines[i];
337
- // Blank line
338
- if (isBlank(line)) {
339
- tokens.push({ type: "blank" });
340
- i++;
341
- continue;
342
- }
343
- // Table: before every other block, as markdown-it tries it, so a header
344
- // row that reads as a heading or a list item is still a header. Its body
345
- // rows run to a blank line or a line that opens a block.
346
- const columns = tableAt(lines, i);
347
- if (columns) {
348
- const rows = [];
349
- let autocompleted = 0;
350
- for (i += 2; i < lines.length && !isBlank(lines[i]) && tableLine(lines[i]) && !opensBlock(lines[i]); i++) {
351
- const cells = rowCells(body(lines[i]));
352
- autocompleted += columns.length - cells.length;
353
- if (autocompleted > MAX_AUTOCOMPLETED_CELLS)
354
- break;
355
- rows.push(columns.map((_, n) => cells[n] ?? ""));
356
- }
357
- tokens.push({ type: "table", columns, rows });
358
- continue;
359
- }
360
- // Indented code: its lines, and the blank lines between them, with the indent cut off
361
- if (indentOf(line) >= CODE_INDENT) {
362
- const code = [];
363
- for (; i < lines.length && (isBlank(lines[i]) || indentOf(lines[i]) >= CODE_INDENT); i++) {
364
- code.push(dropColumns(lines[i], CODE_INDENT));
365
- }
366
- for (; code[code.length - 1].trim() === ""; i--)
367
- code.pop();
368
- tokens.push({ type: "code_block", code: code.join("\n") });
369
- continue;
370
- }
371
- // Heading
372
- const headingMatch = HEADING.exec(body(line));
373
- if (headingMatch) {
374
- tokens.push({ type: "heading", level: headingMatch[1].length, text: headingMatch[2] });
375
- i++;
376
- continue;
377
- }
378
- // Horizontal rule
379
- if (startsWith(line, RULE)) {
380
- tokens.push({ type: "hr" });
381
- i++;
382
- 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);
383
139
  }
384
- // Fenced code block: its lines with the fence's own indentation cut off
385
- if (startsWith(line, FENCE)) {
386
- const indent = indentOf(line);
387
- const codeLines = [];
388
- for (i++; i < lines.length && !startsWith(lines[i], FENCE); i++)
389
- codeLines.push(dropColumns(lines[i], indent));
390
- i++; // skip closing ```
391
- 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();
392
144
  continue;
393
145
  }
394
- // Blockquote: Markdown of its own — its `>` lines with the marker blanked,
395
- // and any unmarked line lazily continuing the paragraph open inside it
396
- if (startsWith(line, QUOTE)) {
397
- const inner = [];
398
- let lazy = false;
399
- for (; i < lines.length; i++) {
400
- const next = lines[i];
401
- const marked = startsWith(next, QUOTE);
402
- lazy = !marked && continuesLazily(inner, next, lazy);
403
- if (!marked && !lazy)
404
- break;
405
- inner.push(marked ? quoted(next) : { ...next, lazy: true });
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;
406
171
  }
407
- tokens.push({ type: "blockquote", children: tokenize(inner) });
408
- continue;
409
- }
410
- // List: its items, each Markdown of its own, as a quote is
411
- const item = itemStart(line);
412
- if (item) {
413
- const { tokens: items, next } = list(item, lines, i);
414
- tokens.push(...items);
415
- i = next;
416
- continue;
417
172
  }
418
- // HTML block: to the line that closes its kind, or to the end of its
419
- // container when none does; a closing blank line is left to what follows
420
- const html = htmlBlockAt(line);
421
- if (html) {
422
- const { end } = html;
423
- const closes = end === "blank" ? isBlank : (closing) => end.test(body(closing));
424
- while (i < lines.length && !closes(lines[i]))
425
- i++;
426
- if (i < lines.length && end !== "blank")
427
- i++;
428
- tokens.push({ type: "html_block" });
429
- continue;
430
- }
431
- // Paragraph, or a setext heading when a line of `=` or `-` underlines it
432
- const { text, next } = continuation(lines, i);
433
- const setext = underline(lines[next]);
434
- tokens.push(setext
435
- ? { type: "heading", level: setext[1].startsWith("=") ? 1 : 2, text }
436
- : { type: "paragraph", text });
437
- i = setext ? next + 1 : next;
438
173
  }
439
- return tokens;
440
- }
441
- const TICKS = /`+/y;
442
- const ANGLED_DESTINATION = /\(\s*<([^<>\n]*)>/y;
443
- const LINK_TAIL = /\s*(?:"[^"]*"|'[^']*'|\([^()]*\))?\s*\)/y;
444
- /** A backslash escape, as markdown-it reads one: the ASCII punctuation character after it, standing for itself. */
445
- const ESCAPE = /\\([!-\/:-@[-`{-~])/y;
446
- const ESCAPES = new RegExp(ESCAPE.source, "g");
447
- /** Inline HTML, as markdown-it-py's HTML_TAG_RE reads it: a tag, a comment, a processing instruction, a declaration or a CDATA section. */
448
- const HTML_TAG = new RegExp([
449
- HTML_OPEN_TAG,
450
- HTML_CLOSE_TAG,
451
- String.raw `<!---?>|<!--(?:[^-]|-[^-]|--[^>])*-->`,
452
- String.raw `<[?][\s\S]*?[?]>`,
453
- String.raw `<![A-Za-z][^>]*>`,
454
- String.raw `<!\[CDATA\[[\s\S]*?\]\]>`,
455
- ].join("|"), "y");
456
- /** `re`, a sticky pattern, matched exactly at `i` in `text`. */
457
- function matchAt(re, text, i) {
458
- re.lastIndex = i;
459
- return re.exec(text);
460
174
  }
175
+ const EXIT = { kind: "exit", newLine: true };
461
176
  /**
462
- * Each `[` in `text` that a `]` closes, to the index of that `]`: brackets
463
- * nest, a backslash escapes the character after it, and a bracket inside a
464
- * code span or inline HTML, which bind tighter than a link, is not one. One
465
- * pass, so a run of text full of brackets that close nothing costs no more
466
- * than one without.
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.
467
181
  */
468
- function bracketPairs(text) {
469
- const pairs = new Map();
470
- const open = [];
471
- for (let i = 0; i < text.length; i++) {
472
- if (text[i] === "\\")
473
- i++;
474
- else if (text[i] === "[")
475
- open.push(i);
476
- else if (text[i] === "]" && open.length > 0)
477
- pairs.set(open.pop(), i);
478
- // Neither construct reads the brackets, so the pairs found so far serve.
479
- else if (text[i] === "`" || text[i] === "<")
480
- i = (inlineAt(text, i, pairs)?.end ?? i + 1) - 1;
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
+ }
200
+ }
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 };
210
+ }
481
211
  }
482
- return pairs;
483
212
  }
484
213
  /**
485
- * The `(destination "title")` of a link opening at `open`, as CommonMark reads
486
- * it: a destination in angle brackets, or a run of non-space characters whose
487
- * parentheses balance — so a URL may itself hold `(…)` — then an optional
488
- * quoted title, then the `)`. The destination, its backslash escapes read, and
489
- * the index past the `)`.
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.
490
216
  */
491
- function linkDestination(text, open) {
492
- if (text[open] !== "(")
493
- return undefined;
494
- const angled = matchAt(ANGLED_DESTINATION, text, open);
495
- let i;
496
- let url;
497
- if (angled) {
498
- url = angled[1];
499
- i = open + angled[0].length;
500
- }
501
- else {
502
- i = open + 1;
503
- while (text[i] === " ")
504
- i++;
505
- const from = i;
506
- for (let depth = 0; i < text.length && !/\s/.test(text[i]) && !(text[i] === ")" && depth === 0); i++) {
507
- if (text[i] === "\\")
508
- i++;
509
- else if (text[i] === "(")
510
- depth++;
511
- else if (text[i] === ")")
512
- depth--;
513
- }
514
- url = text.slice(from, i);
515
- }
516
- const tail = matchAt(LINK_TAIL, text, i);
517
- return tail ? { url: url.replace(ESCAPES, "$1"), end: i + tail[0].length } : undefined;
217
+ function exitStep(token) {
218
+ return { kind: "exit", newLine: token.type !== "hr" };
518
219
  }
519
220
  /**
520
- * The index of the `delimiter` that closes emphasis whose text starts at
521
- * `from`, after at least one character of it. Every other inline construct
522
- * binds tighter than emphasis, as CommonMark has it, so a delimiter inside an
523
- * escape, a code span, inline HTML or a link closes nothing.
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.
524
224
  */
525
- function emphasisClose(text, from, delimiter, brackets) {
526
- for (let i = from; i < text.length;) {
527
- if (i > from && text.startsWith(delimiter, i))
528
- return i;
529
- const tighter = text[i] === "*" ? undefined : inlineAt(text, i, brackets);
530
- i = tighter ? tighter.end : i + 1;
531
- }
532
- return undefined;
533
- }
534
- /** The inline construct opening at `i` in `text`, if one does; `brackets` is `text`'s `bracketPairs`. */
535
- function inlineAt(text, i, brackets) {
536
- switch (text[i]) {
537
- case "`": {
538
- const ticks = matchAt(TICKS, text, i)[0];
539
- const close = new RegExp(`(?<!\`)${ticks}(?!\`)`, "g");
540
- close.lastIndex = i + ticks.length;
541
- const found = close.exec(text);
542
- return found
543
- ? { kind: "code", code: text.slice(i + ticks.length, found.index), end: found.index + ticks.length }
544
- : { kind: "literal", text: ticks, end: i + ticks.length };
545
- }
546
- case "*": {
547
- const strong = text.startsWith("**", i) ? emphasisClose(text, i + 2, "**", brackets) : undefined;
548
- const delimiter = strong === undefined ? "*" : "**";
549
- const close = strong ?? emphasisClose(text, i + 1, "*", brackets);
550
- return close === undefined
551
- ? undefined
552
- : {
553
- kind: "emphasis",
554
- style: strong === undefined ? "italic" : "bold",
555
- inner: text.slice(i + delimiter.length, close),
556
- end: close + delimiter.length,
557
- };
558
- }
559
- case "\\": {
560
- const escape = matchAt(ESCAPE, text, i);
561
- return escape ? { kind: "literal", text: escape[1], end: i + escape[0].length } : undefined;
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);
562
230
  }
563
- case "<": {
564
- const tag = matchAt(HTML_TAG, text, i);
565
- return tag ? { kind: "html", end: i + tag[0].length } : undefined;
566
- }
567
- case "!":
568
- case "[": {
569
- const image = text[i] === "!";
570
- const open = image ? i + 1 : i;
571
- const close = brackets.get(open);
572
- const destination = close === undefined ? undefined : linkDestination(text, close + 1);
573
- if (!destination)
574
- return undefined;
575
- const label = text.slice(open + 1, close);
576
- return image
577
- ? { kind: "image", alt: label, src: destination.url, end: destination.end }
578
- : { kind: "link", inner: label, url: destination.url, end: destination.end };
579
- }
580
- default:
581
- return undefined;
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;
243
+ }
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;
252
+ }
253
+ break;
254
+ case "code_block":
255
+ case "html_block":
256
+ case "hr":
257
+ break;
582
258
  }
583
259
  }
584
260
  /**
585
- * `text` as inline Markdown in `style`: a text of its own whose style sits
586
- * under every style inside it, so appended where it goes it draws as Rich
587
- * draws a nested construct — the inner style on top.
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.
588
263
  */
589
- function styled(text, style, settings, options) {
590
- const result = new RichText("", { style });
591
- appendInline(result, text, settings, options);
592
- return result;
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);
269
+ }
593
270
  }
594
271
  /**
595
- * `text` as inline Markdown appended to `result`: code spans, emphasis, links
596
- * and images, the text inside emphasis, links and images parsed the same way,
597
- * a backslash escape as the character it escapes, inline HTML as nothing, and
598
- * everything else as it is.
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.
599
277
  */
600
- function appendInline(result, text, settings, options) {
601
- const brackets = bracketPairs(text);
602
- let plain = 0;
603
- for (let i = 0; i < text.length;) {
604
- const found = inlineAt(text, i, brackets);
605
- if (!found) {
606
- i++;
607
- continue;
608
- }
609
- result.append(text.slice(plain, i));
610
- switch (found.kind) {
611
- case "code":
612
- result.append(found.code, groundedStyle(options, resolveStyle(options, settings.inlineCodeStyle)));
613
- break;
614
- case "literal":
615
- result.append(found.text);
616
- break;
617
- case "html":
618
- // Rich draws nothing for inline HTML.
619
- break;
620
- case "emphasis":
621
- result.append(styled(found.inner, found.style, settings, options));
622
- break;
623
- case "link": {
624
- // As Rich draws it: without hyperlinks the URL is written out, and it
625
- // is still the link, for a terminal that can click it.
626
- const link = new RichText("", { style: new Style({ link: found.url }) });
627
- if (settings.hyperlinks) {
628
- result.append(link.append(styled(found.inner, "markdown.link_url", settings, options)));
629
- }
630
- else {
631
- result
632
- .append(styled(found.inner, "markdown.link", settings, options))
633
- .append(" (")
634
- .append(link.append(found.url, "markdown.link_url"))
635
- .append(")");
636
- }
637
- break;
638
- }
639
- case "image": {
640
- // Rich's ImageItem: a picture glyph, then the alt text — or, with
641
- // none, the image's file name — linked to the image, then a space.
642
- const title = found.alt || (found.src.replace(/\/+$/, "").split("/").pop() ?? "");
643
- const link = settings.hyperlinks ? new Style({ link: found.src }) : "";
644
- result.append(drawable(options, "🌆 ", "")).append(styled(title, link, settings, options)).append(" ");
645
- break;
646
- }
647
- }
648
- i = plain = found.end;
649
- }
650
- result.append(text.slice(plain));
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);
651
285
  }
652
286
  /**
653
- * What `draw` renders in what is left of the width beside a gutter, each of
287
+ * What `content` renders in what is left of the width beside a gutter, each of
654
288
  * its rows ended. The first row's gutter is `first` and every later row's is
655
289
  * `rest`, so a list item's text hangs clear of its bullet and a quote's bar
656
290
  * runs its full height. A block with no gutter passes two empty segments:
657
- * every block is laid out the same way. What is drawn keeps at least one
658
- * column however wide the gutter, so a row may overrun its width but no gutter
659
- * 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.
660
293
  */
661
- function* guttered(draw, options, first, rest, style) {
662
- const width = Math.max(1, options.maxWidth - cellLen(first.text));
663
- 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) }));
664
296
  for (const [row, line] of rows.entries()) {
665
297
  yield row === 0 ? first : rest;
666
298
  yield* line;
@@ -674,85 +306,111 @@ function* guttered(draw, options, first, rest, style) {
674
306
  * and a heading's padding is not underlined.
675
307
  */
676
308
  function inline(text, settings, justify, style) {
677
- return function* (options) {
678
- const body = new RichText("", { end: "", style });
679
- appendInline(body, text, settings, options);
680
- // `append` lays the body's base style down as the first span.
681
- yield* new RichText("", { end: "", justify }).append(body).render(options);
682
- yield Segment.line();
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
+ },
683
317
  };
684
318
  }
685
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
+ }
686
330
  /**
687
331
  * Each block in `tokens`, every row it draws ended, body text placed by
688
332
  * `settings.justify`. A heading keeps its own placement whatever `settings`
689
- * or `options` say — Rich's: an h1 centred, every other level left — and its
690
- * style stops where its text does.
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.
691
336
  */
692
337
  function* renderTokens(tokens, options, settings) {
693
- for (const token of tokens) {
694
- switch (token.type) {
695
- case "heading": {
696
- const style = getStyle(options, `markdown.h${Math.min(token.level, 4)}`);
697
- const draw = inline(token.text, settings, token.level === 1 ? "center" : undefined, style);
698
- yield* guttered(draw, { ...options, justify: undefined }, NO_GUTTER, NO_GUTTER);
699
- break;
700
- }
701
- case "paragraph": {
702
- yield* guttered(inline(token.text, settings, settings.justify), options, NO_GUTTER, NO_GUTTER);
703
- break;
704
- }
705
- case "code_block": {
706
- const codeStyle = groundedStyle(options, getStyle(options, "markdown.code"));
707
- // Tabs expanded here, as `Syntax` expands them, since a terminal would
708
- // count a raw one from its own column 0 and not the code's.
709
- for (const line of expandTabs(token.code, CODE_TAB_SIZE).text.split("\n")) {
710
- yield new Segment(line, codeStyle);
711
- yield Segment.line();
712
- }
713
- break;
714
- }
715
- case "hr": {
716
- const rule = new Rule(undefined, { style: "markdown.hr" });
717
- yield* rule.render(options);
718
- break;
719
- }
720
- case "list_item": {
721
- const marker = token.marker.kind === "number" ? `${token.marker.label} ` : drawable(options, " • ", " * ");
722
- const hang = new Segment(" ".repeat(cellLen(marker)));
723
- const body = (inner) => renderTokens(token.children, inner, settings);
724
- yield* guttered(body, options, new Segment(marker), hang);
725
- break;
726
- }
727
- case "blockquote": {
728
- const bar = new Segment(drawable(options, "▎ ", "| "), getStyle(options, "markdown.hr"));
729
- const body = (inner) => renderTokens(token.children, inner, settings);
730
- yield* guttered(body, options, bar, bar, Style.parse("dim italic"));
731
- break;
732
- }
733
- case "table": {
734
- // Rich's TableElement: each cell inline Markdown placed by its
735
- // column's alignment, the header's in `markdown.table.header` on top.
736
- const cell = (text, justify) => {
737
- const content = new RichText("", { justify });
738
- appendInline(content, text, settings, options);
739
- return content;
740
- };
741
- const table = new Table({ box: SIMPLE, padEdge: false, style: "markdown.table.border", showEdge: true, collapsePadding: true });
742
- for (const { header, align } of token.columns)
743
- table.addColumn(cell(header, align).stylize("markdown.table.header"));
744
- for (const row of token.rows)
745
- table.addRow(...row.map((text, n) => cell(text, token.columns[n].align)));
746
- yield* table.render(options);
747
- break;
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);
748
382
  }
749
- case "html_block":
750
- // Rich draws nothing for one.
751
- break;
752
- case "blank":
753
- yield Segment.line();
754
- break;
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;
755
410
  }
411
+ case "html_block":
412
+ // Rich draws nothing for it.
413
+ break;
756
414
  }
757
415
  }
758
416
  export class Markdown {
@@ -760,19 +418,34 @@ export class Markdown {
760
418
  inlineCodeStyle;
761
419
  hyperlinks;
762
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;
763
425
  constructor(markdown, options) {
764
426
  this.markdown = markdown;
427
+ this.parsed = parseMarkdown(markdown);
765
428
  this.inlineCodeStyle = options?.inlineCodeStyle ?? "markdown.code";
766
429
  this.hyperlinks = options?.hyperlinks !== false;
767
430
  this.justify = options?.justify ?? "left";
768
431
  }
769
432
  *render(rawOptions) {
770
- // Every block below is one of a stack.
771
- const options = { ...rawOptions, height: stackedHeight(rawOptions.height) };
772
- const lines = this.markdown.split(/\r?\n/).map((text) => ({ text, margin: 0, lazy: false }));
773
- yield* renderTokens(tokenize(lines), options, this);
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
+ }
774
446
  }
775
447
  measure(options) {
776
- return { minimum: 1, maximum: options.maxWidth };
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 };
777
450
  }
778
451
  }