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