@tooee/renderers 0.2.3 → 0.3.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/code-blocks.d.ts +6 -6
- package/dist/code-blocks.d.ts.map +1 -1
- package/dist/code-blocks.js +52 -42
- package/dist/code-blocks.js.map +1 -1
- package/dist/{CodeView.d.ts → code-view.d.ts} +3 -3
- package/dist/code-view.d.ts.map +1 -0
- package/dist/{CodeView.js → code-view.js} +5 -5
- package/dist/code-view.js.map +1 -0
- package/dist/{CommandPalette.d.ts → command-palette.d.ts} +2 -2
- package/dist/command-palette.d.ts.map +1 -0
- package/dist/{CommandPalette.js → command-palette.js} +27 -24
- package/dist/command-palette.js.map +1 -0
- package/dist/{ContextMenu.d.ts → context-menu.d.ts} +2 -2
- package/dist/context-menu.d.ts.map +1 -0
- package/dist/{ContextMenu.js → context-menu.js} +24 -14
- package/dist/context-menu.js.map +1 -0
- package/dist/{DecorationLayer.d.ts → decoration-layer.d.ts} +2 -2
- package/dist/decoration-layer.d.ts.map +1 -0
- package/dist/decoration-layer.js +2 -0
- package/dist/decoration-layer.js.map +1 -0
- package/dist/{DocumentBindings.d.ts → document-bindings.d.ts} +4 -4
- package/dist/document-bindings.d.ts.map +1 -0
- package/dist/document-bindings.js +2 -0
- package/dist/document-bindings.js.map +1 -0
- package/dist/index.d.ts +16 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -9
- package/dist/index.js.map +1 -1
- package/dist/markdown-blocks.d.ts +9 -13
- package/dist/markdown-blocks.d.ts.map +1 -1
- package/dist/markdown-blocks.js +98 -79
- package/dist/markdown-blocks.js.map +1 -1
- package/dist/{MarkdownView.d.ts → markdown-view.d.ts} +20 -6
- package/dist/markdown-view.d.ts.map +1 -0
- package/dist/markdown-view.js +455 -0
- package/dist/markdown-view.js.map +1 -0
- package/dist/mermaid.d.ts +5 -5
- package/dist/mermaid.d.ts.map +1 -1
- package/dist/mermaid.js +33 -25
- package/dist/mermaid.js.map +1 -1
- package/dist/parsers.d.ts +5 -5
- package/dist/parsers.d.ts.map +1 -1
- package/dist/parsers.js +64 -44
- package/dist/parsers.js.map +1 -1
- package/dist/{RowDocumentRenderable.d.ts → row-document-renderable.d.ts} +15 -11
- package/dist/row-document-renderable.d.ts.map +1 -0
- package/dist/{RowDocumentRenderable.js → row-document-renderable.js} +110 -62
- package/dist/row-document-renderable.js.map +1 -0
- package/dist/row-document.d.ts +1 -1
- package/dist/row-document.d.ts.map +1 -1
- package/dist/row-document.js +1 -1
- package/dist/row-document.js.map +1 -1
- package/dist/source.d.ts +6 -6
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js +35 -26
- package/dist/source.js.map +1 -1
- package/dist/table-types.d.ts.map +1 -1
- package/dist/{Table.d.ts → table.d.ts} +6 -6
- package/dist/table.d.ts.map +1 -0
- package/dist/{Table.js → table.js} +65 -35
- package/dist/table.js.map +1 -0
- package/dist/text-table.d.ts.map +1 -1
- package/dist/text-table.js.map +1 -1
- package/dist/use-gutter-palette.d.ts +3 -0
- package/dist/use-gutter-palette.d.ts.map +1 -0
- package/dist/{useGutterPalette.js → use-gutter-palette.js} +4 -4
- package/dist/use-gutter-palette.js.map +1 -0
- package/package.json +5 -5
- package/src/code-blocks.tsx +122 -106
- package/src/{CodeView.tsx → code-view.tsx} +19 -14
- package/src/command-palette.tsx +163 -0
- package/src/context-menu.tsx +157 -0
- package/src/decoration-layer.ts +11 -0
- package/src/document-bindings.ts +16 -0
- package/src/index.ts +24 -23
- package/src/markdown-blocks.ts +178 -138
- package/src/markdown-view.tsx +853 -0
- package/src/mermaid.ts +94 -71
- package/src/parsers.ts +141 -103
- package/src/row-document-renderable.ts +672 -0
- package/src/row-document.ts +4 -4
- package/src/source.ts +92 -72
- package/src/table-types.ts +4 -4
- package/src/table.tsx +346 -0
- package/src/text-table.ts +4 -4
- package/src/use-gutter-palette.ts +15 -0
- package/dist/CodeView.d.ts.map +0 -1
- package/dist/CodeView.js.map +0 -1
- package/dist/CommandPalette.d.ts.map +0 -1
- package/dist/CommandPalette.js.map +0 -1
- package/dist/ContextMenu.d.ts.map +0 -1
- package/dist/ContextMenu.js.map +0 -1
- package/dist/DecorationLayer.d.ts.map +0 -1
- package/dist/DecorationLayer.js +0 -2
- package/dist/DecorationLayer.js.map +0 -1
- package/dist/DocumentBindings.d.ts.map +0 -1
- package/dist/DocumentBindings.js +0 -2
- package/dist/DocumentBindings.js.map +0 -1
- package/dist/MarkdownView.d.ts.map +0 -1
- package/dist/MarkdownView.js +0 -275
- package/dist/MarkdownView.js.map +0 -1
- package/dist/RowDocumentRenderable.d.ts.map +0 -1
- package/dist/RowDocumentRenderable.js.map +0 -1
- package/dist/Table.d.ts.map +0 -1
- package/dist/Table.js.map +0 -1
- package/dist/useGutterPalette.d.ts +0 -3
- package/dist/useGutterPalette.d.ts.map +0 -1
- package/dist/useGutterPalette.js.map +0 -1
- package/src/CommandPalette.tsx +0 -147
- package/src/ContextMenu.tsx +0 -132
- package/src/DecorationLayer.ts +0 -11
- package/src/DocumentBindings.ts +0 -16
- package/src/MarkdownView.tsx +0 -541
- package/src/RowDocumentRenderable.ts +0 -593
- package/src/Table.tsx +0 -300
- package/src/useGutterPalette.ts +0 -15
package/src/markdown-blocks.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import { marked
|
|
2
|
-
import {
|
|
1
|
+
import { marked } from "marked";
|
|
2
|
+
import type { Token, Tokens } from "marked";
|
|
3
|
+
import { SourceIndex } from "./source.js";
|
|
4
|
+
import type { DocumentRowSource } from "./source.js";
|
|
5
|
+
|
|
6
|
+
const LINE_FEED_CODE = 10;
|
|
7
|
+
|
|
8
|
+
/** Leading indent, bullet/ordinal marker, and optional checkbox on a list item's first line. */
|
|
9
|
+
const LIST_MARKER = /^(?<indent>\s*)(?:[-*+]|\d+[.)])[ \t]*(?:\[[ xX]\])?/u;
|
|
3
10
|
|
|
4
11
|
// ---------------------------------------------------------------------------
|
|
5
12
|
// Flat block model
|
|
@@ -11,23 +18,34 @@ import { SourceIndex, type DocumentRowSource } from "./source.js"
|
|
|
11
18
|
* sibling blocks with appropriate indentation.
|
|
12
19
|
*/
|
|
13
20
|
export interface FlatBlock {
|
|
14
|
-
token: Token
|
|
15
|
-
indent: number
|
|
16
|
-
bullet?: string
|
|
17
|
-
checked?: boolean
|
|
21
|
+
token: Token;
|
|
22
|
+
indent: number;
|
|
23
|
+
bullet?: string;
|
|
24
|
+
checked?: boolean;
|
|
18
25
|
|
|
19
26
|
/** Provenance in the Markdown input; `null` only if resolution genuinely failed. */
|
|
20
|
-
source: DocumentRowSource | null
|
|
27
|
+
source: DocumentRowSource | null;
|
|
21
28
|
}
|
|
22
29
|
|
|
23
30
|
export interface FlattenMarkdownOptions {
|
|
24
|
-
sourceId?: string
|
|
31
|
+
sourceId?: string;
|
|
25
32
|
}
|
|
26
33
|
|
|
27
34
|
/** Empty synthetic token stamped onto bullet-only rows (nested-list/block/empty items). */
|
|
28
|
-
function syntheticTextToken(): Token {
|
|
29
|
-
return {
|
|
30
|
-
}
|
|
35
|
+
const syntheticTextToken = function syntheticTextToken(): Token {
|
|
36
|
+
return { raw: "", text: "", tokens: [], type: "text" };
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Task-list marker for a bullet row. Rows that are not checkbox items have no
|
|
41
|
+
* marker. Not re-exported from the package index — internal to the renderers.
|
|
42
|
+
*/
|
|
43
|
+
export const checkboxMarker = function checkboxMarker(checked: boolean | undefined): string {
|
|
44
|
+
if (checked === undefined) {
|
|
45
|
+
return "";
|
|
46
|
+
}
|
|
47
|
+
return checked ? "[x] " : "[ ] ";
|
|
48
|
+
};
|
|
31
49
|
|
|
32
50
|
/**
|
|
33
51
|
* The visible/searchable text for a block. Non-synthetic blocks use the token's
|
|
@@ -35,15 +53,16 @@ function syntheticTextToken(): Token {
|
|
|
35
53
|
* bullet/checkbox text, then to the resolved source text. This keeps default
|
|
36
54
|
* search and copy in agreement with the source mapping on synthetic rows.
|
|
37
55
|
*/
|
|
38
|
-
export function getFlatBlockText(block: FlatBlock): string {
|
|
39
|
-
const raw = "raw" in block.token && typeof block.token.raw === "string" ? block.token.raw : ""
|
|
40
|
-
if (raw.length > 0)
|
|
56
|
+
export const getFlatBlockText = function getFlatBlockText(block: FlatBlock): string {
|
|
57
|
+
const raw = "raw" in block.token && typeof block.token.raw === "string" ? block.token.raw : "";
|
|
58
|
+
if (raw.length > 0) {
|
|
59
|
+
return raw;
|
|
60
|
+
}
|
|
41
61
|
if (block.bullet !== undefined) {
|
|
42
|
-
|
|
43
|
-
return block.bullet + checkbox
|
|
62
|
+
return block.bullet + checkboxMarker(block.checked);
|
|
44
63
|
}
|
|
45
|
-
return block.source?.primary.text ?? ""
|
|
46
|
-
}
|
|
64
|
+
return block.source?.primary.text ?? "";
|
|
65
|
+
};
|
|
47
66
|
|
|
48
67
|
// ---------------------------------------------------------------------------
|
|
49
68
|
// Markdown provenance resolver
|
|
@@ -57,27 +76,28 @@ export function getFlatBlockText(block: FlatBlock): string {
|
|
|
57
76
|
* occurrences instead of backtracking to an earlier one.
|
|
58
77
|
*/
|
|
59
78
|
class MarkdownResolver {
|
|
60
|
-
readonly index: SourceIndex
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
this.index = new SourceIndex(markdown, sourceId)
|
|
79
|
+
readonly index: SourceIndex;
|
|
80
|
+
readonly markdown: string;
|
|
81
|
+
cursor = 0;
|
|
82
|
+
|
|
83
|
+
constructor(markdown: string, sourceId?: string) {
|
|
84
|
+
this.markdown = markdown;
|
|
85
|
+
this.index = new SourceIndex(markdown, sourceId);
|
|
68
86
|
}
|
|
69
87
|
|
|
70
88
|
/** Resolve `raw` at/after the cursor within `[cursor, bound]`, then advance. */
|
|
71
89
|
resolveRaw(raw: string, bound: number): DocumentRowSource | null {
|
|
72
|
-
const match = this.
|
|
73
|
-
if (!match)
|
|
74
|
-
|
|
75
|
-
|
|
90
|
+
const match = this.findRaw(raw, bound);
|
|
91
|
+
if (!match) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
this.cursor = match.end;
|
|
95
|
+
return { primary: this.index.span(match.start, match.end) };
|
|
76
96
|
}
|
|
77
97
|
|
|
78
98
|
/** Find `raw`'s offset range without advancing the cursor. */
|
|
79
99
|
locate(raw: string, bound: number): { start: number; end: number } | null {
|
|
80
|
-
return this.
|
|
100
|
+
return this.findRaw(raw, bound);
|
|
81
101
|
}
|
|
82
102
|
|
|
83
103
|
/**
|
|
@@ -87,73 +107,87 @@ class MarkdownResolver {
|
|
|
87
107
|
* newline-tolerant match (a needle `\n` matches a source `\r\n` or `\n`) so
|
|
88
108
|
* offsets keep addressing the original `\r\n` string without normalizing it.
|
|
89
109
|
*/
|
|
90
|
-
private
|
|
91
|
-
if (raw.length === 0)
|
|
92
|
-
|
|
110
|
+
private findRaw(raw: string, bound: number): { start: number; end: number } | null {
|
|
111
|
+
if (raw.length === 0) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
const md = this.markdown;
|
|
93
115
|
|
|
94
|
-
const exact = md.indexOf(raw, this.cursor)
|
|
95
|
-
if (exact !== -1 && exact + raw.length <= bound)
|
|
96
|
-
return {
|
|
116
|
+
const exact = md.indexOf(raw, this.cursor);
|
|
117
|
+
if (exact !== -1 && exact + raw.length <= bound) {
|
|
118
|
+
return { end: exact + raw.length, start: exact };
|
|
119
|
+
}
|
|
97
120
|
|
|
98
|
-
if (!raw.includes("\n"))
|
|
121
|
+
if (!raw.includes("\n")) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
99
124
|
|
|
100
|
-
const firstSegment = raw.slice(0, raw.indexOf("\n"))
|
|
101
|
-
if (firstSegment.length === 0)
|
|
125
|
+
const firstSegment = raw.slice(0, raw.indexOf("\n"));
|
|
126
|
+
if (firstSegment.length === 0) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
102
129
|
|
|
103
|
-
let from = this.cursor
|
|
130
|
+
let from = this.cursor;
|
|
104
131
|
for (;;) {
|
|
105
|
-
const start = md.indexOf(firstSegment, from)
|
|
106
|
-
if (start === -1 || start >= bound)
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
132
|
+
const start = md.indexOf(firstSegment, from);
|
|
133
|
+
if (start === -1 || start >= bound) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
const end = this.matchFlexibleNewlines(raw, start, bound);
|
|
137
|
+
if (end !== -1) {
|
|
138
|
+
return { end, start };
|
|
139
|
+
}
|
|
140
|
+
from = start + 1;
|
|
110
141
|
}
|
|
111
142
|
}
|
|
112
143
|
|
|
113
144
|
/** Match `raw` from `start`, letting a needle `\n` consume a source `\r\n`. */
|
|
114
145
|
private matchFlexibleNewlines(raw: string, start: number, bound: number): number {
|
|
115
|
-
const md = this.markdown
|
|
116
|
-
const length = md
|
|
117
|
-
let h = start
|
|
118
|
-
let n = 0
|
|
146
|
+
const md = this.markdown;
|
|
147
|
+
const { length } = md;
|
|
148
|
+
let h = start;
|
|
149
|
+
let n = 0;
|
|
119
150
|
while (n < raw.length) {
|
|
120
|
-
const code = raw.
|
|
121
|
-
if (code ===
|
|
122
|
-
if (h + 1 < length && md.
|
|
123
|
-
h += 2
|
|
124
|
-
} else if (h < length && md.
|
|
125
|
-
h += 1
|
|
151
|
+
const code = raw.codePointAt(n);
|
|
152
|
+
if (code === LINE_FEED_CODE) {
|
|
153
|
+
if (h + 1 < length && md.codePointAt(h) === 13 && md.codePointAt(h + 1) === 10) {
|
|
154
|
+
h += 2;
|
|
155
|
+
} else if (h < length && md.codePointAt(h) === 10) {
|
|
156
|
+
h += 1;
|
|
126
157
|
} else {
|
|
127
|
-
return -1
|
|
158
|
+
return -1;
|
|
128
159
|
}
|
|
129
|
-
n += 1
|
|
160
|
+
n += 1;
|
|
161
|
+
} else if (h < length && md.codePointAt(h) === code) {
|
|
162
|
+
h += 1;
|
|
163
|
+
n += 1;
|
|
130
164
|
} else {
|
|
131
|
-
|
|
132
|
-
h += 1
|
|
133
|
-
n += 1
|
|
134
|
-
} else {
|
|
135
|
-
return -1
|
|
136
|
-
}
|
|
165
|
+
return -1;
|
|
137
166
|
}
|
|
138
167
|
}
|
|
139
|
-
return h <= bound ? h : -1
|
|
168
|
+
return h <= bound ? h : -1;
|
|
140
169
|
}
|
|
141
170
|
|
|
142
171
|
/** Zero-consumption marker span for a synthetic bullet on the item's first line. */
|
|
143
172
|
markerSource(itemStart: number, itemEnd: number): DocumentRowSource {
|
|
144
|
-
const nl = this.markdown.indexOf("\n", itemStart)
|
|
145
|
-
const lineEnd = nl === -1 ? itemEnd : Math.min(nl, itemEnd)
|
|
146
|
-
const firstLine = this.markdown.slice(itemStart, lineEnd)
|
|
147
|
-
const match =
|
|
148
|
-
if (!match)
|
|
149
|
-
|
|
150
|
-
|
|
173
|
+
const nl = this.markdown.indexOf("\n", itemStart);
|
|
174
|
+
const lineEnd = nl === -1 ? itemEnd : Math.min(nl, itemEnd);
|
|
175
|
+
const firstLine = this.markdown.slice(itemStart, lineEnd);
|
|
176
|
+
const match = LIST_MARKER.exec(firstLine);
|
|
177
|
+
if (!match) {
|
|
178
|
+
return { primary: this.index.span(itemStart, itemStart, false) };
|
|
179
|
+
}
|
|
180
|
+
const markerStart = itemStart + (match.groups?.indent ?? "").length;
|
|
181
|
+
let spanEnd = itemStart + match[0].length;
|
|
151
182
|
while (spanEnd > markerStart) {
|
|
152
|
-
const code = this.markdown.
|
|
153
|
-
|
|
154
|
-
|
|
183
|
+
const code = this.markdown.codePointAt(spanEnd - 1);
|
|
184
|
+
// Continue while the character is a space or tab.
|
|
185
|
+
if (code !== 32 && code !== 9) {
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
spanEnd -= 1;
|
|
155
189
|
}
|
|
156
|
-
return { primary: this.index.span(markerStart, spanEnd, false) }
|
|
190
|
+
return { primary: this.index.span(markerStart, spanEnd, false) };
|
|
157
191
|
}
|
|
158
192
|
}
|
|
159
193
|
|
|
@@ -161,39 +195,44 @@ class MarkdownResolver {
|
|
|
161
195
|
// Flattening (shared by mapped and unmapped forms)
|
|
162
196
|
// ---------------------------------------------------------------------------
|
|
163
197
|
|
|
164
|
-
function bulletFor(list: Tokens.List, itemIndex: number): string {
|
|
165
|
-
return list.ordered
|
|
166
|
-
}
|
|
198
|
+
const bulletFor = function bulletFor(list: Tokens.List, itemIndex: number): string {
|
|
199
|
+
return list.ordered
|
|
200
|
+
? `${itemIndex + (list.start === "" || list.start === 0 ? 1 : list.start)}. `
|
|
201
|
+
: "- ";
|
|
202
|
+
};
|
|
167
203
|
|
|
168
|
-
function flattenList(
|
|
204
|
+
const flattenList = function flattenList(
|
|
169
205
|
list: Tokens.List,
|
|
170
206
|
indent: number,
|
|
171
207
|
out: FlatBlock[],
|
|
172
208
|
res: MarkdownResolver | null,
|
|
173
209
|
bound: number,
|
|
174
210
|
): void {
|
|
175
|
-
for (let i = 0; i < list.items.length; i
|
|
176
|
-
const item = list.items[i]
|
|
177
|
-
const bullet = bulletFor(list, i)
|
|
211
|
+
for (let i = 0; i < list.items.length; i += 1) {
|
|
212
|
+
const item = list.items[i];
|
|
213
|
+
const bullet = bulletFor(list, i);
|
|
178
214
|
|
|
179
|
-
let itemStart: number | null = null
|
|
180
|
-
let itemEnd = bound
|
|
215
|
+
let itemStart: number | null = null;
|
|
216
|
+
let itemEnd = bound;
|
|
181
217
|
if (res) {
|
|
182
|
-
const loc = res.locate(item.raw, bound)
|
|
218
|
+
const loc = res.locate(item.raw, bound);
|
|
183
219
|
if (loc) {
|
|
184
|
-
itemStart = loc.start
|
|
185
|
-
itemEnd = loc.end
|
|
186
|
-
res.cursor = loc.start
|
|
220
|
+
itemStart = loc.start;
|
|
221
|
+
itemEnd = loc.end;
|
|
222
|
+
res.cursor = loc.start;
|
|
187
223
|
}
|
|
188
224
|
}
|
|
189
225
|
|
|
190
|
-
|
|
226
|
+
// oxlint-disable-next-line no-use-before-define -- Deferred(lint-sweep): preserve deliberate top-down list flattening organization
|
|
227
|
+
flattenListItem(item, indent, bullet, out, res, itemStart, itemEnd);
|
|
191
228
|
|
|
192
|
-
if (res && itemStart !== null)
|
|
229
|
+
if (res && itemStart !== null) {
|
|
230
|
+
res.cursor = itemEnd;
|
|
231
|
+
}
|
|
193
232
|
}
|
|
194
|
-
}
|
|
233
|
+
};
|
|
195
234
|
|
|
196
|
-
function flattenListItem(
|
|
235
|
+
const flattenListItem = function flattenListItem(
|
|
197
236
|
item: Tokens.ListItem,
|
|
198
237
|
indent: number,
|
|
199
238
|
bullet: string,
|
|
@@ -202,53 +241,62 @@ function flattenListItem(
|
|
|
202
241
|
itemStart: number | null,
|
|
203
242
|
itemEnd: number,
|
|
204
243
|
): void {
|
|
205
|
-
const checked = item.checked
|
|
206
|
-
const childTokens = item.tokens
|
|
207
|
-
let bulletUsed = false
|
|
244
|
+
const checked = item.checked === null ? undefined : item.checked;
|
|
245
|
+
const childTokens = item.tokens;
|
|
246
|
+
let bulletUsed = false;
|
|
208
247
|
|
|
209
248
|
const emitBulletMarker = () => {
|
|
210
249
|
out.push({
|
|
211
|
-
token: syntheticTextToken(),
|
|
212
|
-
indent,
|
|
213
250
|
bullet,
|
|
214
251
|
checked,
|
|
252
|
+
indent,
|
|
215
253
|
source: res && itemStart !== null ? res.markerSource(itemStart, itemEnd) : null,
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
254
|
+
token: syntheticTextToken(),
|
|
255
|
+
});
|
|
256
|
+
bulletUsed = true;
|
|
257
|
+
};
|
|
219
258
|
|
|
220
259
|
for (const token of childTokens) {
|
|
221
|
-
if (token.type === "space" || token.type === "checkbox")
|
|
260
|
+
if (token.type === "space" || token.type === "checkbox") {
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
222
263
|
|
|
223
264
|
if (token.type === "text" || token.type === "paragraph") {
|
|
224
265
|
// Inline content — attach the bullet to the first one.
|
|
225
266
|
out.push({
|
|
226
|
-
token,
|
|
227
|
-
indent,
|
|
228
267
|
bullet: bulletUsed ? undefined : bullet,
|
|
229
268
|
checked: bulletUsed ? undefined : checked,
|
|
269
|
+
indent,
|
|
230
270
|
source: res ? res.resolveRaw(token.raw ?? "", itemEnd) : null,
|
|
231
|
-
|
|
232
|
-
|
|
271
|
+
token,
|
|
272
|
+
});
|
|
273
|
+
bulletUsed = true;
|
|
233
274
|
} else if (token.type === "list") {
|
|
234
|
-
if (!bulletUsed)
|
|
235
|
-
|
|
275
|
+
if (!bulletUsed) {
|
|
276
|
+
emitBulletMarker();
|
|
277
|
+
}
|
|
278
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Deferred(lint-sweep): marked token narrowing; schema-based validation in a later sweep
|
|
279
|
+
flattenList(token as Tokens.List, indent + bullet.length, out, res, itemEnd);
|
|
236
280
|
} else {
|
|
237
281
|
// Block content (code, table, blockquote, hr, etc.).
|
|
238
|
-
if (!bulletUsed)
|
|
282
|
+
if (!bulletUsed) {
|
|
283
|
+
emitBulletMarker();
|
|
284
|
+
}
|
|
239
285
|
out.push({
|
|
240
|
-
token,
|
|
241
286
|
indent: indent + bullet.length,
|
|
242
287
|
source: res ? res.resolveRaw(token.raw ?? "", itemEnd) : null,
|
|
243
|
-
|
|
288
|
+
token,
|
|
289
|
+
});
|
|
244
290
|
}
|
|
245
291
|
}
|
|
246
292
|
|
|
247
293
|
// List item had no content tokens — still emit the bullet.
|
|
248
|
-
if (!bulletUsed)
|
|
249
|
-
|
|
294
|
+
if (!bulletUsed) {
|
|
295
|
+
emitBulletMarker();
|
|
296
|
+
}
|
|
297
|
+
};
|
|
250
298
|
|
|
251
|
-
function flattenWalk(
|
|
299
|
+
const flattenWalk = function flattenWalk(
|
|
252
300
|
tokens: Token[],
|
|
253
301
|
indent: number,
|
|
254
302
|
out: FlatBlock[],
|
|
@@ -256,14 +304,17 @@ function flattenWalk(
|
|
|
256
304
|
bound: number,
|
|
257
305
|
): void {
|
|
258
306
|
for (const token of tokens) {
|
|
259
|
-
if (token.type === "space")
|
|
307
|
+
if (token.type === "space") {
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
260
310
|
if (token.type === "list") {
|
|
261
|
-
|
|
311
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Deferred(lint-sweep): marked token narrowing; schema-based validation in a later sweep
|
|
312
|
+
flattenList(token as Tokens.List, indent, out, res, bound);
|
|
262
313
|
} else {
|
|
263
|
-
out.push({
|
|
314
|
+
out.push({ indent, source: res ? res.resolveRaw(token.raw ?? "", bound) : null, token });
|
|
264
315
|
}
|
|
265
316
|
}
|
|
266
|
-
}
|
|
317
|
+
};
|
|
267
318
|
|
|
268
319
|
// ---------------------------------------------------------------------------
|
|
269
320
|
// Public entry points
|
|
@@ -274,23 +325,12 @@ function flattenWalk(
|
|
|
274
325
|
* carries a `source` anchor (or `null` when a marked edge case leaves a token
|
|
275
326
|
* genuinely unresolvable), in the exact order used for navigation/rendering.
|
|
276
327
|
*/
|
|
277
|
-
export
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
* provenance (every block's `source` is `null`).
|
|
287
|
-
*
|
|
288
|
-
* @deprecated Prefer `flattenMarkdown(source)` for source-backed rows. Tokens
|
|
289
|
-
* carry no positions and the original source is unavailable here, so offsets
|
|
290
|
-
* cannot be reconstructed.
|
|
291
|
-
*/
|
|
292
|
-
export function flattenTokens(tokens: Token[]): FlatBlock[] {
|
|
293
|
-
const out: FlatBlock[] = []
|
|
294
|
-
flattenWalk(tokens, 0, out, null, 0)
|
|
295
|
-
return out
|
|
296
|
-
}
|
|
328
|
+
export const flattenMarkdown = function flattenMarkdown(
|
|
329
|
+
markdown: string,
|
|
330
|
+
options?: FlattenMarkdownOptions,
|
|
331
|
+
): FlatBlock[] {
|
|
332
|
+
const res = new MarkdownResolver(markdown, options?.sourceId);
|
|
333
|
+
const out: FlatBlock[] = [];
|
|
334
|
+
flattenWalk(marked.lexer(markdown), 0, out, res, markdown.length);
|
|
335
|
+
return out;
|
|
336
|
+
};
|