markemental 0.4.0 → 0.5.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/README.md +45 -14
- package/dist/index.cjs +1108 -870
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +287 -351
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +287 -351
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1104 -870
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,63 +1,4 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
/**
|
|
3
|
-
* Create a streaming pipeline around any renderer and target.
|
|
4
|
-
*
|
|
5
|
-
* @remarks
|
|
6
|
-
* The pipeline chunks whatever strings are written into lines and feeds
|
|
7
|
-
* them to the renderer one at a time; the renderer owns its parse state
|
|
8
|
-
* and commits finished output to the target. Closing the stream renders
|
|
9
|
-
* the trailing remainder. Pass a built-in renderer (`GFMRenderer`,
|
|
10
|
-
* `CommonMarkRenderer`) or your own `MarkdownRenderer` implementation,
|
|
11
|
-
* and a DOM target from `createDOMTarget` or your own `HTMLTarget`.
|
|
12
|
-
*
|
|
13
|
-
* @param renderer - Line-consuming renderer holding its own parse state.
|
|
14
|
-
* @param target - Destination the renderer commits to.
|
|
15
|
-
* @returns A working pipeline.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```ts
|
|
19
|
-
* const target = createDOMTarget(document, document.getElementById("out")!);
|
|
20
|
-
* const stream = createMarkdownStream(new GFMRenderer(target, {}), target);
|
|
21
|
-
* const writer = stream.getWriter();
|
|
22
|
-
* await writer.write("# Hello\n");
|
|
23
|
-
* await writer.close();
|
|
24
|
-
* writer.releaseLock();
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* @public
|
|
28
|
-
*/
|
|
29
|
-
function createMarkdownStream(renderer, target) {
|
|
30
|
-
renderer.setTarget(target);
|
|
31
|
-
let buffer = "";
|
|
32
|
-
const pipeline = new WritableStream({
|
|
33
|
-
write(chunk) {
|
|
34
|
-
if (typeof chunk !== "string") throw new TypeError(`MarkdownPipeline chunks must be strings, received ${typeof chunk}`);
|
|
35
|
-
buffer += chunk;
|
|
36
|
-
let at = buffer.indexOf("\n");
|
|
37
|
-
while (at !== -1) {
|
|
38
|
-
renderer.writeLine(buffer.slice(0, at).replace(/\r$/, ""));
|
|
39
|
-
buffer = buffer.slice(at + 1);
|
|
40
|
-
at = buffer.indexOf("\n");
|
|
41
|
-
}
|
|
42
|
-
},
|
|
43
|
-
close() {
|
|
44
|
-
if (buffer !== "") {
|
|
45
|
-
const rest = buffer.replace(/\r$/, "");
|
|
46
|
-
buffer = "";
|
|
47
|
-
renderer.writeLine(rest);
|
|
48
|
-
}
|
|
49
|
-
return renderer.flush();
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
pipeline.setTarget = (next) => {
|
|
53
|
-
renderer.setTarget(next);
|
|
54
|
-
};
|
|
55
|
-
pipeline.setConfig = (next) => {
|
|
56
|
-
renderer.setConfig(next);
|
|
57
|
-
};
|
|
58
|
-
return pipeline;
|
|
59
|
-
}
|
|
60
|
-
// src/core/contracts/html-target.ts
|
|
1
|
+
// src/api/target.ts
|
|
61
2
|
/**
|
|
62
3
|
* A target call referenced an id the target never minted or already
|
|
63
4
|
* retired. Carries the offending id.
|
|
@@ -116,211 +57,721 @@ var HierarchyError = class extends Error {
|
|
|
116
57
|
this.parentId = parentId;
|
|
117
58
|
}
|
|
118
59
|
};
|
|
119
|
-
// src/renderers/parser/blocks.ts
|
|
120
|
-
const HEADING_RE = /^(#{1,6})(?:\s+(.*))?\s*$/;
|
|
121
|
-
const RULE_RE = /^(?:\*\s*){3,}$|^(?:-\s*){3,}$|^(?:_\s*){3,}$/;
|
|
122
|
-
const FENCE_RE = /^(`{3,}|~{3,})\s*([^`]*)$/;
|
|
123
|
-
const QUOTE_RE = /^>\s?(.*)$/;
|
|
124
|
-
const BULLET_RE = /^\s*[-*+]\s+(.*)$/;
|
|
125
|
-
const ORDERED_RE = /^\s*\d+[.)]\s+(.*)$/;
|
|
126
|
-
const TABLE_DELIM_CELL_RE = /^:?-{1,}:?$/;
|
|
127
|
-
const LEADING_WS_RE = /^\s*/;
|
|
128
|
-
/**
|
|
129
|
-
* Whether a line is blank (a pure block separator carrying no content).
|
|
130
|
-
*
|
|
131
|
-
* @param line - Source line to test.
|
|
132
|
-
* @returns True for empty or whitespace-only lines.
|
|
133
|
-
*/
|
|
134
|
-
function isBlankLine(line) {
|
|
135
|
-
return line.trim() === "";
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Match a fence marker line.
|
|
139
|
-
*
|
|
140
|
-
* @param line - Source line to test.
|
|
141
|
-
* @returns The marker, or null when the line is not a fence marker.
|
|
142
|
-
*/
|
|
143
|
-
function matchFence(line) {
|
|
144
|
-
const match = FENCE_RE.exec(line.trim());
|
|
145
|
-
if (!match || !match[1]) return null;
|
|
146
|
-
const run = match[1];
|
|
147
|
-
return {
|
|
148
|
-
marker: run[0],
|
|
149
|
-
length: run.length,
|
|
150
|
-
language: (match[2] ?? "").trim() || void 0
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
60
|
/**
|
|
154
|
-
*
|
|
61
|
+
* An element was created with attributes, but the underlying DOM does not
|
|
62
|
+
* implement `setAttribute`. Carries the tag and the rejected attribute
|
|
63
|
+
* names.
|
|
155
64
|
*
|
|
156
|
-
* @
|
|
157
|
-
* @param line - Source line to test.
|
|
158
|
-
* @returns True for a same-character run at least as long.
|
|
65
|
+
* @public
|
|
159
66
|
*/
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
67
|
+
var UnsupportedAttributesError = class extends Error {
|
|
68
|
+
/** Tag the attributes were requested on. */
|
|
69
|
+
tag;
|
|
70
|
+
/** Names of the rejected attributes. */
|
|
71
|
+
attributes;
|
|
72
|
+
/**
|
|
73
|
+
* @param tag - Tag the attributes were requested on.
|
|
74
|
+
* @param attrs - Rejected attributes.
|
|
75
|
+
*/
|
|
76
|
+
constructor(tag, attrs) {
|
|
77
|
+
super("Cannot set attributes: this DOM does not implement setAttribute");
|
|
78
|
+
this.name = "UnsupportedAttributesError";
|
|
79
|
+
this.tag = tag;
|
|
80
|
+
this.attributes = Object.keys(attrs);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
// src/api/stream.ts
|
|
164
84
|
/**
|
|
165
|
-
*
|
|
85
|
+
* `setTarget` or `setConfig` was called on a closed or aborted stream.
|
|
86
|
+
* Carries the rejected action.
|
|
166
87
|
*
|
|
167
|
-
* @
|
|
168
|
-
* @returns The heading, or null for non-heading lines.
|
|
88
|
+
* @public
|
|
169
89
|
*/
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
90
|
+
var ClosedStreamError = class extends Error {
|
|
91
|
+
/** Action that was rejected. */
|
|
92
|
+
action;
|
|
93
|
+
/**
|
|
94
|
+
* @param action - Action that was rejected.
|
|
95
|
+
*/
|
|
96
|
+
constructor(action) {
|
|
97
|
+
super(`Cannot ${action} on a closed MarkdownStream`);
|
|
98
|
+
this.name = "ClosedStreamError";
|
|
99
|
+
this.action = action;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
178
102
|
/**
|
|
179
|
-
*
|
|
103
|
+
* Incremental markdown stream: a `WritableStream` of markdown text.
|
|
180
104
|
*
|
|
181
|
-
* @
|
|
182
|
-
*
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Match a quote line.
|
|
105
|
+
* @remarks
|
|
106
|
+
* Write unicode string chunks in any sizes; the stream splits them into
|
|
107
|
+
* lines itself and feeds them to its renderer one at a time, and complete
|
|
108
|
+
* blocks render to the target as they arrive. Consult the full
|
|
109
|
+
* documentation for more details.
|
|
189
110
|
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
|
|
193
|
-
function matchQuote(line) {
|
|
194
|
-
const match = QUOTE_RE.exec(line);
|
|
195
|
-
if (!match) return null;
|
|
196
|
-
return { text: match[1] ?? "" };
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Match a bullet or ordered list item line.
|
|
111
|
+
* Pass a built-in renderer (`GFMRenderer`, `CommonMarkRenderer`) or your
|
|
112
|
+
* own `MarkdownRenderer` implementation, already pointed at a DOM target
|
|
113
|
+
* from `createDOMTarget` or a custom `HTMLTarget`.
|
|
200
114
|
*
|
|
201
|
-
* @
|
|
202
|
-
*
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
indent: LEADING_WS_RE.exec(line)?.[0].length ?? 0,
|
|
211
|
-
text: bullet?.[1] ?? ordered?.[1] ?? ""
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Whether a line is a table delimiter row.
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* const target = createDOMTarget({ document, container: document.getElementById("out")! });
|
|
118
|
+
* const stream = new MarkdownStream(new GFMRenderer(target));
|
|
119
|
+
* const writer = stream.getWriter();
|
|
120
|
+
* await writer.write("# Hello\n");
|
|
121
|
+
* await writer.close();
|
|
122
|
+
* writer.releaseLock();
|
|
123
|
+
* ```
|
|
216
124
|
*
|
|
217
|
-
* @
|
|
218
|
-
* @returns True for `|---|...` style rows.
|
|
125
|
+
* @public
|
|
219
126
|
*/
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
127
|
+
var MarkdownStream = class extends WritableStream {
|
|
128
|
+
_renderer;
|
|
129
|
+
_settled = false;
|
|
130
|
+
/**
|
|
131
|
+
* @param renderer - Line-consuming renderer holding its own target and
|
|
132
|
+
* parse state.
|
|
133
|
+
*/
|
|
134
|
+
constructor(renderer) {
|
|
135
|
+
let buffer = "";
|
|
136
|
+
super({
|
|
137
|
+
write: (chunk) => {
|
|
138
|
+
if (typeof chunk !== "string") throw new TypeError(`MarkdownStream chunks must be strings, received ${typeof chunk}`);
|
|
139
|
+
buffer += chunk;
|
|
140
|
+
let at = buffer.indexOf("\n");
|
|
141
|
+
while (at !== -1) {
|
|
142
|
+
renderer.writeLine(buffer.slice(0, at).replace(/\r$/, ""));
|
|
143
|
+
buffer = buffer.slice(at + 1);
|
|
144
|
+
at = buffer.indexOf("\n");
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
close: () => {
|
|
148
|
+
this._settled = true;
|
|
149
|
+
if (buffer !== "") {
|
|
150
|
+
const rest = buffer.replace(/\r$/, "");
|
|
151
|
+
buffer = "";
|
|
152
|
+
renderer.writeLine(rest);
|
|
153
|
+
}
|
|
154
|
+
return renderer.flush();
|
|
155
|
+
},
|
|
156
|
+
abort: () => {
|
|
157
|
+
this._settled = true;
|
|
158
|
+
buffer = "";
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
this._renderer = renderer;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Renderer this stream feeds lines to.
|
|
165
|
+
*/
|
|
166
|
+
get renderer() {
|
|
167
|
+
return this._renderer;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Target the renderer commits to.
|
|
171
|
+
*/
|
|
172
|
+
get target() {
|
|
173
|
+
return this._renderer.target;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Retarget this stream for another target: subsequent commits go there.
|
|
177
|
+
*
|
|
178
|
+
* @param target - Target receiving future output.
|
|
179
|
+
* @throws {@link ClosedStreamError} when the stream is closed or
|
|
180
|
+
* aborted: mutating a dead stream would silently act on a renderer the
|
|
181
|
+
* caller has likely moved on from.
|
|
182
|
+
*/
|
|
183
|
+
setTarget(target) {
|
|
184
|
+
this._assertOpen("setTarget");
|
|
185
|
+
this._renderer.setTarget(target);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Update the renderer config on the fly. Applies to subsequent commits.
|
|
189
|
+
*
|
|
190
|
+
* @param config - Next config.
|
|
191
|
+
* @throws {@link ClosedStreamError} when the stream is closed or aborted.
|
|
192
|
+
*/
|
|
193
|
+
setConfig(config) {
|
|
194
|
+
this._assertOpen("setConfig");
|
|
195
|
+
this._renderer.setConfig(config);
|
|
196
|
+
}
|
|
197
|
+
_assertOpen(action) {
|
|
198
|
+
if (this._settled) throw new ClosedStreamError(action);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
// src/targets/internal/animated-dom-target.ts
|
|
225
202
|
/**
|
|
226
|
-
*
|
|
203
|
+
* Throw when attributes were requested but the DOM cannot set them.
|
|
227
204
|
*
|
|
228
|
-
* @param
|
|
229
|
-
* @
|
|
205
|
+
* @param tag - Tag the attributes were requested on.
|
|
206
|
+
* @param node - Freshly created node.
|
|
207
|
+
* @param attrs - Requested attributes.
|
|
230
208
|
*/
|
|
231
|
-
function
|
|
232
|
-
|
|
233
|
-
for (const line of lines) if (open === null) open = matchFence(line);
|
|
234
|
-
else if (fenceCloses(open, line)) open = null;
|
|
235
|
-
return open !== null;
|
|
236
|
-
}
|
|
237
|
-
function makeBlock(block, sourceLineCount) {
|
|
238
|
-
return {
|
|
239
|
-
...block,
|
|
240
|
-
sourceLineCount
|
|
241
|
-
};
|
|
209
|
+
function assertAttributesSupported$1(tag, node, attrs) {
|
|
210
|
+
if (Object.keys(attrs).length > 0 && typeof node.setAttribute !== "function") throw new UnsupportedAttributesError(tag, attrs);
|
|
242
211
|
}
|
|
243
212
|
/**
|
|
244
|
-
*
|
|
213
|
+
* Target that batches node updates on animation frames.
|
|
245
214
|
*
|
|
246
215
|
* @remarks
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
* and rules are split here.
|
|
216
|
+
* Internal implementation behind `createDOMTarget`. See SPEC.md § DOM
|
|
217
|
+
* targets for the batching rules.
|
|
250
218
|
*
|
|
251
|
-
* @
|
|
252
|
-
* @returns Parsed blocks in document order.
|
|
219
|
+
* @internal
|
|
253
220
|
*/
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
221
|
+
var AnimatedDOMTarget = class {
|
|
222
|
+
_container;
|
|
223
|
+
_doc;
|
|
224
|
+
_raf;
|
|
225
|
+
_graph = /* @__PURE__ */ new Map();
|
|
226
|
+
_nodes = /* @__PURE__ */ new Map();
|
|
227
|
+
_nextId = 0;
|
|
228
|
+
_queue = [];
|
|
229
|
+
_scheduled = false;
|
|
230
|
+
_flushResolvers = [];
|
|
231
|
+
/**
|
|
232
|
+
* @param targetNode - Container node receiving rendered output.
|
|
233
|
+
* @param document - Document used to create nodes.
|
|
234
|
+
* @param raf - Frame scheduler used to batch writes.
|
|
235
|
+
*/
|
|
236
|
+
constructor(targetNode, document, raf) {
|
|
237
|
+
this._container = targetNode;
|
|
238
|
+
this._doc = document;
|
|
239
|
+
this._raf = raf;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Queue an element creation for the next frame.
|
|
243
|
+
*
|
|
244
|
+
* @param tag - Lowercase tag name.
|
|
245
|
+
* @param attrs - Attributes to set.
|
|
246
|
+
* @returns Id addressing the new node.
|
|
247
|
+
*/
|
|
248
|
+
createElement(tag, attrs = {}) {
|
|
249
|
+
const id = this._mint();
|
|
250
|
+
const snap = { ...attrs };
|
|
251
|
+
this._enqueue(() => {
|
|
252
|
+
const node = this._doc.createElement(tag);
|
|
253
|
+
assertAttributesSupported$1(tag, node, snap);
|
|
254
|
+
for (const [name, value] of Object.entries(snap)) node.setAttribute?.(name, value);
|
|
255
|
+
this._nodes.set(id, node);
|
|
256
|
+
});
|
|
257
|
+
return id;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Queue a text node creation for the next frame.
|
|
261
|
+
*
|
|
262
|
+
* @param text - Text content.
|
|
263
|
+
* @returns Id addressing the new node.
|
|
264
|
+
*/
|
|
265
|
+
createTextNode(text) {
|
|
266
|
+
const id = this._mint();
|
|
267
|
+
this._enqueue(() => {
|
|
268
|
+
this._nodes.set(id, this._doc.createTextNode(text));
|
|
269
|
+
});
|
|
270
|
+
return id;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Queue an append for the next frame.
|
|
274
|
+
*
|
|
275
|
+
* @param parent - Receiving node, or `null` for the top level.
|
|
276
|
+
* @param child - Node to append.
|
|
277
|
+
*/
|
|
278
|
+
appendChild(parent, child) {
|
|
279
|
+
if (parent !== null) this._mustGraph(parent);
|
|
280
|
+
this._mustGraph(child);
|
|
281
|
+
this._assertAcyclic(child, parent);
|
|
282
|
+
this._reparentGraph(child, parent);
|
|
283
|
+
this._enqueue(() => {
|
|
284
|
+
(parent === null ? this._container : this._mustNode(parent)).appendChild(this._mustNode(child));
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Queue a replacement for the next frame, retiring the old id.
|
|
289
|
+
*
|
|
290
|
+
* @param oldId - Attached node to replace.
|
|
291
|
+
* @param newId - Replacement node.
|
|
292
|
+
*/
|
|
293
|
+
replaceNode(oldId, newId) {
|
|
294
|
+
const old = this._mustGraph(oldId);
|
|
295
|
+
this._mustGraph(newId);
|
|
296
|
+
if (old.parent === void 0) throw new DetachedNodeError(oldId);
|
|
297
|
+
if (oldId === newId) return;
|
|
298
|
+
this._assertAcyclic(newId, old.parent);
|
|
299
|
+
const parent = old.parent;
|
|
300
|
+
this._reparentGraph(newId, parent);
|
|
301
|
+
const doomed = this._retireGraph(oldId);
|
|
302
|
+
this._enqueue(() => {
|
|
303
|
+
(parent === null ? this._container : this._mustNode(parent)).replaceChild(this._mustNode(newId), this._mustNode(oldId));
|
|
304
|
+
for (const id of doomed) this._nodes.delete(id);
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Queue a removal for the next frame, retiring the id.
|
|
309
|
+
*
|
|
310
|
+
* @param id - Node to remove.
|
|
311
|
+
*/
|
|
312
|
+
remove(id) {
|
|
313
|
+
const parent = this._mustGraph(id).parent;
|
|
314
|
+
const doomed = this._retireGraph(id);
|
|
315
|
+
this._enqueue(() => {
|
|
316
|
+
if (parent !== void 0) (parent === null ? this._container : this._mustNode(parent)).removeChild(this._mustNode(id));
|
|
317
|
+
for (const stale of doomed) this._nodes.delete(stale);
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Queue a text replacement for the next frame.
|
|
322
|
+
*
|
|
323
|
+
* @param node - Node to update.
|
|
324
|
+
* @param text - Replacement text.
|
|
325
|
+
*/
|
|
326
|
+
setTextContent(node, text) {
|
|
327
|
+
const entry = this._mustGraph(node);
|
|
328
|
+
const doomed = [];
|
|
329
|
+
for (const child of [...entry.children]) doomed.push(...this._retireGraph(child));
|
|
330
|
+
this._enqueue(() => {
|
|
331
|
+
this._mustNode(node).textContent = text;
|
|
332
|
+
for (const stale of doomed) this._nodes.delete(stale);
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Wait until every queued update has been applied.
|
|
337
|
+
*
|
|
338
|
+
* @returns A promise that resolves after the next frame carrying updates.
|
|
339
|
+
*/
|
|
340
|
+
flush() {
|
|
341
|
+
if (!this._scheduled && this._queue.length === 0) return Promise.resolve();
|
|
342
|
+
return new Promise((resolve) => {
|
|
343
|
+
this._flushResolvers.push(resolve);
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
_enqueue(apply) {
|
|
347
|
+
this._queue.push(apply);
|
|
348
|
+
if (!this._scheduled) {
|
|
349
|
+
this._scheduled = true;
|
|
350
|
+
this._raf(() => {
|
|
351
|
+
this._scheduled = false;
|
|
352
|
+
const batch = this._queue;
|
|
353
|
+
this._queue = [];
|
|
354
|
+
for (const queued of batch) queued();
|
|
355
|
+
const resolvers = this._flushResolvers;
|
|
356
|
+
this._flushResolvers = [];
|
|
357
|
+
for (const resolve of resolvers) resolve();
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
_mint() {
|
|
362
|
+
this._nextId += 1;
|
|
363
|
+
const id = `n${this._nextId}`;
|
|
364
|
+
this._graph.set(id, {
|
|
365
|
+
parent: void 0,
|
|
366
|
+
children: /* @__PURE__ */ new Set()
|
|
367
|
+
});
|
|
368
|
+
return id;
|
|
369
|
+
}
|
|
370
|
+
_mustGraph(id) {
|
|
371
|
+
const entry = this._graph.get(id);
|
|
372
|
+
if (entry === void 0) throw new UnknownNodeError(id);
|
|
373
|
+
return entry;
|
|
374
|
+
}
|
|
375
|
+
_assertAcyclic(child, parent) {
|
|
376
|
+
if (parent === null) return;
|
|
377
|
+
let at = parent;
|
|
378
|
+
while (at !== null && at !== void 0) {
|
|
379
|
+
if (at === child) throw new HierarchyError(child, parent);
|
|
380
|
+
at = this._graph.get(at)?.parent;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
_mustNode(id) {
|
|
384
|
+
const node = this._nodes.get(id);
|
|
385
|
+
if (node === void 0) throw new UnknownNodeError(id);
|
|
386
|
+
return node;
|
|
387
|
+
}
|
|
388
|
+
_reparentGraph(child, parent) {
|
|
389
|
+
const entry = this._graph.get(child);
|
|
390
|
+
if (entry.parent !== void 0 && entry.parent !== null) this._graph.get(entry.parent)?.children.delete(child);
|
|
391
|
+
entry.parent = parent;
|
|
392
|
+
if (parent !== null) this._graph.get(parent)?.children.add(child);
|
|
393
|
+
}
|
|
394
|
+
_retireGraph(id) {
|
|
395
|
+
const entry = this._graph.get(id);
|
|
396
|
+
if (entry !== void 0 && entry.parent !== void 0 && entry.parent !== null) this._graph.get(entry.parent)?.children.delete(id);
|
|
397
|
+
const doomed = [];
|
|
398
|
+
const visit = (at) => {
|
|
399
|
+
const next = this._graph.get(at);
|
|
400
|
+
if (next === void 0) return;
|
|
401
|
+
doomed.push(at);
|
|
402
|
+
for (const child of [...next.children]) visit(child);
|
|
403
|
+
this._graph.delete(at);
|
|
404
|
+
};
|
|
405
|
+
visit(id);
|
|
406
|
+
return doomed;
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
// src/targets/internal/dom-target.ts
|
|
410
|
+
/**
|
|
411
|
+
* Throw when attributes were requested but the DOM cannot set them.
|
|
412
|
+
*
|
|
413
|
+
* @param tag - Tag the attributes were requested on.
|
|
414
|
+
* @param node - Freshly created node.
|
|
415
|
+
* @param attrs - Requested attributes.
|
|
416
|
+
*/
|
|
417
|
+
function assertAttributesSupported(tag, node, attrs) {
|
|
418
|
+
if (Object.keys(attrs).length > 0 && typeof node.setAttribute !== "function") throw new UnsupportedAttributesError(tag, attrs);
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Target that applies node updates to a DOM container synchronously.
|
|
422
|
+
*
|
|
423
|
+
* @remarks
|
|
424
|
+
* Internal implementation behind `createDOMTarget`. Wraps an externally
|
|
425
|
+
* provided container node from a real or simulated DOM, and tracks ids
|
|
426
|
+
* to nodes in memory.
|
|
427
|
+
*
|
|
428
|
+
* @internal
|
|
429
|
+
*/
|
|
430
|
+
var DOMTarget = class {
|
|
431
|
+
_container;
|
|
432
|
+
_doc;
|
|
433
|
+
_entries = /* @__PURE__ */ new Map();
|
|
434
|
+
_nextId = 0;
|
|
435
|
+
/**
|
|
436
|
+
* @param targetNode - Container node receiving rendered output.
|
|
437
|
+
* @param document - Document used to create nodes.
|
|
438
|
+
*/
|
|
439
|
+
constructor(targetNode, document) {
|
|
440
|
+
this._container = targetNode;
|
|
441
|
+
this._doc = document;
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Create a detached element node with attributes.
|
|
445
|
+
*
|
|
446
|
+
* @param tag - Lowercase tag name.
|
|
447
|
+
* @param attrs - Attributes to set.
|
|
448
|
+
* @returns Id addressing the new node.
|
|
449
|
+
*/
|
|
450
|
+
createElement(tag, attrs = {}) {
|
|
451
|
+
const node = this._doc.createElement(tag);
|
|
452
|
+
assertAttributesSupported(tag, node, attrs);
|
|
453
|
+
for (const [name, value] of Object.entries(attrs)) node.setAttribute?.(name, value);
|
|
454
|
+
return this._store(node, void 0);
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Create a detached text node.
|
|
458
|
+
*
|
|
459
|
+
* @param text - Text content.
|
|
460
|
+
* @returns Id addressing the new node.
|
|
461
|
+
*/
|
|
462
|
+
createTextNode(text) {
|
|
463
|
+
return this._store(this._doc.createTextNode(text), void 0);
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Append a child to a parent, or to the top level for a `null` parent.
|
|
467
|
+
*
|
|
468
|
+
* @param parent - Receiving node, or `null` for the top level.
|
|
469
|
+
* @param child - Node to append.
|
|
470
|
+
*/
|
|
471
|
+
appendChild(parent, child) {
|
|
472
|
+
const entry = this._must(child);
|
|
473
|
+
const attachTo = parent === null ? this._container : this._must(parent).node;
|
|
474
|
+
this._assertAcyclic(child, parent);
|
|
475
|
+
attachTo.appendChild(entry.node);
|
|
476
|
+
this._reparent(child, parent);
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Replace an attached node with another node, retiring the old id.
|
|
480
|
+
*
|
|
481
|
+
* @param oldId - Attached node to replace.
|
|
482
|
+
* @param newId - Replacement node.
|
|
483
|
+
*/
|
|
484
|
+
replaceNode(oldId, newId) {
|
|
485
|
+
const old = this._must(oldId);
|
|
486
|
+
const fresh = this._must(newId);
|
|
487
|
+
if (old.parent === void 0) throw new DetachedNodeError(oldId);
|
|
488
|
+
if (oldId === newId) return;
|
|
489
|
+
this._assertAcyclic(newId, old.parent);
|
|
490
|
+
(old.parent === null ? this._container : this._must(old.parent).node).replaceChild(fresh.node, old.node);
|
|
491
|
+
this._reparent(newId, old.parent);
|
|
492
|
+
this._retire(oldId);
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* Detach a node and retire its id.
|
|
496
|
+
*
|
|
497
|
+
* @param id - Node to remove.
|
|
498
|
+
*/
|
|
499
|
+
remove(id) {
|
|
500
|
+
const entry = this._must(id);
|
|
501
|
+
if (entry.parent !== void 0) (entry.parent === null ? this._container : this._must(entry.parent).node).removeChild(entry.node);
|
|
502
|
+
this._retire(id);
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Replace a node's children with a single text run.
|
|
506
|
+
*
|
|
507
|
+
* @param node - Node to update.
|
|
508
|
+
* @param text - Replacement text.
|
|
509
|
+
*/
|
|
510
|
+
setTextContent(node, text) {
|
|
511
|
+
const entry = this._must(node);
|
|
512
|
+
entry.node.textContent = text;
|
|
513
|
+
for (const child of [...entry.children]) this._retire(child);
|
|
514
|
+
entry.children.clear();
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Nothing to wait for: synchronous targets hold every update already.
|
|
518
|
+
*/
|
|
519
|
+
flush() {}
|
|
520
|
+
_must(id) {
|
|
521
|
+
const entry = this._entries.get(id);
|
|
522
|
+
if (entry === void 0) throw new UnknownNodeError(id);
|
|
523
|
+
return entry;
|
|
524
|
+
}
|
|
525
|
+
_assertAcyclic(child, parent) {
|
|
526
|
+
if (parent === null) return;
|
|
527
|
+
let at = parent;
|
|
528
|
+
while (at !== null && at !== void 0) {
|
|
529
|
+
if (at === child) throw new HierarchyError(child, parent);
|
|
530
|
+
at = this._entries.get(at)?.parent;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
_store(node, parent) {
|
|
534
|
+
this._nextId += 1;
|
|
535
|
+
const id = `n${this._nextId}`;
|
|
536
|
+
this._entries.set(id, {
|
|
537
|
+
node,
|
|
538
|
+
parent,
|
|
539
|
+
children: /* @__PURE__ */ new Set()
|
|
540
|
+
});
|
|
541
|
+
return id;
|
|
542
|
+
}
|
|
543
|
+
_reparent(child, parent) {
|
|
544
|
+
const entry = this._entries.get(child);
|
|
545
|
+
if (entry.parent !== void 0 && entry.parent !== null) this._entries.get(entry.parent)?.children.delete(child);
|
|
546
|
+
entry.parent = parent;
|
|
547
|
+
if (parent !== null) this._entries.get(parent)?.children.add(child);
|
|
548
|
+
}
|
|
549
|
+
_retire(id) {
|
|
550
|
+
const entry = this._entries.get(id);
|
|
551
|
+
if (entry === void 0) return;
|
|
552
|
+
for (const child of [...entry.children]) this._retire(child);
|
|
553
|
+
if (entry.parent !== void 0 && entry.parent !== null) this._entries.get(entry.parent)?.children.delete(id);
|
|
554
|
+
this._entries.delete(id);
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
// src/targets/create-dom-target.ts
|
|
558
|
+
/**
|
|
559
|
+
* Create a target rendering into a DOM container.
|
|
560
|
+
*
|
|
561
|
+
* @param options - Document, container, and optional frame scheduler.
|
|
562
|
+
* @returns The target to hand to a renderer.
|
|
563
|
+
*
|
|
564
|
+
* @public
|
|
565
|
+
*/
|
|
566
|
+
function createDOMTarget(options) {
|
|
567
|
+
const { document, container, raf } = options;
|
|
568
|
+
return raf === void 0 ? new DOMTarget(container, document) : new AnimatedDOMTarget(container, document, raf);
|
|
569
|
+
}
|
|
570
|
+
// src/renderers/internal/parser/blocks.ts
|
|
571
|
+
const HEADING_RE = /^(#{1,6})(?:\s+(.*))?\s*$/;
|
|
572
|
+
const RULE_RE = /^(?:\*\s*){3,}$|^(?:-\s*){3,}$|^(?:_\s*){3,}$/;
|
|
573
|
+
const FENCE_RE = /^(`{3,}|~{3,})\s*([^`]*)$/;
|
|
574
|
+
const QUOTE_RE = /^>\s?(.*)$/;
|
|
575
|
+
const BULLET_RE = /^\s*[-*+]\s+(.*)$/;
|
|
576
|
+
const ORDERED_RE = /^\s*\d+[.)]\s+(.*)$/;
|
|
577
|
+
const TABLE_DELIM_CELL_RE = /^:?-{1,}:?$/;
|
|
578
|
+
const LEADING_WS_RE = /^\s*/;
|
|
579
|
+
/**
|
|
580
|
+
* Whether a line is blank (a pure block separator carrying no content).
|
|
581
|
+
*
|
|
582
|
+
* @param line - Source line to test.
|
|
583
|
+
* @returns True for empty or whitespace-only lines.
|
|
584
|
+
*/
|
|
585
|
+
function isBlankLine(line) {
|
|
586
|
+
return line.trim() === "";
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Match a fence marker line.
|
|
590
|
+
*
|
|
591
|
+
* @param line - Source line to test.
|
|
592
|
+
* @returns The marker, or null when the line is not a fence marker.
|
|
593
|
+
*/
|
|
594
|
+
function matchFence(line) {
|
|
595
|
+
const match = FENCE_RE.exec(line.trim());
|
|
596
|
+
if (!match || !match[1]) return null;
|
|
597
|
+
const run = match[1];
|
|
598
|
+
return {
|
|
599
|
+
marker: run[0],
|
|
600
|
+
length: run.length,
|
|
601
|
+
language: (match[2] ?? "").trim() || void 0
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Whether a line closes an open fence.
|
|
606
|
+
*
|
|
607
|
+
* @param open - Marker that opened the fence.
|
|
608
|
+
* @param line - Source line to test.
|
|
609
|
+
* @returns True for a same-character run at least as long.
|
|
610
|
+
*/
|
|
611
|
+
function fenceCloses(open, line) {
|
|
612
|
+
const match = matchFence(line);
|
|
613
|
+
return match !== null && match.marker === open.marker && match.length >= open.length;
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Match an ATX heading line.
|
|
617
|
+
*
|
|
618
|
+
* @param line - Source line to test.
|
|
619
|
+
* @returns The heading, or null for non-heading lines.
|
|
620
|
+
*/
|
|
621
|
+
function matchHeading(line) {
|
|
622
|
+
const match = HEADING_RE.exec(line);
|
|
623
|
+
if (!match || !match[1]) return null;
|
|
624
|
+
return {
|
|
625
|
+
level: Math.min(6, match[1].length),
|
|
626
|
+
text: (match[2] ?? "").replace(/\s+#+\s*$/, "").trim()
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* Whether a line is a thematic break.
|
|
631
|
+
*
|
|
632
|
+
* @param line - Source line to test.
|
|
633
|
+
* @returns True for `***`, `---`, `___` style lines.
|
|
634
|
+
*/
|
|
635
|
+
function isRuleLine(line) {
|
|
636
|
+
return RULE_RE.test(line.trim());
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Match a quote line.
|
|
640
|
+
*
|
|
641
|
+
* @param line - Source line to test.
|
|
642
|
+
* @returns The content, or null for non-quote lines.
|
|
643
|
+
*/
|
|
644
|
+
function matchQuote(line) {
|
|
645
|
+
const match = QUOTE_RE.exec(line);
|
|
646
|
+
if (!match) return null;
|
|
647
|
+
return { text: match[1] ?? "" };
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Match a bullet or ordered list item line.
|
|
651
|
+
*
|
|
652
|
+
* @param line - Source line to test.
|
|
653
|
+
* @returns The item, or null for non-item lines.
|
|
654
|
+
*/
|
|
655
|
+
function matchListItem(line) {
|
|
656
|
+
const bullet = BULLET_RE.exec(line);
|
|
657
|
+
const ordered = ORDERED_RE.exec(line);
|
|
658
|
+
if (!bullet && !ordered) return null;
|
|
659
|
+
return {
|
|
660
|
+
ordered: !bullet,
|
|
661
|
+
indent: LEADING_WS_RE.exec(line)?.[0].length ?? 0,
|
|
662
|
+
text: bullet?.[1] ?? ordered?.[1] ?? ""
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Whether a line is a table delimiter row.
|
|
667
|
+
*
|
|
668
|
+
* @param line - Source line to test.
|
|
669
|
+
* @returns True for `|---|...` style rows.
|
|
670
|
+
*/
|
|
671
|
+
function isTableDelimiter(line) {
|
|
672
|
+
if (!line.includes("|") && !line.includes("-")) return false;
|
|
673
|
+
const cells = line.trim().replace(/^\|/, "").replace(/\|$/, "").split("|");
|
|
674
|
+
return cells.length > 0 && cells.every((cell) => TABLE_DELIM_CELL_RE.test(cell.trim()));
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* Whether `lines` ends inside an unclosed fenced code block.
|
|
678
|
+
*
|
|
679
|
+
* @param lines - Source lines to scan.
|
|
680
|
+
* @returns True when a fence opens without a matching close.
|
|
681
|
+
*/
|
|
682
|
+
function hasOpenFence(lines) {
|
|
683
|
+
let open = null;
|
|
684
|
+
for (const line of lines) if (open === null) open = matchFence(line);
|
|
685
|
+
else if (fenceCloses(open, line)) open = null;
|
|
686
|
+
return open !== null;
|
|
687
|
+
}
|
|
688
|
+
function makeBlock(block, sourceLineCount) {
|
|
689
|
+
return {
|
|
690
|
+
...block,
|
|
691
|
+
sourceLineCount
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* Split leaf-block lines into blocks.
|
|
696
|
+
*
|
|
697
|
+
* @remarks
|
|
698
|
+
* Tables and lists never reach this function: the render state diverts them
|
|
699
|
+
* into streaming fragments, so only headings, paragraphs, fences, quotes,
|
|
700
|
+
* and rules are split here.
|
|
701
|
+
*
|
|
702
|
+
* @param lines - Source lines without line breaks.
|
|
703
|
+
* @returns Parsed blocks in document order.
|
|
704
|
+
*/
|
|
705
|
+
function splitBlocks(lines) {
|
|
706
|
+
const blocks = [];
|
|
707
|
+
let i = 0;
|
|
708
|
+
while (i < lines.length) {
|
|
709
|
+
const line = lines[i];
|
|
710
|
+
if (isBlankLine(line)) {
|
|
711
|
+
i++;
|
|
712
|
+
continue;
|
|
713
|
+
}
|
|
714
|
+
const fence = matchFence(line);
|
|
715
|
+
if (fence) {
|
|
716
|
+
const content = [];
|
|
717
|
+
let j = i + 1;
|
|
718
|
+
while (j < lines.length && !fenceCloses(fence, lines[j])) {
|
|
719
|
+
content.push(lines[j]);
|
|
720
|
+
j++;
|
|
721
|
+
}
|
|
722
|
+
const closed = j < lines.length;
|
|
723
|
+
blocks.push(makeBlock({
|
|
724
|
+
kind: "fence",
|
|
725
|
+
lines: content,
|
|
726
|
+
language: fence.language
|
|
727
|
+
}, j - i + (closed ? 1 : 0)));
|
|
728
|
+
i = j + (closed ? 1 : 0);
|
|
729
|
+
continue;
|
|
730
|
+
}
|
|
731
|
+
const heading = matchHeading(line);
|
|
732
|
+
if (heading) {
|
|
733
|
+
blocks.push(makeBlock({
|
|
734
|
+
kind: "heading",
|
|
735
|
+
lines: [heading.text],
|
|
736
|
+
level: heading.level
|
|
737
|
+
}, 1));
|
|
738
|
+
i++;
|
|
739
|
+
continue;
|
|
740
|
+
}
|
|
741
|
+
if (isRuleLine(line)) {
|
|
742
|
+
blocks.push(makeBlock({
|
|
743
|
+
kind: "rule",
|
|
744
|
+
lines: []
|
|
745
|
+
}, 1));
|
|
746
|
+
i++;
|
|
747
|
+
continue;
|
|
748
|
+
}
|
|
749
|
+
const quote = matchQuote(line);
|
|
750
|
+
if (quote) {
|
|
751
|
+
const quoted = [quote.text];
|
|
752
|
+
let j = i + 1;
|
|
753
|
+
while (j < lines.length) {
|
|
754
|
+
const next = matchQuote(lines[j]);
|
|
755
|
+
if (!next) break;
|
|
756
|
+
quoted.push(next.text);
|
|
757
|
+
j++;
|
|
758
|
+
}
|
|
759
|
+
blocks.push(makeBlock({
|
|
760
|
+
kind: "quote",
|
|
761
|
+
lines: quoted
|
|
762
|
+
}, j - i));
|
|
763
|
+
i = j;
|
|
764
|
+
continue;
|
|
765
|
+
}
|
|
766
|
+
const paragraph = [line];
|
|
767
|
+
let j = i + 1;
|
|
768
|
+
while (j < lines.length) {
|
|
769
|
+
const next = lines[j];
|
|
770
|
+
if (isBlankLine(next) || matchFence(next) !== null || matchHeading(next) !== null || isRuleLine(next) || matchQuote(next) !== null) break;
|
|
771
|
+
paragraph.push(next);
|
|
772
|
+
j++;
|
|
773
|
+
}
|
|
774
|
+
blocks.push(makeBlock({
|
|
324
775
|
kind: "paragraph",
|
|
325
776
|
lines: paragraph
|
|
326
777
|
}, j - i));
|
|
@@ -351,21 +802,17 @@ function parseTableAlignment(cell) {
|
|
|
351
802
|
if (left) return "left";
|
|
352
803
|
return null;
|
|
353
804
|
}
|
|
354
|
-
// src/renderers/
|
|
805
|
+
// src/renderers/internal/parser-state.ts
|
|
355
806
|
/**
|
|
356
|
-
*
|
|
807
|
+
* Shared incremental line-driven parser for block structure.
|
|
357
808
|
*
|
|
358
809
|
* @remarks
|
|
359
|
-
* Internal
|
|
360
|
-
*
|
|
361
|
-
* trailing holdback), and lists commit one fragment per item with an indent
|
|
362
|
-
* stack for nesting. Tables are not recognized here, so table-looking lines
|
|
363
|
-
* stay plain paragraph text; {@link GFMState} extends this class with table
|
|
364
|
-
* support and nothing else.
|
|
810
|
+
* Internal base owned by the flavor states below. See SPEC.md
|
|
811
|
+
* § Incremental parsing for buffering, holdback, and fragment rules.
|
|
365
812
|
*
|
|
366
813
|
* @internal
|
|
367
814
|
*/
|
|
368
|
-
var
|
|
815
|
+
var BlockParserState = class {
|
|
369
816
|
_emit;
|
|
370
817
|
_pending = [];
|
|
371
818
|
_fence = null;
|
|
@@ -574,16 +1021,26 @@ var CommonMarkState = class {
|
|
|
574
1021
|
}
|
|
575
1022
|
};
|
|
576
1023
|
/**
|
|
1024
|
+
* Incremental line-driven parser for CommonMark structure.
|
|
1025
|
+
*
|
|
1026
|
+
* @remarks
|
|
1027
|
+
* The base behavior unchanged: tables are not recognized, so table-looking
|
|
1028
|
+
* lines stay plain paragraph text.
|
|
1029
|
+
*
|
|
1030
|
+
* @internal
|
|
1031
|
+
*/
|
|
1032
|
+
var CommonMarkState = class extends BlockParserState {};
|
|
1033
|
+
/**
|
|
577
1034
|
* Incremental line-driven parser for GFM structure.
|
|
578
1035
|
*
|
|
579
1036
|
* @remarks
|
|
580
|
-
* Extends {@link
|
|
1037
|
+
* Extends {@link BlockParserState} with tables and nothing else: a table
|
|
581
1038
|
* commits at the delimiter row and streams one fragment per row. All leaf
|
|
582
1039
|
* and list parsing is inherited unchanged.
|
|
583
1040
|
*
|
|
584
1041
|
* @internal
|
|
585
1042
|
*/
|
|
586
|
-
var GFMState = class extends
|
|
1043
|
+
var GFMState = class extends BlockParserState {
|
|
587
1044
|
_tableOpen = false;
|
|
588
1045
|
_candidate = null;
|
|
589
1046
|
/**
|
|
@@ -669,7 +1126,7 @@ var GFMState = class extends CommonMarkState {
|
|
|
669
1126
|
return true;
|
|
670
1127
|
}
|
|
671
1128
|
};
|
|
672
|
-
// src/renderers/parser/inline.ts
|
|
1129
|
+
// src/renderers/internal/parser/inline.ts
|
|
673
1130
|
const ESCAPABLE_RE = /^[!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]$/;
|
|
674
1131
|
const AUTOLINK_RE = /^[a-zA-Z][a-zA-Z0-9+.-]*:[^<>\s]*$/;
|
|
675
1132
|
const ALNUM_RE = /[A-Za-z0-9]/;
|
|
@@ -833,710 +1290,438 @@ function appendInline(target, parentId, text, gfm) {
|
|
|
833
1290
|
}
|
|
834
1291
|
literal += ch;
|
|
835
1292
|
i++;
|
|
836
|
-
}
|
|
837
|
-
flushLiteral();
|
|
838
|
-
}
|
|
839
|
-
/**
|
|
840
|
-
* Split a paragraph line into text plus a trailing hard break marker.
|
|
841
|
-
*
|
|
842
|
-
* @param line - Raw paragraph line.
|
|
843
|
-
* @returns Text without the break marker, and whether one was present.
|
|
844
|
-
*/
|
|
845
|
-
function splitHardBreak(line) {
|
|
846
|
-
const hardBreak = / {2,}$/.test(line);
|
|
847
|
-
return {
|
|
848
|
-
text: hardBreak ? line.replace(/ {2,}$/, "") : line,
|
|
849
|
-
hardBreak
|
|
850
|
-
};
|
|
851
|
-
}
|
|
852
|
-
// src/renderers/commonmark.ts
|
|
853
|
-
/**
|
|
854
|
-
* Renderer for CommonMark.
|
|
855
|
-
*
|
|
856
|
-
* @remarks
|
|
857
|
-
* Constructed with its target and config. Feed it lines with
|
|
858
|
-
* {@link CommonMarkRenderer.writeLine}; it holds its own parse state and
|
|
859
|
-
* writes finished blocks to the target as they complete. Open lists are
|
|
860
|
-
* tracked on a stack so items — including nested ones — append to the
|
|
861
|
-
* right container as they arrive.
|
|
862
|
-
*
|
|
863
|
-
* The renderer never touches the DOM: it builds everything through the
|
|
864
|
-
* write-only target, so the same renderer drives live pages, simulated
|
|
865
|
-
* DOMs, and fully custom targets.
|
|
866
|
-
*
|
|
867
|
-
* @public
|
|
868
|
-
*/
|
|
869
|
-
var CommonMarkRenderer = class {
|
|
870
|
-
_target;
|
|
871
|
-
_config;
|
|
872
|
-
_state;
|
|
873
|
-
_open = [];
|
|
874
|
-
/**
|
|
875
|
-
* @param target - Destination for rendered output, from `createDOMTarget`
|
|
876
|
-
* or a custom `HTMLTarget` implementation.
|
|
877
|
-
* @param config - CommonMark rendering config.
|
|
878
|
-
*/
|
|
879
|
-
constructor(target, config) {
|
|
880
|
-
this._target = target;
|
|
881
|
-
this._config = config;
|
|
882
|
-
this._state = this._createState((fragment) => this._applyFragment(fragment));
|
|
883
|
-
}
|
|
884
|
-
/**
|
|
885
|
-
* Feed one source line; completed output is written to the target.
|
|
886
|
-
*
|
|
887
|
-
* @param line - Next source line, without its line break.
|
|
888
|
-
*/
|
|
889
|
-
writeLine(line) {
|
|
890
|
-
this._state.addLine(line);
|
|
891
|
-
}
|
|
892
|
-
/**
|
|
893
|
-
* Write the buffered remainder and settle the target.
|
|
894
|
-
*
|
|
895
|
-
* @returns A promise that resolves once all output has landed in the target.
|
|
896
|
-
*/
|
|
897
|
-
async flush() {
|
|
898
|
-
this._state.flush();
|
|
899
|
-
await this._target.flush();
|
|
900
|
-
}
|
|
901
|
-
/**
|
|
902
|
-
* Point the renderer at another target; subsequent writes go there.
|
|
903
|
-
*
|
|
904
|
-
* @param target - Target receiving future output.
|
|
905
|
-
*/
|
|
906
|
-
setTarget(target) {
|
|
907
|
-
this._target = target;
|
|
908
|
-
}
|
|
909
|
-
/**
|
|
910
|
-
* Replace the renderer config; applies to subsequent writes.
|
|
911
|
-
*
|
|
912
|
-
* @param config - Next config.
|
|
913
|
-
*/
|
|
914
|
-
setConfig(config) {
|
|
915
|
-
this._config = config;
|
|
916
|
-
}
|
|
917
|
-
/**
|
|
918
|
-
* Create the parser state. Overridden by `GFMRenderer` to parse
|
|
919
|
-
* tables.
|
|
920
|
-
*
|
|
921
|
-
* @param emit - Receives parse fragments in document order.
|
|
922
|
-
* @returns The state for this renderer.
|
|
923
|
-
*/
|
|
924
|
-
_createState(emit) {
|
|
925
|
-
return new CommonMarkState(emit);
|
|
926
|
-
}
|
|
927
|
-
_applyFragment(fragment) {
|
|
928
|
-
switch (fragment.kind) {
|
|
929
|
-
case "block":
|
|
930
|
-
this.renderBlock(fragment.block);
|
|
931
|
-
break;
|
|
932
|
-
case "listStart": {
|
|
933
|
-
const parent = this._open[this._open.length - 1]?.currentItem ?? null;
|
|
934
|
-
const list = this._target.createElement(fragment.ordered ? "ol" : "ul");
|
|
935
|
-
this._target.appendChild(parent, list);
|
|
936
|
-
this._open.push({
|
|
937
|
-
id: list,
|
|
938
|
-
currentItem: null
|
|
939
|
-
});
|
|
940
|
-
break;
|
|
941
|
-
}
|
|
942
|
-
case "listItem": {
|
|
943
|
-
const top = this._open[this._open.length - 1];
|
|
944
|
-
const item = this._target.createElement("li");
|
|
945
|
-
appendInline(this._target, item, fragment.text, this.inlineGfm());
|
|
946
|
-
this._target.appendChild(top.id, item);
|
|
947
|
-
top.currentItem = item;
|
|
948
|
-
break;
|
|
949
|
-
}
|
|
950
|
-
case "listEnd": this._open.pop();
|
|
951
|
-
}
|
|
952
|
-
}
|
|
953
|
-
/**
|
|
954
|
-
* Whether GFM-only inlines are enabled. Always false here; overridden by
|
|
955
|
-
* `GFMRenderer`.
|
|
956
|
-
*
|
|
957
|
-
* @returns False.
|
|
958
|
-
*/
|
|
959
|
-
inlineGfm() {
|
|
960
|
-
return false;
|
|
961
|
-
}
|
|
962
|
-
/**
|
|
963
|
-
* Write one parsed block to the target.
|
|
964
|
-
*
|
|
965
|
-
* @param block - Parsed block to write.
|
|
966
|
-
*/
|
|
967
|
-
renderBlock(block) {
|
|
968
|
-
switch (block.kind) {
|
|
969
|
-
case "heading": {
|
|
970
|
-
const heading = this._target.createElement(`h${block.level ?? 1}`);
|
|
971
|
-
this.appendJoinedLines(heading, block.lines);
|
|
972
|
-
this._target.appendChild(null, heading);
|
|
973
|
-
break;
|
|
974
|
-
}
|
|
975
|
-
case "paragraph": {
|
|
976
|
-
const paragraph = this._target.createElement("p");
|
|
977
|
-
this.appendParagraphLines(paragraph, block.lines);
|
|
978
|
-
this._target.appendChild(null, paragraph);
|
|
979
|
-
break;
|
|
980
|
-
}
|
|
981
|
-
case "fence": {
|
|
982
|
-
const pre = this._target.createElement("pre");
|
|
983
|
-
const code = this._target.createElement("code", block.language === void 0 ? {} : { class: `language-${block.language}` });
|
|
984
|
-
const text = block.lines.join("\n");
|
|
985
|
-
this._target.appendChild(code, this._target.createTextNode(text === "" ? "" : `${text}\n`));
|
|
986
|
-
this._target.appendChild(pre, code);
|
|
987
|
-
this._target.appendChild(null, pre);
|
|
988
|
-
break;
|
|
989
|
-
}
|
|
990
|
-
case "quote": {
|
|
991
|
-
const quote = this._target.createElement("blockquote");
|
|
992
|
-
const body = this._target.createElement("p");
|
|
993
|
-
this.appendJoinedLines(body, block.lines);
|
|
994
|
-
this._target.appendChild(quote, body);
|
|
995
|
-
this._target.appendChild(null, quote);
|
|
996
|
-
break;
|
|
997
|
-
}
|
|
998
|
-
case "rule": this._target.appendChild(null, this._target.createElement("hr"));
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
/**
|
|
1002
|
-
* Write paragraph lines into a parent, honoring two-space hard breaks.
|
|
1003
|
-
*
|
|
1004
|
-
* @param parentId - Node the lines append to.
|
|
1005
|
-
* @param lines - Raw paragraph lines.
|
|
1006
|
-
*/
|
|
1007
|
-
appendParagraphLines(parentId, lines) {
|
|
1008
|
-
lines.forEach((line, at) => {
|
|
1009
|
-
const { text, hardBreak } = splitHardBreak(line);
|
|
1010
|
-
appendInline(this._target, parentId, text, this.inlineGfm());
|
|
1011
|
-
if (at < lines.length - 1) {
|
|
1012
|
-
if (hardBreak) this._target.appendChild(parentId, this._target.createElement("br"));
|
|
1013
|
-
else this._target.appendChild(parentId, this._target.createTextNode(" "));
|
|
1014
|
-
}
|
|
1015
|
-
});
|
|
1016
|
-
}
|
|
1017
|
-
appendJoinedLines(parentId, lines) {
|
|
1018
|
-
lines.forEach((line, at) => {
|
|
1019
|
-
if (at > 0) this._target.appendChild(parentId, this._target.createTextNode(" "));
|
|
1020
|
-
appendInline(this._target, parentId, line, this.inlineGfm());
|
|
1021
|
-
});
|
|
1022
|
-
}
|
|
1023
|
-
};
|
|
1024
|
-
// src/renderers/gfm.ts
|
|
1293
|
+
}
|
|
1294
|
+
flushLiteral();
|
|
1295
|
+
}
|
|
1025
1296
|
/**
|
|
1026
|
-
*
|
|
1297
|
+
* Split a paragraph line into text plus a trailing hard break marker.
|
|
1298
|
+
*
|
|
1299
|
+
* @param line - Raw paragraph line.
|
|
1300
|
+
* @returns Text without the break marker, and whether one was present.
|
|
1301
|
+
*/
|
|
1302
|
+
function splitHardBreak(line) {
|
|
1303
|
+
const hardBreak = / {2,}$/.test(line);
|
|
1304
|
+
return {
|
|
1305
|
+
text: hardBreak ? line.replace(/ {2,}$/, "") : line,
|
|
1306
|
+
hardBreak
|
|
1307
|
+
};
|
|
1308
|
+
}
|
|
1309
|
+
// src/renderers/internal/render-shared.ts
|
|
1310
|
+
/**
|
|
1311
|
+
* Tracks open lists so items — including nested ones — commit to the
|
|
1312
|
+
* right container as they arrive.
|
|
1027
1313
|
*
|
|
1028
1314
|
* @remarks
|
|
1029
|
-
*
|
|
1030
|
-
*
|
|
1031
|
-
*
|
|
1032
|
-
* on wide rows are kept as unaligned cells.
|
|
1315
|
+
* Internal helper owned by a renderer. Both flavors use it; the `gfm`
|
|
1316
|
+
* flag only switches inline parsing inside items. See SPEC.md § Renderers
|
|
1317
|
+
* for the target-binding rules.
|
|
1033
1318
|
*
|
|
1034
|
-
* @
|
|
1319
|
+
* @internal
|
|
1035
1320
|
*/
|
|
1036
|
-
var
|
|
1037
|
-
|
|
1321
|
+
var ListStack = class {
|
|
1322
|
+
_target;
|
|
1323
|
+
_gfm;
|
|
1324
|
+
_open = [];
|
|
1038
1325
|
/**
|
|
1039
|
-
* @param target - Destination for
|
|
1040
|
-
*
|
|
1041
|
-
* @param config - GFM rendering config.
|
|
1326
|
+
* @param target - Destination for newly opened root lists.
|
|
1327
|
+
* @param gfm - Enable GFM-only inlines inside items.
|
|
1042
1328
|
*/
|
|
1043
|
-
constructor(target,
|
|
1044
|
-
|
|
1329
|
+
constructor(target, gfm) {
|
|
1330
|
+
this._target = target;
|
|
1331
|
+
this._gfm = gfm;
|
|
1045
1332
|
}
|
|
1046
1333
|
/**
|
|
1047
|
-
*
|
|
1334
|
+
* Point the stack at another target; newly opened root lists go there.
|
|
1335
|
+
* Already open lists stay bound to their own target.
|
|
1048
1336
|
*
|
|
1049
|
-
* @
|
|
1337
|
+
* @param target - Target receiving future output.
|
|
1050
1338
|
*/
|
|
1051
|
-
|
|
1052
|
-
|
|
1339
|
+
setTarget(target) {
|
|
1340
|
+
this._target = target;
|
|
1053
1341
|
}
|
|
1054
1342
|
/**
|
|
1055
|
-
*
|
|
1343
|
+
* Commit a list container, nested inside the latest item when one is open.
|
|
1056
1344
|
*
|
|
1057
|
-
* @param
|
|
1058
|
-
* @returns The state for this renderer.
|
|
1345
|
+
* @param ordered - Whether the list is ordered (`ol`) or bullet (`ul`).
|
|
1059
1346
|
*/
|
|
1060
|
-
|
|
1061
|
-
|
|
1347
|
+
openList(ordered) {
|
|
1348
|
+
const top = this._open[this._open.length - 1];
|
|
1349
|
+
const scope = top?.scope ?? this._target;
|
|
1350
|
+
const parent = top?.currentItem ?? null;
|
|
1351
|
+
const list = scope.createElement(ordered ? "ol" : "ul");
|
|
1352
|
+
scope.appendChild(parent, list);
|
|
1353
|
+
this._open.push({
|
|
1354
|
+
scope,
|
|
1355
|
+
id: list,
|
|
1356
|
+
currentItem: null
|
|
1357
|
+
});
|
|
1062
1358
|
}
|
|
1063
1359
|
/**
|
|
1064
|
-
*
|
|
1360
|
+
* Commit one item to the innermost open list.
|
|
1065
1361
|
*
|
|
1066
|
-
* @param
|
|
1362
|
+
* @param text - Raw item text after the marker.
|
|
1067
1363
|
*/
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
this._applyTableRow(fragment.line);
|
|
1075
|
-
break;
|
|
1076
|
-
case "tableEnd":
|
|
1077
|
-
this._applyTableEnd();
|
|
1078
|
-
break;
|
|
1079
|
-
default: super._applyFragment(fragment);
|
|
1080
|
-
}
|
|
1364
|
+
addItem(text) {
|
|
1365
|
+
const top = this._open[this._open.length - 1];
|
|
1366
|
+
const item = top.scope.createElement("li");
|
|
1367
|
+
appendInline(top.scope, item, text, this._gfm);
|
|
1368
|
+
top.scope.appendChild(top.id, item);
|
|
1369
|
+
top.currentItem = item;
|
|
1081
1370
|
}
|
|
1082
1371
|
/**
|
|
1083
|
-
*
|
|
1084
|
-
*
|
|
1085
|
-
* @param headLine - Raw header row line.
|
|
1086
|
-
* @param delimLine - Delimiter row line carrying column alignment.
|
|
1372
|
+
* Close the innermost open list.
|
|
1087
1373
|
*/
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
const aligns = splitTableRow(delimLine).map(parseTableAlignment);
|
|
1091
|
-
const width = header.length;
|
|
1092
|
-
const table = this._target.createElement("table");
|
|
1093
|
-
const head = this._target.createElement("thead");
|
|
1094
|
-
const headRow = this._target.createElement("tr");
|
|
1095
|
-
for (let col = 0; col < width; col++) {
|
|
1096
|
-
const align = aligns[col] ?? null;
|
|
1097
|
-
const cell = this._target.createElement("th", align === null ? {} : { align });
|
|
1098
|
-
appendInline(this._target, cell, header[col] ?? "", true);
|
|
1099
|
-
this._target.appendChild(headRow, cell);
|
|
1100
|
-
}
|
|
1101
|
-
this._target.appendChild(head, headRow);
|
|
1102
|
-
this._target.appendChild(table, head);
|
|
1103
|
-
const body = this._target.createElement("tbody");
|
|
1104
|
-
this._target.appendChild(table, body);
|
|
1105
|
-
this._target.appendChild(null, table);
|
|
1106
|
-
this._table = {
|
|
1107
|
-
body,
|
|
1108
|
-
aligns,
|
|
1109
|
-
width
|
|
1110
|
-
};
|
|
1374
|
+
closeList() {
|
|
1375
|
+
this._open.pop();
|
|
1111
1376
|
}
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1377
|
+
};
|
|
1378
|
+
/**
|
|
1379
|
+
* Write one parsed leaf block to the target.
|
|
1380
|
+
*
|
|
1381
|
+
* @param target - Destination for rendered output.
|
|
1382
|
+
* @param block - Parsed block to write.
|
|
1383
|
+
* @param gfm - Enable GFM-only inlines.
|
|
1384
|
+
*
|
|
1385
|
+
* @internal
|
|
1386
|
+
*/
|
|
1387
|
+
function renderLeafBlock(target, block, gfm) {
|
|
1388
|
+
switch (block.kind) {
|
|
1389
|
+
case "heading": {
|
|
1390
|
+
const heading = target.createElement(`h${block.level ?? 1}`);
|
|
1391
|
+
appendJoinedLines(target, heading, block.lines, gfm);
|
|
1392
|
+
target.appendChild(null, heading);
|
|
1393
|
+
break;
|
|
1127
1394
|
}
|
|
1128
|
-
|
|
1129
|
-
const
|
|
1130
|
-
|
|
1131
|
-
|
|
1395
|
+
case "paragraph": {
|
|
1396
|
+
const paragraph = target.createElement("p");
|
|
1397
|
+
appendParagraphLines(target, paragraph, block.lines, gfm);
|
|
1398
|
+
target.appendChild(null, paragraph);
|
|
1399
|
+
break;
|
|
1132
1400
|
}
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1401
|
+
case "fence": {
|
|
1402
|
+
const pre = target.createElement("pre");
|
|
1403
|
+
const code = target.createElement("code", block.language === void 0 ? {} : { class: `language-${block.language}` });
|
|
1404
|
+
const text = block.lines.join("\n");
|
|
1405
|
+
target.appendChild(code, target.createTextNode(text === "" ? "" : `${text}\n`));
|
|
1406
|
+
target.appendChild(pre, code);
|
|
1407
|
+
target.appendChild(null, pre);
|
|
1408
|
+
break;
|
|
1409
|
+
}
|
|
1410
|
+
case "quote": {
|
|
1411
|
+
const quote = target.createElement("blockquote");
|
|
1412
|
+
const body = target.createElement("p");
|
|
1413
|
+
appendJoinedLines(target, body, block.lines, gfm);
|
|
1414
|
+
target.appendChild(quote, body);
|
|
1415
|
+
target.appendChild(null, quote);
|
|
1416
|
+
break;
|
|
1417
|
+
}
|
|
1418
|
+
case "rule": target.appendChild(null, target.createElement("hr"));
|
|
1140
1419
|
}
|
|
1141
|
-
}
|
|
1142
|
-
// src/targets/animated-dom-target.ts
|
|
1420
|
+
}
|
|
1143
1421
|
/**
|
|
1144
|
-
*
|
|
1422
|
+
* Write paragraph lines into a parent, honoring two-space hard breaks.
|
|
1145
1423
|
*
|
|
1146
|
-
* @param
|
|
1147
|
-
* @param
|
|
1424
|
+
* @param target - Destination for rendered output.
|
|
1425
|
+
* @param parentId - Node the lines append to.
|
|
1426
|
+
* @param lines - Raw paragraph lines.
|
|
1427
|
+
* @param gfm - Enable GFM-only inlines.
|
|
1428
|
+
*
|
|
1429
|
+
* @internal
|
|
1148
1430
|
*/
|
|
1149
|
-
function
|
|
1150
|
-
|
|
1431
|
+
function appendParagraphLines(target, parentId, lines, gfm) {
|
|
1432
|
+
lines.forEach((line, at) => {
|
|
1433
|
+
const { text, hardBreak } = splitHardBreak(line);
|
|
1434
|
+
appendInline(target, parentId, text, gfm);
|
|
1435
|
+
if (at < lines.length - 1) {
|
|
1436
|
+
if (hardBreak) target.appendChild(parentId, target.createElement("br"));
|
|
1437
|
+
else target.appendChild(parentId, target.createTextNode(" "));
|
|
1438
|
+
}
|
|
1439
|
+
});
|
|
1440
|
+
}
|
|
1441
|
+
function appendJoinedLines(target, parentId, lines, gfm) {
|
|
1442
|
+
lines.forEach((line, at) => {
|
|
1443
|
+
if (at > 0) target.appendChild(parentId, target.createTextNode(" "));
|
|
1444
|
+
appendInline(target, parentId, line, gfm);
|
|
1445
|
+
});
|
|
1151
1446
|
}
|
|
1447
|
+
// src/renderers/gfm.ts
|
|
1152
1448
|
/**
|
|
1153
|
-
*
|
|
1449
|
+
* Renderer for GitHub Flavored Markdown.
|
|
1154
1450
|
*
|
|
1155
1451
|
* @remarks
|
|
1156
|
-
*
|
|
1157
|
-
* injected frame scheduler so streamed output lands once per frame instead
|
|
1158
|
-
* of once per write. Ids are minted synchronously and unknown ids throw
|
|
1159
|
-
* synchronously; updates apply in call order when the frame runs.
|
|
1452
|
+
* Supports CommonMark blocks and inlines plus GFM tables and strikethrough.
|
|
1160
1453
|
*
|
|
1161
|
-
*
|
|
1454
|
+
* Standalone: shares internal helpers with `CommonMarkRenderer`, but
|
|
1455
|
+
* extends nothing flavor-specific. Consult the full documentation for
|
|
1456
|
+
* more details.
|
|
1162
1457
|
*
|
|
1163
|
-
* @
|
|
1458
|
+
* @public
|
|
1164
1459
|
*/
|
|
1165
|
-
var
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
_nextId = 0;
|
|
1172
|
-
_queue = [];
|
|
1173
|
-
_scheduled = false;
|
|
1174
|
-
_flushResolvers = [];
|
|
1175
|
-
/**
|
|
1176
|
-
* @param targetNode - Container node receiving rendered output.
|
|
1177
|
-
* @param document - Document used to create nodes.
|
|
1178
|
-
* @param raf - Frame scheduler used to batch writes.
|
|
1179
|
-
*/
|
|
1180
|
-
constructor(targetNode, document, raf) {
|
|
1181
|
-
this._container = targetNode;
|
|
1182
|
-
this._doc = document;
|
|
1183
|
-
this._raf = raf;
|
|
1184
|
-
}
|
|
1460
|
+
var GFMRenderer = class {
|
|
1461
|
+
_target;
|
|
1462
|
+
_config;
|
|
1463
|
+
_state;
|
|
1464
|
+
_lists;
|
|
1465
|
+
_table = null;
|
|
1185
1466
|
/**
|
|
1186
|
-
*
|
|
1187
|
-
*
|
|
1188
|
-
* @param
|
|
1189
|
-
* @param attrs - Attributes to set.
|
|
1190
|
-
* @returns Id addressing the new node.
|
|
1467
|
+
* @param target - Destination for rendered output, from `createDOMTarget`
|
|
1468
|
+
* or a custom `HTMLTarget` implementation.
|
|
1469
|
+
* @param config - GFM rendering config.
|
|
1191
1470
|
*/
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
this.
|
|
1196
|
-
|
|
1197
|
-
assertAttributesSupported$1(node, snap);
|
|
1198
|
-
for (const [name, value] of Object.entries(snap)) node.setAttribute?.(name, value);
|
|
1199
|
-
this._nodes.set(id, node);
|
|
1200
|
-
});
|
|
1201
|
-
return id;
|
|
1471
|
+
constructor(target, config = {}) {
|
|
1472
|
+
this._target = target;
|
|
1473
|
+
this._config = config;
|
|
1474
|
+
this._state = new GFMState((fragment) => this._applyFragment(fragment));
|
|
1475
|
+
this._lists = new ListStack(target, true);
|
|
1202
1476
|
}
|
|
1203
1477
|
/**
|
|
1204
|
-
*
|
|
1205
|
-
*
|
|
1206
|
-
* @param text - Text content.
|
|
1207
|
-
* @returns Id addressing the new node.
|
|
1478
|
+
* Target receiving committed output.
|
|
1208
1479
|
*/
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
this._enqueue(() => {
|
|
1212
|
-
this._nodes.set(id, this._doc.createTextNode(text));
|
|
1213
|
-
});
|
|
1214
|
-
return id;
|
|
1480
|
+
get target() {
|
|
1481
|
+
return this._target;
|
|
1215
1482
|
}
|
|
1216
1483
|
/**
|
|
1217
|
-
*
|
|
1218
|
-
*
|
|
1219
|
-
* @param parent - Receiving node, or `null` for the top level.
|
|
1220
|
-
* @param child - Node to append.
|
|
1484
|
+
* Config applying to subsequent commits.
|
|
1221
1485
|
*/
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
this._mustGraph(child);
|
|
1225
|
-
this._assertAcyclic(child, parent);
|
|
1226
|
-
this._reparentGraph(child, parent);
|
|
1227
|
-
this._enqueue(() => {
|
|
1228
|
-
(parent === null ? this._container : this._mustNode(parent)).appendChild(this._mustNode(child));
|
|
1229
|
-
});
|
|
1486
|
+
get config() {
|
|
1487
|
+
return this._config;
|
|
1230
1488
|
}
|
|
1231
1489
|
/**
|
|
1232
|
-
*
|
|
1490
|
+
* Feed one source line; completed output is written to the target.
|
|
1233
1491
|
*
|
|
1234
|
-
* @param
|
|
1235
|
-
* @param newId - Replacement node.
|
|
1492
|
+
* @param line - Next source line, without its line break.
|
|
1236
1493
|
*/
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
this._mustGraph(newId);
|
|
1240
|
-
if (old.parent === void 0) throw new DetachedNodeError(oldId);
|
|
1241
|
-
if (oldId === newId) return;
|
|
1242
|
-
this._assertAcyclic(newId, old.parent);
|
|
1243
|
-
const parent = old.parent;
|
|
1244
|
-
this._reparentGraph(newId, parent);
|
|
1245
|
-
const doomed = this._retireGraph(oldId);
|
|
1246
|
-
this._enqueue(() => {
|
|
1247
|
-
(parent === null ? this._container : this._mustNode(parent)).replaceChild(this._mustNode(newId), this._mustNode(oldId));
|
|
1248
|
-
for (const id of doomed) this._nodes.delete(id);
|
|
1249
|
-
});
|
|
1494
|
+
writeLine(line) {
|
|
1495
|
+
this._state.addLine(line);
|
|
1250
1496
|
}
|
|
1251
1497
|
/**
|
|
1252
|
-
*
|
|
1498
|
+
* Write the buffered remainder and settle the target.
|
|
1253
1499
|
*
|
|
1254
|
-
* @
|
|
1500
|
+
* @returns A promise that resolves once all output has landed in the target.
|
|
1255
1501
|
*/
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
this._enqueue(() => {
|
|
1260
|
-
if (parent !== void 0) (parent === null ? this._container : this._mustNode(parent)).removeChild(this._mustNode(id));
|
|
1261
|
-
for (const stale of doomed) this._nodes.delete(stale);
|
|
1262
|
-
});
|
|
1502
|
+
async flush() {
|
|
1503
|
+
this._state.flush();
|
|
1504
|
+
await this._target.flush();
|
|
1263
1505
|
}
|
|
1264
1506
|
/**
|
|
1265
|
-
*
|
|
1507
|
+
* Point the renderer at another target. Open lists and tables drain
|
|
1508
|
+
* into the previous target; only newly opened blocks go to the new one.
|
|
1266
1509
|
*
|
|
1267
|
-
* @param
|
|
1268
|
-
* @param text - Replacement text.
|
|
1510
|
+
* @param target - Target receiving future output.
|
|
1269
1511
|
*/
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
for (const child of [...entry.children]) doomed.push(...this._retireGraph(child));
|
|
1274
|
-
this._enqueue(() => {
|
|
1275
|
-
this._mustNode(node).textContent = text;
|
|
1276
|
-
for (const stale of doomed) this._nodes.delete(stale);
|
|
1277
|
-
});
|
|
1512
|
+
setTarget(target) {
|
|
1513
|
+
this._target = target;
|
|
1514
|
+
this._lists.setTarget(target);
|
|
1278
1515
|
}
|
|
1279
1516
|
/**
|
|
1280
|
-
*
|
|
1517
|
+
* Replace the renderer config; applies to subsequent writes.
|
|
1281
1518
|
*
|
|
1282
|
-
* @
|
|
1519
|
+
* @param config - Next config.
|
|
1283
1520
|
*/
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
return new Promise((resolve) => {
|
|
1287
|
-
this._flushResolvers.push(resolve);
|
|
1288
|
-
});
|
|
1289
|
-
}
|
|
1290
|
-
_enqueue(apply) {
|
|
1291
|
-
this._queue.push(apply);
|
|
1292
|
-
if (!this._scheduled) {
|
|
1293
|
-
this._scheduled = true;
|
|
1294
|
-
this._raf(() => {
|
|
1295
|
-
this._scheduled = false;
|
|
1296
|
-
const batch = this._queue;
|
|
1297
|
-
this._queue = [];
|
|
1298
|
-
for (const queued of batch) queued();
|
|
1299
|
-
const resolvers = this._flushResolvers;
|
|
1300
|
-
this._flushResolvers = [];
|
|
1301
|
-
for (const resolve of resolvers) resolve();
|
|
1302
|
-
});
|
|
1303
|
-
}
|
|
1304
|
-
}
|
|
1305
|
-
_mint() {
|
|
1306
|
-
this._nextId += 1;
|
|
1307
|
-
const id = `n${this._nextId}`;
|
|
1308
|
-
this._graph.set(id, {
|
|
1309
|
-
parent: void 0,
|
|
1310
|
-
children: /* @__PURE__ */ new Set()
|
|
1311
|
-
});
|
|
1312
|
-
return id;
|
|
1313
|
-
}
|
|
1314
|
-
_mustGraph(id) {
|
|
1315
|
-
const entry = this._graph.get(id);
|
|
1316
|
-
if (entry === void 0) throw new UnknownNodeError(id);
|
|
1317
|
-
return entry;
|
|
1521
|
+
setConfig(config) {
|
|
1522
|
+
this._config = config;
|
|
1318
1523
|
}
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1524
|
+
_applyFragment(fragment) {
|
|
1525
|
+
switch (fragment.kind) {
|
|
1526
|
+
case "block":
|
|
1527
|
+
renderLeafBlock(this._target, fragment.block, true);
|
|
1528
|
+
break;
|
|
1529
|
+
case "listStart":
|
|
1530
|
+
this._lists.openList(fragment.ordered);
|
|
1531
|
+
break;
|
|
1532
|
+
case "listItem":
|
|
1533
|
+
this._lists.addItem(fragment.text);
|
|
1534
|
+
break;
|
|
1535
|
+
case "listEnd":
|
|
1536
|
+
this._lists.closeList();
|
|
1537
|
+
break;
|
|
1538
|
+
case "tableHead":
|
|
1539
|
+
this._applyTableHead(fragment.headLine, fragment.delimLine);
|
|
1540
|
+
break;
|
|
1541
|
+
case "tableRow":
|
|
1542
|
+
this._applyTableRow(fragment.line);
|
|
1543
|
+
break;
|
|
1544
|
+
case "tableEnd": this._applyTableEnd();
|
|
1325
1545
|
}
|
|
1326
1546
|
}
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1547
|
+
/**
|
|
1548
|
+
* Commit the table with its header; body rows stream in afterwards.
|
|
1549
|
+
*
|
|
1550
|
+
* @param headLine - Raw header row line.
|
|
1551
|
+
* @param delimLine - Delimiter row line carrying column alignment.
|
|
1552
|
+
*/
|
|
1553
|
+
_applyTableHead(headLine, delimLine) {
|
|
1554
|
+
const scope = this._target;
|
|
1555
|
+
const header = splitTableRow(headLine);
|
|
1556
|
+
const aligns = splitTableRow(delimLine).map(parseTableAlignment);
|
|
1557
|
+
const width = header.length;
|
|
1558
|
+
const table = scope.createElement("table");
|
|
1559
|
+
const head = scope.createElement("thead");
|
|
1560
|
+
const headRow = scope.createElement("tr");
|
|
1561
|
+
for (let col = 0; col < width; col++) {
|
|
1562
|
+
const align = aligns[col] ?? null;
|
|
1563
|
+
const cell = scope.createElement("th", align === null ? {} : { align });
|
|
1564
|
+
appendInline(scope, cell, header[col] ?? "", true);
|
|
1565
|
+
scope.appendChild(headRow, cell);
|
|
1566
|
+
}
|
|
1567
|
+
scope.appendChild(head, headRow);
|
|
1568
|
+
scope.appendChild(table, head);
|
|
1569
|
+
const body = scope.createElement("tbody");
|
|
1570
|
+
scope.appendChild(table, body);
|
|
1571
|
+
scope.appendChild(null, table);
|
|
1572
|
+
this._table = {
|
|
1573
|
+
scope,
|
|
1574
|
+
body,
|
|
1575
|
+
aligns,
|
|
1576
|
+
width
|
|
1577
|
+
};
|
|
1331
1578
|
}
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1579
|
+
/**
|
|
1580
|
+
* Append one body row to the open table.
|
|
1581
|
+
*
|
|
1582
|
+
* @param line - Raw body row line.
|
|
1583
|
+
*/
|
|
1584
|
+
_applyTableRow(line) {
|
|
1585
|
+
const table = this._table;
|
|
1586
|
+
if (table === null) return;
|
|
1587
|
+
const scope = table.scope;
|
|
1588
|
+
const cells = splitTableRow(line);
|
|
1589
|
+
const row = scope.createElement("tr");
|
|
1590
|
+
for (let col = 0; col < table.width; col++) {
|
|
1591
|
+
const align = table.aligns[col] ?? null;
|
|
1592
|
+
const cell = scope.createElement("td", align === null ? {} : { align });
|
|
1593
|
+
appendInline(scope, cell, cells[col] ?? "", true);
|
|
1594
|
+
scope.appendChild(row, cell);
|
|
1595
|
+
}
|
|
1596
|
+
for (let col = table.width; col < cells.length; col++) {
|
|
1597
|
+
const cell = scope.createElement("td");
|
|
1598
|
+
appendInline(scope, cell, cells[col], true);
|
|
1599
|
+
scope.appendChild(row, cell);
|
|
1600
|
+
}
|
|
1601
|
+
scope.appendChild(table.body, row);
|
|
1337
1602
|
}
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
const next = this._graph.get(at);
|
|
1344
|
-
if (next === void 0) return;
|
|
1345
|
-
doomed.push(at);
|
|
1346
|
-
for (const child of [...next.children]) visit(child);
|
|
1347
|
-
this._graph.delete(at);
|
|
1348
|
-
};
|
|
1349
|
-
visit(id);
|
|
1350
|
-
return doomed;
|
|
1603
|
+
/**
|
|
1604
|
+
* Close the open table.
|
|
1605
|
+
*/
|
|
1606
|
+
_applyTableEnd() {
|
|
1607
|
+
this._table = null;
|
|
1351
1608
|
}
|
|
1352
1609
|
};
|
|
1353
|
-
// src/
|
|
1354
|
-
/**
|
|
1355
|
-
* Throw when attributes were requested but the DOM cannot set them.
|
|
1356
|
-
*
|
|
1357
|
-
* @param node - Freshly created node.
|
|
1358
|
-
* @param attrs - Requested attributes.
|
|
1359
|
-
*/
|
|
1360
|
-
function assertAttributesSupported(node, attrs) {
|
|
1361
|
-
if (Object.keys(attrs).length > 0 && typeof node.setAttribute !== "function") throw new Error("Cannot set attributes: this DOM does not implement setAttribute");
|
|
1362
|
-
}
|
|
1610
|
+
// src/renderers/commonmark.ts
|
|
1363
1611
|
/**
|
|
1364
|
-
*
|
|
1612
|
+
* Renderer for CommonMark.
|
|
1365
1613
|
*
|
|
1366
1614
|
* @remarks
|
|
1367
|
-
*
|
|
1368
|
-
*
|
|
1369
|
-
* to
|
|
1615
|
+
* Constructed with its target and config. Feed it lines with
|
|
1616
|
+
* {@link CommonMarkRenderer.writeLine}; it holds its own parse state and
|
|
1617
|
+
* writes finished blocks to the target as they complete. An internal list
|
|
1618
|
+
* stack tracks open lists so items — including nested ones — append to
|
|
1619
|
+
* the right container as they arrive.
|
|
1370
1620
|
*
|
|
1371
|
-
*
|
|
1621
|
+
* The renderer never touches the DOM: it builds everything through the
|
|
1622
|
+
* write-only target, so the same renderer drives live pages, simulated
|
|
1623
|
+
* DOMs, and fully custom targets. It is standalone: GFM shares internal
|
|
1624
|
+
* helpers with it, but neither renderer extends the other.
|
|
1625
|
+
*
|
|
1626
|
+
* @public
|
|
1372
1627
|
*/
|
|
1373
|
-
var
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1628
|
+
var CommonMarkRenderer = class {
|
|
1629
|
+
_target;
|
|
1630
|
+
_config;
|
|
1631
|
+
_state;
|
|
1632
|
+
_lists;
|
|
1378
1633
|
/**
|
|
1379
|
-
* @param
|
|
1380
|
-
*
|
|
1634
|
+
* @param target - Destination for rendered output, from `createDOMTarget`
|
|
1635
|
+
* or a custom `HTMLTarget` implementation.
|
|
1636
|
+
* @param config - CommonMark rendering config.
|
|
1381
1637
|
*/
|
|
1382
|
-
constructor(
|
|
1383
|
-
this.
|
|
1384
|
-
this.
|
|
1638
|
+
constructor(target, config = {}) {
|
|
1639
|
+
this._target = target;
|
|
1640
|
+
this._config = config;
|
|
1641
|
+
this._state = new CommonMarkState((fragment) => this._applyFragment(fragment));
|
|
1642
|
+
this._lists = new ListStack(target, false);
|
|
1385
1643
|
}
|
|
1386
1644
|
/**
|
|
1387
|
-
*
|
|
1388
|
-
*
|
|
1389
|
-
* @param tag - Lowercase tag name.
|
|
1390
|
-
* @param attrs - Attributes to set.
|
|
1391
|
-
* @returns Id addressing the new node.
|
|
1645
|
+
* Target receiving committed output.
|
|
1392
1646
|
*/
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
assertAttributesSupported(node, attrs);
|
|
1396
|
-
for (const [name, value] of Object.entries(attrs)) node.setAttribute?.(name, value);
|
|
1397
|
-
return this._store(node, void 0);
|
|
1647
|
+
get target() {
|
|
1648
|
+
return this._target;
|
|
1398
1649
|
}
|
|
1399
1650
|
/**
|
|
1400
|
-
*
|
|
1401
|
-
*
|
|
1402
|
-
* @param text - Text content.
|
|
1403
|
-
* @returns Id addressing the new node.
|
|
1651
|
+
* Config applying to subsequent commits.
|
|
1404
1652
|
*/
|
|
1405
|
-
|
|
1406
|
-
return this.
|
|
1653
|
+
get config() {
|
|
1654
|
+
return this._config;
|
|
1407
1655
|
}
|
|
1408
1656
|
/**
|
|
1409
|
-
*
|
|
1657
|
+
* Feed one source line; completed output is written to the target.
|
|
1410
1658
|
*
|
|
1411
|
-
* @param
|
|
1412
|
-
* @param child - Node to append.
|
|
1659
|
+
* @param line - Next source line, without its line break.
|
|
1413
1660
|
*/
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
const attachTo = parent === null ? this._container : this._must(parent).node;
|
|
1417
|
-
this._assertAcyclic(child, parent);
|
|
1418
|
-
attachTo.appendChild(entry.node);
|
|
1419
|
-
this._reparent(child, parent);
|
|
1661
|
+
writeLine(line) {
|
|
1662
|
+
this._state.addLine(line);
|
|
1420
1663
|
}
|
|
1421
1664
|
/**
|
|
1422
|
-
*
|
|
1665
|
+
* Write the buffered remainder and settle the target.
|
|
1423
1666
|
*
|
|
1424
|
-
* @
|
|
1425
|
-
* @param newId - Replacement node.
|
|
1667
|
+
* @returns A promise that resolves once all output has landed in the target.
|
|
1426
1668
|
*/
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
if (old.parent === void 0) throw new DetachedNodeError(oldId);
|
|
1431
|
-
if (oldId === newId) return;
|
|
1432
|
-
this._assertAcyclic(newId, old.parent);
|
|
1433
|
-
(old.parent === null ? this._container : this._must(old.parent).node).replaceChild(fresh.node, old.node);
|
|
1434
|
-
this._reparent(newId, old.parent);
|
|
1435
|
-
this._retire(oldId);
|
|
1669
|
+
async flush() {
|
|
1670
|
+
this._state.flush();
|
|
1671
|
+
await this._target.flush();
|
|
1436
1672
|
}
|
|
1437
1673
|
/**
|
|
1438
|
-
*
|
|
1674
|
+
* Point the renderer at another target. Open lists drain into the
|
|
1675
|
+
* previous target; only newly opened blocks go to the new one.
|
|
1439
1676
|
*
|
|
1440
|
-
* @param
|
|
1677
|
+
* @param target - Target receiving future output.
|
|
1441
1678
|
*/
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
this._retire(id);
|
|
1679
|
+
setTarget(target) {
|
|
1680
|
+
this._target = target;
|
|
1681
|
+
this._lists.setTarget(target);
|
|
1446
1682
|
}
|
|
1447
1683
|
/**
|
|
1448
|
-
* Replace
|
|
1684
|
+
* Replace the renderer config; applies to subsequent writes.
|
|
1449
1685
|
*
|
|
1450
|
-
* @param
|
|
1451
|
-
* @param text - Replacement text.
|
|
1452
|
-
*/
|
|
1453
|
-
setTextContent(node, text) {
|
|
1454
|
-
const entry = this._must(node);
|
|
1455
|
-
entry.node.textContent = text;
|
|
1456
|
-
for (const child of [...entry.children]) this._retire(child);
|
|
1457
|
-
entry.children.clear();
|
|
1458
|
-
}
|
|
1459
|
-
/**
|
|
1460
|
-
* Nothing to wait for: synchronous targets hold every update already.
|
|
1686
|
+
* @param config - Next config.
|
|
1461
1687
|
*/
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
const entry = this._entries.get(id);
|
|
1465
|
-
if (entry === void 0) throw new UnknownNodeError(id);
|
|
1466
|
-
return entry;
|
|
1688
|
+
setConfig(config) {
|
|
1689
|
+
this._config = config;
|
|
1467
1690
|
}
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1691
|
+
_applyFragment(fragment) {
|
|
1692
|
+
switch (fragment.kind) {
|
|
1693
|
+
case "block":
|
|
1694
|
+
renderLeafBlock(this._target, fragment.block, false);
|
|
1695
|
+
break;
|
|
1696
|
+
case "listStart":
|
|
1697
|
+
this._lists.openList(fragment.ordered);
|
|
1698
|
+
break;
|
|
1699
|
+
case "listItem":
|
|
1700
|
+
this._lists.addItem(fragment.text);
|
|
1701
|
+
break;
|
|
1702
|
+
case "listEnd": this._lists.closeList();
|
|
1474
1703
|
}
|
|
1475
1704
|
}
|
|
1476
|
-
_store(node, parent) {
|
|
1477
|
-
this._nextId += 1;
|
|
1478
|
-
const id = `n${this._nextId}`;
|
|
1479
|
-
this._entries.set(id, {
|
|
1480
|
-
node,
|
|
1481
|
-
parent,
|
|
1482
|
-
children: /* @__PURE__ */ new Set()
|
|
1483
|
-
});
|
|
1484
|
-
return id;
|
|
1485
|
-
}
|
|
1486
|
-
_reparent(child, parent) {
|
|
1487
|
-
const entry = this._entries.get(child);
|
|
1488
|
-
if (entry.parent !== void 0 && entry.parent !== null) this._entries.get(entry.parent)?.children.delete(child);
|
|
1489
|
-
entry.parent = parent;
|
|
1490
|
-
if (parent !== null) this._entries.get(parent)?.children.add(child);
|
|
1491
|
-
}
|
|
1492
|
-
_retire(id) {
|
|
1493
|
-
const entry = this._entries.get(id);
|
|
1494
|
-
if (entry === void 0) return;
|
|
1495
|
-
for (const child of [...entry.children]) this._retire(child);
|
|
1496
|
-
if (entry.parent !== void 0 && entry.parent !== null) this._entries.get(entry.parent)?.children.delete(id);
|
|
1497
|
-
this._entries.delete(id);
|
|
1498
|
-
}
|
|
1499
1705
|
};
|
|
1500
|
-
// src/targets/create-dom-target.ts
|
|
1501
|
-
/**
|
|
1502
|
-
* Create a target rendering into a DOM container.
|
|
1503
|
-
*
|
|
1504
|
-
* @param document - Document used to create nodes: a real browser document
|
|
1505
|
-
* for live pages, or a simulated one such as linkedom for server-side
|
|
1506
|
-
* rendering.
|
|
1507
|
-
* @param targetNode - Container node receiving rendered output.
|
|
1508
|
-
* @param options - Frame scheduler.
|
|
1509
|
-
* @returns The target to hand to a renderer.
|
|
1510
|
-
*
|
|
1511
|
-
* @public
|
|
1512
|
-
*/
|
|
1513
|
-
function createDOMTarget(document, targetNode, options = {}) {
|
|
1514
|
-
const { raf } = options;
|
|
1515
|
-
return raf === void 0 ? new DOMTarget(targetNode, document) : new AnimatedDOMTarget(targetNode, document, raf);
|
|
1516
|
-
}
|
|
1517
1706
|
// src/streams/gfm-stream.ts
|
|
1518
1707
|
/**
|
|
1519
1708
|
* Create a GFM stream rendering into the given container.
|
|
1520
1709
|
*
|
|
1521
1710
|
* @remarks
|
|
1522
|
-
*
|
|
1523
|
-
*
|
|
1524
|
-
*
|
|
1711
|
+
* One-call wiring of a DOM target, a `GFMRenderer`, and a `MarkdownStream`.
|
|
1712
|
+
* For a custom renderer or target, wire them yourself with
|
|
1713
|
+
* `new MarkdownStream(renderer)`.
|
|
1525
1714
|
*
|
|
1526
|
-
* @param
|
|
1527
|
-
* @
|
|
1528
|
-
* @param config - GFM rendering config.
|
|
1529
|
-
* @param targetOptions - Frame scheduler for the DOM target.
|
|
1530
|
-
* @returns The configured pipeline.
|
|
1715
|
+
* @param options - Container, document, config, and optional frame scheduler.
|
|
1716
|
+
* @returns The configured stream.
|
|
1531
1717
|
*
|
|
1532
1718
|
* @example
|
|
1533
1719
|
* ```ts
|
|
1534
|
-
* const stream = createGFMStream(
|
|
1535
|
-
* document.getElementById("out")!,
|
|
1720
|
+
* const stream = createGFMStream({
|
|
1721
|
+
* container: document.getElementById("out")!,
|
|
1536
1722
|
* document,
|
|
1537
|
-
*
|
|
1538
|
-
*
|
|
1539
|
-
* );
|
|
1723
|
+
* raf: requestAnimationFrame,
|
|
1724
|
+
* });
|
|
1540
1725
|
* const writer = stream.getWriter();
|
|
1541
1726
|
* await writer.write("# Hello\n");
|
|
1542
1727
|
* await writer.close();
|
|
@@ -1544,33 +1729,33 @@ function createDOMTarget(document, targetNode, options = {}) {
|
|
|
1544
1729
|
*
|
|
1545
1730
|
* @public
|
|
1546
1731
|
*/
|
|
1547
|
-
function createGFMStream(
|
|
1548
|
-
const
|
|
1549
|
-
return
|
|
1732
|
+
function createGFMStream(options) {
|
|
1733
|
+
const { container, document, config = {}, raf } = options;
|
|
1734
|
+
return new MarkdownStream(new GFMRenderer(createDOMTarget({
|
|
1735
|
+
document,
|
|
1736
|
+
container,
|
|
1737
|
+
raf
|
|
1738
|
+
}), config));
|
|
1550
1739
|
}
|
|
1551
1740
|
// src/streams/commonmark-stream.ts
|
|
1552
1741
|
/**
|
|
1553
1742
|
* Create a CommonMark stream rendering into the given container.
|
|
1554
1743
|
*
|
|
1555
1744
|
* @remarks
|
|
1556
|
-
*
|
|
1557
|
-
*
|
|
1558
|
-
*
|
|
1745
|
+
* One-call wiring of a DOM target, a `CommonMarkRenderer`, and a
|
|
1746
|
+
* `MarkdownStream`. For a custom renderer or target, wire them yourself
|
|
1747
|
+
* with `new MarkdownStream(renderer)`.
|
|
1559
1748
|
*
|
|
1560
|
-
* @param
|
|
1561
|
-
* @
|
|
1562
|
-
* @param config - CommonMark rendering config.
|
|
1563
|
-
* @param targetOptions - Frame scheduler for the DOM target.
|
|
1564
|
-
* @returns The configured pipeline.
|
|
1749
|
+
* @param options - Container, document, config, and optional frame scheduler.
|
|
1750
|
+
* @returns The configured stream.
|
|
1565
1751
|
*
|
|
1566
1752
|
* @example
|
|
1567
1753
|
* ```ts
|
|
1568
|
-
* const stream = createCommonMarkStream(
|
|
1569
|
-
* document.getElementById("out")!,
|
|
1754
|
+
* const stream = createCommonMarkStream({
|
|
1755
|
+
* container: document.getElementById("out")!,
|
|
1570
1756
|
* document,
|
|
1571
|
-
*
|
|
1572
|
-
*
|
|
1573
|
-
* );
|
|
1757
|
+
* raf: requestAnimationFrame,
|
|
1758
|
+
* });
|
|
1574
1759
|
* const writer = stream.getWriter();
|
|
1575
1760
|
* await writer.write("# Hello\n");
|
|
1576
1761
|
* await writer.close();
|
|
@@ -1578,10 +1763,59 @@ function createGFMStream(target, dom, config, targetOptions) {
|
|
|
1578
1763
|
*
|
|
1579
1764
|
* @public
|
|
1580
1765
|
*/
|
|
1581
|
-
function createCommonMarkStream(
|
|
1582
|
-
const
|
|
1583
|
-
return
|
|
1766
|
+
function createCommonMarkStream(options) {
|
|
1767
|
+
const { container, document, config = {}, raf } = options;
|
|
1768
|
+
return new MarkdownStream(new CommonMarkRenderer(createDOMTarget({
|
|
1769
|
+
document,
|
|
1770
|
+
container,
|
|
1771
|
+
raf
|
|
1772
|
+
}), config));
|
|
1773
|
+
}
|
|
1774
|
+
// src/utils.ts
|
|
1775
|
+
/**
|
|
1776
|
+
* Detect the JavaScript runtime hosting this module.
|
|
1777
|
+
*
|
|
1778
|
+
* @remarks
|
|
1779
|
+
* Checks `Bun` first, then `Deno`, then Node's `process.versions`, then
|
|
1780
|
+
* browser globals. Bun and Deno both expose Node-compatible globals, so
|
|
1781
|
+
* order matters: the native brand always wins over the compatibility
|
|
1782
|
+
* layer. Used by the smart utilities to prefer runtime built-ins (for
|
|
1783
|
+
* example `Bun.escapeHTML`) over bundled fallbacks.
|
|
1784
|
+
*
|
|
1785
|
+
* @returns The detected runtime, or `"unknown"` when none matches.
|
|
1786
|
+
*
|
|
1787
|
+
* @public
|
|
1788
|
+
*/
|
|
1789
|
+
function getRuntime() {
|
|
1790
|
+
const host = globalThis;
|
|
1791
|
+
if (host.Bun !== void 0) return "bun";
|
|
1792
|
+
if (host.Deno !== void 0) return "deno";
|
|
1793
|
+
if (typeof host.process?.versions?.node === "string") return "node";
|
|
1794
|
+
if (host.window !== void 0 || host.document !== void 0) return "browser";
|
|
1795
|
+
return "unknown";
|
|
1796
|
+
}
|
|
1797
|
+
/**
|
|
1798
|
+
* Escape text for insertion into HTML.
|
|
1799
|
+
*
|
|
1800
|
+
* @remarks
|
|
1801
|
+
* Handy for custom `HTMLTarget` implementations that serialize collected
|
|
1802
|
+
* nodes to strings. Consult the full documentation for more details.
|
|
1803
|
+
*
|
|
1804
|
+
* @param text - Raw text to escape.
|
|
1805
|
+
* @returns The escaped text.
|
|
1806
|
+
*
|
|
1807
|
+
* @example
|
|
1808
|
+
* ```ts
|
|
1809
|
+
* escapeHTML(`<a href="x&y">`) // "<a href="x&y">"
|
|
1810
|
+
* ```
|
|
1811
|
+
*
|
|
1812
|
+
* @public
|
|
1813
|
+
*/
|
|
1814
|
+
function escapeHTML(text) {
|
|
1815
|
+
const bun = globalThis.Bun;
|
|
1816
|
+
if (typeof bun?.escapeHTML === "function") return bun.escapeHTML(text);
|
|
1817
|
+
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
1584
1818
|
}
|
|
1585
|
-
export { CommonMarkRenderer, DetachedNodeError, GFMRenderer, HierarchyError, UnknownNodeError, createCommonMarkStream, createDOMTarget, createGFMStream,
|
|
1819
|
+
export { ClosedStreamError, CommonMarkRenderer, DetachedNodeError, GFMRenderer, HierarchyError, MarkdownStream, UnknownNodeError, UnsupportedAttributesError, createCommonMarkStream, createDOMTarget, createGFMStream, escapeHTML, getRuntime };
|
|
1586
1820
|
|
|
1587
1821
|
//# sourceMappingURL=index.js.map
|