markemental 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,19 +1,25 @@
1
1
  # markemental
2
2
 
3
- > **Note:** This project is still under active development. APIs may change between releases.
3
+ [![npm version](https://img.shields.io/npm/v/markemental.svg)](https://www.npmjs.com/package/markemental)
4
+ [![CI](https://github.com/neyapey/markemental/actions/workflows/ci.yml/badge.svg)](https://github.com/neyapey/markemental/actions/workflows/ci.yml)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/neyapey/markemental/blob/main/LICENSE)
4
6
 
5
- Incremental modular markdown renderer. Push markdown in arbitrarily-sized chunks and complete blocks render to the target as they arrive, instead of waiting for the full document.
7
+ > [!WARNING]
8
+ > This project is still under active development. APIs may change between releases.
6
9
 
7
- Two pipelines cover the GFM/CommonMark flavors, and the environment is implicit: the same code drives a live page in the browser and lightweight DOMs like linkedom on the server through a minimal `DOMNodeLike` interface.
10
+ Incremental markdown renderer for streamed content. Write markdown chunks as they arrive and watch complete blocks render progressively — in the browser or on the server.
11
+
12
+ Two stream factories cover the GFM and CommonMark flavors.
8
13
 
9
14
  ## Features
10
15
 
11
- - Streaming input: `push()` chunks as they arrive, `flush()` the remainder.
16
+ - Streaming input: `write()` chunks as they arrive, `close()` the remainder — or `pipeTo` a response body straight in.
12
17
  - Incremental commit: tables render row-by-row from the delimiter row on, and lists item-by-item, instead of waiting for the whole block.
13
18
  - Blocks: headings, paragraphs, fenced code, bullet and ordered lists, blockquotes, thematic breaks.
14
19
  - Inlines: emphasis, strong, inline code, links, images, autolinks, escapes, hard breaks.
15
20
  - GFM extras: tables (with column alignment) and strikethrough; CommonMark has no tables, so table syntax renders as plain paragraph text.
16
- - Pipelines batch DOM writes on `requestAnimationFrame` when given a scheduler.
21
+ - Targets batch DOM writes on `requestAnimationFrame` when given a scheduler.
22
+ - Reusable streams: retarget a live stream or swap renderer config mid-stream.
17
23
 
18
24
  ## Install
19
25
 
@@ -32,65 +38,96 @@ npm install linkedom
32
38
  Render a streamed response into a live page:
33
39
 
34
40
  ```ts
35
- import { createGFMPipeline } from "markemental";
41
+ import { createGFMStream } from "markemental";
36
42
 
37
- const pipeline = createGFMPipeline(
43
+ const stream = createGFMStream(
38
44
  document.getElementById("out")!,
39
45
  document,
40
46
  {},
41
- requestAnimationFrame,
47
+ { raf: requestAnimationFrame },
42
48
  );
43
49
 
50
+ const writer = stream.getWriter();
44
51
  for await (const chunk of streamResponse()) {
45
- await pipeline.push(chunk);
52
+ await writer.write(chunk);
46
53
  }
47
- await pipeline.flush();
54
+ await writer.close();
55
+ writer.releaseLock();
56
+ ```
57
+
58
+ Or pipe a byte stream straight in (decode it to text first — the stream consumes unicode strings):
59
+
60
+ ```ts
61
+ await textStream.pipeTo(createGFMStream(target, document, {}));
48
62
  ```
49
63
 
50
64
  Render on the server with linkedom:
51
65
 
52
66
  ```ts
53
67
  import { parseHTML } from "linkedom";
54
- import { createGFMPipeline } from "markemental";
68
+ import { createGFMStream } from "markemental";
55
69
 
56
70
  const { document } = parseHTML("<div></div>");
57
71
  const target = document.querySelector("div")!;
58
- const pipeline = createGFMPipeline(target, document, {});
72
+ const stream = createGFMStream(target, document, {});
59
73
 
60
- await pipeline.push("# Hello\n\nWorld\n");
61
- await pipeline.flush();
74
+ const writer = stream.getWriter();
75
+ await writer.write("# Hello\n\nWorld\n");
76
+ await writer.close();
77
+ writer.releaseLock();
62
78
  console.log(target.innerHTML);
63
79
  ```
64
80
 
65
- Pick a pipeline by flavor:
81
+ Pick a stream by flavor:
66
82
 
67
83
  | Factory | Flavor |
68
84
  |---|---|
69
- | `createGFMPipeline` | GFM |
70
- | `createCommonMarkPipeline` | CommonMark |
85
+ | `createGFMStream` | GFM |
86
+ | `createCommonMarkStream` | CommonMark |
71
87
 
72
- The trailing partial block is held back until more input completes it or `flush()` runs. Tables commit once the delimiter row arrives and lists commit per item, so large blocks render progressively. Pushing after `flush()` throws. See the TSDoc on each factory for details.
88
+ The trailing partial block is held back until more input completes it or the stream closes. Tables commit once the delimiter row arrives and lists commit per item, so large blocks render progressively. Writes after close reject with the platform's native error. See the TSDoc on each factory for details.
73
89
 
74
- ## Custom renderers
90
+ ## Custom renderers and targets
75
91
 
76
- Implement `MarkdownRenderer` to bring your own flavor or wrap a third-party parser, and compose it with any target through `createMarkdownPipeline`:
92
+ Renderers consume lines and build output through a write-only `HTMLTarget`; they never touch the DOM. Every creation call returns an opaque node id, and a `null` parent addresses the top level:
77
93
 
78
94
  ```ts
79
- import { createMarkdownPipeline, createRenderTarget } from "markemental";
95
+ const paragraph = target.createElement("p");
96
+ target.appendChild(paragraph, target.createTextNode("hello"));
97
+ target.appendChild(null, paragraph);
98
+ ```
80
99
 
81
- const pipeline = createMarkdownPipeline(
82
- new MyRenderer(createRenderTarget(document.getElementById("out")!), document),
83
- );
100
+ Implement `MarkdownRenderer<YourConfig>` to bring your own flavor or wrap a third-party parser, and compose it with any target through `createMarkdownStream`:
101
+
102
+ ```ts
103
+ import { createDOMTarget, createMarkdownStream } from "markemental";
104
+
105
+ const target = createDOMTarget(document, document.getElementById("out")!);
106
+ const stream = createMarkdownStream(new MyRenderer(target, {}), target);
84
107
  ```
85
108
 
86
- The pipeline splits pushed chunks into lines and feeds them to the renderer one at a time; the renderer owns its target, document, and parse state. See the TSDoc on `createMarkdownPipeline` and `MarkdownRenderer` for details.
109
+ A custom renderer owns its constructor and parse state, and implements `writeLine`, `flush`, `setTarget`, and `setConfig`. Attributes are fixed when an element is created — pass them to `createElement` and replace the node to change them. A custom target implements the same `HTMLTarget` interface as the DOM targets: `createElement`, `createTextNode`, `appendChild`, `replaceNode`, `remove`, `setTextContent`, and `flush`. Targets report misuse through dedicated errors (`UnknownNodeError`, `DetachedNodeError`, `HierarchyError`) so renderers can handle them. Sanitization is up to the renderer: the target creates exactly what it is told to. See the TSDoc on `createMarkdownStream`, `MarkdownRenderer`, and `HTMLTarget` for details.
87
110
 
88
111
  ## MVP limitations
89
112
 
90
113
  Single-line list items with basic indented nesting, single-paragraph quotes, and same-line code spans. No task lists, lazy list continuation, bare-URL autolinking, footnotes, or raw HTML passthrough yet.
91
114
 
115
+ ## Roadmap
116
+
117
+ - [x] Github repo and npm package
118
+ - [ ] (in progress) project architecture and MVP
119
+ - [ ] Full CommonMark implementation
120
+ - [ ] Full GFM implementation
121
+ - [ ] Integrated sanitizer
122
+ - [ ] Server-side rendering support
123
+ - [ ] Client side rendering support
124
+ - [ ] HTMX support
125
+ - [ ] Extensions feature
126
+
92
127
  ## Development
93
128
 
129
+ Development needs Node 22 or newer for the toolchain; the published package itself runs on Node 18 and up.
130
+
94
131
  ```sh
95
132
  npm install
96
133
  npm run check # typecheck + build + test