markemental 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,708 +1,707 @@
1
1
  /**
2
- * Incremental markdown rendering pipeline.
2
+ * Incremental modular markdown renderer.
3
3
  *
4
4
  * @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.
9
- *
10
- * @public
5
+ * This barrel is the entire public API. Layout convention: `api/<name>.ts`
6
+ * holds one main contract (`api/target.ts` is the `HTMLTarget` contract,
7
+ * `api/renderer.ts` the `MarkdownRenderer` one, `api/stream.ts` the
8
+ * `MarkdownStream` one), while the plural directories hold what implements
9
+ * or composes them. Public modules sit at the top of each directory;
10
+ * private ones live in `internal/` and are never exported here.
11
+ *
12
+ * @packageDocumentation
11
13
  */
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>;
27
- }
28
-
29
14
  /**
30
- * Incremental markdown renderer: consumes lines, updates a target.
15
+ * Opaque handle for a node held by a target.
31
16
  *
32
17
  * @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.
18
+ * Minted by creation calls; never construct one by hand. Consult the
19
+ * full documentation for more details.
38
20
  *
39
- * Implement this interface to plug in your own renderer, hand-written or a
40
- * third-party adapter.
21
+ * @public
22
+ */
23
+ type NodeId = string;
24
+ /**
25
+ * A target call referenced an id the target never minted or already
26
+ * retired. Carries the offending id.
41
27
  *
42
28
  * @public
43
29
  */
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>;
30
+ export declare class UnknownNodeError extends Error {
31
+ /** The unknown id. */
32
+ readonly nodeId: string;
33
+ /**
34
+ * @param nodeId - The unknown id.
35
+ */
36
+ constructor(nodeId: string);
58
37
  }
59
-
60
38
  /**
61
- * Create a pipeline around any renderer.
62
- *
63
- * @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.
39
+ * A replace targeted a node that was never attached. Carries the
40
+ * detached id.
68
41
  *
69
- * @param renderer - Line-consuming renderer holding its own target and state.
70
- * @returns A working pipeline.
42
+ * @public
43
+ */
44
+ export declare class DetachedNodeError extends Error {
45
+ /** The detached id. */
46
+ readonly nodeId: string;
47
+ /**
48
+ * @param nodeId - The detached id.
49
+ */
50
+ constructor(nodeId: string);
51
+ }
52
+ /**
53
+ * An attach would have built a cycle: the node is an ancestor of the
54
+ * parent. Carries both ids.
71
55
  *
72
- * @example
73
- * ```ts
74
- * const pipeline = createMarkdownPipeline(
75
- * new GFMRenderer(createRenderTarget(target), document, {}),
76
- * );
77
- * await pipeline.push("# Hello\n");
78
- * await pipeline.flush();
79
- * ```
56
+ * @public
57
+ */
58
+ export declare class HierarchyError extends Error {
59
+ /** The node being attached. */
60
+ readonly nodeId: string;
61
+ /** The parent it was attached to. */
62
+ readonly parentId: string;
63
+ /**
64
+ * @param nodeId - The node being attached.
65
+ * @param parentId - The parent it was attached to.
66
+ */
67
+ constructor(nodeId: string, parentId: string);
68
+ }
69
+ /**
70
+ * An element was created with attributes, but the underlying DOM does not
71
+ * implement `setAttribute`. Carries the tag and the rejected attribute
72
+ * names.
80
73
  *
81
74
  * @public
82
75
  */
83
- declare function createMarkdownPipeline(renderer: MarkdownRenderer): MarkdownPipeline;
84
-
76
+ export declare class UnsupportedAttributesError extends Error {
77
+ /** Tag the attributes were requested on. */
78
+ readonly tag: string;
79
+ /** Names of the rejected attributes. */
80
+ readonly attributes: string[];
81
+ /**
82
+ * @param tag - Tag the attributes were requested on.
83
+ * @param attrs - Rejected attributes.
84
+ */
85
+ constructor(tag: string, attrs: Record<string, string>);
86
+ }
85
87
  /**
86
- * Minimal subset of the DOM `Node` interface that markemental needs.
88
+ * Write-only destination for rendered output.
87
89
  *
88
90
  * @remarks
89
- * These interfaces describe DOMs provided from outside the library: a real
90
- * browser document, or a lightweight server-side implementation (for
91
- * example linkedom). Both satisfy this interface.
91
+ * An HTML-like target: renderers build nodes through these methods without
92
+ * ever touching a DOM. Custom targets (collectors, serializers, remote
93
+ * renderers) implement this same interface. Consult the full
94
+ * documentation for more details.
92
95
  *
93
96
  * @public
94
97
  */
95
- 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;
98
+ interface HTMLTarget {
99
+ /**
100
+ * Create a detached element node with attributes.
101
+ *
102
+ * @param tag - Lowercase tag name (for example `"p"`).
103
+ * @param attrs - Attributes to set. Fixed at creation: replace the node
104
+ * to change them.
105
+ * @returns Id addressing the new node.
106
+ * @throws {@link UnsupportedAttributesError} when attrs is non-empty
107
+ * and the DOM cannot set attributes: synchronously, or when the frame
108
+ * runs for batched targets.
109
+ */
110
+ createElement(tag: string, attrs?: Record<string, string>): NodeId;
111
+ /**
112
+ * Create a detached text node.
113
+ *
114
+ * @param text - Text content.
115
+ * @returns Id addressing the new node.
116
+ */
117
+ createTextNode(text: string): NodeId;
118
+ /**
119
+ * Append a child to a parent, or to the top level for a `null` parent.
120
+ * Appending an attached node moves it.
121
+ *
122
+ * @param parent - Receiving node, or `null` for the top level.
123
+ * @param child - Node to append.
124
+ * @throws {@link UnknownNodeError} for unknown ids.
125
+ * @throws {@link HierarchyError} when the child is an ancestor of the parent.
126
+ */
127
+ appendChild(parent: NodeId | null, child: NodeId): void;
128
+ /**
129
+ * Replace an attached node with another node, which takes its position.
130
+ * The old id is retired. Replacing a detached node throws; replacing a
131
+ * node with itself is a no-op.
132
+ *
133
+ * @param oldId - Attached node to replace.
134
+ * @param newId - Replacement node.
135
+ * @throws {@link UnknownNodeError} for unknown ids.
136
+ * @throws {@link DetachedNodeError} when the old node was never attached.
137
+ * @throws {@link HierarchyError} when the new node is an ancestor of the position.
138
+ */
139
+ replaceNode(oldId: NodeId, newId: NodeId): void;
140
+ /**
141
+ * Detach a node and retire its id.
142
+ *
143
+ * @param id - Node to remove.
144
+ * @throws {@link UnknownNodeError} for unknown ids.
145
+ */
146
+ remove(id: NodeId): void;
147
+ /**
148
+ * Replace a node's children with a single text run, retiring the
149
+ * children's ids. On a text node, replaces its content.
150
+ *
151
+ * @param node - Node to update.
152
+ * @param text - Replacement text.
153
+ * @throws {@link UnknownNodeError} for unknown ids.
154
+ */
155
+ setTextContent(node: NodeId, text: string): void;
156
+ /**
157
+ * Wait for all committed updates to land.
158
+ *
159
+ * @returns Nothing for synchronous targets, or a promise that resolves
160
+ * once every update has landed.
161
+ */
162
+ flush(): void | Promise<void>;
147
163
  }
148
164
  /**
149
- * Minimal subset of the DOM `Document` interface that markemental needs.
165
+ * Incremental markdown renderer: consumes lines, updates a target.
166
+ *
167
+ * @remarks
168
+ * Implement this interface to plug in your own renderer, hand-written or a
169
+ * third-party adapter. The constructor is yours; hand the instance to
170
+ * {@link MarkdownStream}, which feeds it lines. Consult the full
171
+ * documentation for more details.
150
172
  *
151
173
  * @public
152
174
  */
153
- 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;
175
+ interface MarkdownRenderer<Config> {
176
+ /**
177
+ * Target receiving committed output.
178
+ */
179
+ readonly target: HTMLTarget;
180
+ /**
181
+ * Config applying to subsequent commits.
182
+ */
183
+ readonly config: Config;
184
+ /**
185
+ * Feed one complete line to the renderer.
186
+ *
187
+ * @param line - Next source line, without its line break. The renderer
188
+ * buffers what it needs and writes completed output to its target.
189
+ */
190
+ writeLine(line: string): void;
191
+ /**
192
+ * Flush buffered partial state to the target and settle it.
193
+ *
194
+ * @returns A promise that resolves once all output has landed in the target.
195
+ */
196
+ flush(): Promise<void>;
197
+ /**
198
+ * Point the renderer at another target. Queued updates stay with the old
199
+ * target; subsequent commits go to the new one. Constructs already open
200
+ * (lists, tables) drain into the old target — the only split that keeps
201
+ * document structure intact — and only newly opened blocks bind the new
202
+ * one.
203
+ *
204
+ * @param target - Target receiving future output.
205
+ */
206
+ setTarget(target: HTMLTarget): void;
207
+ /**
208
+ * Replace the renderer config. Applies to subsequent commits; output
209
+ * already committed is left alone.
210
+ *
211
+ * @param config - Next config.
212
+ */
213
+ setConfig(config: Config): void;
168
214
  }
169
215
  /**
170
- * `requestAnimationFrame` callback scheduler.
171
- *
172
- * @param callback - Invoked with the frame timestamp.
173
- * @returns A handle identifying the scheduled frame.
216
+ * `setTarget` or `setConfig` was called on a closed or aborted stream.
217
+ * Carries the rejected action.
174
218
  *
175
219
  * @public
176
220
  */
177
- type RAF = (callback: (time: number) => void) => number;
178
-
221
+ export declare class ClosedStreamError extends Error {
222
+ /** Action that was rejected. */
223
+ readonly action: "setTarget" | "setConfig";
224
+ /**
225
+ * @param action - Action that was rejected.
226
+ */
227
+ constructor(action: "setTarget" | "setConfig");
228
+ }
179
229
  /**
180
- * Destination for rendered output.
230
+ * Incremental markdown stream: a `WritableStream` of markdown text.
181
231
  *
182
232
  * @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.
233
+ * Write unicode string chunks in any sizes; the stream splits them into
234
+ * lines itself and feeds them to its renderer one at a time, and complete
235
+ * blocks render to the target as they arrive. Consult the full
236
+ * documentation for more details.
237
+ *
238
+ * Pass a built-in renderer (`GFMRenderer`, `CommonMarkRenderer`) or your
239
+ * own `MarkdownRenderer` implementation, already pointed at a DOM target
240
+ * from `createDOMTarget` or a custom `HTMLTarget`.
241
+ *
242
+ * @example
243
+ * ```ts
244
+ * const target = createDOMTarget({ document, container: document.getElementById("out")! });
245
+ * const stream = new MarkdownStream(new GFMRenderer(target));
246
+ * const writer = stream.getWriter();
247
+ * await writer.write("# Hello\n");
248
+ * await writer.close();
249
+ * writer.releaseLock();
250
+ * ```
188
251
  *
189
252
  * @public
190
253
  */
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>;
254
+ export declare class MarkdownStream<Config> extends WritableStream<string> {
255
+ private _renderer;
256
+ private _settled;
257
+ /**
258
+ * @param renderer - Line-consuming renderer holding its own target and
259
+ * parse state.
260
+ */
261
+ constructor(renderer: MarkdownRenderer<Config>);
262
+ /**
263
+ * Renderer this stream feeds lines to.
264
+ */
265
+ get renderer(): MarkdownRenderer<Config>;
266
+ /**
267
+ * Target the renderer commits to.
268
+ */
269
+ get target(): HTMLTarget;
270
+ /**
271
+ * Retarget this stream for another target: subsequent commits go there.
272
+ *
273
+ * @param target - Target receiving future output.
274
+ * @throws {@link ClosedStreamError} when the stream is closed or
275
+ * aborted: mutating a dead stream would silently act on a renderer the
276
+ * caller has likely moved on from.
277
+ */
278
+ setTarget(target: HTMLTarget): void;
279
+ /**
280
+ * Update the renderer config on the fly. Applies to subsequent commits.
281
+ *
282
+ * @param config - Next config.
283
+ * @throws {@link ClosedStreamError} when the stream is closed or aborted.
284
+ */
285
+ setConfig(config: Config): void;
286
+ private _assertOpen;
205
287
  }
206
-
207
288
  /**
208
- * Leaf block constructs recognized by the MVP parser.
289
+ * Minimal subset of the DOM `Node` interface that the DOM targets need.
209
290
  *
210
291
  * @remarks
211
- * Internal to the built-in renderers: the public renderer interface consumes
212
- * lines, and each renderer is free to parse them however it wants. Tables
213
- * and lists are not blocks: they stream as fragments (see ParseFragment).
292
+ * These interfaces belong to the built-in DOM targets, not to the main
293
+ * contracts: they describe DOMs provided from outside the library — a real
294
+ * browser document, or a lightweight server-side implementation (for
295
+ * example linkedom). Both satisfy this interface. Custom `HTMLTarget`
296
+ * implementations never touch these types.
214
297
  *
215
- * @internal
298
+ * @public
216
299
  */
217
- type BlockKind = "heading" | "paragraph" | "fence" | "quote" | "rule";
300
+ interface DOMNodeLike {
301
+ /** Numeric node type, mirroring `Node.nodeType`. */
302
+ nodeType: number;
303
+ /** Text content of this node, mirroring `Node.textContent`. */
304
+ textContent: string | null;
305
+ /** Child nodes, mirroring `Node.childNodes`. */
306
+ childNodes: ArrayLike<DOMNodeLike>;
307
+ /** Owning document, or `null` for detached nodes. */
308
+ ownerDocument: DOMDocumentLike | null;
309
+ /**
310
+ * Append a child node.
311
+ *
312
+ * @param node - Node to append.
313
+ * @returns The appended node.
314
+ */
315
+ appendChild(node: DOMNodeLike): DOMNodeLike;
316
+ /**
317
+ * Insert a node before a reference child.
318
+ *
319
+ * @param newNode - Node to insert.
320
+ * @param referenceNode - Child to insert before, or `null` to append.
321
+ * @returns The inserted node.
322
+ */
323
+ insertBefore(newNode: DOMNodeLike, referenceNode: DOMNodeLike | null): DOMNodeLike;
324
+ /**
325
+ * Remove a child node.
326
+ *
327
+ * @param child - Child to remove.
328
+ * @returns The removed node.
329
+ */
330
+ removeChild(child: DOMNodeLike): DOMNodeLike;
331
+ /**
332
+ * Replace a child node.
333
+ *
334
+ * @param newChild - Replacement node.
335
+ * @param oldChild - Child to replace.
336
+ * @returns The replaced node.
337
+ */
338
+ replaceChild(newChild: DOMNodeLike, oldChild: DOMNodeLike): DOMNodeLike;
339
+ /**
340
+ * Set an attribute on this node, when supported.
341
+ *
342
+ * @param name - Attribute name.
343
+ * @param value - Attribute value.
344
+ */
345
+ setAttribute?(name: string, value: string): void;
346
+ /**
347
+ * Remove an attribute from this node, when supported.
348
+ *
349
+ * @param name - Attribute name.
350
+ */
351
+ removeAttribute?(name: string): void;
352
+ }
218
353
  /**
219
- * A parsed block of markdown lines.
354
+ * Minimal subset of the DOM `Document` interface that markemental needs.
220
355
  *
221
- * @internal
356
+ * @public
222
357
  */
223
- 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;
358
+ interface DOMDocumentLike {
359
+ /**
360
+ * Create an element node.
361
+ *
362
+ * @param tagName - Lowercase tag name (for example `"p"`).
363
+ * @returns The new element.
364
+ */
365
+ createElement(tagName: string): DOMNodeLike;
366
+ /**
367
+ * Create a text node.
368
+ *
369
+ * @param data - Text content.
370
+ * @returns The new text node.
371
+ */
372
+ createTextNode(data: string): DOMNodeLike;
241
373
  }
242
-
243
374
  /**
244
- * Structural piece of a document emitted as lines are parsed.
375
+ * `requestAnimationFrame` callback scheduler.
245
376
  *
246
- * @remarks
247
- * Whole leaf blocks (headings, paragraphs, fences, quotes, rules) arrive as
248
- * single `block` fragments once complete. Lists stream: an opening fragment
249
- * commits the container, one fragment per item follows, and a closing
250
- * fragment ends it. Tables stream the same way (head, rows, end), but only
251
- * {@link GFMState} emits those; {@link CommonMarkState} never does.
377
+ * @param callback - Invoked with the frame timestamp.
378
+ * @returns A handle identifying the scheduled frame.
252
379
  *
253
- * @internal
380
+ * @public
254
381
  */
255
- type ParseFragment = {
256
- kind: "block";
257
- block: Block;
258
- } | {
259
- kind: "listStart";
260
- ordered: boolean;
261
- } | {
262
- kind: "listItem";
263
- text: string;
264
- } | {
265
- kind: "listEnd";
266
- } | {
267
- kind: "tableHead";
268
- headLine: string;
269
- delimLine: string;
270
- } | {
271
- kind: "tableRow";
272
- line: string;
273
- } | {
274
- kind: "tableEnd";
275
- };
382
+ type RAF = (callback: (time: number) => void) => number;
276
383
  /**
277
- * Incremental line-driven parser for CommonMark structure.
384
+ * Options for {@link createDOMTarget}.
278
385
  *
279
- * @remarks
280
- * Internal helper owned by a renderer. Lines arrive one at a time: leaf
281
- * blocks buffer until a new block starts or the stream flushes (preserving
282
- * trailing holdback), and lists commit one fragment per item with an indent
283
- * stack for nesting. Tables are not recognized here, so table-looking lines
284
- * stay plain paragraph text; {@link GFMState} extends this class with table
285
- * support and nothing else.
286
- *
287
- * @internal
386
+ * @public
288
387
  */
289
- 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;
388
+ interface DOMTargetOptions {
389
+ /**
390
+ * Document used to create nodes: a real browser document for live pages,
391
+ * or a simulated one such as linkedom for server-side rendering.
392
+ */
393
+ document: DOMDocumentLike;
394
+ /** Container node receiving rendered output. */
395
+ container: DOMNodeLike;
396
+ /**
397
+ * Optional frame scheduler. When provided, writes are batched on
398
+ * animation frames; otherwise updates apply synchronously.
399
+ */
400
+ raf?: RAF;
351
401
  }
352
402
  /**
353
- * Incremental line-driven parser for GFM structure.
403
+ * Create a target rendering into a DOM container.
354
404
  *
355
- * @remarks
356
- * Extends {@link CommonMarkState} with tables and nothing else: a table
357
- * commits at the delimiter row and streams one fragment per row. All leaf
358
- * and list parsing is inherited unchanged.
405
+ * @param options - Document, container, and optional frame scheduler.
406
+ * @returns The target to hand to a renderer.
359
407
  *
360
- * @internal
408
+ * @public
361
409
  */
362
- 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;
397
- }
398
-
410
+ export declare function createDOMTarget(options: DOMTargetOptions): HTMLTarget;
399
411
  /**
400
412
  * Renderer for CommonMark.
401
413
  *
402
414
  * @remarks
403
- * Constructed with its target, document, and options. Feed it lines with
415
+ * Constructed with its target and config. Feed it lines with
404
416
  * {@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.
417
+ * writes finished blocks to the target as they complete. An internal list
418
+ * stack tracks open lists so items — including nested ones — append to
419
+ * the right container as they arrive.
420
+ *
421
+ * The renderer never touches the DOM: it builds everything through the
422
+ * write-only target, so the same renderer drives live pages, simulated
423
+ * DOMs, and fully custom targets. It is standalone: GFM shares internal
424
+ * helpers with it, but neither renderer extends the other.
409
425
  *
410
426
  * @public
411
427
  */
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;
428
+ export declare class CommonMarkRenderer implements MarkdownRenderer<CommonMarkRendererConfig> {
429
+ private _target;
430
+ private _config;
431
+ private _state;
432
+ private _lists;
433
+ /**
434
+ * @param target - Destination for rendered output, from `createDOMTarget`
435
+ * or a custom `HTMLTarget` implementation.
436
+ * @param config - CommonMark rendering config.
437
+ */
438
+ constructor(target: HTMLTarget, config?: CommonMarkRendererConfig);
439
+ /**
440
+ * Target receiving committed output.
441
+ */
442
+ get target(): HTMLTarget;
443
+ /**
444
+ * Config applying to subsequent commits.
445
+ */
446
+ get config(): CommonMarkRendererConfig;
447
+ /**
448
+ * Feed one source line; completed output is written to the target.
449
+ *
450
+ * @param line - Next source line, without its line break.
451
+ */
452
+ writeLine(line: string): void;
453
+ /**
454
+ * Write the buffered remainder and settle the target.
455
+ *
456
+ * @returns A promise that resolves once all output has landed in the target.
457
+ */
458
+ flush(): Promise<void>;
459
+ /**
460
+ * Point the renderer at another target. Open lists drain into the
461
+ * previous target; only newly opened blocks go to the new one.
462
+ *
463
+ * @param target - Target receiving future output.
464
+ */
465
+ setTarget(target: HTMLTarget): void;
466
+ /**
467
+ * Replace the renderer config; applies to subsequent writes.
468
+ *
469
+ * @param config - Next config.
470
+ */
471
+ setConfig(config: CommonMarkRendererConfig): void;
472
+ private _applyFragment;
480
473
  }
481
474
  /**
482
- * Sanitizes one node committed to the target.
475
+ * Config for {@link CommonMarkRenderer}.
483
476
  *
484
477
  * @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.
478
+ * Reserved for future renderer options.
490
479
  *
491
480
  * @public
492
481
  */
493
- type BlockSanitizer = (node: DOMNodeLike) => DOMNodeLike;
494
- /**
495
- * Options for {@link CommonMarkRenderer}.
496
- *
497
- * @public
498
- */
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
-
482
+ type CommonMarkRendererConfig = Record<string, never>;
511
483
  /**
512
484
  * Renderer for GitHub Flavored Markdown.
513
485
  *
514
486
  * @remarks
515
487
  * Supports CommonMark blocks and inlines plus GFM tables and strikethrough.
516
- * Tables commit at the delimiter row and stream one row at a time; column
517
- * widths come from the header row, short rows are padded, and extra cells
518
- * on wide rows are kept as unaligned cells.
488
+ *
489
+ * Standalone: shares internal helpers with `CommonMarkRenderer`, but
490
+ * extends nothing flavor-specific. Consult the full documentation for
491
+ * more details.
519
492
  *
520
493
  * @public
521
494
  */
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;
495
+ export declare class GFMRenderer implements MarkdownRenderer<GFMRendererConfig> {
496
+ private _target;
497
+ private _config;
498
+ private _state;
499
+ private _lists;
500
+ private _table;
501
+ /**
502
+ * @param target - Destination for rendered output, from `createDOMTarget`
503
+ * or a custom `HTMLTarget` implementation.
504
+ * @param config - GFM rendering config.
505
+ */
506
+ constructor(target: HTMLTarget, config?: GFMRendererConfig);
507
+ /**
508
+ * Target receiving committed output.
509
+ */
510
+ get target(): HTMLTarget;
511
+ /**
512
+ * Config applying to subsequent commits.
513
+ */
514
+ get config(): GFMRendererConfig;
515
+ /**
516
+ * Feed one source line; completed output is written to the target.
517
+ *
518
+ * @param line - Next source line, without its line break.
519
+ */
520
+ writeLine(line: string): void;
521
+ /**
522
+ * Write the buffered remainder and settle the target.
523
+ *
524
+ * @returns A promise that resolves once all output has landed in the target.
525
+ */
526
+ flush(): Promise<void>;
527
+ /**
528
+ * Point the renderer at another target. Open lists and tables drain
529
+ * into the previous target; only newly opened blocks go to the new one.
530
+ *
531
+ * @param target - Target receiving future output.
532
+ */
533
+ setTarget(target: HTMLTarget): void;
534
+ /**
535
+ * Replace the renderer config; applies to subsequent writes.
536
+ *
537
+ * @param config - Next config.
538
+ */
539
+ setConfig(config: GFMRendererConfig): void;
540
+ private _applyFragment;
541
+ /**
542
+ * Commit the table with its header; body rows stream in afterwards.
543
+ *
544
+ * @param headLine - Raw header row line.
545
+ * @param delimLine - Delimiter row line carrying column alignment.
546
+ */
547
+ private _applyTableHead;
548
+ /**
549
+ * Append one body row to the open table.
550
+ *
551
+ * @param line - Raw body row line.
552
+ */
553
+ private _applyTableRow;
554
+ /**
555
+ * Close the open table.
556
+ */
557
+ private _applyTableEnd;
570
558
  }
571
559
  /**
572
- * Options for {@link GFMRenderer}.
560
+ * Config for {@link GFMRenderer}.
573
561
  *
574
562
  * @public
575
563
  */
576
- type GFMRenderOptions = CommonMarkRenderOptions;
577
-
564
+ type GFMRendererConfig = CommonMarkRendererConfig;
578
565
  /**
579
- * Create a GFM pipeline rendering into the given container.
566
+ * Options for {@link createGFMStream}.
567
+ *
568
+ * @public
569
+ */
570
+ interface GFMStreamOptions {
571
+ /** Container node receiving rendered output. */
572
+ container: DOMNodeLike;
573
+ /**
574
+ * Document used to create nodes: a real browser document for live pages,
575
+ * or a simulated one such as linkedom for server-side rendering.
576
+ */
577
+ document: DOMDocumentLike;
578
+ /** GFM rendering config. */
579
+ config?: GFMRendererConfig;
580
+ /**
581
+ * Optional frame scheduler. When provided, DOM writes are batched on
582
+ * animation frames; otherwise updates apply synchronously.
583
+ */
584
+ raf?: RAF;
585
+ }
586
+ /**
587
+ * Create a GFM stream rendering into the given container.
580
588
  *
581
589
  * @remarks
582
- * 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.
590
+ * One-call wiring of a DOM target, a `GFMRenderer`, and a `MarkdownStream`.
591
+ * For a custom renderer or target, wire them yourself with
592
+ * `new MarkdownStream(renderer)`.
585
593
  *
586
- * @param target - Container node receiving rendered output.
587
- * @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.
591
- * @returns The configured pipeline.
594
+ * @param options - Container, document, config, and optional frame scheduler.
595
+ * @returns The configured stream.
592
596
  *
593
597
  * @example
594
598
  * ```ts
595
- * const pipeline = createGFMPipeline(
596
- * document.getElementById("out"),
599
+ * const stream = createGFMStream({
600
+ * container: document.getElementById("out")!,
597
601
  * document,
598
- * {},
599
- * requestAnimationFrame,
600
- * );
601
- * await pipeline.push("# Hello\n");
602
- * await pipeline.flush();
602
+ * raf: requestAnimationFrame,
603
+ * });
604
+ * const writer = stream.getWriter();
605
+ * await writer.write("# Hello\n");
606
+ * await writer.close();
603
607
  * ```
604
608
  *
605
609
  * @public
606
610
  */
607
- declare function createGFMPipeline(target: DOMNodeLike, dom: DOMDocumentLike, opts: GFMRenderOptions, raf?: RAF): MarkdownPipeline;
611
+ export declare function createGFMStream(options: GFMStreamOptions): MarkdownStream<GFMRendererConfig>;
608
612
  /**
609
- * Incremental GFM pipeline.
613
+ * Options for {@link createCommonMarkStream}.
610
614
  *
611
615
  * @public
612
616
  */
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>;
617
+ interface CommonMarkStreamOptions {
618
+ /** Container node receiving rendered output. */
619
+ container: DOMNodeLike;
620
+ /**
621
+ * Document used to create nodes: a real browser document for live pages,
622
+ * or a simulated one such as linkedom for server-side rendering.
623
+ */
624
+ document: DOMDocumentLike;
625
+ /** CommonMark rendering config. */
626
+ config?: CommonMarkRendererConfig;
627
+ /**
628
+ * Optional frame scheduler. When provided, DOM writes are batched on
629
+ * animation frames; otherwise updates apply synchronously.
630
+ */
631
+ raf?: RAF;
635
632
  }
636
-
637
633
  /**
638
- * Create a CommonMark pipeline rendering into the given container.
634
+ * Create a CommonMark stream rendering into the given container.
639
635
  *
640
636
  * @remarks
641
- * 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.
637
+ * One-call wiring of a DOM target, a `CommonMarkRenderer`, and a
638
+ * `MarkdownStream`. For a custom renderer or target, wire them yourself
639
+ * with `new MarkdownStream(renderer)`.
644
640
  *
645
- * @param target - Container node receiving rendered output.
646
- * @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.
650
- * @returns The configured pipeline.
641
+ * @param options - Container, document, config, and optional frame scheduler.
642
+ * @returns The configured stream.
651
643
  *
652
644
  * @example
653
645
  * ```ts
654
- * const pipeline = createCommonMarkPipeline(
655
- * document.getElementById("out"),
646
+ * const stream = createCommonMarkStream({
647
+ * container: document.getElementById("out")!,
656
648
  * document,
657
- * {},
658
- * requestAnimationFrame,
659
- * );
660
- * await pipeline.push("# Hello\n");
661
- * await pipeline.flush();
649
+ * raf: requestAnimationFrame,
650
+ * });
651
+ * const writer = stream.getWriter();
652
+ * await writer.write("# Hello\n");
653
+ * await writer.close();
662
654
  * ```
663
655
  *
664
656
  * @public
665
657
  */
666
- declare function createCommonMarkPipeline(target: DOMNodeLike, dom: DOMDocumentLike, opts: CommonMarkRenderOptions, raf?: RAF): MarkdownPipeline;
658
+ export declare function createCommonMarkStream(options: CommonMarkStreamOptions): MarkdownStream<CommonMarkRendererConfig>;
667
659
  /**
668
- * Incremental CommonMark pipeline.
660
+ * Runtime-aware utilities. Each prefers a native built-in when one is
661
+ * available and falls back to a bundled implementation with identical
662
+ * output otherwise.
663
+ *
664
+ * @remarks
665
+ * Kept in one file while small; split when a utility outgrows it.
666
+ */
667
+ /**
668
+ * JavaScript runtime hosting this module.
669
669
  *
670
670
  * @public
671
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
-
672
+ type RuntimeKind = "bun" | "deno" | "node" | "browser" | "unknown";
696
673
  /**
697
- * Create the render target for a container node.
674
+ * Detect the JavaScript runtime hosting this module.
698
675
  *
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.
676
+ * @remarks
677
+ * Checks `Bun` first, then `Deno`, then Node's `process.versions`, then
678
+ * browser globals. Bun and Deno both expose Node-compatible globals, so
679
+ * order matters: the native brand always wins over the compatibility
680
+ * layer. Used by the smart utilities to prefer runtime built-ins (for
681
+ * example `Bun.escapeHTML`) over bundled fallbacks.
682
+ *
683
+ * @returns The detected runtime, or `"unknown"` when none matches.
684
+ *
685
+ * @public
686
+ */
687
+ export declare function getRuntime(): RuntimeKind;
688
+ /**
689
+ * Escape text for insertion into HTML.
690
+ *
691
+ * @remarks
692
+ * Handy for custom `HTMLTarget` implementations that serialize collected
693
+ * nodes to strings. Consult the full documentation for more details.
694
+ *
695
+ * @param text - Raw text to escape.
696
+ * @returns The escaped text.
697
+ *
698
+ * @example
699
+ * ```ts
700
+ * escapeHTML(`<a href="x&y">`) // "&lt;a href=&quot;x&amp;y&quot;&gt;"
701
+ * ```
703
702
  *
704
703
  * @public
705
704
  */
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 };
705
+ export declare function escapeHTML(text: string): string;
706
+ export type { CommonMarkRendererConfig, CommonMarkStreamOptions, DOMDocumentLike, DOMNodeLike, DOMTargetOptions, GFMRendererConfig, GFMStreamOptions, HTMLTarget, MarkdownRenderer, NodeId, RAF, RuntimeKind };
707
+ //# sourceMappingURL=index.d.cts.map