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.cjs
CHANGED
|
@@ -1,64 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
// src/
|
|
3
|
-
/**
|
|
4
|
-
* Create a streaming pipeline around any renderer and target.
|
|
5
|
-
*
|
|
6
|
-
* @remarks
|
|
7
|
-
* The pipeline chunks whatever strings are written into lines and feeds
|
|
8
|
-
* them to the renderer one at a time; the renderer owns its parse state
|
|
9
|
-
* and commits finished output to the target. Closing the stream renders
|
|
10
|
-
* the trailing remainder. Pass a built-in renderer (`GFMRenderer`,
|
|
11
|
-
* `CommonMarkRenderer`) or your own `MarkdownRenderer` implementation,
|
|
12
|
-
* and a DOM target from `createDOMTarget` or your own `HTMLTarget`.
|
|
13
|
-
*
|
|
14
|
-
* @param renderer - Line-consuming renderer holding its own parse state.
|
|
15
|
-
* @param target - Destination the renderer commits to.
|
|
16
|
-
* @returns A working pipeline.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```ts
|
|
20
|
-
* const target = createDOMTarget(document, document.getElementById("out")!);
|
|
21
|
-
* const stream = createMarkdownStream(new GFMRenderer(target, {}), target);
|
|
22
|
-
* const writer = stream.getWriter();
|
|
23
|
-
* await writer.write("# Hello\n");
|
|
24
|
-
* await writer.close();
|
|
25
|
-
* writer.releaseLock();
|
|
26
|
-
* ```
|
|
27
|
-
*
|
|
28
|
-
* @public
|
|
29
|
-
*/
|
|
30
|
-
function createMarkdownStream(renderer, target) {
|
|
31
|
-
renderer.setTarget(target);
|
|
32
|
-
let buffer = "";
|
|
33
|
-
const pipeline = new WritableStream({
|
|
34
|
-
write(chunk) {
|
|
35
|
-
if (typeof chunk !== "string") throw new TypeError(`MarkdownPipeline chunks must be strings, received ${typeof chunk}`);
|
|
36
|
-
buffer += chunk;
|
|
37
|
-
let at = buffer.indexOf("\n");
|
|
38
|
-
while (at !== -1) {
|
|
39
|
-
renderer.writeLine(buffer.slice(0, at).replace(/\r$/, ""));
|
|
40
|
-
buffer = buffer.slice(at + 1);
|
|
41
|
-
at = buffer.indexOf("\n");
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
close() {
|
|
45
|
-
if (buffer !== "") {
|
|
46
|
-
const rest = buffer.replace(/\r$/, "");
|
|
47
|
-
buffer = "";
|
|
48
|
-
renderer.writeLine(rest);
|
|
49
|
-
}
|
|
50
|
-
return renderer.flush();
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
pipeline.setTarget = (next) => {
|
|
54
|
-
renderer.setTarget(next);
|
|
55
|
-
};
|
|
56
|
-
pipeline.setConfig = (next) => {
|
|
57
|
-
renderer.setConfig(next);
|
|
58
|
-
};
|
|
59
|
-
return pipeline;
|
|
60
|
-
}
|
|
61
|
-
// src/core/contracts/html-target.ts
|
|
2
|
+
// src/api/target.ts
|
|
62
3
|
/**
|
|
63
4
|
* A target call referenced an id the target never minted or already
|
|
64
5
|
* retired. Carries the offending id.
|
|
@@ -117,211 +58,721 @@ var HierarchyError = class extends Error {
|
|
|
117
58
|
this.parentId = parentId;
|
|
118
59
|
}
|
|
119
60
|
};
|
|
120
|
-
// src/renderers/parser/blocks.ts
|
|
121
|
-
const HEADING_RE = /^(#{1,6})(?:\s+(.*))?\s*$/;
|
|
122
|
-
const RULE_RE = /^(?:\*\s*){3,}$|^(?:-\s*){3,}$|^(?:_\s*){3,}$/;
|
|
123
|
-
const FENCE_RE = /^(`{3,}|~{3,})\s*([^`]*)$/;
|
|
124
|
-
const QUOTE_RE = /^>\s?(.*)$/;
|
|
125
|
-
const BULLET_RE = /^\s*[-*+]\s+(.*)$/;
|
|
126
|
-
const ORDERED_RE = /^\s*\d+[.)]\s+(.*)$/;
|
|
127
|
-
const TABLE_DELIM_CELL_RE = /^:?-{1,}:?$/;
|
|
128
|
-
const LEADING_WS_RE = /^\s*/;
|
|
129
|
-
/**
|
|
130
|
-
* Whether a line is blank (a pure block separator carrying no content).
|
|
131
|
-
*
|
|
132
|
-
* @param line - Source line to test.
|
|
133
|
-
* @returns True for empty or whitespace-only lines.
|
|
134
|
-
*/
|
|
135
|
-
function isBlankLine(line) {
|
|
136
|
-
return line.trim() === "";
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Match a fence marker line.
|
|
140
|
-
*
|
|
141
|
-
* @param line - Source line to test.
|
|
142
|
-
* @returns The marker, or null when the line is not a fence marker.
|
|
143
|
-
*/
|
|
144
|
-
function matchFence(line) {
|
|
145
|
-
const match = FENCE_RE.exec(line.trim());
|
|
146
|
-
if (!match || !match[1]) return null;
|
|
147
|
-
const run = match[1];
|
|
148
|
-
return {
|
|
149
|
-
marker: run[0],
|
|
150
|
-
length: run.length,
|
|
151
|
-
language: (match[2] ?? "").trim() || void 0
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
61
|
/**
|
|
155
|
-
*
|
|
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.
|
|
156
65
|
*
|
|
157
|
-
* @
|
|
158
|
-
* @param line - Source line to test.
|
|
159
|
-
* @returns True for a same-character run at least as long.
|
|
66
|
+
* @public
|
|
160
67
|
*/
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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
|
|
165
85
|
/**
|
|
166
|
-
*
|
|
86
|
+
* `setTarget` or `setConfig` was called on a closed or aborted stream.
|
|
87
|
+
* Carries the rejected action.
|
|
167
88
|
*
|
|
168
|
-
* @
|
|
169
|
-
* @returns The heading, or null for non-heading lines.
|
|
89
|
+
* @public
|
|
170
90
|
*/
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
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
|
+
};
|
|
179
103
|
/**
|
|
180
|
-
*
|
|
104
|
+
* Incremental markdown stream: a `WritableStream` of markdown text.
|
|
181
105
|
*
|
|
182
|
-
* @
|
|
183
|
-
*
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Match a quote line.
|
|
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.
|
|
190
111
|
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
|
|
194
|
-
function matchQuote(line) {
|
|
195
|
-
const match = QUOTE_RE.exec(line);
|
|
196
|
-
if (!match) return null;
|
|
197
|
-
return { text: match[1] ?? "" };
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Match a bullet or ordered list item line.
|
|
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`.
|
|
201
115
|
*
|
|
202
|
-
* @
|
|
203
|
-
*
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
indent: LEADING_WS_RE.exec(line)?.[0].length ?? 0,
|
|
212
|
-
text: bullet?.[1] ?? ordered?.[1] ?? ""
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Whether a line is a table delimiter row.
|
|
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
|
+
* ```
|
|
217
125
|
*
|
|
218
|
-
* @
|
|
219
|
-
* @returns True for `|---|...` style rows.
|
|
126
|
+
* @public
|
|
220
127
|
*/
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
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
|
|
226
203
|
/**
|
|
227
|
-
*
|
|
204
|
+
* Throw when attributes were requested but the DOM cannot set them.
|
|
228
205
|
*
|
|
229
|
-
* @param
|
|
230
|
-
* @
|
|
206
|
+
* @param tag - Tag the attributes were requested on.
|
|
207
|
+
* @param node - Freshly created node.
|
|
208
|
+
* @param attrs - Requested attributes.
|
|
231
209
|
*/
|
|
232
|
-
function
|
|
233
|
-
|
|
234
|
-
for (const line of lines) if (open === null) open = matchFence(line);
|
|
235
|
-
else if (fenceCloses(open, line)) open = null;
|
|
236
|
-
return open !== null;
|
|
237
|
-
}
|
|
238
|
-
function makeBlock(block, sourceLineCount) {
|
|
239
|
-
return {
|
|
240
|
-
...block,
|
|
241
|
-
sourceLineCount
|
|
242
|
-
};
|
|
210
|
+
function assertAttributesSupported$1(tag, node, attrs) {
|
|
211
|
+
if (Object.keys(attrs).length > 0 && typeof node.setAttribute !== "function") throw new UnsupportedAttributesError(tag, attrs);
|
|
243
212
|
}
|
|
244
213
|
/**
|
|
245
|
-
*
|
|
214
|
+
* Target that batches node updates on animation frames.
|
|
246
215
|
*
|
|
247
216
|
* @remarks
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
* and rules are split here.
|
|
217
|
+
* Internal implementation behind `createDOMTarget`. See SPEC.md § DOM
|
|
218
|
+
* targets for the batching rules.
|
|
251
219
|
*
|
|
252
|
-
* @
|
|
253
|
-
* @returns Parsed blocks in document order.
|
|
220
|
+
* @internal
|
|
254
221
|
*/
|
|
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
|
-
|
|
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);
|
|
420
|
+
}
|
|
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
|
+
*/
|
|
586
|
+
function isBlankLine(line) {
|
|
587
|
+
return line.trim() === "";
|
|
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
|
+
*/
|
|
595
|
+
function matchFence(line) {
|
|
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
|
+
};
|
|
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
|
+
*/
|
|
612
|
+
function fenceCloses(open, line) {
|
|
613
|
+
const match = matchFence(line);
|
|
614
|
+
return match !== null && match.marker === open.marker && match.length >= open.length;
|
|
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
|
+
*/
|
|
622
|
+
function matchHeading(line) {
|
|
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
|
+
};
|
|
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
|
+
*/
|
|
636
|
+
function isRuleLine(line) {
|
|
637
|
+
return RULE_RE.test(line.trim());
|
|
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
|
+
*/
|
|
645
|
+
function matchQuote(line) {
|
|
646
|
+
const match = QUOTE_RE.exec(line);
|
|
647
|
+
if (!match) return null;
|
|
648
|
+
return { text: match[1] ?? "" };
|
|
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
|
+
*/
|
|
656
|
+
function matchListItem(line) {
|
|
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
|
+
};
|
|
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
|
+
*/
|
|
672
|
+
function isTableDelimiter(line) {
|
|
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()));
|
|
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
|
+
*/
|
|
683
|
+
function hasOpenFence(lines) {
|
|
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;
|
|
688
|
+
}
|
|
689
|
+
function makeBlock(block, sourceLineCount) {
|
|
690
|
+
return {
|
|
691
|
+
...block,
|
|
692
|
+
sourceLineCount
|
|
693
|
+
};
|
|
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
|
+
*/
|
|
706
|
+
function splitBlocks(lines) {
|
|
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({
|
|
325
776
|
kind: "paragraph",
|
|
326
777
|
lines: paragraph
|
|
327
778
|
}, j - i));
|
|
@@ -352,21 +803,17 @@ function parseTableAlignment(cell) {
|
|
|
352
803
|
if (left) return "left";
|
|
353
804
|
return null;
|
|
354
805
|
}
|
|
355
|
-
// src/renderers/
|
|
806
|
+
// src/renderers/internal/parser-state.ts
|
|
356
807
|
/**
|
|
357
|
-
*
|
|
808
|
+
* Shared incremental line-driven parser for block structure.
|
|
358
809
|
*
|
|
359
810
|
* @remarks
|
|
360
|
-
* Internal
|
|
361
|
-
*
|
|
362
|
-
* trailing holdback), and lists commit one fragment per item with an indent
|
|
363
|
-
* stack for nesting. Tables are not recognized here, so table-looking lines
|
|
364
|
-
* stay plain paragraph text; {@link GFMState} extends this class with table
|
|
365
|
-
* support and nothing else.
|
|
811
|
+
* Internal base owned by the flavor states below. See SPEC.md
|
|
812
|
+
* § Incremental parsing for buffering, holdback, and fragment rules.
|
|
366
813
|
*
|
|
367
814
|
* @internal
|
|
368
815
|
*/
|
|
369
|
-
var
|
|
816
|
+
var BlockParserState = class {
|
|
370
817
|
_emit;
|
|
371
818
|
_pending = [];
|
|
372
819
|
_fence = null;
|
|
@@ -575,16 +1022,26 @@ var CommonMarkState = class {
|
|
|
575
1022
|
}
|
|
576
1023
|
};
|
|
577
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
|
+
/**
|
|
578
1035
|
* Incremental line-driven parser for GFM structure.
|
|
579
1036
|
*
|
|
580
1037
|
* @remarks
|
|
581
|
-
* Extends {@link
|
|
1038
|
+
* Extends {@link BlockParserState} with tables and nothing else: a table
|
|
582
1039
|
* commits at the delimiter row and streams one fragment per row. All leaf
|
|
583
1040
|
* and list parsing is inherited unchanged.
|
|
584
1041
|
*
|
|
585
1042
|
* @internal
|
|
586
1043
|
*/
|
|
587
|
-
var GFMState = class extends
|
|
1044
|
+
var GFMState = class extends BlockParserState {
|
|
588
1045
|
_tableOpen = false;
|
|
589
1046
|
_candidate = null;
|
|
590
1047
|
/**
|
|
@@ -670,7 +1127,7 @@ var GFMState = class extends CommonMarkState {
|
|
|
670
1127
|
return true;
|
|
671
1128
|
}
|
|
672
1129
|
};
|
|
673
|
-
// src/renderers/parser/inline.ts
|
|
1130
|
+
// src/renderers/internal/parser/inline.ts
|
|
674
1131
|
const ESCAPABLE_RE = /^[!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]$/;
|
|
675
1132
|
const AUTOLINK_RE = /^[a-zA-Z][a-zA-Z0-9+.-]*:[^<>\s]*$/;
|
|
676
1133
|
const ALNUM_RE = /[A-Za-z0-9]/;
|
|
@@ -834,710 +1291,438 @@ function appendInline(target, parentId, text, gfm) {
|
|
|
834
1291
|
}
|
|
835
1292
|
literal += ch;
|
|
836
1293
|
i++;
|
|
837
|
-
}
|
|
838
|
-
flushLiteral();
|
|
839
|
-
}
|
|
840
|
-
/**
|
|
841
|
-
* Split a paragraph line into text plus a trailing hard break marker.
|
|
842
|
-
*
|
|
843
|
-
* @param line - Raw paragraph line.
|
|
844
|
-
* @returns Text without the break marker, and whether one was present.
|
|
845
|
-
*/
|
|
846
|
-
function splitHardBreak(line) {
|
|
847
|
-
const hardBreak = / {2,}$/.test(line);
|
|
848
|
-
return {
|
|
849
|
-
text: hardBreak ? line.replace(/ {2,}$/, "") : line,
|
|
850
|
-
hardBreak
|
|
851
|
-
};
|
|
852
|
-
}
|
|
853
|
-
// src/renderers/commonmark.ts
|
|
854
|
-
/**
|
|
855
|
-
* Renderer for CommonMark.
|
|
856
|
-
*
|
|
857
|
-
* @remarks
|
|
858
|
-
* Constructed with its target and config. Feed it lines with
|
|
859
|
-
* {@link CommonMarkRenderer.writeLine}; it holds its own parse state and
|
|
860
|
-
* writes finished blocks to the target as they complete. Open lists are
|
|
861
|
-
* tracked on a stack so items — including nested ones — append to the
|
|
862
|
-
* right container as they arrive.
|
|
863
|
-
*
|
|
864
|
-
* The renderer never touches the DOM: it builds everything through the
|
|
865
|
-
* write-only target, so the same renderer drives live pages, simulated
|
|
866
|
-
* DOMs, and fully custom targets.
|
|
867
|
-
*
|
|
868
|
-
* @public
|
|
869
|
-
*/
|
|
870
|
-
var CommonMarkRenderer = class {
|
|
871
|
-
_target;
|
|
872
|
-
_config;
|
|
873
|
-
_state;
|
|
874
|
-
_open = [];
|
|
875
|
-
/**
|
|
876
|
-
* @param target - Destination for rendered output, from `createDOMTarget`
|
|
877
|
-
* or a custom `HTMLTarget` implementation.
|
|
878
|
-
* @param config - CommonMark rendering config.
|
|
879
|
-
*/
|
|
880
|
-
constructor(target, config) {
|
|
881
|
-
this._target = target;
|
|
882
|
-
this._config = config;
|
|
883
|
-
this._state = this._createState((fragment) => this._applyFragment(fragment));
|
|
884
|
-
}
|
|
885
|
-
/**
|
|
886
|
-
* Feed one source line; completed output is written to the target.
|
|
887
|
-
*
|
|
888
|
-
* @param line - Next source line, without its line break.
|
|
889
|
-
*/
|
|
890
|
-
writeLine(line) {
|
|
891
|
-
this._state.addLine(line);
|
|
892
|
-
}
|
|
893
|
-
/**
|
|
894
|
-
* Write the buffered remainder and settle the target.
|
|
895
|
-
*
|
|
896
|
-
* @returns A promise that resolves once all output has landed in the target.
|
|
897
|
-
*/
|
|
898
|
-
async flush() {
|
|
899
|
-
this._state.flush();
|
|
900
|
-
await this._target.flush();
|
|
901
|
-
}
|
|
902
|
-
/**
|
|
903
|
-
* Point the renderer at another target; subsequent writes go there.
|
|
904
|
-
*
|
|
905
|
-
* @param target - Target receiving future output.
|
|
906
|
-
*/
|
|
907
|
-
setTarget(target) {
|
|
908
|
-
this._target = target;
|
|
909
|
-
}
|
|
910
|
-
/**
|
|
911
|
-
* Replace the renderer config; applies to subsequent writes.
|
|
912
|
-
*
|
|
913
|
-
* @param config - Next config.
|
|
914
|
-
*/
|
|
915
|
-
setConfig(config) {
|
|
916
|
-
this._config = config;
|
|
917
|
-
}
|
|
918
|
-
/**
|
|
919
|
-
* Create the parser state. Overridden by `GFMRenderer` to parse
|
|
920
|
-
* tables.
|
|
921
|
-
*
|
|
922
|
-
* @param emit - Receives parse fragments in document order.
|
|
923
|
-
* @returns The state for this renderer.
|
|
924
|
-
*/
|
|
925
|
-
_createState(emit) {
|
|
926
|
-
return new CommonMarkState(emit);
|
|
927
|
-
}
|
|
928
|
-
_applyFragment(fragment) {
|
|
929
|
-
switch (fragment.kind) {
|
|
930
|
-
case "block":
|
|
931
|
-
this.renderBlock(fragment.block);
|
|
932
|
-
break;
|
|
933
|
-
case "listStart": {
|
|
934
|
-
const parent = this._open[this._open.length - 1]?.currentItem ?? null;
|
|
935
|
-
const list = this._target.createElement(fragment.ordered ? "ol" : "ul");
|
|
936
|
-
this._target.appendChild(parent, list);
|
|
937
|
-
this._open.push({
|
|
938
|
-
id: list,
|
|
939
|
-
currentItem: null
|
|
940
|
-
});
|
|
941
|
-
break;
|
|
942
|
-
}
|
|
943
|
-
case "listItem": {
|
|
944
|
-
const top = this._open[this._open.length - 1];
|
|
945
|
-
const item = this._target.createElement("li");
|
|
946
|
-
appendInline(this._target, item, fragment.text, this.inlineGfm());
|
|
947
|
-
this._target.appendChild(top.id, item);
|
|
948
|
-
top.currentItem = item;
|
|
949
|
-
break;
|
|
950
|
-
}
|
|
951
|
-
case "listEnd": this._open.pop();
|
|
952
|
-
}
|
|
953
|
-
}
|
|
954
|
-
/**
|
|
955
|
-
* Whether GFM-only inlines are enabled. Always false here; overridden by
|
|
956
|
-
* `GFMRenderer`.
|
|
957
|
-
*
|
|
958
|
-
* @returns False.
|
|
959
|
-
*/
|
|
960
|
-
inlineGfm() {
|
|
961
|
-
return false;
|
|
962
|
-
}
|
|
963
|
-
/**
|
|
964
|
-
* Write one parsed block to the target.
|
|
965
|
-
*
|
|
966
|
-
* @param block - Parsed block to write.
|
|
967
|
-
*/
|
|
968
|
-
renderBlock(block) {
|
|
969
|
-
switch (block.kind) {
|
|
970
|
-
case "heading": {
|
|
971
|
-
const heading = this._target.createElement(`h${block.level ?? 1}`);
|
|
972
|
-
this.appendJoinedLines(heading, block.lines);
|
|
973
|
-
this._target.appendChild(null, heading);
|
|
974
|
-
break;
|
|
975
|
-
}
|
|
976
|
-
case "paragraph": {
|
|
977
|
-
const paragraph = this._target.createElement("p");
|
|
978
|
-
this.appendParagraphLines(paragraph, block.lines);
|
|
979
|
-
this._target.appendChild(null, paragraph);
|
|
980
|
-
break;
|
|
981
|
-
}
|
|
982
|
-
case "fence": {
|
|
983
|
-
const pre = this._target.createElement("pre");
|
|
984
|
-
const code = this._target.createElement("code", block.language === void 0 ? {} : { class: `language-${block.language}` });
|
|
985
|
-
const text = block.lines.join("\n");
|
|
986
|
-
this._target.appendChild(code, this._target.createTextNode(text === "" ? "" : `${text}\n`));
|
|
987
|
-
this._target.appendChild(pre, code);
|
|
988
|
-
this._target.appendChild(null, pre);
|
|
989
|
-
break;
|
|
990
|
-
}
|
|
991
|
-
case "quote": {
|
|
992
|
-
const quote = this._target.createElement("blockquote");
|
|
993
|
-
const body = this._target.createElement("p");
|
|
994
|
-
this.appendJoinedLines(body, block.lines);
|
|
995
|
-
this._target.appendChild(quote, body);
|
|
996
|
-
this._target.appendChild(null, quote);
|
|
997
|
-
break;
|
|
998
|
-
}
|
|
999
|
-
case "rule": this._target.appendChild(null, this._target.createElement("hr"));
|
|
1000
|
-
}
|
|
1001
|
-
}
|
|
1002
|
-
/**
|
|
1003
|
-
* Write paragraph lines into a parent, honoring two-space hard breaks.
|
|
1004
|
-
*
|
|
1005
|
-
* @param parentId - Node the lines append to.
|
|
1006
|
-
* @param lines - Raw paragraph lines.
|
|
1007
|
-
*/
|
|
1008
|
-
appendParagraphLines(parentId, lines) {
|
|
1009
|
-
lines.forEach((line, at) => {
|
|
1010
|
-
const { text, hardBreak } = splitHardBreak(line);
|
|
1011
|
-
appendInline(this._target, parentId, text, this.inlineGfm());
|
|
1012
|
-
if (at < lines.length - 1) {
|
|
1013
|
-
if (hardBreak) this._target.appendChild(parentId, this._target.createElement("br"));
|
|
1014
|
-
else this._target.appendChild(parentId, this._target.createTextNode(" "));
|
|
1015
|
-
}
|
|
1016
|
-
});
|
|
1017
|
-
}
|
|
1018
|
-
appendJoinedLines(parentId, lines) {
|
|
1019
|
-
lines.forEach((line, at) => {
|
|
1020
|
-
if (at > 0) this._target.appendChild(parentId, this._target.createTextNode(" "));
|
|
1021
|
-
appendInline(this._target, parentId, line, this.inlineGfm());
|
|
1022
|
-
});
|
|
1023
|
-
}
|
|
1024
|
-
};
|
|
1025
|
-
// src/renderers/gfm.ts
|
|
1294
|
+
}
|
|
1295
|
+
flushLiteral();
|
|
1296
|
+
}
|
|
1026
1297
|
/**
|
|
1027
|
-
*
|
|
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
|
+
*/
|
|
1303
|
+
function splitHardBreak(line) {
|
|
1304
|
+
const hardBreak = / {2,}$/.test(line);
|
|
1305
|
+
return {
|
|
1306
|
+
text: hardBreak ? line.replace(/ {2,}$/, "") : line,
|
|
1307
|
+
hardBreak
|
|
1308
|
+
};
|
|
1309
|
+
}
|
|
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.
|
|
1028
1314
|
*
|
|
1029
1315
|
* @remarks
|
|
1030
|
-
*
|
|
1031
|
-
*
|
|
1032
|
-
*
|
|
1033
|
-
* on wide rows are kept as unaligned cells.
|
|
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.
|
|
1034
1319
|
*
|
|
1035
|
-
* @
|
|
1320
|
+
* @internal
|
|
1036
1321
|
*/
|
|
1037
|
-
var
|
|
1038
|
-
|
|
1322
|
+
var ListStack = class {
|
|
1323
|
+
_target;
|
|
1324
|
+
_gfm;
|
|
1325
|
+
_open = [];
|
|
1039
1326
|
/**
|
|
1040
|
-
* @param target - Destination for
|
|
1041
|
-
*
|
|
1042
|
-
* @param config - GFM rendering config.
|
|
1327
|
+
* @param target - Destination for newly opened root lists.
|
|
1328
|
+
* @param gfm - Enable GFM-only inlines inside items.
|
|
1043
1329
|
*/
|
|
1044
|
-
constructor(target,
|
|
1045
|
-
|
|
1330
|
+
constructor(target, gfm) {
|
|
1331
|
+
this._target = target;
|
|
1332
|
+
this._gfm = gfm;
|
|
1046
1333
|
}
|
|
1047
1334
|
/**
|
|
1048
|
-
*
|
|
1335
|
+
* Point the stack at another target; newly opened root lists go there.
|
|
1336
|
+
* Already open lists stay bound to their own target.
|
|
1049
1337
|
*
|
|
1050
|
-
* @
|
|
1338
|
+
* @param target - Target receiving future output.
|
|
1051
1339
|
*/
|
|
1052
|
-
|
|
1053
|
-
|
|
1340
|
+
setTarget(target) {
|
|
1341
|
+
this._target = target;
|
|
1054
1342
|
}
|
|
1055
1343
|
/**
|
|
1056
|
-
*
|
|
1344
|
+
* Commit a list container, nested inside the latest item when one is open.
|
|
1057
1345
|
*
|
|
1058
|
-
* @param
|
|
1059
|
-
* @returns The state for this renderer.
|
|
1346
|
+
* @param ordered - Whether the list is ordered (`ol`) or bullet (`ul`).
|
|
1060
1347
|
*/
|
|
1061
|
-
|
|
1062
|
-
|
|
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
|
+
});
|
|
1063
1359
|
}
|
|
1064
1360
|
/**
|
|
1065
|
-
*
|
|
1361
|
+
* Commit one item to the innermost open list.
|
|
1066
1362
|
*
|
|
1067
|
-
* @param
|
|
1363
|
+
* @param text - Raw item text after the marker.
|
|
1068
1364
|
*/
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
this._applyTableRow(fragment.line);
|
|
1076
|
-
break;
|
|
1077
|
-
case "tableEnd":
|
|
1078
|
-
this._applyTableEnd();
|
|
1079
|
-
break;
|
|
1080
|
-
default: super._applyFragment(fragment);
|
|
1081
|
-
}
|
|
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;
|
|
1082
1371
|
}
|
|
1083
1372
|
/**
|
|
1084
|
-
*
|
|
1085
|
-
*
|
|
1086
|
-
* @param headLine - Raw header row line.
|
|
1087
|
-
* @param delimLine - Delimiter row line carrying column alignment.
|
|
1373
|
+
* Close the innermost open list.
|
|
1088
1374
|
*/
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
const aligns = splitTableRow(delimLine).map(parseTableAlignment);
|
|
1092
|
-
const width = header.length;
|
|
1093
|
-
const table = this._target.createElement("table");
|
|
1094
|
-
const head = this._target.createElement("thead");
|
|
1095
|
-
const headRow = this._target.createElement("tr");
|
|
1096
|
-
for (let col = 0; col < width; col++) {
|
|
1097
|
-
const align = aligns[col] ?? null;
|
|
1098
|
-
const cell = this._target.createElement("th", align === null ? {} : { align });
|
|
1099
|
-
appendInline(this._target, cell, header[col] ?? "", true);
|
|
1100
|
-
this._target.appendChild(headRow, cell);
|
|
1101
|
-
}
|
|
1102
|
-
this._target.appendChild(head, headRow);
|
|
1103
|
-
this._target.appendChild(table, head);
|
|
1104
|
-
const body = this._target.createElement("tbody");
|
|
1105
|
-
this._target.appendChild(table, body);
|
|
1106
|
-
this._target.appendChild(null, table);
|
|
1107
|
-
this._table = {
|
|
1108
|
-
body,
|
|
1109
|
-
aligns,
|
|
1110
|
-
width
|
|
1111
|
-
};
|
|
1375
|
+
closeList() {
|
|
1376
|
+
this._open.pop();
|
|
1112
1377
|
}
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1378
|
+
};
|
|
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;
|
|
1128
1395
|
}
|
|
1129
|
-
|
|
1130
|
-
const
|
|
1131
|
-
|
|
1132
|
-
|
|
1396
|
+
case "paragraph": {
|
|
1397
|
+
const paragraph = target.createElement("p");
|
|
1398
|
+
appendParagraphLines(target, paragraph, block.lines, gfm);
|
|
1399
|
+
target.appendChild(null, paragraph);
|
|
1400
|
+
break;
|
|
1133
1401
|
}
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
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"));
|
|
1141
1420
|
}
|
|
1142
|
-
}
|
|
1143
|
-
// src/targets/animated-dom-target.ts
|
|
1421
|
+
}
|
|
1144
1422
|
/**
|
|
1145
|
-
*
|
|
1423
|
+
* Write paragraph lines into a parent, honoring two-space hard breaks.
|
|
1146
1424
|
*
|
|
1147
|
-
* @param
|
|
1148
|
-
* @param
|
|
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
|
|
1149
1431
|
*/
|
|
1150
|
-
function
|
|
1151
|
-
|
|
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
|
+
});
|
|
1152
1447
|
}
|
|
1448
|
+
// src/renderers/gfm.ts
|
|
1153
1449
|
/**
|
|
1154
|
-
*
|
|
1450
|
+
* Renderer for GitHub Flavored Markdown.
|
|
1155
1451
|
*
|
|
1156
1452
|
* @remarks
|
|
1157
|
-
*
|
|
1158
|
-
* injected frame scheduler so streamed output lands once per frame instead
|
|
1159
|
-
* of once per write. Ids are minted synchronously and unknown ids throw
|
|
1160
|
-
* synchronously; updates apply in call order when the frame runs.
|
|
1453
|
+
* Supports CommonMark blocks and inlines plus GFM tables and strikethrough.
|
|
1161
1454
|
*
|
|
1162
|
-
*
|
|
1455
|
+
* Standalone: shares internal helpers with `CommonMarkRenderer`, but
|
|
1456
|
+
* extends nothing flavor-specific. Consult the full documentation for
|
|
1457
|
+
* more details.
|
|
1163
1458
|
*
|
|
1164
|
-
* @
|
|
1459
|
+
* @public
|
|
1165
1460
|
*/
|
|
1166
|
-
var
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
_nextId = 0;
|
|
1173
|
-
_queue = [];
|
|
1174
|
-
_scheduled = false;
|
|
1175
|
-
_flushResolvers = [];
|
|
1176
|
-
/**
|
|
1177
|
-
* @param targetNode - Container node receiving rendered output.
|
|
1178
|
-
* @param document - Document used to create nodes.
|
|
1179
|
-
* @param raf - Frame scheduler used to batch writes.
|
|
1180
|
-
*/
|
|
1181
|
-
constructor(targetNode, document, raf) {
|
|
1182
|
-
this._container = targetNode;
|
|
1183
|
-
this._doc = document;
|
|
1184
|
-
this._raf = raf;
|
|
1185
|
-
}
|
|
1461
|
+
var GFMRenderer = class {
|
|
1462
|
+
_target;
|
|
1463
|
+
_config;
|
|
1464
|
+
_state;
|
|
1465
|
+
_lists;
|
|
1466
|
+
_table = null;
|
|
1186
1467
|
/**
|
|
1187
|
-
*
|
|
1188
|
-
*
|
|
1189
|
-
* @param
|
|
1190
|
-
* @param attrs - Attributes to set.
|
|
1191
|
-
* @returns Id addressing the new node.
|
|
1468
|
+
* @param target - Destination for rendered output, from `createDOMTarget`
|
|
1469
|
+
* or a custom `HTMLTarget` implementation.
|
|
1470
|
+
* @param config - GFM rendering config.
|
|
1192
1471
|
*/
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
this.
|
|
1197
|
-
|
|
1198
|
-
assertAttributesSupported$1(node, snap);
|
|
1199
|
-
for (const [name, value] of Object.entries(snap)) node.setAttribute?.(name, value);
|
|
1200
|
-
this._nodes.set(id, node);
|
|
1201
|
-
});
|
|
1202
|
-
return id;
|
|
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);
|
|
1203
1477
|
}
|
|
1204
1478
|
/**
|
|
1205
|
-
*
|
|
1206
|
-
*
|
|
1207
|
-
* @param text - Text content.
|
|
1208
|
-
* @returns Id addressing the new node.
|
|
1479
|
+
* Target receiving committed output.
|
|
1209
1480
|
*/
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
this._enqueue(() => {
|
|
1213
|
-
this._nodes.set(id, this._doc.createTextNode(text));
|
|
1214
|
-
});
|
|
1215
|
-
return id;
|
|
1481
|
+
get target() {
|
|
1482
|
+
return this._target;
|
|
1216
1483
|
}
|
|
1217
1484
|
/**
|
|
1218
|
-
*
|
|
1219
|
-
*
|
|
1220
|
-
* @param parent - Receiving node, or `null` for the top level.
|
|
1221
|
-
* @param child - Node to append.
|
|
1485
|
+
* Config applying to subsequent commits.
|
|
1222
1486
|
*/
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
this._mustGraph(child);
|
|
1226
|
-
this._assertAcyclic(child, parent);
|
|
1227
|
-
this._reparentGraph(child, parent);
|
|
1228
|
-
this._enqueue(() => {
|
|
1229
|
-
(parent === null ? this._container : this._mustNode(parent)).appendChild(this._mustNode(child));
|
|
1230
|
-
});
|
|
1487
|
+
get config() {
|
|
1488
|
+
return this._config;
|
|
1231
1489
|
}
|
|
1232
1490
|
/**
|
|
1233
|
-
*
|
|
1491
|
+
* Feed one source line; completed output is written to the target.
|
|
1234
1492
|
*
|
|
1235
|
-
* @param
|
|
1236
|
-
* @param newId - Replacement node.
|
|
1493
|
+
* @param line - Next source line, without its line break.
|
|
1237
1494
|
*/
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
this._mustGraph(newId);
|
|
1241
|
-
if (old.parent === void 0) throw new DetachedNodeError(oldId);
|
|
1242
|
-
if (oldId === newId) return;
|
|
1243
|
-
this._assertAcyclic(newId, old.parent);
|
|
1244
|
-
const parent = old.parent;
|
|
1245
|
-
this._reparentGraph(newId, parent);
|
|
1246
|
-
const doomed = this._retireGraph(oldId);
|
|
1247
|
-
this._enqueue(() => {
|
|
1248
|
-
(parent === null ? this._container : this._mustNode(parent)).replaceChild(this._mustNode(newId), this._mustNode(oldId));
|
|
1249
|
-
for (const id of doomed) this._nodes.delete(id);
|
|
1250
|
-
});
|
|
1495
|
+
writeLine(line) {
|
|
1496
|
+
this._state.addLine(line);
|
|
1251
1497
|
}
|
|
1252
1498
|
/**
|
|
1253
|
-
*
|
|
1499
|
+
* Write the buffered remainder and settle the target.
|
|
1254
1500
|
*
|
|
1255
|
-
* @
|
|
1501
|
+
* @returns A promise that resolves once all output has landed in the target.
|
|
1256
1502
|
*/
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
this._enqueue(() => {
|
|
1261
|
-
if (parent !== void 0) (parent === null ? this._container : this._mustNode(parent)).removeChild(this._mustNode(id));
|
|
1262
|
-
for (const stale of doomed) this._nodes.delete(stale);
|
|
1263
|
-
});
|
|
1503
|
+
async flush() {
|
|
1504
|
+
this._state.flush();
|
|
1505
|
+
await this._target.flush();
|
|
1264
1506
|
}
|
|
1265
1507
|
/**
|
|
1266
|
-
*
|
|
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.
|
|
1267
1510
|
*
|
|
1268
|
-
* @param
|
|
1269
|
-
* @param text - Replacement text.
|
|
1511
|
+
* @param target - Target receiving future output.
|
|
1270
1512
|
*/
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
for (const child of [...entry.children]) doomed.push(...this._retireGraph(child));
|
|
1275
|
-
this._enqueue(() => {
|
|
1276
|
-
this._mustNode(node).textContent = text;
|
|
1277
|
-
for (const stale of doomed) this._nodes.delete(stale);
|
|
1278
|
-
});
|
|
1513
|
+
setTarget(target) {
|
|
1514
|
+
this._target = target;
|
|
1515
|
+
this._lists.setTarget(target);
|
|
1279
1516
|
}
|
|
1280
1517
|
/**
|
|
1281
|
-
*
|
|
1518
|
+
* Replace the renderer config; applies to subsequent writes.
|
|
1282
1519
|
*
|
|
1283
|
-
* @
|
|
1520
|
+
* @param config - Next config.
|
|
1284
1521
|
*/
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
return new Promise((resolve) => {
|
|
1288
|
-
this._flushResolvers.push(resolve);
|
|
1289
|
-
});
|
|
1290
|
-
}
|
|
1291
|
-
_enqueue(apply) {
|
|
1292
|
-
this._queue.push(apply);
|
|
1293
|
-
if (!this._scheduled) {
|
|
1294
|
-
this._scheduled = true;
|
|
1295
|
-
this._raf(() => {
|
|
1296
|
-
this._scheduled = false;
|
|
1297
|
-
const batch = this._queue;
|
|
1298
|
-
this._queue = [];
|
|
1299
|
-
for (const queued of batch) queued();
|
|
1300
|
-
const resolvers = this._flushResolvers;
|
|
1301
|
-
this._flushResolvers = [];
|
|
1302
|
-
for (const resolve of resolvers) resolve();
|
|
1303
|
-
});
|
|
1304
|
-
}
|
|
1305
|
-
}
|
|
1306
|
-
_mint() {
|
|
1307
|
-
this._nextId += 1;
|
|
1308
|
-
const id = `n${this._nextId}`;
|
|
1309
|
-
this._graph.set(id, {
|
|
1310
|
-
parent: void 0,
|
|
1311
|
-
children: /* @__PURE__ */ new Set()
|
|
1312
|
-
});
|
|
1313
|
-
return id;
|
|
1314
|
-
}
|
|
1315
|
-
_mustGraph(id) {
|
|
1316
|
-
const entry = this._graph.get(id);
|
|
1317
|
-
if (entry === void 0) throw new UnknownNodeError(id);
|
|
1318
|
-
return entry;
|
|
1522
|
+
setConfig(config) {
|
|
1523
|
+
this._config = config;
|
|
1319
1524
|
}
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
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();
|
|
1326
1546
|
}
|
|
1327
1547
|
}
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
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
|
+
};
|
|
1332
1579
|
}
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
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);
|
|
1338
1603
|
}
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
const next = this._graph.get(at);
|
|
1345
|
-
if (next === void 0) return;
|
|
1346
|
-
doomed.push(at);
|
|
1347
|
-
for (const child of [...next.children]) visit(child);
|
|
1348
|
-
this._graph.delete(at);
|
|
1349
|
-
};
|
|
1350
|
-
visit(id);
|
|
1351
|
-
return doomed;
|
|
1604
|
+
/**
|
|
1605
|
+
* Close the open table.
|
|
1606
|
+
*/
|
|
1607
|
+
_applyTableEnd() {
|
|
1608
|
+
this._table = null;
|
|
1352
1609
|
}
|
|
1353
1610
|
};
|
|
1354
|
-
// src/
|
|
1355
|
-
/**
|
|
1356
|
-
* Throw when attributes were requested but the DOM cannot set them.
|
|
1357
|
-
*
|
|
1358
|
-
* @param node - Freshly created node.
|
|
1359
|
-
* @param attrs - Requested attributes.
|
|
1360
|
-
*/
|
|
1361
|
-
function assertAttributesSupported(node, attrs) {
|
|
1362
|
-
if (Object.keys(attrs).length > 0 && typeof node.setAttribute !== "function") throw new Error("Cannot set attributes: this DOM does not implement setAttribute");
|
|
1363
|
-
}
|
|
1611
|
+
// src/renderers/commonmark.ts
|
|
1364
1612
|
/**
|
|
1365
|
-
*
|
|
1613
|
+
* Renderer for CommonMark.
|
|
1366
1614
|
*
|
|
1367
1615
|
* @remarks
|
|
1368
|
-
*
|
|
1369
|
-
*
|
|
1370
|
-
* to
|
|
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.
|
|
1371
1621
|
*
|
|
1372
|
-
*
|
|
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
|
|
1373
1628
|
*/
|
|
1374
|
-
var
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1629
|
+
var CommonMarkRenderer = class {
|
|
1630
|
+
_target;
|
|
1631
|
+
_config;
|
|
1632
|
+
_state;
|
|
1633
|
+
_lists;
|
|
1379
1634
|
/**
|
|
1380
|
-
* @param
|
|
1381
|
-
*
|
|
1635
|
+
* @param target - Destination for rendered output, from `createDOMTarget`
|
|
1636
|
+
* or a custom `HTMLTarget` implementation.
|
|
1637
|
+
* @param config - CommonMark rendering config.
|
|
1382
1638
|
*/
|
|
1383
|
-
constructor(
|
|
1384
|
-
this.
|
|
1385
|
-
this.
|
|
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);
|
|
1386
1644
|
}
|
|
1387
1645
|
/**
|
|
1388
|
-
*
|
|
1389
|
-
*
|
|
1390
|
-
* @param tag - Lowercase tag name.
|
|
1391
|
-
* @param attrs - Attributes to set.
|
|
1392
|
-
* @returns Id addressing the new node.
|
|
1646
|
+
* Target receiving committed output.
|
|
1393
1647
|
*/
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
assertAttributesSupported(node, attrs);
|
|
1397
|
-
for (const [name, value] of Object.entries(attrs)) node.setAttribute?.(name, value);
|
|
1398
|
-
return this._store(node, void 0);
|
|
1648
|
+
get target() {
|
|
1649
|
+
return this._target;
|
|
1399
1650
|
}
|
|
1400
1651
|
/**
|
|
1401
|
-
*
|
|
1402
|
-
*
|
|
1403
|
-
* @param text - Text content.
|
|
1404
|
-
* @returns Id addressing the new node.
|
|
1652
|
+
* Config applying to subsequent commits.
|
|
1405
1653
|
*/
|
|
1406
|
-
|
|
1407
|
-
return this.
|
|
1654
|
+
get config() {
|
|
1655
|
+
return this._config;
|
|
1408
1656
|
}
|
|
1409
1657
|
/**
|
|
1410
|
-
*
|
|
1658
|
+
* Feed one source line; completed output is written to the target.
|
|
1411
1659
|
*
|
|
1412
|
-
* @param
|
|
1413
|
-
* @param child - Node to append.
|
|
1660
|
+
* @param line - Next source line, without its line break.
|
|
1414
1661
|
*/
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
const attachTo = parent === null ? this._container : this._must(parent).node;
|
|
1418
|
-
this._assertAcyclic(child, parent);
|
|
1419
|
-
attachTo.appendChild(entry.node);
|
|
1420
|
-
this._reparent(child, parent);
|
|
1662
|
+
writeLine(line) {
|
|
1663
|
+
this._state.addLine(line);
|
|
1421
1664
|
}
|
|
1422
1665
|
/**
|
|
1423
|
-
*
|
|
1666
|
+
* Write the buffered remainder and settle the target.
|
|
1424
1667
|
*
|
|
1425
|
-
* @
|
|
1426
|
-
* @param newId - Replacement node.
|
|
1668
|
+
* @returns A promise that resolves once all output has landed in the target.
|
|
1427
1669
|
*/
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
if (old.parent === void 0) throw new DetachedNodeError(oldId);
|
|
1432
|
-
if (oldId === newId) return;
|
|
1433
|
-
this._assertAcyclic(newId, old.parent);
|
|
1434
|
-
(old.parent === null ? this._container : this._must(old.parent).node).replaceChild(fresh.node, old.node);
|
|
1435
|
-
this._reparent(newId, old.parent);
|
|
1436
|
-
this._retire(oldId);
|
|
1670
|
+
async flush() {
|
|
1671
|
+
this._state.flush();
|
|
1672
|
+
await this._target.flush();
|
|
1437
1673
|
}
|
|
1438
1674
|
/**
|
|
1439
|
-
*
|
|
1675
|
+
* Point the renderer at another target. Open lists drain into the
|
|
1676
|
+
* previous target; only newly opened blocks go to the new one.
|
|
1440
1677
|
*
|
|
1441
|
-
* @param
|
|
1678
|
+
* @param target - Target receiving future output.
|
|
1442
1679
|
*/
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
this._retire(id);
|
|
1680
|
+
setTarget(target) {
|
|
1681
|
+
this._target = target;
|
|
1682
|
+
this._lists.setTarget(target);
|
|
1447
1683
|
}
|
|
1448
1684
|
/**
|
|
1449
|
-
* Replace
|
|
1685
|
+
* Replace the renderer config; applies to subsequent writes.
|
|
1450
1686
|
*
|
|
1451
|
-
* @param
|
|
1452
|
-
* @param text - Replacement text.
|
|
1453
|
-
*/
|
|
1454
|
-
setTextContent(node, text) {
|
|
1455
|
-
const entry = this._must(node);
|
|
1456
|
-
entry.node.textContent = text;
|
|
1457
|
-
for (const child of [...entry.children]) this._retire(child);
|
|
1458
|
-
entry.children.clear();
|
|
1459
|
-
}
|
|
1460
|
-
/**
|
|
1461
|
-
* Nothing to wait for: synchronous targets hold every update already.
|
|
1687
|
+
* @param config - Next config.
|
|
1462
1688
|
*/
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
const entry = this._entries.get(id);
|
|
1466
|
-
if (entry === void 0) throw new UnknownNodeError(id);
|
|
1467
|
-
return entry;
|
|
1689
|
+
setConfig(config) {
|
|
1690
|
+
this._config = config;
|
|
1468
1691
|
}
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
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();
|
|
1475
1704
|
}
|
|
1476
1705
|
}
|
|
1477
|
-
_store(node, parent) {
|
|
1478
|
-
this._nextId += 1;
|
|
1479
|
-
const id = `n${this._nextId}`;
|
|
1480
|
-
this._entries.set(id, {
|
|
1481
|
-
node,
|
|
1482
|
-
parent,
|
|
1483
|
-
children: /* @__PURE__ */ new Set()
|
|
1484
|
-
});
|
|
1485
|
-
return id;
|
|
1486
|
-
}
|
|
1487
|
-
_reparent(child, parent) {
|
|
1488
|
-
const entry = this._entries.get(child);
|
|
1489
|
-
if (entry.parent !== void 0 && entry.parent !== null) this._entries.get(entry.parent)?.children.delete(child);
|
|
1490
|
-
entry.parent = parent;
|
|
1491
|
-
if (parent !== null) this._entries.get(parent)?.children.add(child);
|
|
1492
|
-
}
|
|
1493
|
-
_retire(id) {
|
|
1494
|
-
const entry = this._entries.get(id);
|
|
1495
|
-
if (entry === void 0) return;
|
|
1496
|
-
for (const child of [...entry.children]) this._retire(child);
|
|
1497
|
-
if (entry.parent !== void 0 && entry.parent !== null) this._entries.get(entry.parent)?.children.delete(id);
|
|
1498
|
-
this._entries.delete(id);
|
|
1499
|
-
}
|
|
1500
1706
|
};
|
|
1501
|
-
// src/targets/create-dom-target.ts
|
|
1502
|
-
/**
|
|
1503
|
-
* Create a target rendering into a DOM container.
|
|
1504
|
-
*
|
|
1505
|
-
* @param document - Document used to create nodes: a real browser document
|
|
1506
|
-
* for live pages, or a simulated one such as linkedom for server-side
|
|
1507
|
-
* rendering.
|
|
1508
|
-
* @param targetNode - Container node receiving rendered output.
|
|
1509
|
-
* @param options - Frame scheduler.
|
|
1510
|
-
* @returns The target to hand to a renderer.
|
|
1511
|
-
*
|
|
1512
|
-
* @public
|
|
1513
|
-
*/
|
|
1514
|
-
function createDOMTarget(document, targetNode, options = {}) {
|
|
1515
|
-
const { raf } = options;
|
|
1516
|
-
return raf === void 0 ? new DOMTarget(targetNode, document) : new AnimatedDOMTarget(targetNode, document, raf);
|
|
1517
|
-
}
|
|
1518
1707
|
// src/streams/gfm-stream.ts
|
|
1519
1708
|
/**
|
|
1520
1709
|
* Create a GFM stream rendering into the given container.
|
|
1521
1710
|
*
|
|
1522
1711
|
* @remarks
|
|
1523
|
-
*
|
|
1524
|
-
*
|
|
1525
|
-
*
|
|
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)`.
|
|
1526
1715
|
*
|
|
1527
|
-
* @param
|
|
1528
|
-
* @
|
|
1529
|
-
* @param config - GFM rendering config.
|
|
1530
|
-
* @param targetOptions - Frame scheduler for the DOM target.
|
|
1531
|
-
* @returns The configured pipeline.
|
|
1716
|
+
* @param options - Container, document, config, and optional frame scheduler.
|
|
1717
|
+
* @returns The configured stream.
|
|
1532
1718
|
*
|
|
1533
1719
|
* @example
|
|
1534
1720
|
* ```ts
|
|
1535
|
-
* const stream = createGFMStream(
|
|
1536
|
-
* document.getElementById("out")!,
|
|
1721
|
+
* const stream = createGFMStream({
|
|
1722
|
+
* container: document.getElementById("out")!,
|
|
1537
1723
|
* document,
|
|
1538
|
-
*
|
|
1539
|
-
*
|
|
1540
|
-
* );
|
|
1724
|
+
* raf: requestAnimationFrame,
|
|
1725
|
+
* });
|
|
1541
1726
|
* const writer = stream.getWriter();
|
|
1542
1727
|
* await writer.write("# Hello\n");
|
|
1543
1728
|
* await writer.close();
|
|
@@ -1545,33 +1730,33 @@ function createDOMTarget(document, targetNode, options = {}) {
|
|
|
1545
1730
|
*
|
|
1546
1731
|
* @public
|
|
1547
1732
|
*/
|
|
1548
|
-
function createGFMStream(
|
|
1549
|
-
const
|
|
1550
|
-
return
|
|
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));
|
|
1551
1740
|
}
|
|
1552
1741
|
// src/streams/commonmark-stream.ts
|
|
1553
1742
|
/**
|
|
1554
1743
|
* Create a CommonMark stream rendering into the given container.
|
|
1555
1744
|
*
|
|
1556
1745
|
* @remarks
|
|
1557
|
-
*
|
|
1558
|
-
*
|
|
1559
|
-
*
|
|
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)`.
|
|
1560
1749
|
*
|
|
1561
|
-
* @param
|
|
1562
|
-
* @
|
|
1563
|
-
* @param config - CommonMark rendering config.
|
|
1564
|
-
* @param targetOptions - Frame scheduler for the DOM target.
|
|
1565
|
-
* @returns The configured pipeline.
|
|
1750
|
+
* @param options - Container, document, config, and optional frame scheduler.
|
|
1751
|
+
* @returns The configured stream.
|
|
1566
1752
|
*
|
|
1567
1753
|
* @example
|
|
1568
1754
|
* ```ts
|
|
1569
|
-
* const stream = createCommonMarkStream(
|
|
1570
|
-
* document.getElementById("out")!,
|
|
1755
|
+
* const stream = createCommonMarkStream({
|
|
1756
|
+
* container: document.getElementById("out")!,
|
|
1571
1757
|
* document,
|
|
1572
|
-
*
|
|
1573
|
-
*
|
|
1574
|
-
* );
|
|
1758
|
+
* raf: requestAnimationFrame,
|
|
1759
|
+
* });
|
|
1575
1760
|
* const writer = stream.getWriter();
|
|
1576
1761
|
* await writer.write("# Hello\n");
|
|
1577
1762
|
* await writer.close();
|
|
@@ -1579,18 +1764,71 @@ function createGFMStream(target, dom, config, targetOptions) {
|
|
|
1579
1764
|
*
|
|
1580
1765
|
* @public
|
|
1581
1766
|
*/
|
|
1582
|
-
function createCommonMarkStream(
|
|
1583
|
-
const
|
|
1584
|
-
return
|
|
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));
|
|
1774
|
+
}
|
|
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";
|
|
1797
|
+
}
|
|
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, "'");
|
|
1585
1819
|
}
|
|
1820
|
+
exports.ClosedStreamError = ClosedStreamError;
|
|
1586
1821
|
exports.CommonMarkRenderer = CommonMarkRenderer;
|
|
1587
1822
|
exports.DetachedNodeError = DetachedNodeError;
|
|
1588
1823
|
exports.GFMRenderer = GFMRenderer;
|
|
1589
1824
|
exports.HierarchyError = HierarchyError;
|
|
1825
|
+
exports.MarkdownStream = MarkdownStream;
|
|
1590
1826
|
exports.UnknownNodeError = UnknownNodeError;
|
|
1827
|
+
exports.UnsupportedAttributesError = UnsupportedAttributesError;
|
|
1591
1828
|
exports.createCommonMarkStream = createCommonMarkStream;
|
|
1592
1829
|
exports.createDOMTarget = createDOMTarget;
|
|
1593
1830
|
exports.createGFMStream = createGFMStream;
|
|
1594
|
-
exports.
|
|
1831
|
+
exports.escapeHTML = escapeHTML;
|
|
1832
|
+
exports.getRuntime = getRuntime;
|
|
1595
1833
|
|
|
1596
1834
|
//# sourceMappingURL=index.cjs.map
|