markemental 0.2.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,18 +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
- Four pipelines cover the frontend/backend × GFM/CommonMark matrix. Rendering targets a minimal `DOMNodeLike` interface, so the same code drives a live page in the browser and lightweight DOMs like linkedom on the server.
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.
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.
12
18
  - Blocks: headings, paragraphs, fenced code, bullet and ordered lists, blockquotes, thematic breaks.
13
19
  - Inlines: emphasis, strong, inline code, links, images, autolinks, escapes, hard breaks.
14
- - GFM extras: tables (with column alignment) and strikethrough; CommonMark pipelines degrade tables to paragraphs.
15
- - Frontend pipelines batch DOM writes on `requestAnimationFrame` when given a scheduler.
20
+ - GFM extras: tables (with column alignment) and strikethrough; CommonMark has no tables, so table syntax renders as plain paragraph text.
21
+ - Targets batch DOM writes on `requestAnimationFrame` when given a scheduler.
22
+ - Reusable streams: retarget a live stream or swap renderer config mid-stream.
16
23
 
17
24
  ## Install
18
25
 
@@ -31,53 +38,96 @@ npm install linkedom
31
38
  Render a streamed response into a live page:
32
39
 
33
40
  ```ts
34
- import { createFrontendGFMPipeline } from "markemental";
41
+ import { createGFMStream } from "markemental";
35
42
 
36
- const pipeline = createFrontendGFMPipeline(
43
+ const stream = createGFMStream(
37
44
  document.getElementById("out")!,
38
45
  document,
39
46
  {},
40
- requestAnimationFrame,
47
+ { raf: requestAnimationFrame },
41
48
  );
42
49
 
50
+ const writer = stream.getWriter();
43
51
  for await (const chunk of streamResponse()) {
44
- await pipeline.push(chunk);
52
+ await writer.write(chunk);
45
53
  }
46
- 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, {}));
47
62
  ```
48
63
 
49
64
  Render on the server with linkedom:
50
65
 
51
66
  ```ts
52
67
  import { parseHTML } from "linkedom";
53
- import { createBackendGFMPipeline } from "markemental";
68
+ import { createGFMStream } from "markemental";
54
69
 
55
70
  const { document } = parseHTML("<div></div>");
56
71
  const target = document.querySelector("div")!;
57
- const pipeline = createBackendGFMPipeline(target, document, {});
72
+ const stream = createGFMStream(target, document, {});
58
73
 
59
- await pipeline.push("# Hello\n\nWorld\n");
60
- await pipeline.flush();
74
+ const writer = stream.getWriter();
75
+ await writer.write("# Hello\n\nWorld\n");
76
+ await writer.close();
77
+ writer.releaseLock();
61
78
  console.log(target.innerHTML);
62
79
  ```
63
80
 
64
- Pick a pipeline by environment and flavor:
81
+ Pick a stream by flavor:
65
82
 
66
- | Factory | Environment | Flavor |
67
- |---|---|---|
68
- | `createFrontendGFMPipeline` | browser | GFM |
69
- | `createFrontendCommonMarkPipeline` | browser | CommonMark |
70
- | `createBackendGFMPipeline` | server | GFM |
71
- | `createBackendCommonMarkPipeline` | server | CommonMark |
83
+ | Factory | Flavor |
84
+ |---|---|
85
+ | `createGFMStream` | GFM |
86
+ | `createCommonMarkStream` | CommonMark |
72
87
 
73
- The trailing partial block is held back until more input completes it or `flush()` runs. 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.
89
+
90
+ ## Custom renderers and targets
91
+
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:
93
+
94
+ ```ts
95
+ const paragraph = target.createElement("p");
96
+ target.appendChild(paragraph, target.createTextNode("hello"));
97
+ target.appendChild(null, paragraph);
98
+ ```
99
+
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);
107
+ ```
108
+
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.
74
110
 
75
111
  ## MVP limitations
76
112
 
77
- Single-line list items, flat lists, single-paragraph quotes, and same-line code spans. No task lists, bare-URL autolinking, footnotes, or raw HTML passthrough yet.
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.
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
78
126
 
79
127
  ## Development
80
128
 
129
+ Development needs Node 22 or newer for the toolchain; the published package itself runs on Node 18 and up.
130
+
81
131
  ```sh
82
132
  npm install
83
133
  npm run check # typecheck + build + test