markemental 0.2.0 → 0.3.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 +29 -16
- package/dist/index.cjs +653 -394
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +388 -304
- package/dist/index.d.ts +388 -304
- package/dist/index.js +648 -383
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,69 +1,94 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Incremental markdown rendering pipeline.
|
|
3
3
|
*
|
|
4
|
-
* @
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
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
9
|
*
|
|
10
10
|
* @public
|
|
11
11
|
*/
|
|
12
|
-
interface
|
|
13
|
-
/** What kind of block this is. */
|
|
14
|
-
kind: BlockKind;
|
|
12
|
+
interface MarkdownPipeline {
|
|
15
13
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
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
19
|
*/
|
|
20
|
-
|
|
21
|
-
/** Heading level (1-6) for `"heading"` blocks. */
|
|
22
|
-
level?: number;
|
|
23
|
-
/** Fence info string for `"fence"` blocks, when present. */
|
|
24
|
-
language?: string;
|
|
25
|
-
/** Whether a `"list"` block is ordered. */
|
|
26
|
-
ordered?: boolean;
|
|
20
|
+
push(chunk: string | Promise<string>): Promise<void>;
|
|
27
21
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
22
|
+
* Render any buffered remainder and release pipeline resources.
|
|
23
|
+
*
|
|
24
|
+
* @returns A promise that resolves once all output has been flushed.
|
|
30
25
|
*/
|
|
31
|
-
|
|
26
|
+
flush(): Promise<void>;
|
|
32
27
|
}
|
|
33
28
|
|
|
34
29
|
/**
|
|
35
|
-
* Incremental markdown
|
|
30
|
+
* Incremental markdown renderer: consumes lines, updates a target.
|
|
36
31
|
*
|
|
37
32
|
* @remarks
|
|
38
|
-
* A
|
|
39
|
-
*
|
|
40
|
-
*
|
|
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.
|
|
38
|
+
*
|
|
39
|
+
* Implement this interface to plug in your own renderer, hand-written or a
|
|
40
|
+
* third-party adapter.
|
|
41
41
|
*
|
|
42
42
|
* @public
|
|
43
43
|
*/
|
|
44
|
-
interface
|
|
44
|
+
interface MarkdownRenderer {
|
|
45
45
|
/**
|
|
46
|
-
*
|
|
46
|
+
* Feed one complete line to the renderer.
|
|
47
47
|
*
|
|
48
|
-
* @param
|
|
49
|
-
*
|
|
50
|
-
* complete blocks have been rendered to the target.
|
|
48
|
+
* @param line - Next source line, without its line break. The renderer
|
|
49
|
+
* buffers what it needs and writes completed output to its target.
|
|
51
50
|
*/
|
|
52
|
-
|
|
51
|
+
writeLine(line: string): void;
|
|
53
52
|
/**
|
|
54
|
-
*
|
|
53
|
+
* Flush buffered partial state to the target and settle it.
|
|
55
54
|
*
|
|
56
|
-
* @returns A promise that resolves once all output has
|
|
55
|
+
* @returns A promise that resolves once all output has landed in the target.
|
|
57
56
|
*/
|
|
58
57
|
flush(): Promise<void>;
|
|
59
58
|
}
|
|
60
59
|
|
|
60
|
+
/**
|
|
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.
|
|
68
|
+
*
|
|
69
|
+
* @param renderer - Line-consuming renderer holding its own target and state.
|
|
70
|
+
* @returns A working pipeline.
|
|
71
|
+
*
|
|
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
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @public
|
|
82
|
+
*/
|
|
83
|
+
declare function createMarkdownPipeline(renderer: MarkdownRenderer): MarkdownPipeline;
|
|
84
|
+
|
|
61
85
|
/**
|
|
62
86
|
* Minimal subset of the DOM `Node` interface that markemental needs.
|
|
63
87
|
*
|
|
64
88
|
* @remarks
|
|
65
|
-
*
|
|
66
|
-
*
|
|
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.
|
|
67
92
|
*
|
|
68
93
|
* @public
|
|
69
94
|
*/
|
|
@@ -152,92 +177,285 @@ interface DOMDocumentLike {
|
|
|
152
177
|
type RAF = (callback: (time: number) => void) => number;
|
|
153
178
|
|
|
154
179
|
/**
|
|
155
|
-
*
|
|
180
|
+
* Destination for rendered output.
|
|
156
181
|
*
|
|
157
|
-
* @
|
|
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.
|
|
158
188
|
*
|
|
159
189
|
* @public
|
|
160
190
|
*/
|
|
161
|
-
interface
|
|
162
|
-
/** Renderer-specific options. */
|
|
163
|
-
readonly options: Opts;
|
|
191
|
+
interface RenderTarget {
|
|
164
192
|
/**
|
|
165
|
-
*
|
|
193
|
+
* Attach a rendered node to the target.
|
|
166
194
|
*
|
|
167
|
-
* @param
|
|
168
|
-
*
|
|
169
|
-
|
|
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.
|
|
170
203
|
*/
|
|
171
|
-
|
|
204
|
+
flush(): Promise<void>;
|
|
172
205
|
}
|
|
173
206
|
|
|
174
207
|
/**
|
|
175
|
-
*
|
|
208
|
+
* Leaf block constructs recognized by the MVP parser.
|
|
176
209
|
*
|
|
177
|
-
* @
|
|
210
|
+
* @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).
|
|
214
|
+
*
|
|
215
|
+
* @internal
|
|
216
|
+
*/
|
|
217
|
+
type BlockKind = "heading" | "paragraph" | "fence" | "quote" | "rule";
|
|
218
|
+
/**
|
|
219
|
+
* A parsed block of markdown lines.
|
|
220
|
+
*
|
|
221
|
+
* @internal
|
|
222
|
+
*/
|
|
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;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Structural piece of a document emitted as lines are parsed.
|
|
245
|
+
*
|
|
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.
|
|
252
|
+
*
|
|
253
|
+
* @internal
|
|
254
|
+
*/
|
|
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
|
+
};
|
|
276
|
+
/**
|
|
277
|
+
* Incremental line-driven parser for CommonMark structure.
|
|
278
|
+
*
|
|
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
|
|
178
288
|
*/
|
|
179
|
-
|
|
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;
|
|
180
305
|
/**
|
|
181
|
-
*
|
|
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.
|
|
182
319
|
*
|
|
183
|
-
* @param
|
|
320
|
+
* @param line - Next source line.
|
|
321
|
+
* @returns True when the line was consumed or reprocessed.
|
|
184
322
|
*/
|
|
185
|
-
|
|
323
|
+
protected _consumeLists(line: string): boolean;
|
|
186
324
|
/**
|
|
187
|
-
*
|
|
325
|
+
* Feed a line to the open quote, if any, dumping it back to pending when
|
|
326
|
+
* the line does not continue the quote.
|
|
188
327
|
*
|
|
189
|
-
* @
|
|
328
|
+
* @param line - Next source line.
|
|
329
|
+
* @returns True when the line was consumed.
|
|
190
330
|
*/
|
|
191
|
-
|
|
331
|
+
protected _consumeQuote(line: string): boolean;
|
|
192
332
|
/**
|
|
193
|
-
*
|
|
333
|
+
* Classify a line that continues no open context.
|
|
194
334
|
*
|
|
195
|
-
* @
|
|
335
|
+
* @param line - Next source line.
|
|
196
336
|
*/
|
|
197
|
-
|
|
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;
|
|
198
351
|
}
|
|
199
|
-
|
|
200
352
|
/**
|
|
201
|
-
*
|
|
353
|
+
* Incremental line-driven parser for GFM structure.
|
|
202
354
|
*
|
|
203
355
|
* @remarks
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
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.
|
|
207
359
|
*
|
|
208
|
-
* @
|
|
360
|
+
* @internal
|
|
209
361
|
*/
|
|
210
|
-
|
|
362
|
+
declare class GFMState extends CommonMarkState {
|
|
363
|
+
private _tableOpen;
|
|
364
|
+
private _candidate;
|
|
211
365
|
/**
|
|
212
|
-
*
|
|
366
|
+
* Feed one source line; completed fragments emit to the handler.
|
|
213
367
|
*
|
|
214
|
-
* @param
|
|
215
|
-
* the next animation frame); use {@link RenderTarget.flush} to wait for it.
|
|
368
|
+
* @param line - Next source line, without its line break.
|
|
216
369
|
*/
|
|
217
|
-
|
|
370
|
+
addLine(line: string): void;
|
|
218
371
|
/**
|
|
219
|
-
*
|
|
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.
|
|
220
378
|
*
|
|
221
|
-
* @
|
|
379
|
+
* @param line - Next source line.
|
|
380
|
+
* @returns True when the line was consumed or reprocessed.
|
|
222
381
|
*/
|
|
223
|
-
|
|
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;
|
|
224
397
|
}
|
|
225
398
|
|
|
226
399
|
/**
|
|
227
400
|
* Renderer for CommonMark.
|
|
228
401
|
*
|
|
402
|
+
* @remarks
|
|
403
|
+
* Constructed with its target, document, and options. Feed it lines with
|
|
404
|
+
* {@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.
|
|
409
|
+
*
|
|
229
410
|
* @public
|
|
230
411
|
*/
|
|
231
|
-
declare class CommonMarkRenderer implements MarkdownRenderer
|
|
412
|
+
declare class CommonMarkRenderer implements MarkdownRenderer {
|
|
232
413
|
/** Rendering options. */
|
|
233
414
|
readonly options: CommonMarkRenderOptions;
|
|
234
|
-
|
|
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.
|
|
235
424
|
* @param opts - CommonMark rendering options.
|
|
236
425
|
*/
|
|
237
|
-
constructor(opts: CommonMarkRenderOptions);
|
|
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;
|
|
238
456
|
/**
|
|
239
457
|
* Whether GFM-only inlines are enabled. Always false here; overridden by
|
|
240
|
-
*
|
|
458
|
+
* `GFMRenderer`.
|
|
241
459
|
*
|
|
242
460
|
* @returns False.
|
|
243
461
|
*/
|
|
@@ -249,7 +467,7 @@ declare class CommonMarkRenderer implements MarkdownRenderer<CommonMarkRenderOpt
|
|
|
249
467
|
* @param doc - Document used to create nodes.
|
|
250
468
|
* @returns The rendered node, or `null` to emit nothing.
|
|
251
469
|
*/
|
|
252
|
-
renderBlock(block: Block, doc: DOMDocumentLike): DOMNodeLike | null;
|
|
470
|
+
protected renderBlock(block: Block, doc: DOMDocumentLike): DOMNodeLike | null;
|
|
253
471
|
/**
|
|
254
472
|
* Render paragraph lines, honoring two-space hard breaks.
|
|
255
473
|
*
|
|
@@ -260,28 +478,59 @@ declare class CommonMarkRenderer implements MarkdownRenderer<CommonMarkRenderOpt
|
|
|
260
478
|
protected renderParagraph(lines: string[], doc: DOMDocumentLike): DOMNodeLike;
|
|
261
479
|
private appendLinesInline;
|
|
262
480
|
}
|
|
481
|
+
/**
|
|
482
|
+
* Sanitizes one node committed to the target.
|
|
483
|
+
*
|
|
484
|
+
* @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;
|
|
263
494
|
/**
|
|
264
495
|
* Options for {@link CommonMarkRenderer}.
|
|
265
496
|
*
|
|
266
497
|
* @public
|
|
267
498
|
*/
|
|
268
|
-
type CommonMarkRenderOptions = {
|
|
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
|
+
};
|
|
269
510
|
|
|
270
511
|
/**
|
|
271
512
|
* Renderer for GitHub Flavored Markdown.
|
|
272
513
|
*
|
|
273
514
|
* @remarks
|
|
274
515
|
* 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.
|
|
275
519
|
*
|
|
276
520
|
* @public
|
|
277
521
|
*/
|
|
278
|
-
declare class GFMRenderer extends CommonMarkRenderer implements MarkdownRenderer
|
|
522
|
+
declare class GFMRenderer extends CommonMarkRenderer implements MarkdownRenderer {
|
|
279
523
|
/** Rendering options. */
|
|
280
524
|
readonly options: GFMRenderOptions;
|
|
525
|
+
private _table;
|
|
281
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.
|
|
282
531
|
* @param opts - GFM rendering options.
|
|
283
532
|
*/
|
|
284
|
-
constructor(opts: GFMRenderOptions);
|
|
533
|
+
constructor(target: RenderTarget, doc: DOMDocumentLike, opts: GFMRenderOptions);
|
|
285
534
|
/**
|
|
286
535
|
* GFM-only inlines are enabled.
|
|
287
536
|
*
|
|
@@ -289,25 +538,52 @@ declare class GFMRenderer extends CommonMarkRenderer implements MarkdownRenderer
|
|
|
289
538
|
*/
|
|
290
539
|
protected inlineGfm(): boolean;
|
|
291
540
|
/**
|
|
292
|
-
*
|
|
541
|
+
* Create the GFM parser state.
|
|
293
542
|
*
|
|
294
|
-
* @param
|
|
295
|
-
* @
|
|
296
|
-
|
|
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.
|
|
297
568
|
*/
|
|
298
|
-
|
|
569
|
+
protected _applyTableEnd(): void;
|
|
299
570
|
}
|
|
300
571
|
/**
|
|
301
572
|
* Options for {@link GFMRenderer}.
|
|
302
573
|
*
|
|
303
574
|
* @public
|
|
304
575
|
*/
|
|
305
|
-
type GFMRenderOptions =
|
|
576
|
+
type GFMRenderOptions = CommonMarkRenderOptions;
|
|
306
577
|
|
|
307
578
|
/**
|
|
308
|
-
* Create a GFM pipeline
|
|
579
|
+
* Create a GFM pipeline rendering into the given container.
|
|
309
580
|
*
|
|
310
|
-
* @
|
|
581
|
+
* @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.
|
|
585
|
+
*
|
|
586
|
+
* @param target - Container node receiving rendered output.
|
|
311
587
|
* @param dom - Document used to create nodes.
|
|
312
588
|
* @param opts - GFM rendering options.
|
|
313
589
|
* @param raf - Optional frame scheduler. When provided, DOM writes are
|
|
@@ -316,7 +592,7 @@ type GFMRenderOptions = {};
|
|
|
316
592
|
*
|
|
317
593
|
* @example
|
|
318
594
|
* ```ts
|
|
319
|
-
* const pipeline =
|
|
595
|
+
* const pipeline = createGFMPipeline(
|
|
320
596
|
* document.getElementById("out"),
|
|
321
597
|
* document,
|
|
322
598
|
* {},
|
|
@@ -328,16 +604,16 @@ type GFMRenderOptions = {};
|
|
|
328
604
|
*
|
|
329
605
|
* @public
|
|
330
606
|
*/
|
|
331
|
-
declare function
|
|
607
|
+
declare function createGFMPipeline(target: DOMNodeLike, dom: DOMDocumentLike, opts: GFMRenderOptions, raf?: RAF): MarkdownPipeline;
|
|
332
608
|
/**
|
|
333
|
-
* Incremental GFM pipeline
|
|
609
|
+
* Incremental GFM pipeline.
|
|
334
610
|
*
|
|
335
611
|
* @public
|
|
336
612
|
*/
|
|
337
|
-
declare class
|
|
613
|
+
declare class GFMPipeline implements MarkdownPipeline {
|
|
338
614
|
private _engine;
|
|
339
615
|
/**
|
|
340
|
-
* @param target - Container node receiving rendered
|
|
616
|
+
* @param target - Container node receiving rendered output.
|
|
341
617
|
* @param dom - Document used to create nodes.
|
|
342
618
|
* @param opts - GFM rendering options.
|
|
343
619
|
* @param raf - Optional frame scheduler for batched DOM writes.
|
|
@@ -359,9 +635,14 @@ declare class FrontendGFMPipeline implements MarkdownPipeline {
|
|
|
359
635
|
}
|
|
360
636
|
|
|
361
637
|
/**
|
|
362
|
-
* Create a CommonMark pipeline
|
|
638
|
+
* Create a CommonMark pipeline rendering into the given container.
|
|
363
639
|
*
|
|
364
|
-
* @
|
|
640
|
+
* @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.
|
|
644
|
+
*
|
|
645
|
+
* @param target - Container node receiving rendered output.
|
|
365
646
|
* @param dom - Document used to create nodes.
|
|
366
647
|
* @param opts - CommonMark rendering options.
|
|
367
648
|
* @param raf - Optional frame scheduler. When provided, DOM writes are
|
|
@@ -370,7 +651,7 @@ declare class FrontendGFMPipeline implements MarkdownPipeline {
|
|
|
370
651
|
*
|
|
371
652
|
* @example
|
|
372
653
|
* ```ts
|
|
373
|
-
* const pipeline =
|
|
654
|
+
* const pipeline = createCommonMarkPipeline(
|
|
374
655
|
* document.getElementById("out"),
|
|
375
656
|
* document,
|
|
376
657
|
* {},
|
|
@@ -382,16 +663,16 @@ declare class FrontendGFMPipeline implements MarkdownPipeline {
|
|
|
382
663
|
*
|
|
383
664
|
* @public
|
|
384
665
|
*/
|
|
385
|
-
declare function
|
|
666
|
+
declare function createCommonMarkPipeline(target: DOMNodeLike, dom: DOMDocumentLike, opts: CommonMarkRenderOptions, raf?: RAF): MarkdownPipeline;
|
|
386
667
|
/**
|
|
387
|
-
* Incremental CommonMark pipeline
|
|
668
|
+
* Incremental CommonMark pipeline.
|
|
388
669
|
*
|
|
389
670
|
* @public
|
|
390
671
|
*/
|
|
391
|
-
declare class
|
|
672
|
+
declare class CommonMarkPipeline implements MarkdownPipeline {
|
|
392
673
|
private _engine;
|
|
393
674
|
/**
|
|
394
|
-
* @param target - Container node receiving rendered
|
|
675
|
+
* @param target - Container node receiving rendered output.
|
|
395
676
|
* @param dom - Document used to create nodes.
|
|
396
677
|
* @param opts - CommonMark rendering options.
|
|
397
678
|
* @param raf - Optional frame scheduler for batched DOM writes.
|
|
@@ -413,212 +694,15 @@ declare class FrontendCommonMarkPipeline implements MarkdownPipeline {
|
|
|
413
694
|
}
|
|
414
695
|
|
|
415
696
|
/**
|
|
416
|
-
* Create
|
|
417
|
-
*
|
|
418
|
-
* @remarks
|
|
419
|
-
* Backend pipelines write synchronously and never schedule animation frames.
|
|
420
|
-
* Pair with a lightweight `DOMDocumentLike` such as linkedom.
|
|
421
|
-
*
|
|
422
|
-
* @param target - Container node receiving rendered blocks.
|
|
423
|
-
* @param dom - Document used to create nodes.
|
|
424
|
-
* @param opts - GFM rendering options.
|
|
425
|
-
* @returns The configured pipeline.
|
|
426
|
-
*
|
|
427
|
-
* @example
|
|
428
|
-
* ```ts
|
|
429
|
-
* import { parseHTML } from "linkedom";
|
|
430
|
-
*
|
|
431
|
-
* const { document } = parseHTML("<div></div>");
|
|
432
|
-
* const pipeline = createBackendGFMPipeline(document.querySelector("div"), document, {});
|
|
433
|
-
* await pipeline.push("# Hello\n");
|
|
434
|
-
* await pipeline.flush();
|
|
435
|
-
* ```
|
|
436
|
-
*
|
|
437
|
-
* @public
|
|
438
|
-
*/
|
|
439
|
-
declare function createBackendGFMPipeline(target: DOMNodeLike, dom: DOMDocumentLike, opts: GFMRenderOptions): MarkdownPipeline;
|
|
440
|
-
/**
|
|
441
|
-
* Incremental GFM pipeline for server-side targets.
|
|
442
|
-
*
|
|
443
|
-
* @public
|
|
444
|
-
*/
|
|
445
|
-
declare class BackendGFMPipeline implements MarkdownPipeline {
|
|
446
|
-
private _engine;
|
|
447
|
-
/**
|
|
448
|
-
* @param target - Container node receiving rendered blocks.
|
|
449
|
-
* @param dom - Document used to create nodes.
|
|
450
|
-
* @param opts - GFM rendering options.
|
|
451
|
-
*/
|
|
452
|
-
constructor(target: DOMNodeLike, dom: DOMDocumentLike, opts: GFMRenderOptions);
|
|
453
|
-
/**
|
|
454
|
-
* Push a chunk of markdown into the pipeline.
|
|
455
|
-
*
|
|
456
|
-
* @param chunk - Markdown text, or a promise resolving to markdown text.
|
|
457
|
-
* @returns A promise that resolves once the chunk has been consumed.
|
|
458
|
-
*/
|
|
459
|
-
push(chunk: string | Promise<string>): Promise<void>;
|
|
460
|
-
/**
|
|
461
|
-
* Render any buffered remainder.
|
|
462
|
-
*
|
|
463
|
-
* @returns A promise that resolves once all output has been flushed.
|
|
464
|
-
*/
|
|
465
|
-
flush(): Promise<void>;
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
/**
|
|
469
|
-
* Create a CommonMark pipeline for server-side rendering.
|
|
470
|
-
*
|
|
471
|
-
* @remarks
|
|
472
|
-
* Backend pipelines write synchronously and never schedule animation frames.
|
|
473
|
-
* Pair with a lightweight `DOMDocumentLike` such as linkedom.
|
|
474
|
-
*
|
|
475
|
-
* @param target - Container node receiving rendered blocks.
|
|
476
|
-
* @param dom - Document used to create nodes.
|
|
477
|
-
* @param opts - CommonMark rendering options.
|
|
478
|
-
* @returns The configured pipeline.
|
|
697
|
+
* Create the render target for a container node.
|
|
479
698
|
*
|
|
480
|
-
* @
|
|
481
|
-
*
|
|
482
|
-
*
|
|
483
|
-
*
|
|
484
|
-
* const { document } = parseHTML("<div></div>");
|
|
485
|
-
* const pipeline = createBackendCommonMarkPipeline(document.querySelector("div"), document, {});
|
|
486
|
-
* await pipeline.push("# Hello\n");
|
|
487
|
-
* await pipeline.flush();
|
|
488
|
-
* ```
|
|
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.
|
|
489
703
|
*
|
|
490
704
|
* @public
|
|
491
705
|
*/
|
|
492
|
-
declare function
|
|
493
|
-
/**
|
|
494
|
-
* Incremental CommonMark pipeline for server-side targets.
|
|
495
|
-
*
|
|
496
|
-
* @public
|
|
497
|
-
*/
|
|
498
|
-
declare class BackendCommonMarkPipeline implements MarkdownPipeline {
|
|
499
|
-
private _engine;
|
|
500
|
-
/**
|
|
501
|
-
* @param target - Container node receiving rendered blocks.
|
|
502
|
-
* @param dom - Document used to create nodes.
|
|
503
|
-
* @param opts - CommonMark rendering options.
|
|
504
|
-
*/
|
|
505
|
-
constructor(target: DOMNodeLike, dom: DOMDocumentLike, opts: CommonMarkRenderOptions);
|
|
506
|
-
/**
|
|
507
|
-
* Push a chunk of markdown into the pipeline.
|
|
508
|
-
*
|
|
509
|
-
* @param chunk - Markdown text, or a promise resolving to markdown text.
|
|
510
|
-
* @returns A promise that resolves once the chunk has been consumed.
|
|
511
|
-
*/
|
|
512
|
-
push(chunk: string | Promise<string>): Promise<void>;
|
|
513
|
-
/**
|
|
514
|
-
* Render any buffered remainder.
|
|
515
|
-
*
|
|
516
|
-
* @returns A promise that resolves once all output has been flushed.
|
|
517
|
-
*/
|
|
518
|
-
flush(): Promise<void>;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
/**
|
|
522
|
-
* Mutable parse state carried across `push` calls.
|
|
523
|
-
*
|
|
524
|
-
* @remarks
|
|
525
|
-
* Holds buffered lines for the currently open block so a pipeline can render
|
|
526
|
-
* complete blocks incrementally while holding back a trailing partial block.
|
|
527
|
-
*
|
|
528
|
-
* @public
|
|
529
|
-
*/
|
|
530
|
-
declare class RendererState {
|
|
531
|
-
private _pending;
|
|
532
|
-
/**
|
|
533
|
-
* Buffer newly arrived lines.
|
|
534
|
-
*
|
|
535
|
-
* @param lines - Complete source lines to buffer.
|
|
536
|
-
*/
|
|
537
|
-
addLines(lines: string[]): void;
|
|
538
|
-
/**
|
|
539
|
-
* Parse buffered lines, returning every block except a trailing partial one.
|
|
540
|
-
*
|
|
541
|
-
* @remarks
|
|
542
|
-
* The last block is held back because more input may still extend it.
|
|
543
|
-
* Nothing is returned while the buffer ends inside an unclosed fence.
|
|
544
|
-
*
|
|
545
|
-
* @returns Complete blocks ready to render.
|
|
546
|
-
*/
|
|
547
|
-
takeCompleteBlocks(): Block[];
|
|
548
|
-
/**
|
|
549
|
-
* Parse and drain all buffered lines, including a trailing partial block.
|
|
550
|
-
*
|
|
551
|
-
* @returns Every remaining block.
|
|
552
|
-
*/
|
|
553
|
-
takeAllBlocks(): Block[];
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
/**
|
|
557
|
-
* Create a line source that splits pushed text into lines.
|
|
558
|
-
*
|
|
559
|
-
* @returns A new line source.
|
|
560
|
-
*
|
|
561
|
-
* @public
|
|
562
|
-
*/
|
|
563
|
-
declare function createLineSource(): LineSource;
|
|
564
|
-
|
|
565
|
-
/**
|
|
566
|
-
* Target that appends rendered nodes synchronously.
|
|
567
|
-
*
|
|
568
|
-
* @public
|
|
569
|
-
*/
|
|
570
|
-
declare class DOMTarget implements RenderTarget {
|
|
571
|
-
private _targetNode;
|
|
572
|
-
/**
|
|
573
|
-
* @param targetNode - Container node receiving rendered output.
|
|
574
|
-
*/
|
|
575
|
-
constructor(targetNode: DOMNodeLike);
|
|
576
|
-
/**
|
|
577
|
-
* Append a rendered node immediately.
|
|
578
|
-
*
|
|
579
|
-
* @param node - Node to attach.
|
|
580
|
-
*/
|
|
581
|
-
append(node: DOMNodeLike): void;
|
|
582
|
-
/**
|
|
583
|
-
* Resolve immediately: synchronous targets hold every node already.
|
|
584
|
-
*
|
|
585
|
-
* @returns A resolved promise.
|
|
586
|
-
*/
|
|
587
|
-
flush(): Promise<void>;
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
/**
|
|
591
|
-
* Target that batches DOM writes on animation frames.
|
|
592
|
-
*
|
|
593
|
-
* @remarks
|
|
594
|
-
* Uses requestAnimationFrame on the real DOM so streamed output lands once
|
|
595
|
-
* per frame instead of once per chunk.
|
|
596
|
-
*
|
|
597
|
-
* @public
|
|
598
|
-
*/
|
|
599
|
-
declare class AnimatedDOMTarget implements RenderTarget {
|
|
600
|
-
private _targetNode;
|
|
601
|
-
private _raf;
|
|
602
|
-
private _queue;
|
|
603
|
-
private _scheduled;
|
|
604
|
-
private _flushResolvers;
|
|
605
|
-
/**
|
|
606
|
-
* @param targetNode - Container node receiving rendered output.
|
|
607
|
-
* @param raf - Frame scheduler used to batch writes.
|
|
608
|
-
*/
|
|
609
|
-
constructor(targetNode: DOMNodeLike, raf: RAF);
|
|
610
|
-
/**
|
|
611
|
-
* Queue a rendered node for the next frame.
|
|
612
|
-
*
|
|
613
|
-
* @param node - Node to attach.
|
|
614
|
-
*/
|
|
615
|
-
append(node: DOMNodeLike): void;
|
|
616
|
-
/**
|
|
617
|
-
* Wait until every queued node has been attached.
|
|
618
|
-
*
|
|
619
|
-
* @returns A promise that resolves after the next frame carrying nodes.
|
|
620
|
-
*/
|
|
621
|
-
flush(): Promise<void>;
|
|
622
|
-
}
|
|
706
|
+
declare function createRenderTarget(targetNode: DOMNodeLike, raf?: RAF): RenderTarget;
|
|
623
707
|
|
|
624
|
-
export {
|
|
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 };
|