markemental 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -14
- package/dist/index.cjs +1108 -870
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +287 -351
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +287 -351
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1104 -870
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Incremental modular markdown renderer.
|
|
3
3
|
*
|
|
4
|
+
* @remarks
|
|
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
|
+
*
|
|
4
12
|
* @packageDocumentation
|
|
5
13
|
*/
|
|
6
14
|
/**
|
|
7
15
|
* Opaque handle for a node held by a target.
|
|
8
16
|
*
|
|
9
17
|
* @remarks
|
|
10
|
-
* Minted by creation calls; never construct one by hand.
|
|
11
|
-
*
|
|
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.
|
|
18
|
+
* Minted by creation calls; never construct one by hand. Consult the
|
|
19
|
+
* full documentation for more details.
|
|
14
20
|
*
|
|
15
21
|
* @public
|
|
16
22
|
*/
|
|
@@ -60,21 +66,32 @@ export declare class HierarchyError extends Error {
|
|
|
60
66
|
*/
|
|
61
67
|
constructor(nodeId: string, parentId: string);
|
|
62
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.
|
|
73
|
+
*
|
|
74
|
+
* @public
|
|
75
|
+
*/
|
|
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
|
+
}
|
|
63
87
|
/**
|
|
64
88
|
* Write-only destination for rendered output.
|
|
65
89
|
*
|
|
66
90
|
* @remarks
|
|
67
91
|
* An HTML-like target: renderers build nodes through these methods without
|
|
68
|
-
* ever touching a DOM.
|
|
69
|
-
*
|
|
70
|
-
*
|
|
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.
|
|
92
|
+
* ever touching a DOM. Custom targets (collectors, serializers, remote
|
|
93
|
+
* renderers) implement this same interface. Consult the full
|
|
94
|
+
* documentation for more details.
|
|
78
95
|
*
|
|
79
96
|
* @public
|
|
80
97
|
*/
|
|
@@ -84,9 +101,11 @@ interface HTMLTarget {
|
|
|
84
101
|
*
|
|
85
102
|
* @param tag - Lowercase tag name (for example `"p"`).
|
|
86
103
|
* @param attrs - Attributes to set. Fixed at creation: replace the node
|
|
87
|
-
* to change them.
|
|
88
|
-
* this is non-empty.
|
|
104
|
+
* to change them.
|
|
89
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.
|
|
90
109
|
*/
|
|
91
110
|
createElement(tag: string, attrs?: Record<string, string>): NodeId;
|
|
92
111
|
/**
|
|
@@ -143,53 +162,25 @@ interface HTMLTarget {
|
|
|
143
162
|
flush(): void | Promise<void>;
|
|
144
163
|
}
|
|
145
164
|
/**
|
|
146
|
-
* Incremental markdown
|
|
165
|
+
* Incremental markdown renderer: consumes lines, updates a target.
|
|
147
166
|
*
|
|
148
167
|
* @remarks
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
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.
|
|
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.
|
|
156
172
|
*
|
|
157
173
|
* @public
|
|
158
174
|
*/
|
|
159
|
-
interface
|
|
175
|
+
interface MarkdownRenderer<Config> {
|
|
160
176
|
/**
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
* @param target - Target receiving future output.
|
|
177
|
+
* Target receiving committed output.
|
|
164
178
|
*/
|
|
165
|
-
|
|
179
|
+
readonly target: HTMLTarget;
|
|
166
180
|
/**
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
* @param config - Next config.
|
|
181
|
+
* Config applying to subsequent commits.
|
|
170
182
|
*/
|
|
171
|
-
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Incremental markdown renderer: consumes lines, updates a target.
|
|
175
|
-
*
|
|
176
|
-
* @remarks
|
|
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.
|
|
180
|
-
*
|
|
181
|
-
* Implement this interface to plug in your own renderer, hand-written or a
|
|
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.
|
|
189
|
-
*
|
|
190
|
-
* @public
|
|
191
|
-
*/
|
|
192
|
-
interface MarkdownRenderer<Config> {
|
|
183
|
+
readonly config: Config;
|
|
193
184
|
/**
|
|
194
185
|
* Feed one complete line to the renderer.
|
|
195
186
|
*
|
|
@@ -205,7 +196,10 @@ interface MarkdownRenderer<Config> {
|
|
|
205
196
|
flush(): Promise<void>;
|
|
206
197
|
/**
|
|
207
198
|
* Point the renderer at another target. Queued updates stay with the old
|
|
208
|
-
* target; subsequent commits go to the new one.
|
|
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.
|
|
209
203
|
*
|
|
210
204
|
* @param target - Target receiving future output.
|
|
211
205
|
*/
|
|
@@ -219,24 +213,36 @@ interface MarkdownRenderer<Config> {
|
|
|
219
213
|
setConfig(config: Config): void;
|
|
220
214
|
}
|
|
221
215
|
/**
|
|
222
|
-
*
|
|
216
|
+
* `setTarget` or `setConfig` was called on a closed or aborted stream.
|
|
217
|
+
* Carries the rejected action.
|
|
218
|
+
*
|
|
219
|
+
* @public
|
|
220
|
+
*/
|
|
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
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Incremental markdown stream: a `WritableStream` of markdown text.
|
|
223
231
|
*
|
|
224
232
|
* @remarks
|
|
225
|
-
*
|
|
226
|
-
* them to
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
* `CommonMarkRenderer`) or your own `MarkdownRenderer` implementation,
|
|
230
|
-
* and a DOM target from `createDOMTarget` or your own `HTMLTarget`.
|
|
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.
|
|
231
237
|
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
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`.
|
|
235
241
|
*
|
|
236
242
|
* @example
|
|
237
243
|
* ```ts
|
|
238
|
-
* const target = createDOMTarget(document, document.getElementById("out")!);
|
|
239
|
-
* const stream =
|
|
244
|
+
* const target = createDOMTarget({ document, container: document.getElementById("out")! });
|
|
245
|
+
* const stream = new MarkdownStream(new GFMRenderer(target));
|
|
240
246
|
* const writer = stream.getWriter();
|
|
241
247
|
* await writer.write("# Hello\n");
|
|
242
248
|
* await writer.close();
|
|
@@ -245,14 +251,49 @@ interface MarkdownRenderer<Config> {
|
|
|
245
251
|
*
|
|
246
252
|
* @public
|
|
247
253
|
*/
|
|
248
|
-
export declare
|
|
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;
|
|
287
|
+
}
|
|
249
288
|
/**
|
|
250
|
-
* Minimal subset of the DOM `Node` interface that
|
|
289
|
+
* Minimal subset of the DOM `Node` interface that the DOM targets need.
|
|
251
290
|
*
|
|
252
291
|
* @remarks
|
|
253
|
-
* These interfaces
|
|
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
|
|
254
294
|
* browser document, or a lightweight server-side implementation (for
|
|
255
|
-
* example linkedom). Both satisfy this interface.
|
|
295
|
+
* example linkedom). Both satisfy this interface. Custom `HTMLTarget`
|
|
296
|
+
* implementations never touch these types.
|
|
256
297
|
*
|
|
257
298
|
* @public
|
|
258
299
|
*/
|
|
@@ -340,222 +381,69 @@ interface DOMDocumentLike {
|
|
|
340
381
|
*/
|
|
341
382
|
type RAF = (callback: (time: number) => void) => number;
|
|
342
383
|
/**
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
* @remarks
|
|
346
|
-
* Internal to the built-in renderers: the public renderer interface consumes
|
|
347
|
-
* lines, and each renderer is free to parse them however it wants. Tables
|
|
348
|
-
* and lists are not blocks: they stream as fragments (see ParseFragment).
|
|
349
|
-
*
|
|
350
|
-
* @internal
|
|
351
|
-
*/
|
|
352
|
-
type BlockKind = "heading" | "paragraph" | "fence" | "quote" | "rule";
|
|
353
|
-
/**
|
|
354
|
-
* A parsed block of markdown lines.
|
|
384
|
+
* Options for {@link createDOMTarget}.
|
|
355
385
|
*
|
|
356
|
-
* @
|
|
386
|
+
* @public
|
|
357
387
|
*/
|
|
358
|
-
interface
|
|
359
|
-
/** What kind of block this is. */
|
|
360
|
-
kind: BlockKind;
|
|
388
|
+
interface DOMTargetOptions {
|
|
361
389
|
/**
|
|
362
|
-
*
|
|
363
|
-
*
|
|
364
|
-
* lines are raw.
|
|
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.
|
|
365
392
|
*/
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
|
|
369
|
-
/** Fence info string for `"fence"` blocks, when present. */
|
|
370
|
-
language?: string;
|
|
393
|
+
document: DOMDocumentLike;
|
|
394
|
+
/** Container node receiving rendered output. */
|
|
395
|
+
container: DOMNodeLike;
|
|
371
396
|
/**
|
|
372
|
-
*
|
|
373
|
-
*
|
|
397
|
+
* Optional frame scheduler. When provided, writes are batched on
|
|
398
|
+
* animation frames; otherwise updates apply synchronously.
|
|
374
399
|
*/
|
|
375
|
-
|
|
400
|
+
raf?: RAF;
|
|
376
401
|
}
|
|
377
402
|
/**
|
|
378
|
-
*
|
|
379
|
-
*
|
|
380
|
-
* @remarks
|
|
381
|
-
* Whole leaf blocks (headings, paragraphs, fences, quotes, rules) arrive as
|
|
382
|
-
* single `block` fragments once complete. Lists stream: an opening fragment
|
|
383
|
-
* commits the container, one fragment per item follows, and a closing
|
|
384
|
-
* fragment ends it. Tables stream the same way (head, rows, end), but only
|
|
385
|
-
* {@link GFMState} emits those; {@link CommonMarkState} never does.
|
|
403
|
+
* Create a target rendering into a DOM container.
|
|
386
404
|
*
|
|
387
|
-
* @
|
|
388
|
-
|
|
389
|
-
type ParseFragment = {
|
|
390
|
-
kind: "block";
|
|
391
|
-
block: Block;
|
|
392
|
-
} | {
|
|
393
|
-
kind: "listStart";
|
|
394
|
-
ordered: boolean;
|
|
395
|
-
} | {
|
|
396
|
-
kind: "listItem";
|
|
397
|
-
text: string;
|
|
398
|
-
} | {
|
|
399
|
-
kind: "listEnd";
|
|
400
|
-
} | {
|
|
401
|
-
kind: "tableHead";
|
|
402
|
-
headLine: string;
|
|
403
|
-
delimLine: string;
|
|
404
|
-
} | {
|
|
405
|
-
kind: "tableRow";
|
|
406
|
-
line: string;
|
|
407
|
-
} | {
|
|
408
|
-
kind: "tableEnd";
|
|
409
|
-
};
|
|
410
|
-
/**
|
|
411
|
-
* Incremental line-driven parser for CommonMark structure.
|
|
405
|
+
* @param options - Document, container, and optional frame scheduler.
|
|
406
|
+
* @returns The target to hand to a renderer.
|
|
412
407
|
*
|
|
413
|
-
* @
|
|
414
|
-
* Internal helper owned by a renderer. Lines arrive one at a time: leaf
|
|
415
|
-
* blocks buffer until a new block starts or the stream flushes (preserving
|
|
416
|
-
* trailing holdback), and lists commit one fragment per item with an indent
|
|
417
|
-
* stack for nesting. Tables are not recognized here, so table-looking lines
|
|
418
|
-
* stay plain paragraph text; {@link GFMState} extends this class with table
|
|
419
|
-
* support and nothing else.
|
|
420
|
-
*
|
|
421
|
-
* @internal
|
|
408
|
+
* @public
|
|
422
409
|
*/
|
|
423
|
-
declare
|
|
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;
|
|
485
|
-
}
|
|
486
|
-
/**
|
|
487
|
-
* Incremental line-driven parser for GFM structure.
|
|
488
|
-
*
|
|
489
|
-
* @remarks
|
|
490
|
-
* Extends {@link CommonMarkState} with tables and nothing else: a table
|
|
491
|
-
* commits at the delimiter row and streams one fragment per row. All leaf
|
|
492
|
-
* and list parsing is inherited unchanged.
|
|
493
|
-
*
|
|
494
|
-
* @internal
|
|
495
|
-
*/
|
|
496
|
-
declare class GFMState extends CommonMarkState {
|
|
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;
|
|
531
|
-
}
|
|
410
|
+
export declare function createDOMTarget(options: DOMTargetOptions): HTMLTarget;
|
|
532
411
|
/**
|
|
533
412
|
* Renderer for CommonMark.
|
|
534
413
|
*
|
|
535
414
|
* @remarks
|
|
536
415
|
* Constructed with its target and config. Feed it lines with
|
|
537
416
|
* {@link CommonMarkRenderer.writeLine}; it holds its own parse state and
|
|
538
|
-
* writes finished blocks to the target as they complete.
|
|
539
|
-
*
|
|
540
|
-
* right 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.
|
|
541
420
|
*
|
|
542
421
|
* The renderer never touches the DOM: it builds everything through the
|
|
543
422
|
* write-only target, so the same renderer drives live pages, simulated
|
|
544
|
-
* DOMs, and fully custom targets.
|
|
423
|
+
* DOMs, and fully custom targets. It is standalone: GFM shares internal
|
|
424
|
+
* helpers with it, but neither renderer extends the other.
|
|
545
425
|
*
|
|
546
426
|
* @public
|
|
547
427
|
*/
|
|
548
428
|
export declare class CommonMarkRenderer implements MarkdownRenderer<CommonMarkRendererConfig> {
|
|
549
|
-
|
|
429
|
+
private _target;
|
|
550
430
|
private _config;
|
|
551
431
|
private _state;
|
|
552
|
-
private
|
|
432
|
+
private _lists;
|
|
553
433
|
/**
|
|
554
434
|
* @param target - Destination for rendered output, from `createDOMTarget`
|
|
555
435
|
* or a custom `HTMLTarget` implementation.
|
|
556
436
|
* @param config - CommonMark rendering config.
|
|
557
437
|
*/
|
|
558
|
-
constructor(target: HTMLTarget, config
|
|
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;
|
|
559
447
|
/**
|
|
560
448
|
* Feed one source line; completed output is written to the target.
|
|
561
449
|
*
|
|
@@ -569,7 +457,8 @@ export declare class CommonMarkRenderer implements MarkdownRenderer<CommonMarkRe
|
|
|
569
457
|
*/
|
|
570
458
|
flush(): Promise<void>;
|
|
571
459
|
/**
|
|
572
|
-
* Point the renderer at another target
|
|
460
|
+
* Point the renderer at another target. Open lists drain into the
|
|
461
|
+
* previous target; only newly opened blocks go to the new one.
|
|
573
462
|
*
|
|
574
463
|
* @param target - Target receiving future output.
|
|
575
464
|
*/
|
|
@@ -580,36 +469,7 @@ export declare class CommonMarkRenderer implements MarkdownRenderer<CommonMarkRe
|
|
|
580
469
|
* @param config - Next config.
|
|
581
470
|
*/
|
|
582
471
|
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;
|
|
472
|
+
private _applyFragment;
|
|
613
473
|
}
|
|
614
474
|
/**
|
|
615
475
|
* Config for {@link CommonMarkRenderer}.
|
|
@@ -625,56 +485,76 @@ type CommonMarkRendererConfig = Record<string, never>;
|
|
|
625
485
|
*
|
|
626
486
|
* @remarks
|
|
627
487
|
* Supports CommonMark blocks and inlines plus GFM tables and strikethrough.
|
|
628
|
-
*
|
|
629
|
-
*
|
|
630
|
-
*
|
|
488
|
+
*
|
|
489
|
+
* Standalone: shares internal helpers with `CommonMarkRenderer`, but
|
|
490
|
+
* extends nothing flavor-specific. Consult the full documentation for
|
|
491
|
+
* more details.
|
|
631
492
|
*
|
|
632
493
|
* @public
|
|
633
494
|
*/
|
|
634
|
-
export declare class GFMRenderer
|
|
495
|
+
export declare class GFMRenderer implements MarkdownRenderer<GFMRendererConfig> {
|
|
496
|
+
private _target;
|
|
497
|
+
private _config;
|
|
498
|
+
private _state;
|
|
499
|
+
private _lists;
|
|
635
500
|
private _table;
|
|
636
501
|
/**
|
|
637
502
|
* @param target - Destination for rendered output, from `createDOMTarget`
|
|
638
503
|
* or a custom `HTMLTarget` implementation.
|
|
639
504
|
* @param config - GFM rendering config.
|
|
640
505
|
*/
|
|
641
|
-
constructor(target: HTMLTarget, config
|
|
506
|
+
constructor(target: HTMLTarget, config?: GFMRendererConfig);
|
|
507
|
+
/**
|
|
508
|
+
* Target receiving committed output.
|
|
509
|
+
*/
|
|
510
|
+
get target(): HTMLTarget;
|
|
642
511
|
/**
|
|
643
|
-
*
|
|
512
|
+
* Config applying to subsequent commits.
|
|
513
|
+
*/
|
|
514
|
+
get config(): GFMRendererConfig;
|
|
515
|
+
/**
|
|
516
|
+
* Feed one source line; completed output is written to the target.
|
|
644
517
|
*
|
|
645
|
-
* @
|
|
518
|
+
* @param line - Next source line, without its line break.
|
|
646
519
|
*/
|
|
647
|
-
|
|
520
|
+
writeLine(line: string): void;
|
|
648
521
|
/**
|
|
649
|
-
*
|
|
522
|
+
* Write the buffered remainder and settle the target.
|
|
650
523
|
*
|
|
651
|
-
* @
|
|
652
|
-
* @returns The state for this renderer.
|
|
524
|
+
* @returns A promise that resolves once all output has landed in the target.
|
|
653
525
|
*/
|
|
654
|
-
|
|
526
|
+
flush(): Promise<void>;
|
|
655
527
|
/**
|
|
656
|
-
*
|
|
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.
|
|
657
530
|
*
|
|
658
|
-
* @param
|
|
531
|
+
* @param target - Target receiving future output.
|
|
659
532
|
*/
|
|
660
|
-
|
|
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;
|
|
661
541
|
/**
|
|
662
542
|
* Commit the table with its header; body rows stream in afterwards.
|
|
663
543
|
*
|
|
664
544
|
* @param headLine - Raw header row line.
|
|
665
545
|
* @param delimLine - Delimiter row line carrying column alignment.
|
|
666
546
|
*/
|
|
667
|
-
|
|
547
|
+
private _applyTableHead;
|
|
668
548
|
/**
|
|
669
549
|
* Append one body row to the open table.
|
|
670
550
|
*
|
|
671
551
|
* @param line - Raw body row line.
|
|
672
552
|
*/
|
|
673
|
-
|
|
553
|
+
private _applyTableRow;
|
|
674
554
|
/**
|
|
675
555
|
* Close the open table.
|
|
676
556
|
*/
|
|
677
|
-
|
|
557
|
+
private _applyTableEnd;
|
|
678
558
|
}
|
|
679
559
|
/**
|
|
680
560
|
* Config for {@link GFMRenderer}.
|
|
@@ -683,52 +563,44 @@ export declare class GFMRenderer extends CommonMarkRenderer implements MarkdownR
|
|
|
683
563
|
*/
|
|
684
564
|
type GFMRendererConfig = CommonMarkRendererConfig;
|
|
685
565
|
/**
|
|
686
|
-
* Options for {@link
|
|
566
|
+
* Options for {@link createGFMStream}.
|
|
687
567
|
*
|
|
688
568
|
* @public
|
|
689
569
|
*/
|
|
690
|
-
|
|
570
|
+
interface GFMStreamOptions {
|
|
571
|
+
/** Container node receiving rendered output. */
|
|
572
|
+
container: DOMNodeLike;
|
|
691
573
|
/**
|
|
692
|
-
*
|
|
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
|
|
693
582
|
* animation frames; otherwise updates apply synchronously.
|
|
694
583
|
*/
|
|
695
584
|
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.
|
|
706
|
-
*
|
|
707
|
-
* @public
|
|
708
|
-
*/
|
|
709
|
-
export declare function createDOMTarget(document: DOMDocumentLike, targetNode: DOMNodeLike, options?: DOMTargetOptions): HTMLTarget;
|
|
585
|
+
}
|
|
710
586
|
/**
|
|
711
587
|
* Create a GFM stream rendering into the given container.
|
|
712
588
|
*
|
|
713
589
|
* @remarks
|
|
714
|
-
*
|
|
715
|
-
*
|
|
716
|
-
*
|
|
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)`.
|
|
717
593
|
*
|
|
718
|
-
* @param
|
|
719
|
-
* @
|
|
720
|
-
* @param config - GFM rendering config.
|
|
721
|
-
* @param targetOptions - Frame scheduler for the DOM target.
|
|
722
|
-
* @returns The configured pipeline.
|
|
594
|
+
* @param options - Container, document, config, and optional frame scheduler.
|
|
595
|
+
* @returns The configured stream.
|
|
723
596
|
*
|
|
724
597
|
* @example
|
|
725
598
|
* ```ts
|
|
726
|
-
* const stream = createGFMStream(
|
|
727
|
-
* document.getElementById("out")!,
|
|
599
|
+
* const stream = createGFMStream({
|
|
600
|
+
* container: document.getElementById("out")!,
|
|
728
601
|
* document,
|
|
729
|
-
*
|
|
730
|
-
*
|
|
731
|
-
* );
|
|
602
|
+
* raf: requestAnimationFrame,
|
|
603
|
+
* });
|
|
732
604
|
* const writer = stream.getWriter();
|
|
733
605
|
* await writer.write("# Hello\n");
|
|
734
606
|
* await writer.close();
|
|
@@ -736,29 +608,46 @@ export declare function createDOMTarget(document: DOMDocumentLike, targetNode: D
|
|
|
736
608
|
*
|
|
737
609
|
* @public
|
|
738
610
|
*/
|
|
739
|
-
export declare function createGFMStream(
|
|
611
|
+
export declare function createGFMStream(options: GFMStreamOptions): MarkdownStream<GFMRendererConfig>;
|
|
612
|
+
/**
|
|
613
|
+
* Options for {@link createCommonMarkStream}.
|
|
614
|
+
*
|
|
615
|
+
* @public
|
|
616
|
+
*/
|
|
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;
|
|
632
|
+
}
|
|
740
633
|
/**
|
|
741
634
|
* Create a CommonMark stream rendering into the given container.
|
|
742
635
|
*
|
|
743
636
|
* @remarks
|
|
744
|
-
*
|
|
745
|
-
*
|
|
746
|
-
*
|
|
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)`.
|
|
747
640
|
*
|
|
748
|
-
* @param
|
|
749
|
-
* @
|
|
750
|
-
* @param config - CommonMark rendering config.
|
|
751
|
-
* @param targetOptions - Frame scheduler for the DOM target.
|
|
752
|
-
* @returns The configured pipeline.
|
|
641
|
+
* @param options - Container, document, config, and optional frame scheduler.
|
|
642
|
+
* @returns The configured stream.
|
|
753
643
|
*
|
|
754
644
|
* @example
|
|
755
645
|
* ```ts
|
|
756
|
-
* const stream = createCommonMarkStream(
|
|
757
|
-
* document.getElementById("out")!,
|
|
646
|
+
* const stream = createCommonMarkStream({
|
|
647
|
+
* container: document.getElementById("out")!,
|
|
758
648
|
* document,
|
|
759
|
-
*
|
|
760
|
-
*
|
|
761
|
-
* );
|
|
649
|
+
* raf: requestAnimationFrame,
|
|
650
|
+
* });
|
|
762
651
|
* const writer = stream.getWriter();
|
|
763
652
|
* await writer.write("# Hello\n");
|
|
764
653
|
* await writer.close();
|
|
@@ -766,6 +655,53 @@ export declare function createGFMStream(target: DOMNodeLike, dom: DOMDocumentLik
|
|
|
766
655
|
*
|
|
767
656
|
* @public
|
|
768
657
|
*/
|
|
769
|
-
export declare function createCommonMarkStream(
|
|
770
|
-
|
|
658
|
+
export declare function createCommonMarkStream(options: CommonMarkStreamOptions): MarkdownStream<CommonMarkRendererConfig>;
|
|
659
|
+
/**
|
|
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
|
+
*
|
|
670
|
+
* @public
|
|
671
|
+
*/
|
|
672
|
+
type RuntimeKind = "bun" | "deno" | "node" | "browser" | "unknown";
|
|
673
|
+
/**
|
|
674
|
+
* Detect the JavaScript runtime hosting this module.
|
|
675
|
+
*
|
|
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">`) // "<a href="x&y">"
|
|
701
|
+
* ```
|
|
702
|
+
*
|
|
703
|
+
* @public
|
|
704
|
+
*/
|
|
705
|
+
export declare function escapeHTML(text: string): string;
|
|
706
|
+
export type { CommonMarkRendererConfig, CommonMarkStreamOptions, DOMDocumentLike, DOMNodeLike, DOMTargetOptions, GFMRendererConfig, GFMStreamOptions, HTMLTarget, MarkdownRenderer, NodeId, RAF, RuntimeKind };
|
|
771
707
|
//# sourceMappingURL=index.d.cts.map
|