markemental 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,87 +1,251 @@
1
+ /**
2
+ * Incremental modular markdown renderer.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+ /**
7
+ * Opaque handle for a node held by a target.
8
+ *
9
+ * @remarks
10
+ * Minted by creation calls; never construct one by hand. Valid for
11
+ * detached nodes too, so renderers may build a subtree detached and
12
+ * attach it afterwards. Replacing or removing a node retires its id —
13
+ * and the ids of its subtree — and reusing a retired id throws.
14
+ *
15
+ * @public
16
+ */
17
+ type NodeId = string;
18
+ /**
19
+ * A target call referenced an id the target never minted or already
20
+ * retired. Carries the offending id.
21
+ *
22
+ * @public
23
+ */
24
+ export declare class UnknownNodeError extends Error {
25
+ /** The unknown id. */
26
+ readonly nodeId: string;
27
+ /**
28
+ * @param nodeId - The unknown id.
29
+ */
30
+ constructor(nodeId: string);
31
+ }
32
+ /**
33
+ * A replace targeted a node that was never attached. Carries the
34
+ * detached id.
35
+ *
36
+ * @public
37
+ */
38
+ export declare class DetachedNodeError extends Error {
39
+ /** The detached id. */
40
+ readonly nodeId: string;
41
+ /**
42
+ * @param nodeId - The detached id.
43
+ */
44
+ constructor(nodeId: string);
45
+ }
46
+ /**
47
+ * An attach would have built a cycle: the node is an ancestor of the
48
+ * parent. Carries both ids.
49
+ *
50
+ * @public
51
+ */
52
+ export declare class HierarchyError extends Error {
53
+ /** The node being attached. */
54
+ readonly nodeId: string;
55
+ /** The parent it was attached to. */
56
+ readonly parentId: string;
57
+ /**
58
+ * @param nodeId - The node being attached.
59
+ * @param parentId - The parent it was attached to.
60
+ */
61
+ constructor(nodeId: string, parentId: string);
62
+ }
63
+ /**
64
+ * Write-only destination for rendered output.
65
+ *
66
+ * @remarks
67
+ * An HTML-like target: renderers build nodes through these methods without
68
+ * ever touching a DOM. Nodes start detached; appending attaches them, and
69
+ * a `null` parent addresses the top level. The target returns opaque ids
70
+ * and owns the id-to-node mapping (in memory, or however it likes); it
71
+ * cannot be queried.
72
+ *
73
+ * Attributes are set once at creation so remote targets never need
74
+ * mutation calls; replace the node to change them. Implementations must
75
+ * behave like the DOM for the operations below: appending an attached
76
+ * node moves it, and unknown ids throw. Custom targets (collectors,
77
+ * serializers, remote renderers) implement this same interface.
78
+ *
79
+ * @public
80
+ */
81
+ interface HTMLTarget {
82
+ /**
83
+ * Create a detached element node with attributes.
84
+ *
85
+ * @param tag - Lowercase tag name (for example `"p"`).
86
+ * @param attrs - Attributes to set. Fixed at creation: replace the node
87
+ * to change them. Targets whose DOM cannot set attributes throw when
88
+ * this is non-empty.
89
+ * @returns Id addressing the new node.
90
+ */
91
+ createElement(tag: string, attrs?: Record<string, string>): NodeId;
92
+ /**
93
+ * Create a detached text node.
94
+ *
95
+ * @param text - Text content.
96
+ * @returns Id addressing the new node.
97
+ */
98
+ createTextNode(text: string): NodeId;
99
+ /**
100
+ * Append a child to a parent, or to the top level for a `null` parent.
101
+ * Appending an attached node moves it.
102
+ *
103
+ * @param parent - Receiving node, or `null` for the top level.
104
+ * @param child - Node to append.
105
+ * @throws {@link UnknownNodeError} for unknown ids.
106
+ * @throws {@link HierarchyError} when the child is an ancestor of the parent.
107
+ */
108
+ appendChild(parent: NodeId | null, child: NodeId): void;
109
+ /**
110
+ * Replace an attached node with another node, which takes its position.
111
+ * The old id is retired. Replacing a detached node throws; replacing a
112
+ * node with itself is a no-op.
113
+ *
114
+ * @param oldId - Attached node to replace.
115
+ * @param newId - Replacement node.
116
+ * @throws {@link UnknownNodeError} for unknown ids.
117
+ * @throws {@link DetachedNodeError} when the old node was never attached.
118
+ * @throws {@link HierarchyError} when the new node is an ancestor of the position.
119
+ */
120
+ replaceNode(oldId: NodeId, newId: NodeId): void;
121
+ /**
122
+ * Detach a node and retire its id.
123
+ *
124
+ * @param id - Node to remove.
125
+ * @throws {@link UnknownNodeError} for unknown ids.
126
+ */
127
+ remove(id: NodeId): void;
128
+ /**
129
+ * Replace a node's children with a single text run, retiring the
130
+ * children's ids. On a text node, replaces its content.
131
+ *
132
+ * @param node - Node to update.
133
+ * @param text - Replacement text.
134
+ * @throws {@link UnknownNodeError} for unknown ids.
135
+ */
136
+ setTextContent(node: NodeId, text: string): void;
137
+ /**
138
+ * Wait for all committed updates to land.
139
+ *
140
+ * @returns Nothing for synchronous targets, or a promise that resolves
141
+ * once every update has landed.
142
+ */
143
+ flush(): void | Promise<void>;
144
+ }
1
145
  /**
2
146
  * Incremental markdown rendering pipeline.
3
147
  *
4
148
  * @remarks
5
- * A pipeline accepts markdown in arbitrarily-sized chunks from an external
6
- * producer and renders complete blocks to its target as they arrive. A
7
- * trailing partial block is held back until more input completes it or
8
- * {@link MarkdownPipeline.flush} is called.
149
+ * A pipeline is a `WritableStream` of markdown text: write unicode string
150
+ * chunks in any sizes and complete blocks render to the target as they
151
+ * arrive. Closing the stream renders the trailing remainder. The pipeline
152
+ * chunks writes into lines itself; writes after close reject with the
153
+ * platform's native closed-stream error, and non-string chunks reject with
154
+ * a `TypeError`. Aborting discards the buffered remainder without
155
+ * rendering it.
9
156
  *
10
157
  * @public
11
158
  */
12
- interface MarkdownPipeline {
13
- /**
14
- * Push a chunk of markdown into the pipeline.
15
- *
16
- * @param chunk - Markdown text, or a promise resolving to markdown text.
17
- * @returns A promise that resolves once the chunk has been consumed and any
18
- * complete blocks have been rendered to the target.
19
- */
20
- push(chunk: string | Promise<string>): Promise<void>;
21
- /**
22
- * Render any buffered remainder and release pipeline resources.
23
- *
24
- * @returns A promise that resolves once all output has been flushed.
25
- */
26
- flush(): Promise<void>;
159
+ interface MarkdownPipeline<Config = unknown> extends WritableStream<string> {
160
+ /**
161
+ * Reuse this pipeline for another target: subsequent commits go there.
162
+ *
163
+ * @param target - Target receiving future output.
164
+ */
165
+ setTarget(target: HTMLTarget): void;
166
+ /**
167
+ * Update the renderer config on the fly. Applies to subsequent commits.
168
+ *
169
+ * @param config - Next config.
170
+ */
171
+ setConfig(config: Config): void;
27
172
  }
28
-
29
173
  /**
30
174
  * Incremental markdown renderer: consumes lines, updates a target.
31
175
  *
32
176
  * @remarks
33
- * A renderer is constructed with everything it needs: the target it writes
34
- * to, the document used to create nodes, and its flavor options. The
35
- * pipeline feeds it one complete line at a time; the renderer holds whatever
36
- * parse state it needs and writes finished output to its target as input
37
- * completes.
177
+ * A renderer holds whatever parse state it needs and writes finished
178
+ * output to its target as input completes. `Config` is the renderer's own
179
+ * config type (for example `GFMRendererConfig`); there is no shared base.
38
180
  *
39
181
  * Implement this interface to plug in your own renderer, hand-written or a
40
- * third-party adapter.
182
+ * third-party adapter. The constructor is yours; the pipeline wires the
183
+ * target through {@link MarkdownRenderer.setTarget}.
184
+ *
185
+ * Sanitization is the renderer's job: the target creates exactly the tags
186
+ * and attributes it is told to, so a renderer that passes untrusted input
187
+ * through (raw HTML, link targets) must filter it before calling the
188
+ * target.
41
189
  *
42
190
  * @public
43
191
  */
44
- interface MarkdownRenderer {
45
- /**
46
- * Feed one complete line to the renderer.
47
- *
48
- * @param line - Next source line, without its line break. The renderer
49
- * buffers what it needs and writes completed output to its target.
50
- */
51
- writeLine(line: string): void;
52
- /**
53
- * Flush buffered partial state to the target and settle it.
54
- *
55
- * @returns A promise that resolves once all output has landed in the target.
56
- */
57
- flush(): Promise<void>;
192
+ interface MarkdownRenderer<Config> {
193
+ /**
194
+ * Feed one complete line to the renderer.
195
+ *
196
+ * @param line - Next source line, without its line break. The renderer
197
+ * buffers what it needs and writes completed output to its target.
198
+ */
199
+ writeLine(line: string): void;
200
+ /**
201
+ * Flush buffered partial state to the target and settle it.
202
+ *
203
+ * @returns A promise that resolves once all output has landed in the target.
204
+ */
205
+ flush(): Promise<void>;
206
+ /**
207
+ * Point the renderer at another target. Queued updates stay with the old
208
+ * target; subsequent commits go to the new one.
209
+ *
210
+ * @param target - Target receiving future output.
211
+ */
212
+ setTarget(target: HTMLTarget): void;
213
+ /**
214
+ * Replace the renderer config. Applies to subsequent commits; output
215
+ * already committed is left alone.
216
+ *
217
+ * @param config - Next config.
218
+ */
219
+ setConfig(config: Config): void;
58
220
  }
59
-
60
221
  /**
61
- * Create a pipeline around any renderer.
222
+ * Create a streaming pipeline around any renderer and target.
62
223
  *
63
224
  * @remarks
64
- * The pipeline feeds the renderer one line at a time from whatever chunks
65
- * are pushed; the renderer owns its target, document, and parse state. Pass
66
- * a built-in renderer (`GFMRenderer`, `CommonMarkRenderer`) or your own
67
- * `MarkdownRenderer` implementation.
68
- *
69
- * @param renderer - Line-consuming renderer holding its own target and state.
225
+ * The pipeline chunks whatever strings are written into lines and feeds
226
+ * them to the renderer one at a time; the renderer owns its parse state
227
+ * and commits finished output to the target. Closing the stream renders
228
+ * the trailing remainder. Pass a built-in renderer (`GFMRenderer`,
229
+ * `CommonMarkRenderer`) or your own `MarkdownRenderer` implementation,
230
+ * and a DOM target from `createDOMTarget` or your own `HTMLTarget`.
231
+ *
232
+ * @param renderer - Line-consuming renderer holding its own parse state.
233
+ * @param target - Destination the renderer commits to.
70
234
  * @returns A working pipeline.
71
235
  *
72
236
  * @example
73
237
  * ```ts
74
- * const pipeline = createMarkdownPipeline(
75
- * new GFMRenderer(createRenderTarget(target), document, {}),
76
- * );
77
- * await pipeline.push("# Hello\n");
78
- * await pipeline.flush();
238
+ * const target = createDOMTarget(document, document.getElementById("out")!);
239
+ * const stream = createMarkdownStream(new GFMRenderer(target, {}), target);
240
+ * const writer = stream.getWriter();
241
+ * await writer.write("# Hello\n");
242
+ * await writer.close();
243
+ * writer.releaseLock();
79
244
  * ```
80
245
  *
81
246
  * @public
82
247
  */
83
- declare function createMarkdownPipeline(renderer: MarkdownRenderer): MarkdownPipeline;
84
-
248
+ export declare function createMarkdownStream<Config>(renderer: MarkdownRenderer<Config>, target: HTMLTarget): MarkdownPipeline<Config>;
85
249
  /**
86
250
  * Minimal subset of the DOM `Node` interface that markemental needs.
87
251
  *
@@ -93,57 +257,57 @@ declare function createMarkdownPipeline(renderer: MarkdownRenderer): MarkdownPip
93
257
  * @public
94
258
  */
95
259
  interface DOMNodeLike {
96
- /** Numeric node type, mirroring `Node.nodeType`. */
97
- nodeType: number;
98
- /** Text content of this node, mirroring `Node.textContent`. */
99
- textContent: string | null;
100
- /** Child nodes, mirroring `Node.childNodes`. */
101
- childNodes: ArrayLike<DOMNodeLike>;
102
- /** Owning document, or `null` for detached nodes. */
103
- ownerDocument: DOMDocumentLike | null;
104
- /**
105
- * Append a child node.
106
- *
107
- * @param node - Node to append.
108
- * @returns The appended node.
109
- */
110
- appendChild(node: DOMNodeLike): DOMNodeLike;
111
- /**
112
- * Insert a node before a reference child.
113
- *
114
- * @param newNode - Node to insert.
115
- * @param referenceNode - Child to insert before, or `null` to append.
116
- * @returns The inserted node.
117
- */
118
- insertBefore(newNode: DOMNodeLike, referenceNode: DOMNodeLike | null): DOMNodeLike;
119
- /**
120
- * Remove a child node.
121
- *
122
- * @param child - Child to remove.
123
- * @returns The removed node.
124
- */
125
- removeChild(child: DOMNodeLike): DOMNodeLike;
126
- /**
127
- * Replace a child node.
128
- *
129
- * @param newChild - Replacement node.
130
- * @param oldChild - Child to replace.
131
- * @returns The replaced node.
132
- */
133
- replaceChild(newChild: DOMNodeLike, oldChild: DOMNodeLike): DOMNodeLike;
134
- /**
135
- * Set an attribute on this node, when supported.
136
- *
137
- * @param name - Attribute name.
138
- * @param value - Attribute value.
139
- */
140
- setAttribute?(name: string, value: string): void;
141
- /**
142
- * Remove an attribute from this node, when supported.
143
- *
144
- * @param name - Attribute name.
145
- */
146
- removeAttribute?(name: string): void;
260
+ /** Numeric node type, mirroring `Node.nodeType`. */
261
+ nodeType: number;
262
+ /** Text content of this node, mirroring `Node.textContent`. */
263
+ textContent: string | null;
264
+ /** Child nodes, mirroring `Node.childNodes`. */
265
+ childNodes: ArrayLike<DOMNodeLike>;
266
+ /** Owning document, or `null` for detached nodes. */
267
+ ownerDocument: DOMDocumentLike | null;
268
+ /**
269
+ * Append a child node.
270
+ *
271
+ * @param node - Node to append.
272
+ * @returns The appended node.
273
+ */
274
+ appendChild(node: DOMNodeLike): DOMNodeLike;
275
+ /**
276
+ * Insert a node before a reference child.
277
+ *
278
+ * @param newNode - Node to insert.
279
+ * @param referenceNode - Child to insert before, or `null` to append.
280
+ * @returns The inserted node.
281
+ */
282
+ insertBefore(newNode: DOMNodeLike, referenceNode: DOMNodeLike | null): DOMNodeLike;
283
+ /**
284
+ * Remove a child node.
285
+ *
286
+ * @param child - Child to remove.
287
+ * @returns The removed node.
288
+ */
289
+ removeChild(child: DOMNodeLike): DOMNodeLike;
290
+ /**
291
+ * Replace a child node.
292
+ *
293
+ * @param newChild - Replacement node.
294
+ * @param oldChild - Child to replace.
295
+ * @returns The replaced node.
296
+ */
297
+ replaceChild(newChild: DOMNodeLike, oldChild: DOMNodeLike): DOMNodeLike;
298
+ /**
299
+ * Set an attribute on this node, when supported.
300
+ *
301
+ * @param name - Attribute name.
302
+ * @param value - Attribute value.
303
+ */
304
+ setAttribute?(name: string, value: string): void;
305
+ /**
306
+ * Remove an attribute from this node, when supported.
307
+ *
308
+ * @param name - Attribute name.
309
+ */
310
+ removeAttribute?(name: string): void;
147
311
  }
148
312
  /**
149
313
  * Minimal subset of the DOM `Document` interface that markemental needs.
@@ -151,20 +315,20 @@ interface DOMNodeLike {
151
315
  * @public
152
316
  */
153
317
  interface DOMDocumentLike {
154
- /**
155
- * Create an element node.
156
- *
157
- * @param tagName - Lowercase tag name (for example `"p"`).
158
- * @returns The new element.
159
- */
160
- createElement(tagName: string): DOMNodeLike;
161
- /**
162
- * Create a text node.
163
- *
164
- * @param data - Text content.
165
- * @returns The new text node.
166
- */
167
- createTextNode(data: string): DOMNodeLike;
318
+ /**
319
+ * Create an element node.
320
+ *
321
+ * @param tagName - Lowercase tag name (for example `"p"`).
322
+ * @returns The new element.
323
+ */
324
+ createElement(tagName: string): DOMNodeLike;
325
+ /**
326
+ * Create a text node.
327
+ *
328
+ * @param data - Text content.
329
+ * @returns The new text node.
330
+ */
331
+ createTextNode(data: string): DOMNodeLike;
168
332
  }
169
333
  /**
170
334
  * `requestAnimationFrame` callback scheduler.
@@ -175,35 +339,6 @@ interface DOMDocumentLike {
175
339
  * @public
176
340
  */
177
341
  type RAF = (callback: (time: number) => void) => number;
178
-
179
- /**
180
- * Destination for rendered output.
181
- *
182
- * @remarks
183
- * A target receives rendered nodes from a renderer and attaches them to an
184
- * external DOM: either a real browser document or a simulated one such as
185
- * linkedom. The caller provides that DOM; the target only decides when
186
- * attachment happens: immediately, or batched on animation frames for
187
- * smooth streaming into a live page.
188
- *
189
- * @public
190
- */
191
- interface RenderTarget {
192
- /**
193
- * Attach a rendered node to the target.
194
- *
195
- * @param node - Node to attach. Attachment may be deferred (for example to
196
- * the next animation frame); use {@link RenderTarget.flush} to wait for it.
197
- */
198
- append(node: DOMNodeLike): void;
199
- /**
200
- * Wait for all appended nodes to be attached.
201
- *
202
- * @returns A promise that resolves once the target holds every node.
203
- */
204
- flush(): Promise<void>;
205
- }
206
-
207
342
  /**
208
343
  * Leaf block constructs recognized by the MVP parser.
209
344
  *
@@ -221,25 +356,24 @@ type BlockKind = "heading" | "paragraph" | "fence" | "quote" | "rule";
221
356
  * @internal
222
357
  */
223
358
  interface Block {
224
- /** What kind of block this is. */
225
- kind: BlockKind;
226
- /**
227
- * Block content lines. Markers are stripped: `#` prefixes for headings,
228
- * `>` prefixes for quotes, and fence lines for fenced code. Paragraph
229
- * lines are raw.
230
- */
231
- lines: string[];
232
- /** Heading level (1-6) for `"heading"` blocks. */
233
- level?: number;
234
- /** Fence info string for `"fence"` blocks, when present. */
235
- language?: string;
236
- /**
237
- * Number of source lines this block was parsed from, used to hold back a
238
- * trailing partial block across `push` calls.
239
- */
240
- sourceLineCount: number;
359
+ /** What kind of block this is. */
360
+ kind: BlockKind;
361
+ /**
362
+ * Block content lines. Markers are stripped: `#` prefixes for headings,
363
+ * `>` prefixes for quotes, and fence lines for fenced code. Paragraph
364
+ * lines are raw.
365
+ */
366
+ lines: string[];
367
+ /** Heading level (1-6) for `"heading"` blocks. */
368
+ level?: number;
369
+ /** Fence info string for `"fence"` blocks, when present. */
370
+ language?: string;
371
+ /**
372
+ * Number of source lines this block was parsed from, used to hold back a
373
+ * trailing partial block across writes.
374
+ */
375
+ sourceLineCount: number;
241
376
  }
242
-
243
377
  /**
244
378
  * Structural piece of a document emitted as lines are parsed.
245
379
  *
@@ -253,25 +387,25 @@ interface Block {
253
387
  * @internal
254
388
  */
255
389
  type ParseFragment = {
256
- kind: "block";
257
- block: Block;
390
+ kind: "block";
391
+ block: Block;
258
392
  } | {
259
- kind: "listStart";
260
- ordered: boolean;
393
+ kind: "listStart";
394
+ ordered: boolean;
261
395
  } | {
262
- kind: "listItem";
263
- text: string;
396
+ kind: "listItem";
397
+ text: string;
264
398
  } | {
265
- kind: "listEnd";
399
+ kind: "listEnd";
266
400
  } | {
267
- kind: "tableHead";
268
- headLine: string;
269
- delimLine: string;
401
+ kind: "tableHead";
402
+ headLine: string;
403
+ delimLine: string;
270
404
  } | {
271
- kind: "tableRow";
272
- line: string;
405
+ kind: "tableRow";
406
+ line: string;
273
407
  } | {
274
- kind: "tableEnd";
408
+ kind: "tableEnd";
275
409
  };
276
410
  /**
277
411
  * Incremental line-driven parser for CommonMark structure.
@@ -287,67 +421,67 @@ type ParseFragment = {
287
421
  * @internal
288
422
  */
289
423
  declare class CommonMarkState {
290
- protected _emit: (fragment: ParseFragment) => void;
291
- protected _pending: string[];
292
- private _fence;
293
- private _quote;
294
- private _lists;
295
- /**
296
- * @param emit - Receives parse fragments in document order.
297
- */
298
- constructor(_emit: (fragment: ParseFragment) => void);
299
- /**
300
- * Feed one source line; completed fragments emit to the handler.
301
- *
302
- * @param line - Next source line, without its line break.
303
- */
304
- addLine(line: string): void;
305
- /**
306
- * Close every open context and emit the trailing remainder.
307
- */
308
- flush(): void;
309
- /**
310
- * Feed a line to the open fence, if any.
311
- *
312
- * @param line - Next source line.
313
- * @returns True when the line was consumed.
314
- */
315
- protected _consumeFence(line: string): boolean;
316
- /**
317
- * Feed a line to the open lists, if any, closing and reprocessing when
318
- * the line is not an item.
319
- *
320
- * @param line - Next source line.
321
- * @returns True when the line was consumed or reprocessed.
322
- */
323
- protected _consumeLists(line: string): boolean;
324
- /**
325
- * Feed a line to the open quote, if any, dumping it back to pending when
326
- * the line does not continue the quote.
327
- *
328
- * @param line - Next source line.
329
- * @returns True when the line was consumed.
330
- */
331
- protected _consumeQuote(line: string): boolean;
332
- /**
333
- * Classify a line that continues no open context.
334
- *
335
- * @param line - Next source line.
336
- */
337
- protected _classify(line: string): void;
338
- /**
339
- * Classify a line that matches no CommonMark opener. The base parser has
340
- * no pipe syntax, so this never consumes; {@link GFMState} overrides it
341
- * to start table candidates.
342
- *
343
- * @param _line - Next source line.
344
- * @returns Always false here.
345
- */
346
- protected _classifyPipe(_line: string): boolean;
347
- private _listItem;
348
- private _closeLists;
349
- private _takeComplete;
350
- protected _takeAll(): void;
424
+ protected _emit: (fragment: ParseFragment) => void;
425
+ protected _pending: string[];
426
+ private _fence;
427
+ private _quote;
428
+ private _lists;
429
+ /**
430
+ * @param emit - Receives parse fragments in document order.
431
+ */
432
+ constructor(_emit: (fragment: ParseFragment) => void);
433
+ /**
434
+ * Feed one source line; completed fragments emit to the handler.
435
+ *
436
+ * @param line - Next source line, without its line break.
437
+ */
438
+ addLine(line: string): void;
439
+ /**
440
+ * Close every open context and emit the trailing remainder.
441
+ */
442
+ flush(): void;
443
+ /**
444
+ * Feed a line to the open fence, if any.
445
+ *
446
+ * @param line - Next source line.
447
+ * @returns True when the line was consumed.
448
+ */
449
+ protected _consumeFence(line: string): boolean;
450
+ /**
451
+ * Feed a line to the open lists, if any, closing and reprocessing when
452
+ * the line is not an item.
453
+ *
454
+ * @param line - Next source line.
455
+ * @returns True when the line was consumed or reprocessed.
456
+ */
457
+ protected _consumeLists(line: string): boolean;
458
+ /**
459
+ * Feed a line to the open quote, if any, dumping it back to pending when
460
+ * the line does not continue the quote.
461
+ *
462
+ * @param line - Next source line.
463
+ * @returns True when the line was consumed.
464
+ */
465
+ protected _consumeQuote(line: string): boolean;
466
+ /**
467
+ * Classify a line that continues no open context.
468
+ *
469
+ * @param line - Next source line.
470
+ */
471
+ protected _classify(line: string): void;
472
+ /**
473
+ * Classify a line that matches no CommonMark opener. The base parser has
474
+ * no pipe syntax, so this never consumes; {@link GFMState} overrides it
475
+ * to start table candidates.
476
+ *
477
+ * @param _line - Next source line.
478
+ * @returns Always false here.
479
+ */
480
+ protected _classifyPipe(_line: string): boolean;
481
+ private _listItem;
482
+ private _closeLists;
483
+ private _takeComplete;
484
+ protected _takeAll(): void;
351
485
  }
352
486
  /**
353
487
  * Incremental line-driven parser for GFM structure.
@@ -360,154 +494,132 @@ declare class CommonMarkState {
360
494
  * @internal
361
495
  */
362
496
  declare class GFMState extends CommonMarkState {
363
- private _tableOpen;
364
- private _candidate;
365
- /**
366
- * Feed one source line; completed fragments emit to the handler.
367
- *
368
- * @param line - Next source line, without its line break.
369
- */
370
- addLine(line: string): void;
371
- /**
372
- * Close every open context and emit the trailing remainder.
373
- */
374
- flush(): void;
375
- /**
376
- * Feed a line to the open table, if any, closing and reprocessing when
377
- * the line is not a row.
378
- *
379
- * @param line - Next source line.
380
- * @returns True when the line was consumed or reprocessed.
381
- */
382
- private _consumeTable;
383
- /**
384
- * Resolve a pending table header candidate against the next line.
385
- *
386
- * @param line - Next source line.
387
- * @returns True when a table opened.
388
- */
389
- private _consumeCandidate;
390
- /**
391
- * Start a table header candidate on pipe lines.
392
- *
393
- * @param line - Next source line.
394
- * @returns True when the line starts a candidate.
395
- */
396
- protected _classifyPipe(line: string): boolean;
497
+ private _tableOpen;
498
+ private _candidate;
499
+ /**
500
+ * Feed one source line; completed fragments emit to the handler.
501
+ *
502
+ * @param line - Next source line, without its line break.
503
+ */
504
+ addLine(line: string): void;
505
+ /**
506
+ * Close every open context and emit the trailing remainder.
507
+ */
508
+ flush(): void;
509
+ /**
510
+ * Feed a line to the open table, if any, closing and reprocessing when
511
+ * the line is not a row.
512
+ *
513
+ * @param line - Next source line.
514
+ * @returns True when the line was consumed or reprocessed.
515
+ */
516
+ private _consumeTable;
517
+ /**
518
+ * Resolve a pending table header candidate against the next line.
519
+ *
520
+ * @param line - Next source line.
521
+ * @returns True when a table opened.
522
+ */
523
+ private _consumeCandidate;
524
+ /**
525
+ * Start a table header candidate on pipe lines.
526
+ *
527
+ * @param line - Next source line.
528
+ * @returns True when the line starts a candidate.
529
+ */
530
+ protected _classifyPipe(line: string): boolean;
397
531
  }
398
-
399
532
  /**
400
533
  * Renderer for CommonMark.
401
534
  *
402
535
  * @remarks
403
- * Constructed with its target, document, and options. Feed it lines with
536
+ * Constructed with its target and config. Feed it lines with
404
537
  * {@link CommonMarkRenderer.writeLine}; it holds its own parse state and
405
- * writes finished blocks to the target as they complete. An optional
406
- * `sanitizer` post-processes every committed node. Open lists are tracked
407
- * on a commit stack so items — including nested ones — append to the right
408
- * container as they arrive.
538
+ * writes finished blocks to the target as they complete. Open lists are
539
+ * tracked on a stack so items — including nested ones — append to the
540
+ * right container as they arrive.
541
+ *
542
+ * The renderer never touches the DOM: it builds everything through the
543
+ * write-only target, so the same renderer drives live pages, simulated
544
+ * DOMs, and fully custom targets.
409
545
  *
410
546
  * @public
411
547
  */
412
- declare class CommonMarkRenderer implements MarkdownRenderer {
413
- /** Rendering options. */
414
- readonly options: CommonMarkRenderOptions;
415
- private _target;
416
- protected _doc: DOMDocumentLike;
417
- private _state;
418
- private _open;
419
- /**
420
- * @param target - Destination for rendered output, from
421
- * `createRenderTarget`. Pass `requestAnimationFrame` there to batch
422
- * writes on animation frames, or nothing for synchronous writes.
423
- * @param doc - Document used to create nodes.
424
- * @param opts - CommonMark rendering options.
425
- */
426
- constructor(target: RenderTarget, doc: DOMDocumentLike, opts: CommonMarkRenderOptions);
427
- /**
428
- * Create the parser state. Overridden by `GFMRenderer` to parse
429
- * tables.
430
- *
431
- * @param emit - Receives parse fragments in document order.
432
- * @returns The state for this renderer.
433
- */
434
- protected _createState(emit: (fragment: ParseFragment) => void): CommonMarkState;
435
- /**
436
- * Feed one source line; completed output is written to the target.
437
- *
438
- * @param line - Next source line, without its line break.
439
- */
440
- writeLine(line: string): void;
441
- /**
442
- * Render the buffered remainder and settle the target.
443
- *
444
- * @returns A promise that resolves once all output has landed in the target.
445
- */
446
- flush(): Promise<void>;
447
- protected _applyFragment(fragment: ParseFragment): void;
448
- /**
449
- * Append a node to the target (top level) or to its parent container,
450
- * running the sanitizer first when one is set.
451
- *
452
- * @param node - Node to commit.
453
- * @param parent - Parent container, or null to commit to the target.
454
- */
455
- protected _commit(node: DOMNodeLike, parent: DOMNodeLike | null): void;
456
- /**
457
- * Whether GFM-only inlines are enabled. Always false here; overridden by
458
- * `GFMRenderer`.
459
- *
460
- * @returns False.
461
- */
462
- protected inlineGfm(): boolean;
463
- /**
464
- * Render one block to a DOM-like node.
465
- *
466
- * @param block - Parsed block to render.
467
- * @param doc - Document used to create nodes.
468
- * @returns The rendered node, or `null` to emit nothing.
469
- */
470
- protected renderBlock(block: Block, doc: DOMDocumentLike): DOMNodeLike | null;
471
- /**
472
- * Render paragraph lines, honoring two-space hard breaks.
473
- *
474
- * @param lines - Raw paragraph lines.
475
- * @param doc - Document used to create nodes.
476
- * @returns The paragraph element.
477
- */
478
- protected renderParagraph(lines: string[], doc: DOMDocumentLike): DOMNodeLike;
479
- private appendLinesInline;
548
+ export declare class CommonMarkRenderer implements MarkdownRenderer<CommonMarkRendererConfig> {
549
+ protected _target: HTMLTarget;
550
+ private _config;
551
+ private _state;
552
+ private _open;
553
+ /**
554
+ * @param target - Destination for rendered output, from `createDOMTarget`
555
+ * or a custom `HTMLTarget` implementation.
556
+ * @param config - CommonMark rendering config.
557
+ */
558
+ constructor(target: HTMLTarget, config: CommonMarkRendererConfig);
559
+ /**
560
+ * Feed one source line; completed output is written to the target.
561
+ *
562
+ * @param line - Next source line, without its line break.
563
+ */
564
+ writeLine(line: string): void;
565
+ /**
566
+ * Write the buffered remainder and settle the target.
567
+ *
568
+ * @returns A promise that resolves once all output has landed in the target.
569
+ */
570
+ flush(): Promise<void>;
571
+ /**
572
+ * Point the renderer at another target; subsequent writes go there.
573
+ *
574
+ * @param target - Target receiving future output.
575
+ */
576
+ setTarget(target: HTMLTarget): void;
577
+ /**
578
+ * Replace the renderer config; applies to subsequent writes.
579
+ *
580
+ * @param config - Next config.
581
+ */
582
+ setConfig(config: CommonMarkRendererConfig): void;
583
+ /**
584
+ * Create the parser state. Overridden by `GFMRenderer` to parse
585
+ * tables.
586
+ *
587
+ * @param emit - Receives parse fragments in document order.
588
+ * @returns The state for this renderer.
589
+ */
590
+ protected _createState(emit: (fragment: ParseFragment) => void): CommonMarkState;
591
+ protected _applyFragment(fragment: ParseFragment): void;
592
+ /**
593
+ * Whether GFM-only inlines are enabled. Always false here; overridden by
594
+ * `GFMRenderer`.
595
+ *
596
+ * @returns False.
597
+ */
598
+ protected inlineGfm(): boolean;
599
+ /**
600
+ * Write one parsed block to the target.
601
+ *
602
+ * @param block - Parsed block to write.
603
+ */
604
+ protected renderBlock(block: Block): void;
605
+ /**
606
+ * Write paragraph lines into a parent, honoring two-space hard breaks.
607
+ *
608
+ * @param parentId - Node the lines append to.
609
+ * @param lines - Raw paragraph lines.
610
+ */
611
+ protected appendParagraphLines(parentId: NodeId, lines: string[]): void;
612
+ private appendJoinedLines;
480
613
  }
481
614
  /**
482
- * Sanitizes one node committed to the target.
615
+ * Config for {@link CommonMarkRenderer}.
483
616
  *
484
617
  * @remarks
485
- * Applied to every node as it commits: leaf blocks whole, tables and lists
486
- * piece by piece (container first, then rows and items).
487
- *
488
- * @param node - Node being committed.
489
- * @returns The node to append to the target.
490
- *
491
- * @public
492
- */
493
- type BlockSanitizer = (node: DOMNodeLike) => DOMNodeLike;
494
- /**
495
- * Options for {@link CommonMarkRenderer}.
618
+ * Reserved for future renderer options.
496
619
  *
497
620
  * @public
498
621
  */
499
- type CommonMarkRenderOptions = {
500
- /**
501
- * Optional post-render sanitizer applied to every node committed to the
502
- * target.
503
- *
504
- * @remarks
505
- * Intended for HTML safety once raw HTML detection and parsing lands
506
- * (see the TODO in `parser/inline`).
507
- */
508
- sanitizer?: BlockSanitizer;
509
- };
510
-
622
+ type CommonMarkRendererConfig = Record<string, never>;
511
623
  /**
512
624
  * Renderer for GitHub Flavored Markdown.
513
625
  *
@@ -519,190 +631,141 @@ type CommonMarkRenderOptions = {
519
631
  *
520
632
  * @public
521
633
  */
522
- declare class GFMRenderer extends CommonMarkRenderer implements MarkdownRenderer {
523
- /** Rendering options. */
524
- readonly options: GFMRenderOptions;
525
- private _table;
526
- /**
527
- * @param target - Destination for rendered output, from
528
- * `createRenderTarget`. Pass `requestAnimationFrame` there to batch
529
- * writes on animation frames, or nothing for synchronous writes.
530
- * @param doc - Document used to create nodes.
531
- * @param opts - GFM rendering options.
532
- */
533
- constructor(target: RenderTarget, doc: DOMDocumentLike, opts: GFMRenderOptions);
534
- /**
535
- * GFM-only inlines are enabled.
536
- *
537
- * @returns True.
538
- */
539
- protected inlineGfm(): boolean;
540
- /**
541
- * Create the GFM parser state.
542
- *
543
- * @param emit - Receives parse fragments in document order.
544
- * @returns The state for this renderer.
545
- */
546
- protected _createState(emit: (fragment: ParseFragment) => void): GFMState;
547
- /**
548
- * Apply a fragment, handling tables here and delegating the rest.
549
- *
550
- * @param fragment - Fragment to apply.
551
- */
552
- protected _applyFragment(fragment: ParseFragment): void;
553
- /**
554
- * Commit the table with its header; body rows stream in afterwards.
555
- *
556
- * @param headLine - Raw header row line.
557
- * @param delimLine - Delimiter row line carrying column alignment.
558
- */
559
- protected _applyTableHead(headLine: string, delimLine: string): void;
560
- /**
561
- * Append one body row to the open table.
562
- *
563
- * @param line - Raw body row line.
564
- */
565
- protected _applyTableRow(line: string): void;
566
- /**
567
- * Close the open table.
568
- */
569
- protected _applyTableEnd(): void;
634
+ export declare class GFMRenderer extends CommonMarkRenderer implements MarkdownRenderer<GFMRendererConfig> {
635
+ private _table;
636
+ /**
637
+ * @param target - Destination for rendered output, from `createDOMTarget`
638
+ * or a custom `HTMLTarget` implementation.
639
+ * @param config - GFM rendering config.
640
+ */
641
+ constructor(target: HTMLTarget, config: GFMRendererConfig);
642
+ /**
643
+ * GFM-only inlines are enabled.
644
+ *
645
+ * @returns True.
646
+ */
647
+ protected inlineGfm(): boolean;
648
+ /**
649
+ * Create the GFM parser state.
650
+ *
651
+ * @param emit - Receives parse fragments in document order.
652
+ * @returns The state for this renderer.
653
+ */
654
+ protected _createState(emit: (fragment: ParseFragment) => void): GFMState;
655
+ /**
656
+ * Apply a fragment, handling tables here and delegating the rest.
657
+ *
658
+ * @param fragment - Fragment to apply.
659
+ */
660
+ protected _applyFragment(fragment: ParseFragment): void;
661
+ /**
662
+ * Commit the table with its header; body rows stream in afterwards.
663
+ *
664
+ * @param headLine - Raw header row line.
665
+ * @param delimLine - Delimiter row line carrying column alignment.
666
+ */
667
+ protected _applyTableHead(headLine: string, delimLine: string): void;
668
+ /**
669
+ * Append one body row to the open table.
670
+ *
671
+ * @param line - Raw body row line.
672
+ */
673
+ protected _applyTableRow(line: string): void;
674
+ /**
675
+ * Close the open table.
676
+ */
677
+ protected _applyTableEnd(): void;
570
678
  }
571
679
  /**
572
- * Options for {@link GFMRenderer}.
680
+ * Config for {@link GFMRenderer}.
681
+ *
682
+ * @public
683
+ */
684
+ type GFMRendererConfig = CommonMarkRendererConfig;
685
+ /**
686
+ * Options for {@link createDOMTarget}.
687
+ *
688
+ * @public
689
+ */
690
+ type DOMTargetOptions = {
691
+ /**
692
+ * Optional frame scheduler. When provided, writes are batched on
693
+ * animation frames; otherwise updates apply synchronously.
694
+ */
695
+ raf?: RAF;
696
+ };
697
+ /**
698
+ * Create a target rendering into a DOM container.
699
+ *
700
+ * @param document - Document used to create nodes: a real browser document
701
+ * for live pages, or a simulated one such as linkedom for server-side
702
+ * rendering.
703
+ * @param targetNode - Container node receiving rendered output.
704
+ * @param options - Frame scheduler.
705
+ * @returns The target to hand to a renderer.
573
706
  *
574
707
  * @public
575
708
  */
576
- type GFMRenderOptions = CommonMarkRenderOptions;
577
-
709
+ export declare function createDOMTarget(document: DOMDocumentLike, targetNode: DOMNodeLike, options?: DOMTargetOptions): HTMLTarget;
578
710
  /**
579
- * Create a GFM pipeline rendering into the given container.
711
+ * Create a GFM stream rendering into the given container.
580
712
  *
581
713
  * @remarks
582
714
  * The environment is implicit: pass a real browser document for live pages
583
- * or a simulated one such as linkedom for server-side rendering. Pass a
584
- * frame scheduler to batch DOM writes on animation frames.
715
+ * or a simulated one such as linkedom for server-side rendering. Pass
716
+ * target options to batch DOM writes on animation frames.
585
717
  *
586
718
  * @param target - Container node receiving rendered output.
587
719
  * @param dom - Document used to create nodes.
588
- * @param opts - GFM rendering options.
589
- * @param raf - Optional frame scheduler. When provided, DOM writes are
590
- * batched on animation frames for smooth streaming.
720
+ * @param config - GFM rendering config.
721
+ * @param targetOptions - Frame scheduler for the DOM target.
591
722
  * @returns The configured pipeline.
592
723
  *
593
724
  * @example
594
725
  * ```ts
595
- * const pipeline = createGFMPipeline(
596
- * document.getElementById("out"),
726
+ * const stream = createGFMStream(
727
+ * document.getElementById("out")!,
597
728
  * document,
598
729
  * {},
599
- * requestAnimationFrame,
730
+ * { raf: requestAnimationFrame },
600
731
  * );
601
- * await pipeline.push("# Hello\n");
602
- * await pipeline.flush();
732
+ * const writer = stream.getWriter();
733
+ * await writer.write("# Hello\n");
734
+ * await writer.close();
603
735
  * ```
604
736
  *
605
737
  * @public
606
738
  */
607
- declare function createGFMPipeline(target: DOMNodeLike, dom: DOMDocumentLike, opts: GFMRenderOptions, raf?: RAF): MarkdownPipeline;
608
- /**
609
- * Incremental GFM pipeline.
610
- *
611
- * @public
612
- */
613
- declare class GFMPipeline implements MarkdownPipeline {
614
- private _engine;
615
- /**
616
- * @param target - Container node receiving rendered output.
617
- * @param dom - Document used to create nodes.
618
- * @param opts - GFM rendering options.
619
- * @param raf - Optional frame scheduler for batched DOM writes.
620
- */
621
- constructor(target: DOMNodeLike, dom: DOMDocumentLike, opts: GFMRenderOptions, raf?: RAF);
622
- /**
623
- * Push a chunk of markdown into the pipeline.
624
- *
625
- * @param chunk - Markdown text, or a promise resolving to markdown text.
626
- * @returns A promise that resolves once the chunk has been consumed.
627
- */
628
- push(chunk: string | Promise<string>): Promise<void>;
629
- /**
630
- * Render any buffered remainder.
631
- *
632
- * @returns A promise that resolves once all output has been flushed.
633
- */
634
- flush(): Promise<void>;
635
- }
636
-
739
+ export declare function createGFMStream(target: DOMNodeLike, dom: DOMDocumentLike, config: GFMRendererConfig, targetOptions?: DOMTargetOptions): MarkdownPipeline<GFMRendererConfig>;
637
740
  /**
638
- * Create a CommonMark pipeline rendering into the given container.
741
+ * Create a CommonMark stream rendering into the given container.
639
742
  *
640
743
  * @remarks
641
744
  * The environment is implicit: pass a real browser document for live pages
642
- * or a simulated one such as linkedom for server-side rendering. Pass a
643
- * frame scheduler to batch DOM writes on animation frames.
745
+ * or a simulated one such as linkedom for server-side rendering. Pass
746
+ * target options to batch DOM writes on animation frames.
644
747
  *
645
748
  * @param target - Container node receiving rendered output.
646
749
  * @param dom - Document used to create nodes.
647
- * @param opts - CommonMark rendering options.
648
- * @param raf - Optional frame scheduler. When provided, DOM writes are
649
- * batched on animation frames for smooth streaming.
750
+ * @param config - CommonMark rendering config.
751
+ * @param targetOptions - Frame scheduler for the DOM target.
650
752
  * @returns The configured pipeline.
651
753
  *
652
754
  * @example
653
755
  * ```ts
654
- * const pipeline = createCommonMarkPipeline(
655
- * document.getElementById("out"),
756
+ * const stream = createCommonMarkStream(
757
+ * document.getElementById("out")!,
656
758
  * document,
657
759
  * {},
658
- * requestAnimationFrame,
760
+ * { raf: requestAnimationFrame },
659
761
  * );
660
- * await pipeline.push("# Hello\n");
661
- * await pipeline.flush();
762
+ * const writer = stream.getWriter();
763
+ * await writer.write("# Hello\n");
764
+ * await writer.close();
662
765
  * ```
663
766
  *
664
767
  * @public
665
768
  */
666
- declare function createCommonMarkPipeline(target: DOMNodeLike, dom: DOMDocumentLike, opts: CommonMarkRenderOptions, raf?: RAF): MarkdownPipeline;
667
- /**
668
- * Incremental CommonMark pipeline.
669
- *
670
- * @public
671
- */
672
- declare class CommonMarkPipeline implements MarkdownPipeline {
673
- private _engine;
674
- /**
675
- * @param target - Container node receiving rendered output.
676
- * @param dom - Document used to create nodes.
677
- * @param opts - CommonMark rendering options.
678
- * @param raf - Optional frame scheduler for batched DOM writes.
679
- */
680
- constructor(target: DOMNodeLike, dom: DOMDocumentLike, opts: CommonMarkRenderOptions, raf?: RAF);
681
- /**
682
- * Push a chunk of markdown into the pipeline.
683
- *
684
- * @param chunk - Markdown text, or a promise resolving to markdown text.
685
- * @returns A promise that resolves once the chunk has been consumed.
686
- */
687
- push(chunk: string | Promise<string>): Promise<void>;
688
- /**
689
- * Render any buffered remainder.
690
- *
691
- * @returns A promise that resolves once all output has been flushed.
692
- */
693
- flush(): Promise<void>;
694
- }
695
-
696
- /**
697
- * Create the render target for a container node.
698
- *
699
- * @param targetNode - Container node receiving rendered output.
700
- * @param raf - Optional frame scheduler. When provided, writes are batched
701
- * on animation frames; otherwise nodes are attached synchronously.
702
- * @returns The target to hand to a renderer.
703
- *
704
- * @public
705
- */
706
- declare function createRenderTarget(targetNode: DOMNodeLike, raf?: RAF): RenderTarget;
707
-
708
- export { type BlockSanitizer, CommonMarkPipeline, type CommonMarkRenderOptions, CommonMarkRenderer, type DOMDocumentLike, type DOMNodeLike, GFMPipeline, type GFMRenderOptions, GFMRenderer, type MarkdownPipeline, type MarkdownRenderer, type RAF, type RenderTarget, createCommonMarkPipeline, createGFMPipeline, createMarkdownPipeline, createRenderTarget };
769
+ export declare function createCommonMarkStream(target: DOMNodeLike, dom: DOMDocumentLike, config: CommonMarkRendererConfig, targetOptions?: DOMTargetOptions): MarkdownPipeline<CommonMarkRendererConfig>;
770
+ export type { CommonMarkRendererConfig, DOMDocumentLike, DOMNodeLike, DOMTargetOptions, GFMRendererConfig, HTMLTarget, MarkdownPipeline, MarkdownRenderer, NodeId, RAF };
771
+ //# sourceMappingURL=index.d.ts.map