@tooee/renderers 0.12.0 → 0.14.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.
- package/dist/chooser/choose-filter.d.ts.map +1 -1
- package/dist/chooser/choose-filter.js.map +1 -1
- package/dist/chooser/choose-highlighted-text.d.ts.map +1 -1
- package/dist/chooser/choose-highlighted-text.js.map +1 -1
- package/dist/chooser/choose-list.d.ts.map +1 -1
- package/dist/chooser/choose-list.js.map +1 -1
- package/dist/chooser/choose-store.d.ts +3 -3
- package/dist/chooser/choose-store.d.ts.map +1 -1
- package/dist/chooser/choose-store.js.map +1 -1
- package/dist/chooser/chooser.d.ts.map +1 -1
- package/dist/chooser/chooser.js.map +1 -1
- package/dist/chooser/source.d.ts.map +1 -1
- package/dist/chooser/source.js.map +1 -1
- package/dist/chooser/use-choose.d.ts.map +1 -1
- package/dist/chooser/use-choose.js.map +1 -1
- package/dist/code-blocks.d.ts.map +1 -1
- package/dist/code-blocks.js.map +1 -1
- package/dist/command-palette.d.ts.map +1 -1
- package/dist/command-palette.js.map +1 -1
- package/dist/context-menu.d.ts.map +1 -1
- package/dist/context-menu.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/markdown/blocks/blockquote.d.ts.map +1 -1
- package/dist/markdown/blocks/blockquote.js.map +1 -1
- package/dist/markdown/blocks/flat-block.d.ts.map +1 -1
- package/dist/markdown/blocks/flat-block.js +38 -33
- package/dist/markdown/blocks/flat-block.js.map +1 -1
- package/dist/markdown/blocks/heading.d.ts.map +1 -1
- package/dist/markdown/blocks/heading.js.map +1 -1
- package/dist/markdown/blocks/list-line.d.ts +2 -2
- package/dist/markdown/blocks/list-line.d.ts.map +1 -1
- package/dist/markdown/blocks/list-line.js +2 -3
- package/dist/markdown/blocks/list-line.js.map +1 -1
- package/dist/markdown/blocks/paragraph.d.ts.map +1 -1
- package/dist/markdown/blocks/paragraph.js.map +1 -1
- package/dist/markdown/blocks/table.d.ts +17 -3
- package/dist/markdown/blocks/table.d.ts.map +1 -1
- package/dist/markdown/blocks/table.js +29 -29
- package/dist/markdown/blocks/table.js.map +1 -1
- package/dist/markdown/links.d.ts +10 -0
- package/dist/markdown/links.d.ts.map +1 -1
- package/dist/markdown/links.js +30 -18
- package/dist/markdown/links.js.map +1 -1
- package/dist/markdown/plain-text.d.ts.map +1 -1
- package/dist/markdown/plain-text.js.map +1 -1
- package/dist/markdown/table-layout.d.ts +28 -0
- package/dist/markdown/table-layout.d.ts.map +1 -0
- package/dist/markdown/table-layout.js +100 -0
- package/dist/markdown/table-layout.js.map +1 -0
- package/dist/markdown/table-links.d.ts +5 -4
- package/dist/markdown/table-links.d.ts.map +1 -1
- package/dist/markdown/table-links.js +6 -23
- package/dist/markdown/table-links.js.map +1 -1
- package/dist/markdown/tokens.d.ts +2 -0
- package/dist/markdown/tokens.d.ts.map +1 -1
- package/dist/markdown/tokens.js.map +1 -1
- package/dist/markdown/visit-inline.d.ts.map +1 -1
- package/dist/markdown/visit-inline.js.map +1 -1
- package/dist/markdown-blocks.d.ts +72 -14
- package/dist/markdown-blocks.d.ts.map +1 -1
- package/dist/markdown-blocks.js +137 -77
- package/dist/markdown-blocks.js.map +1 -1
- package/dist/markdown-images.d.ts.map +1 -1
- package/dist/markdown-images.js +5 -8
- package/dist/markdown-images.js.map +1 -1
- package/dist/markdown-view.d.ts +0 -1
- package/dist/markdown-view.d.ts.map +1 -1
- package/dist/markdown-view.js +0 -1
- package/dist/markdown-view.js.map +1 -1
- package/dist/mermaid.d.ts.map +1 -1
- package/dist/mermaid.js +8 -4
- package/dist/mermaid.js.map +1 -1
- package/dist/parsers.d.ts.map +1 -1
- package/dist/parsers.js.map +1 -1
- package/dist/row-document-renderable.d.ts.map +1 -1
- package/dist/row-document-renderable.js.map +1 -1
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js.map +1 -1
- package/dist/table.d.ts.map +1 -1
- package/dist/table.js.map +1 -1
- package/package.json +6 -6
- package/src/chooser/choose-filter.tsx +1 -0
- package/src/chooser/choose-highlighted-text.tsx +2 -0
- package/src/chooser/choose-list.tsx +6 -0
- package/src/chooser/choose-store.ts +20 -3
- package/src/chooser/chooser.tsx +2 -0
- package/src/chooser/source.ts +5 -0
- package/src/chooser/use-choose.ts +44 -0
- package/src/code-blocks.tsx +14 -0
- package/src/command-palette.tsx +4 -0
- package/src/context-menu.tsx +13 -0
- package/src/index.ts +56 -1
- package/src/markdown/blocks/blockquote.tsx +2 -0
- package/src/markdown/blocks/flat-block.tsx +113 -80
- package/src/markdown/blocks/heading.tsx +1 -0
- package/src/markdown/blocks/list-line.tsx +6 -6
- package/src/markdown/blocks/paragraph.tsx +1 -0
- package/src/markdown/blocks/table.tsx +84 -49
- package/src/markdown/links.ts +69 -25
- package/src/markdown/plain-text.ts +5 -0
- package/src/markdown/table-layout.ts +156 -0
- package/src/markdown/table-links.ts +17 -25
- package/src/markdown/tokens.ts +4 -0
- package/src/markdown/visit-inline.ts +20 -0
- package/src/markdown-blocks.ts +298 -100
- package/src/markdown-images.ts +30 -9
- package/src/markdown-view.tsx +4 -1
- package/src/mermaid.ts +21 -4
- package/src/parsers.ts +39 -0
- package/src/row-document-renderable.ts +53 -0
- package/src/source.ts +18 -0
- package/src/table.tsx +32 -0
- package/dist/text-table-internals.d.ts +0 -34
- package/dist/text-table-internals.d.ts.map +0 -1
- package/dist/text-table-internals.js +0 -68
- package/dist/text-table-internals.js.map +0 -1
- package/dist/text-table.d.ts +0 -7
- package/dist/text-table.d.ts.map +0 -1
- package/dist/text-table.js +0 -4
- package/dist/text-table.js.map +0 -1
- package/src/text-table-internals.ts +0 -124
- package/src/text-table.ts +0 -10
package/src/markdown-blocks.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { marked } from "marked";
|
|
2
2
|
import type { Token, Tokens } from "marked";
|
|
3
3
|
|
|
4
|
+
import { narrowToken } from "./markdown/tokens.js";
|
|
4
5
|
import { SourceIndex } from "./source.js";
|
|
5
6
|
import type { DocumentRowSource } from "./source.js";
|
|
6
7
|
|
|
@@ -13,19 +14,94 @@ const LIST_MARKER = /^(?<indent>\s*)(?:[-*+]|\d+[.)])[ \t]*(?:\[[ xX]\])?/u;
|
|
|
13
14
|
// Flat block model
|
|
14
15
|
// ---------------------------------------------------------------------------
|
|
15
16
|
|
|
17
|
+
/** Fields that every flattened block has, whatever its kind. */
|
|
18
|
+
interface FlatBlockBase {
|
|
19
|
+
/** Columns of nesting, such as the bullet width of each parent list item. */
|
|
20
|
+
indent: number;
|
|
21
|
+
/** Provenance in the Markdown input; `null` only if resolution genuinely failed. */
|
|
22
|
+
source: DocumentRowSource | null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface FlatHeadingBlock extends FlatBlockBase {
|
|
26
|
+
kind: "heading";
|
|
27
|
+
token: Tokens.Heading;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface FlatParagraphBlock extends FlatBlockBase {
|
|
31
|
+
kind: "paragraph";
|
|
32
|
+
token: Tokens.Paragraph;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface FlatCodeBlock extends FlatBlockBase {
|
|
36
|
+
kind: "code";
|
|
37
|
+
token: Tokens.Code;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface FlatBlockquoteBlock extends FlatBlockBase {
|
|
41
|
+
kind: "blockquote";
|
|
42
|
+
token: Tokens.Blockquote;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface FlatRuleBlock extends FlatBlockBase {
|
|
46
|
+
kind: "hr";
|
|
47
|
+
token: Tokens.Hr;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The first line of a list item: the bullet, an optional checkbox and the
|
|
52
|
+
* item's first inline content. An item that starts with block content, or
|
|
53
|
+
* has no content, gets an empty `text` token.
|
|
54
|
+
*/
|
|
55
|
+
export interface FlatListLineBlock extends FlatBlockBase {
|
|
56
|
+
kind: "list-line";
|
|
57
|
+
token: Tokens.Text | Tokens.Paragraph;
|
|
58
|
+
bullet: string;
|
|
59
|
+
/** Set on task-list items only. */
|
|
60
|
+
checked?: boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** One header or body row of a Markdown table. A table is one block per row. */
|
|
64
|
+
export interface FlatTableRowBlock extends FlatBlockBase {
|
|
65
|
+
kind: "table-row";
|
|
66
|
+
row: FlatTableRow;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Any other block token, such as a `text` token inside a list item. */
|
|
70
|
+
export interface FlatOtherBlock extends FlatBlockBase {
|
|
71
|
+
kind: "other";
|
|
72
|
+
token: Token;
|
|
73
|
+
}
|
|
74
|
+
|
|
16
75
|
/**
|
|
17
76
|
* A flattened block — all blocks exist at the top level with an indent.
|
|
18
77
|
* Nested structures (lists containing code blocks, etc.) are flattened into
|
|
19
|
-
* sibling blocks with appropriate indentation.
|
|
78
|
+
* sibling blocks with appropriate indentation. `kind` tells readers which
|
|
79
|
+
* fields the block has, so they do not need to inspect the Marked token.
|
|
20
80
|
*/
|
|
21
|
-
export
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
81
|
+
export type FlatBlock =
|
|
82
|
+
| FlatHeadingBlock
|
|
83
|
+
| FlatParagraphBlock
|
|
84
|
+
| FlatCodeBlock
|
|
85
|
+
| FlatBlockquoteBlock
|
|
86
|
+
| FlatRuleBlock
|
|
87
|
+
| FlatListLineBlock
|
|
88
|
+
| FlatTableRowBlock
|
|
89
|
+
| FlatOtherBlock;
|
|
90
|
+
|
|
91
|
+
export type FlatBlockKind = FlatBlock["kind"];
|
|
26
92
|
|
|
27
|
-
|
|
28
|
-
|
|
93
|
+
/**
|
|
94
|
+
* One row of a Markdown table. The delimiter line (`| --- |`) is not a row.
|
|
95
|
+
* Rows of the same table share `table`, so a renderer can give every row the
|
|
96
|
+
* same column widths.
|
|
97
|
+
*/
|
|
98
|
+
export interface FlatTableRow {
|
|
99
|
+
/** The parent table token, the same object for every row of the table. */
|
|
100
|
+
table: Tokens.Table;
|
|
101
|
+
/** `0` is the header row. Body rows start at `1`. */
|
|
102
|
+
index: number;
|
|
103
|
+
/** This row's Markdown line, without indent or line ending. */
|
|
104
|
+
raw: string;
|
|
29
105
|
}
|
|
30
106
|
|
|
31
107
|
export interface FlattenMarkdownOptions {
|
|
@@ -33,7 +109,7 @@ export interface FlattenMarkdownOptions {
|
|
|
33
109
|
}
|
|
34
110
|
|
|
35
111
|
/** Empty synthetic token stamped onto bullet-only rows (nested-list/block/empty items). */
|
|
36
|
-
const syntheticTextToken = function syntheticTextToken():
|
|
112
|
+
const syntheticTextToken = function syntheticTextToken(): Tokens.Text {
|
|
37
113
|
return { raw: "", text: "", tokens: [], type: "text" };
|
|
38
114
|
};
|
|
39
115
|
|
|
@@ -45,23 +121,31 @@ export const checkboxMarker = function checkboxMarker(checked: boolean | undefin
|
|
|
45
121
|
if (checked === undefined) {
|
|
46
122
|
return "";
|
|
47
123
|
}
|
|
124
|
+
|
|
48
125
|
return checked ? "[x] " : "[ ] ";
|
|
49
126
|
};
|
|
50
127
|
|
|
51
128
|
/**
|
|
52
|
-
* The visible/searchable text for a block.
|
|
53
|
-
* `raw`; synthetic bullet rows (empty raw) fall back to the visible
|
|
129
|
+
* The visible/searchable text for a block. A table row uses its own Markdown
|
|
130
|
+
* line. Other non-synthetic blocks use the token's `raw`; synthetic bullet rows (empty raw) fall back to the visible
|
|
54
131
|
* bullet/checkbox text, then to the resolved source text. This keeps default
|
|
55
132
|
* search and copy in agreement with the source mapping on synthetic rows.
|
|
56
133
|
*/
|
|
57
134
|
export const getFlatBlockText = function getFlatBlockText(block: FlatBlock): string {
|
|
135
|
+
if (block.kind === "table-row") {
|
|
136
|
+
return block.row.raw;
|
|
137
|
+
}
|
|
138
|
+
|
|
58
139
|
const { raw } = block.token;
|
|
140
|
+
|
|
59
141
|
if (raw.length > 0) {
|
|
60
142
|
return raw;
|
|
61
143
|
}
|
|
62
|
-
|
|
144
|
+
|
|
145
|
+
if (block.kind === "list-line") {
|
|
63
146
|
return block.bullet + checkboxMarker(block.checked);
|
|
64
147
|
}
|
|
148
|
+
|
|
65
149
|
return block.source?.primary.text ?? "";
|
|
66
150
|
};
|
|
67
151
|
|
|
@@ -89,10 +173,13 @@ class MarkdownResolver {
|
|
|
89
173
|
/** Resolve `raw` at/after the cursor within `[cursor, bound]`, then advance. */
|
|
90
174
|
resolveRaw(raw: string, bound: number): DocumentRowSource | null {
|
|
91
175
|
const match = this.findRaw(raw, bound);
|
|
176
|
+
|
|
92
177
|
if (!match) {
|
|
93
178
|
return null;
|
|
94
179
|
}
|
|
180
|
+
|
|
95
181
|
this.cursor = match.end;
|
|
182
|
+
|
|
96
183
|
return { primary: this.index.span(match.start, match.end) };
|
|
97
184
|
}
|
|
98
185
|
|
|
@@ -112,9 +199,11 @@ class MarkdownResolver {
|
|
|
112
199
|
if (raw.length === 0) {
|
|
113
200
|
return null;
|
|
114
201
|
}
|
|
202
|
+
|
|
115
203
|
const md = this.markdown;
|
|
116
204
|
|
|
117
205
|
const exact = md.indexOf(raw, this.cursor);
|
|
206
|
+
|
|
118
207
|
if (exact !== -1 && exact + raw.length <= bound) {
|
|
119
208
|
return { end: exact + raw.length, start: exact };
|
|
120
209
|
}
|
|
@@ -124,20 +213,26 @@ class MarkdownResolver {
|
|
|
124
213
|
}
|
|
125
214
|
|
|
126
215
|
const firstSegment = raw.slice(0, raw.indexOf("\n"));
|
|
216
|
+
|
|
127
217
|
if (firstSegment.length === 0) {
|
|
128
218
|
return null;
|
|
129
219
|
}
|
|
130
220
|
|
|
131
221
|
let from = this.cursor;
|
|
222
|
+
|
|
132
223
|
for (;;) {
|
|
133
224
|
const start = md.indexOf(firstSegment, from);
|
|
225
|
+
|
|
134
226
|
if (start === -1 || start >= bound) {
|
|
135
227
|
return null;
|
|
136
228
|
}
|
|
229
|
+
|
|
137
230
|
const end = this.matchFlexibleNewlines(raw, start, bound);
|
|
231
|
+
|
|
138
232
|
if (end !== -1) {
|
|
139
233
|
return { end, start };
|
|
140
234
|
}
|
|
235
|
+
|
|
141
236
|
from = start + 1;
|
|
142
237
|
}
|
|
143
238
|
}
|
|
@@ -148,8 +243,10 @@ class MarkdownResolver {
|
|
|
148
243
|
const { length } = md;
|
|
149
244
|
let h = start;
|
|
150
245
|
let n = 0;
|
|
246
|
+
|
|
151
247
|
while (n < raw.length) {
|
|
152
248
|
const code = raw.codePointAt(n);
|
|
249
|
+
|
|
153
250
|
if (code === LINE_FEED_CODE) {
|
|
154
251
|
if (h + 1 < length && md.codePointAt(h) === 13 && md.codePointAt(h + 1) === 10) {
|
|
155
252
|
h += 2;
|
|
@@ -158,6 +255,7 @@ class MarkdownResolver {
|
|
|
158
255
|
} else {
|
|
159
256
|
return -1;
|
|
160
257
|
}
|
|
258
|
+
|
|
161
259
|
n += 1;
|
|
162
260
|
} else if (h < length && md.codePointAt(h) === code) {
|
|
163
261
|
h += 1;
|
|
@@ -166,6 +264,7 @@ class MarkdownResolver {
|
|
|
166
264
|
return -1;
|
|
167
265
|
}
|
|
168
266
|
}
|
|
267
|
+
|
|
169
268
|
return h <= bound ? h : -1;
|
|
170
269
|
}
|
|
171
270
|
|
|
@@ -175,19 +274,25 @@ class MarkdownResolver {
|
|
|
175
274
|
const lineEnd = nl === -1 ? itemEnd : Math.min(nl, itemEnd);
|
|
176
275
|
const firstLine = this.markdown.slice(itemStart, lineEnd);
|
|
177
276
|
const match = LIST_MARKER.exec(firstLine);
|
|
277
|
+
|
|
178
278
|
if (!match) {
|
|
179
279
|
return { primary: this.index.span(itemStart, itemStart, false) };
|
|
180
280
|
}
|
|
281
|
+
|
|
181
282
|
const markerStart = itemStart + (match.groups?.indent ?? "").length;
|
|
182
283
|
let spanEnd = itemStart + match[0].length;
|
|
284
|
+
|
|
183
285
|
while (spanEnd > markerStart) {
|
|
184
286
|
const code = this.markdown.codePointAt(spanEnd - 1);
|
|
287
|
+
|
|
185
288
|
// Continue while the character is a space or tab.
|
|
186
289
|
if (code !== 32 && code !== 9) {
|
|
187
290
|
break;
|
|
188
291
|
}
|
|
292
|
+
|
|
189
293
|
spanEnd -= 1;
|
|
190
294
|
}
|
|
295
|
+
|
|
191
296
|
return { primary: this.index.span(markerStart, spanEnd, false) };
|
|
192
297
|
}
|
|
193
298
|
}
|
|
@@ -202,94 +307,173 @@ const bulletFor = function bulletFor(list: Tokens.List, itemIndex: number): stri
|
|
|
202
307
|
: "- ";
|
|
203
308
|
};
|
|
204
309
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
const item = list.items[i];
|
|
214
|
-
const bullet = bulletFor(list, i);
|
|
310
|
+
/** Where flattened blocks go, and the limits for source resolution. */
|
|
311
|
+
interface FlattenContext {
|
|
312
|
+
/** Source offset that resolution must not pass, such as the end of the parent list item. */
|
|
313
|
+
bound: number;
|
|
314
|
+
indent: number;
|
|
315
|
+
out: FlatBlock[];
|
|
316
|
+
res: MarkdownResolver | null;
|
|
317
|
+
}
|
|
215
318
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
319
|
+
const resolveSource = function resolveSource(
|
|
320
|
+
raw: string,
|
|
321
|
+
ctx: FlattenContext
|
|
322
|
+
): DocumentRowSource | null {
|
|
323
|
+
return ctx.res ? ctx.res.resolveRaw(raw, ctx.bound) : null;
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* The typed block for a token that is not a list, a table or HTML. This is the
|
|
328
|
+
* one place that maps a Marked token type to a block `kind`.
|
|
329
|
+
*/
|
|
330
|
+
const leafBlock = function leafBlock(token: Token, base: FlatBlockBase): FlatBlock {
|
|
331
|
+
const heading = narrowToken(token, "heading");
|
|
226
332
|
|
|
227
|
-
|
|
228
|
-
|
|
333
|
+
if (heading !== null) {
|
|
334
|
+
return { ...base, kind: "heading", token: heading };
|
|
335
|
+
}
|
|
229
336
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
337
|
+
const paragraph = narrowToken(token, "paragraph");
|
|
338
|
+
|
|
339
|
+
if (paragraph !== null) {
|
|
340
|
+
return { ...base, kind: "paragraph", token: paragraph };
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const code = narrowToken(token, "code");
|
|
344
|
+
|
|
345
|
+
if (code !== null) {
|
|
346
|
+
return { ...base, kind: "code", token: code };
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const blockquote = narrowToken(token, "blockquote");
|
|
350
|
+
|
|
351
|
+
if (blockquote !== null) {
|
|
352
|
+
return { ...base, kind: "blockquote", token: blockquote };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const hr = narrowToken(token, "hr");
|
|
356
|
+
|
|
357
|
+
if (hr !== null) {
|
|
358
|
+
return { ...base, kind: "hr", token: hr };
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return { ...base, kind: "other", token };
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Emit one block per table row: the header, then each body row. Each row
|
|
366
|
+
* resolves its own source line, so the delimiter line between the header and
|
|
367
|
+
* the body stays unmapped. Lines are trimmed, because marked strips the list
|
|
368
|
+
* indent from a table inside a list item but the source keeps it.
|
|
369
|
+
*/
|
|
370
|
+
const flattenTable = function flattenTable(table: Tokens.Table, ctx: FlattenContext): void {
|
|
371
|
+
const lines = table.raw
|
|
372
|
+
.split("\n")
|
|
373
|
+
.map((line) => line.trim())
|
|
374
|
+
.filter((line) => line.length > 0);
|
|
375
|
+
|
|
376
|
+
for (let index = 0; index <= table.rows.length; index += 1) {
|
|
377
|
+
// Line 1 is the delimiter, so body row `index` is on line `index + 1`.
|
|
378
|
+
const raw = lines[index === 0 ? 0 : index + 1] ?? "";
|
|
379
|
+
|
|
380
|
+
ctx.out.push({
|
|
381
|
+
indent: ctx.indent,
|
|
382
|
+
kind: "table-row",
|
|
383
|
+
row: { index, raw, table },
|
|
384
|
+
source: resolveSource(raw, ctx),
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Flatten one block token at the context's indent. The top level and list
|
|
391
|
+
* items share this step, so lists and tables nest the same way everywhere.
|
|
392
|
+
* Block HTML is not rendered, so it gets no block. It is still resolved, so
|
|
393
|
+
* later blocks keep their sequential source anchors.
|
|
394
|
+
*/
|
|
395
|
+
const flattenBlockToken = function flattenBlockToken(token: Token, ctx: FlattenContext): void {
|
|
396
|
+
const list = narrowToken(token, "list");
|
|
397
|
+
|
|
398
|
+
if (list !== null) {
|
|
399
|
+
// oxlint-disable-next-line no-use-before-define -- lists and list items recurse through this step
|
|
400
|
+
flattenList(list, ctx);
|
|
401
|
+
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const table = narrowToken(token, "table");
|
|
406
|
+
|
|
407
|
+
if (table !== null) {
|
|
408
|
+
flattenTable(table, ctx);
|
|
409
|
+
|
|
410
|
+
return;
|
|
233
411
|
}
|
|
412
|
+
|
|
413
|
+
const source = resolveSource(token.raw ?? "", ctx);
|
|
414
|
+
|
|
415
|
+
if (token.type === "html") {
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
ctx.out.push(leafBlock(token, { indent: ctx.indent, source }));
|
|
234
420
|
};
|
|
235
421
|
|
|
422
|
+
/**
|
|
423
|
+
* Flatten one list item. `ctx.bound` is the end of the item. `itemStart` is
|
|
424
|
+
* its start offset, or `null` when the item was not found in the source.
|
|
425
|
+
*/
|
|
236
426
|
const flattenListItem = function flattenListItem(
|
|
237
427
|
item: Tokens.ListItem,
|
|
238
|
-
indent: number,
|
|
239
428
|
bullet: string,
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
itemStart: number | null,
|
|
243
|
-
itemEnd: number
|
|
429
|
+
ctx: FlattenContext,
|
|
430
|
+
itemStart: number | null
|
|
244
431
|
): void {
|
|
245
432
|
const checked = item.checked === null ? undefined : item.checked;
|
|
246
|
-
const
|
|
433
|
+
const childCtx: FlattenContext = { ...ctx, indent: ctx.indent + bullet.length };
|
|
247
434
|
let bulletUsed = false;
|
|
248
435
|
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
source: res && itemStart !== null ? res.markerSource(itemStart, itemEnd) : null,
|
|
255
|
-
token: syntheticTextToken(),
|
|
256
|
-
});
|
|
436
|
+
const pushListLine = (
|
|
437
|
+
token: Tokens.Text | Tokens.Paragraph,
|
|
438
|
+
source: DocumentRowSource | null
|
|
439
|
+
) => {
|
|
440
|
+
ctx.out.push({ bullet, checked, indent: ctx.indent, kind: "list-line", source, token });
|
|
257
441
|
bulletUsed = true;
|
|
258
442
|
};
|
|
259
443
|
|
|
260
|
-
|
|
444
|
+
const emitBulletMarker = () => {
|
|
445
|
+
const source =
|
|
446
|
+
ctx.res && itemStart !== null ? ctx.res.markerSource(itemStart, ctx.bound) : null;
|
|
447
|
+
|
|
448
|
+
pushListLine(syntheticTextToken(), source);
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
for (const token of item.tokens) {
|
|
261
452
|
if (token.type === "space" || token.type === "checkbox") {
|
|
262
453
|
continue;
|
|
263
454
|
}
|
|
264
455
|
|
|
265
|
-
|
|
456
|
+
const inline = narrowToken(token, "text") ?? narrowToken(token, "paragraph");
|
|
457
|
+
|
|
458
|
+
if (inline !== null) {
|
|
266
459
|
// Inline content — attach the bullet to the first one.
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
indent,
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
});
|
|
274
|
-
bulletUsed = true;
|
|
275
|
-
} else if (token.type === "list") {
|
|
276
|
-
if (!bulletUsed) {
|
|
277
|
-
emitBulletMarker();
|
|
278
|
-
}
|
|
279
|
-
// SAFETY: Marked creates a List token for the adjacent token.type branch.
|
|
280
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Marked's broad Token fallback prevents discriminator narrowing
|
|
281
|
-
flattenList(token as Tokens.List, indent + bullet.length, out, res, itemEnd);
|
|
282
|
-
} else {
|
|
283
|
-
// Block content (code, table, blockquote, hr, etc.).
|
|
284
|
-
if (!bulletUsed) {
|
|
285
|
-
emitBulletMarker();
|
|
460
|
+
const source = resolveSource(inline.raw, ctx);
|
|
461
|
+
|
|
462
|
+
if (bulletUsed) {
|
|
463
|
+
ctx.out.push(leafBlock(inline, { indent: ctx.indent, source }));
|
|
464
|
+
} else {
|
|
465
|
+
pushListLine(inline, source);
|
|
286
466
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
source: res ? res.resolveRaw(token.raw ?? "", itemEnd) : null,
|
|
290
|
-
token,
|
|
291
|
-
});
|
|
467
|
+
|
|
468
|
+
continue;
|
|
292
469
|
}
|
|
470
|
+
|
|
471
|
+
// Block content (list, table, code, blockquote, hr, etc.) sits under the bullet.
|
|
472
|
+
if (!bulletUsed) {
|
|
473
|
+
emitBulletMarker();
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
flattenBlockToken(token, childCtx);
|
|
293
477
|
}
|
|
294
478
|
|
|
295
479
|
// List item had no content tokens — still emit the bullet.
|
|
@@ -298,23 +482,27 @@ const flattenListItem = function flattenListItem(
|
|
|
298
482
|
}
|
|
299
483
|
};
|
|
300
484
|
|
|
301
|
-
const
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
485
|
+
const flattenList = function flattenList(list: Tokens.List, ctx: FlattenContext): void {
|
|
486
|
+
for (let i = 0; i < list.items.length; i += 1) {
|
|
487
|
+
const item = list.items[i];
|
|
488
|
+
|
|
489
|
+
let itemStart: number | null = null;
|
|
490
|
+
let itemEnd = ctx.bound;
|
|
491
|
+
|
|
492
|
+
if (ctx.res) {
|
|
493
|
+
const loc = ctx.res.locate(item.raw, ctx.bound);
|
|
494
|
+
|
|
495
|
+
if (loc) {
|
|
496
|
+
itemStart = loc.start;
|
|
497
|
+
itemEnd = loc.end;
|
|
498
|
+
ctx.res.cursor = loc.start;
|
|
499
|
+
}
|
|
311
500
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
out.push({ indent, source: res ? res.resolveRaw(token.raw ?? "", bound) : null, token });
|
|
501
|
+
|
|
502
|
+
flattenListItem(item, bulletFor(list, i), { ...ctx, bound: itemEnd }, itemStart);
|
|
503
|
+
|
|
504
|
+
if (ctx.res && itemStart !== null) {
|
|
505
|
+
ctx.res.cursor = itemEnd;
|
|
318
506
|
}
|
|
319
507
|
}
|
|
320
508
|
};
|
|
@@ -324,18 +512,28 @@ const flattenWalk = function flattenWalk(
|
|
|
324
512
|
// ---------------------------------------------------------------------------
|
|
325
513
|
|
|
326
514
|
/**
|
|
327
|
-
* Lex, flatten, and source-map Markdown in one operation.
|
|
515
|
+
* Lex, flatten, and source-map Markdown in one operation. A table becomes one
|
|
516
|
+
* `table-row` block per header or body row. Every returned block
|
|
328
517
|
* carries a `source` anchor (or `null` when a marked edge case leaves a token
|
|
329
518
|
* genuinely unresolvable), in the exact order used for navigation/rendering.
|
|
330
|
-
* Block HTML is not rendered, so it does not occupy a navigation row.
|
|
331
|
-
* it before filtering so later blocks retain their sequential source anchors.
|
|
519
|
+
* Block HTML is not rendered, so it does not occupy a navigation row.
|
|
332
520
|
*/
|
|
333
521
|
export const flattenMarkdown = function flattenMarkdown(
|
|
334
522
|
markdown: string,
|
|
335
523
|
options?: FlattenMarkdownOptions
|
|
336
524
|
): FlatBlock[] {
|
|
337
|
-
const
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
525
|
+
const ctx: FlattenContext = {
|
|
526
|
+
bound: markdown.length,
|
|
527
|
+
indent: 0,
|
|
528
|
+
out: [],
|
|
529
|
+
res: new MarkdownResolver(markdown, options?.sourceId),
|
|
530
|
+
};
|
|
531
|
+
|
|
532
|
+
for (const token of marked.lexer(markdown)) {
|
|
533
|
+
if (token.type !== "space") {
|
|
534
|
+
flattenBlockToken(token, ctx);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
return ctx.out;
|
|
341
539
|
};
|