markemental 0.0.0-stage → 0.2.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.
@@ -0,0 +1,624 @@
1
+ /**
2
+ * Block-level markdown constructs recognized by the MVP parser.
3
+ *
4
+ * @public
5
+ */
6
+ type BlockKind = "heading" | "paragraph" | "fence" | "list" | "quote" | "rule" | "table";
7
+ /**
8
+ * A parsed block of markdown lines.
9
+ *
10
+ * @public
11
+ */
12
+ interface Block {
13
+ /** What kind of block this is. */
14
+ kind: BlockKind;
15
+ /**
16
+ * Block content lines. Markers are stripped: `#` prefixes for headings,
17
+ * list markers for lists, `>` prefixes for quotes, and fence lines for
18
+ * fenced code. Paragraph lines are raw.
19
+ */
20
+ lines: string[];
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;
27
+ /**
28
+ * Number of source lines this block was parsed from, used to hold back a
29
+ * trailing partial block across `push` calls.
30
+ */
31
+ sourceLineCount: number;
32
+ }
33
+
34
+ /**
35
+ * Incremental markdown rendering pipeline.
36
+ *
37
+ * @remarks
38
+ * A pipeline accepts markdown in arbitrarily-sized chunks and renders complete
39
+ * blocks to its target as they arrive. A trailing partial block is held back
40
+ * until more input completes it or {@link MarkdownPipeline.flush} is called.
41
+ *
42
+ * @public
43
+ */
44
+ interface MarkdownPipeline {
45
+ /**
46
+ * Push a chunk of markdown into the pipeline.
47
+ *
48
+ * @param chunk - Markdown text, or a promise resolving to markdown text.
49
+ * @returns A promise that resolves once the chunk has been consumed and any
50
+ * complete blocks have been rendered to the target.
51
+ */
52
+ push(chunk: string | Promise<string>): Promise<void>;
53
+ /**
54
+ * Render any buffered remainder and release pipeline resources.
55
+ *
56
+ * @returns A promise that resolves once all output has been flushed.
57
+ */
58
+ flush(): Promise<void>;
59
+ }
60
+
61
+ /**
62
+ * Minimal subset of the DOM `Node` interface that markemental needs.
63
+ *
64
+ * @remarks
65
+ * Both real DOM nodes and lightweight server-side implementations (for
66
+ * example linkedom) satisfy this interface.
67
+ *
68
+ * @public
69
+ */
70
+ interface DOMNodeLike {
71
+ /** Numeric node type, mirroring `Node.nodeType`. */
72
+ nodeType: number;
73
+ /** Text content of this node, mirroring `Node.textContent`. */
74
+ textContent: string | null;
75
+ /** Child nodes, mirroring `Node.childNodes`. */
76
+ childNodes: ArrayLike<DOMNodeLike>;
77
+ /** Owning document, or `null` for detached nodes. */
78
+ ownerDocument: DOMDocumentLike | null;
79
+ /**
80
+ * Append a child node.
81
+ *
82
+ * @param node - Node to append.
83
+ * @returns The appended node.
84
+ */
85
+ appendChild(node: DOMNodeLike): DOMNodeLike;
86
+ /**
87
+ * Insert a node before a reference child.
88
+ *
89
+ * @param newNode - Node to insert.
90
+ * @param referenceNode - Child to insert before, or `null` to append.
91
+ * @returns The inserted node.
92
+ */
93
+ insertBefore(newNode: DOMNodeLike, referenceNode: DOMNodeLike | null): DOMNodeLike;
94
+ /**
95
+ * Remove a child node.
96
+ *
97
+ * @param child - Child to remove.
98
+ * @returns The removed node.
99
+ */
100
+ removeChild(child: DOMNodeLike): DOMNodeLike;
101
+ /**
102
+ * Replace a child node.
103
+ *
104
+ * @param newChild - Replacement node.
105
+ * @param oldChild - Child to replace.
106
+ * @returns The replaced node.
107
+ */
108
+ replaceChild(newChild: DOMNodeLike, oldChild: DOMNodeLike): DOMNodeLike;
109
+ /**
110
+ * Set an attribute on this node, when supported.
111
+ *
112
+ * @param name - Attribute name.
113
+ * @param value - Attribute value.
114
+ */
115
+ setAttribute?(name: string, value: string): void;
116
+ /**
117
+ * Remove an attribute from this node, when supported.
118
+ *
119
+ * @param name - Attribute name.
120
+ */
121
+ removeAttribute?(name: string): void;
122
+ }
123
+ /**
124
+ * Minimal subset of the DOM `Document` interface that markemental needs.
125
+ *
126
+ * @public
127
+ */
128
+ interface DOMDocumentLike {
129
+ /**
130
+ * Create an element node.
131
+ *
132
+ * @param tagName - Lowercase tag name (for example `"p"`).
133
+ * @returns The new element.
134
+ */
135
+ createElement(tagName: string): DOMNodeLike;
136
+ /**
137
+ * Create a text node.
138
+ *
139
+ * @param data - Text content.
140
+ * @returns The new text node.
141
+ */
142
+ createTextNode(data: string): DOMNodeLike;
143
+ }
144
+ /**
145
+ * `requestAnimationFrame` callback scheduler.
146
+ *
147
+ * @param callback - Invoked with the frame timestamp.
148
+ * @returns A handle identifying the scheduled frame.
149
+ *
150
+ * @public
151
+ */
152
+ type RAF = (callback: (time: number) => void) => number;
153
+
154
+ /**
155
+ * Renders parsed markdown to DOM-like nodes.
156
+ *
157
+ * @typeParam Opts - Renderer-specific options.
158
+ *
159
+ * @public
160
+ */
161
+ interface MarkdownRenderer<Opts> {
162
+ /** Renderer-specific options. */
163
+ readonly options: Opts;
164
+ /**
165
+ * Render one block to a DOM-like node.
166
+ *
167
+ * @param block - Parsed block to render.
168
+ * @param doc - Document used to create nodes.
169
+ * @returns The rendered node, or `null` to emit nothing.
170
+ */
171
+ renderBlock(block: Block, doc: DOMDocumentLike): DOMNodeLike | null;
172
+ }
173
+
174
+ /**
175
+ * Buffers pushed text and emits complete lines.
176
+ *
177
+ * @public
178
+ */
179
+ interface LineSource {
180
+ /**
181
+ * Push text into the buffer.
182
+ *
183
+ * @param chunk - Raw markdown text, with or without line breaks.
184
+ */
185
+ push(chunk: string): void;
186
+ /**
187
+ * Drain newline-terminated lines, holding back any trailing partial line.
188
+ *
189
+ * @returns Complete lines without line breaks.
190
+ */
191
+ readLines(): string[];
192
+ /**
193
+ * Close the source and drain the trailing partial line, if any.
194
+ *
195
+ * @returns Zero or one remaining line.
196
+ */
197
+ close(): string[];
198
+ }
199
+
200
+ /**
201
+ * Destination for rendered output.
202
+ *
203
+ * @remarks
204
+ * A target receives rendered nodes from a pipeline and attaches them to the
205
+ * host document. Targets decide when attachment happens: immediately, or
206
+ * batched on animation frames for smooth streaming into a live page.
207
+ *
208
+ * @public
209
+ */
210
+ interface RenderTarget {
211
+ /**
212
+ * Attach a rendered node to the target.
213
+ *
214
+ * @param node - Node to attach. Attachment may be deferred (for example to
215
+ * the next animation frame); use {@link RenderTarget.flush} to wait for it.
216
+ */
217
+ append(node: DOMNodeLike): void;
218
+ /**
219
+ * Wait for all appended nodes to be attached.
220
+ *
221
+ * @returns A promise that resolves once the target holds every node.
222
+ */
223
+ flush(): Promise<void>;
224
+ }
225
+
226
+ /**
227
+ * Renderer for CommonMark.
228
+ *
229
+ * @public
230
+ */
231
+ declare class CommonMarkRenderer implements MarkdownRenderer<CommonMarkRenderOptions> {
232
+ /** Rendering options. */
233
+ readonly options: CommonMarkRenderOptions;
234
+ /**
235
+ * @param opts - CommonMark rendering options.
236
+ */
237
+ constructor(opts: CommonMarkRenderOptions);
238
+ /**
239
+ * Whether GFM-only inlines are enabled. Always false here; overridden by
240
+ * {@link GFMRenderer}.
241
+ *
242
+ * @returns False.
243
+ */
244
+ protected inlineGfm(): boolean;
245
+ /**
246
+ * Render one block to a DOM-like node.
247
+ *
248
+ * @param block - Parsed block to render.
249
+ * @param doc - Document used to create nodes.
250
+ * @returns The rendered node, or `null` to emit nothing.
251
+ */
252
+ renderBlock(block: Block, doc: DOMDocumentLike): DOMNodeLike | null;
253
+ /**
254
+ * Render paragraph lines, honoring two-space hard breaks.
255
+ *
256
+ * @param lines - Raw paragraph lines.
257
+ * @param doc - Document used to create nodes.
258
+ * @returns The paragraph element.
259
+ */
260
+ protected renderParagraph(lines: string[], doc: DOMDocumentLike): DOMNodeLike;
261
+ private appendLinesInline;
262
+ }
263
+ /**
264
+ * Options for {@link CommonMarkRenderer}.
265
+ *
266
+ * @public
267
+ */
268
+ type CommonMarkRenderOptions = {};
269
+
270
+ /**
271
+ * Renderer for GitHub Flavored Markdown.
272
+ *
273
+ * @remarks
274
+ * Supports CommonMark blocks and inlines plus GFM tables and strikethrough.
275
+ *
276
+ * @public
277
+ */
278
+ declare class GFMRenderer extends CommonMarkRenderer implements MarkdownRenderer<GFMRenderOptions> {
279
+ /** Rendering options. */
280
+ readonly options: GFMRenderOptions;
281
+ /**
282
+ * @param opts - GFM rendering options.
283
+ */
284
+ constructor(opts: GFMRenderOptions);
285
+ /**
286
+ * GFM-only inlines are enabled.
287
+ *
288
+ * @returns True.
289
+ */
290
+ protected inlineGfm(): boolean;
291
+ /**
292
+ * Render one block to a DOM-like node, handling GFM tables.
293
+ *
294
+ * @param block - Parsed block to render.
295
+ * @param doc - Document used to create nodes.
296
+ * @returns The rendered node, or `null` to emit nothing.
297
+ */
298
+ renderBlock(block: Block, doc: DOMDocumentLike): DOMNodeLike | null;
299
+ }
300
+ /**
301
+ * Options for {@link GFMRenderer}.
302
+ *
303
+ * @public
304
+ */
305
+ type GFMRenderOptions = {};
306
+
307
+ /**
308
+ * Create a GFM pipeline that renders into a live page.
309
+ *
310
+ * @param target - Container node receiving rendered blocks.
311
+ * @param dom - Document used to create nodes.
312
+ * @param opts - GFM rendering options.
313
+ * @param raf - Optional frame scheduler. When provided, DOM writes are
314
+ * batched on animation frames for smooth streaming.
315
+ * @returns The configured pipeline.
316
+ *
317
+ * @example
318
+ * ```ts
319
+ * const pipeline = createFrontendGFMPipeline(
320
+ * document.getElementById("out"),
321
+ * document,
322
+ * {},
323
+ * requestAnimationFrame,
324
+ * );
325
+ * await pipeline.push("# Hello\n");
326
+ * await pipeline.flush();
327
+ * ```
328
+ *
329
+ * @public
330
+ */
331
+ declare function createFrontendGFMPipeline(target: DOMNodeLike, dom: DOMDocumentLike, opts: GFMRenderOptions, raf?: RAF): MarkdownPipeline;
332
+ /**
333
+ * Incremental GFM pipeline for browser targets.
334
+ *
335
+ * @public
336
+ */
337
+ declare class FrontendGFMPipeline implements MarkdownPipeline {
338
+ private _engine;
339
+ /**
340
+ * @param target - Container node receiving rendered blocks.
341
+ * @param dom - Document used to create nodes.
342
+ * @param opts - GFM rendering options.
343
+ * @param raf - Optional frame scheduler for batched DOM writes.
344
+ */
345
+ constructor(target: DOMNodeLike, dom: DOMDocumentLike, opts: GFMRenderOptions, raf?: RAF);
346
+ /**
347
+ * Push a chunk of markdown into the pipeline.
348
+ *
349
+ * @param chunk - Markdown text, or a promise resolving to markdown text.
350
+ * @returns A promise that resolves once the chunk has been consumed.
351
+ */
352
+ push(chunk: string | Promise<string>): Promise<void>;
353
+ /**
354
+ * Render any buffered remainder.
355
+ *
356
+ * @returns A promise that resolves once all output has been flushed.
357
+ */
358
+ flush(): Promise<void>;
359
+ }
360
+
361
+ /**
362
+ * Create a CommonMark pipeline that renders into a live page.
363
+ *
364
+ * @param target - Container node receiving rendered blocks.
365
+ * @param dom - Document used to create nodes.
366
+ * @param opts - CommonMark rendering options.
367
+ * @param raf - Optional frame scheduler. When provided, DOM writes are
368
+ * batched on animation frames for smooth streaming.
369
+ * @returns The configured pipeline.
370
+ *
371
+ * @example
372
+ * ```ts
373
+ * const pipeline = createFrontendCommonMarkPipeline(
374
+ * document.getElementById("out"),
375
+ * document,
376
+ * {},
377
+ * requestAnimationFrame,
378
+ * );
379
+ * await pipeline.push("# Hello\n");
380
+ * await pipeline.flush();
381
+ * ```
382
+ *
383
+ * @public
384
+ */
385
+ declare function createFrontendCommonMarkPipeline(target: DOMNodeLike, dom: DOMDocumentLike, opts: CommonMarkRenderOptions, raf?: RAF): MarkdownPipeline;
386
+ /**
387
+ * Incremental CommonMark pipeline for browser targets.
388
+ *
389
+ * @public
390
+ */
391
+ declare class FrontendCommonMarkPipeline implements MarkdownPipeline {
392
+ private _engine;
393
+ /**
394
+ * @param target - Container node receiving rendered blocks.
395
+ * @param dom - Document used to create nodes.
396
+ * @param opts - CommonMark rendering options.
397
+ * @param raf - Optional frame scheduler for batched DOM writes.
398
+ */
399
+ constructor(target: DOMNodeLike, dom: DOMDocumentLike, opts: CommonMarkRenderOptions, raf?: RAF);
400
+ /**
401
+ * Push a chunk of markdown into the pipeline.
402
+ *
403
+ * @param chunk - Markdown text, or a promise resolving to markdown text.
404
+ * @returns A promise that resolves once the chunk has been consumed.
405
+ */
406
+ push(chunk: string | Promise<string>): Promise<void>;
407
+ /**
408
+ * Render any buffered remainder.
409
+ *
410
+ * @returns A promise that resolves once all output has been flushed.
411
+ */
412
+ flush(): Promise<void>;
413
+ }
414
+
415
+ /**
416
+ * Create a GFM pipeline for server-side rendering.
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.
479
+ *
480
+ * @example
481
+ * ```ts
482
+ * import { parseHTML } from "linkedom";
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
+ * ```
489
+ *
490
+ * @public
491
+ */
492
+ declare function createBackendCommonMarkPipeline(target: DOMNodeLike, dom: DOMDocumentLike, opts: CommonMarkRenderOptions): MarkdownPipeline;
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
+ }
623
+
624
+ export { AnimatedDOMTarget, BackendCommonMarkPipeline, BackendGFMPipeline, type Block, type BlockKind, type CommonMarkRenderOptions, CommonMarkRenderer, type DOMDocumentLike, type DOMNodeLike, DOMTarget, FrontendCommonMarkPipeline, FrontendGFMPipeline, type GFMRenderOptions, GFMRenderer, type LineSource, type MarkdownPipeline, type MarkdownRenderer, type RAF, type RenderTarget, RendererState, createBackendCommonMarkPipeline, createBackendGFMPipeline, createFrontendCommonMarkPipeline, createFrontendGFMPipeline, createLineSource };