markemental 0.3.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 +96 -28
- package/dist/index.cjs +1785 -985
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +579 -580
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +579 -580
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1773 -977
- package/dist/index.js.map +1 -1
- package/package.json +16 -13
package/README.md
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
1
1
|
# markemental
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/markemental)
|
|
4
|
+
[](https://github.com/neyapey/markemental/actions/workflows/ci.yml)
|
|
5
|
+
[](https://github.com/neyapey/markemental/blob/main/LICENSE)
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
> [!WARNING]
|
|
8
|
+
> This project is still under active development. APIs may change between releases.
|
|
6
9
|
|
|
7
|
-
|
|
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: `
|
|
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
|
-
-
|
|
21
|
+
- Targets batch DOM writes on `requestAnimationFrame` when given a scheduler.
|
|
22
|
+
- Reusable wiring: retarget a live stream or swap renderer config mid-stream.
|
|
23
|
+
- Runtime-aware utilities: native built-ins are preferred when available (for example `Bun.escapeHTML`), with identical-output fallbacks elsewhere.
|
|
17
24
|
|
|
18
25
|
## Install
|
|
19
26
|
|
|
@@ -32,65 +39,126 @@ npm install linkedom
|
|
|
32
39
|
Render a streamed response into a live page:
|
|
33
40
|
|
|
34
41
|
```ts
|
|
35
|
-
import {
|
|
42
|
+
import { createGFMStream } from "markemental";
|
|
36
43
|
|
|
37
|
-
const
|
|
38
|
-
document.getElementById("out")!,
|
|
44
|
+
const stream = createGFMStream({
|
|
45
|
+
container: document.getElementById("out")!,
|
|
39
46
|
document,
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
);
|
|
47
|
+
raf: requestAnimationFrame,
|
|
48
|
+
});
|
|
43
49
|
|
|
50
|
+
const writer = stream.getWriter();
|
|
44
51
|
for await (const chunk of streamResponse()) {
|
|
45
|
-
await
|
|
52
|
+
await writer.write(chunk);
|
|
46
53
|
}
|
|
47
|
-
await
|
|
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({ container: 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 {
|
|
68
|
+
import { createGFMStream } from "markemental";
|
|
55
69
|
|
|
56
70
|
const { document } = parseHTML("<div></div>");
|
|
57
71
|
const target = document.querySelector("div")!;
|
|
58
|
-
const
|
|
72
|
+
const stream = createGFMStream({ container: target, document });
|
|
59
73
|
|
|
60
|
-
|
|
61
|
-
await
|
|
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
|
|
81
|
+
Pick a stream by flavor:
|
|
66
82
|
|
|
67
83
|
| Factory | Flavor |
|
|
68
84
|
|---|---|
|
|
69
|
-
| `
|
|
70
|
-
| `
|
|
85
|
+
| `createGFMStream` | GFM |
|
|
86
|
+
| `createCommonMarkStream` | CommonMark |
|
|
87
|
+
|
|
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
|
+
## Architecture
|
|
71
91
|
|
|
72
|
-
|
|
92
|
+
Three main contracts:
|
|
73
93
|
|
|
74
|
-
|
|
94
|
+
- `HTMLTarget` — high-level write-only destination. Methods deal in opaque node ids, never actual objects: create, attach, overwrite, and delete only.
|
|
95
|
+
- `MarkdownStream` — a `WritableStream<string>` that splits chunks into lines and feeds them to the renderer it owns. One stream renders one document.
|
|
96
|
+
- `MarkdownRenderer` — one implementation per flavor. Owns its target (swappable, so one renderer renders into many targets) and its parse state.
|
|
75
97
|
|
|
76
|
-
|
|
98
|
+
Construction is split by role: `new` for stateful identity objects (`MarkdownStream`, `GFMRenderer`, `CommonMarkRenderer`), factory functions for implementation selection and one-call wiring (`createDOMTarget` picks the sync or frame-batched target; `createGFMStream` / `createCommonMarkStream` wire target + renderer + stream). There is no general stream factory — compose it yourself:
|
|
77
99
|
|
|
78
100
|
```ts
|
|
79
|
-
import {
|
|
101
|
+
import { GFMRenderer, MarkdownStream, createDOMTarget } from "markemental";
|
|
80
102
|
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
);
|
|
103
|
+
const target = createDOMTarget({ document, container: document.getElementById("out")! });
|
|
104
|
+
const stream = new MarkdownStream(new GFMRenderer(target));
|
|
84
105
|
```
|
|
85
106
|
|
|
86
|
-
|
|
107
|
+
Everything is single-document: a closed stream is terminal by platform design, and mutating one throws. Render the next document with new instances all the way down — benchmarking showed reuse buys nothing over fresh construction, so keep it simple:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
const target = createDOMTarget({ document, container });
|
|
111
|
+
const stream = new MarkdownStream(new GFMRenderer(target));
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Retargeting mid-stream moves only newly opened blocks: already open lists and tables drain into the previous target, the only split that keeps document structure intact.
|
|
115
|
+
|
|
116
|
+
Sanitization belongs to the renderer: targets create exactly what they are told to and never filter.
|
|
117
|
+
|
|
118
|
+
## Custom renderers and targets
|
|
119
|
+
|
|
120
|
+
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:
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
const paragraph = target.createElement("p");
|
|
124
|
+
target.appendChild(paragraph, target.createTextNode("hello"));
|
|
125
|
+
target.appendChild(null, paragraph);
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Implement `MarkdownRenderer<YourConfig>` to bring your own flavor or wrap a third-party parser, and compose it with any target through `MarkdownStream`:
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
import { MarkdownStream, createDOMTarget } from "markemental";
|
|
132
|
+
|
|
133
|
+
const target = createDOMTarget({ document, container: document.getElementById("out")! });
|
|
134
|
+
const stream = new MarkdownStream(new MyRenderer(target));
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
A custom renderer owns its constructor and parse state, and implements `target` / `config` getters plus `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`, `UnsupportedAttributesError`) so renderers can handle them. Sanitization is up to the renderer: the target creates exactly what it is told to. Targets that serialize to strings can use the exported `escapeHTML` helper, which prefers `Bun.escapeHTML` when available. See the TSDoc on `MarkdownStream`, `MarkdownRenderer`, and `HTMLTarget` for details.
|
|
87
138
|
|
|
88
139
|
## MVP limitations
|
|
89
140
|
|
|
90
141
|
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
142
|
|
|
143
|
+
## Roadmap
|
|
144
|
+
|
|
145
|
+
- [x] Github repo and npm package
|
|
146
|
+
- [x] project architecture
|
|
147
|
+
- [ ] (in progress) MVP
|
|
148
|
+
- [ ] Full CommonMark implementation
|
|
149
|
+
- [ ] Full GFM implementation
|
|
150
|
+
- [ ] Integrated sanitizer
|
|
151
|
+
- [ ] Server-side rendering support
|
|
152
|
+
- [ ] Client side rendering support
|
|
153
|
+
- [ ] HTMX support
|
|
154
|
+
- [ ] Extensions feature
|
|
155
|
+
- [ ] Parallel pipeline (separate pipeline with different contracts for efficient parallel rendering)
|
|
156
|
+
- [ ] Documentation webpage (GitHub Pages)
|
|
157
|
+
|
|
92
158
|
## Development
|
|
93
159
|
|
|
160
|
+
Development needs Node 22 or newer for the toolchain; the published package itself runs on Node 18 and up.
|
|
161
|
+
|
|
94
162
|
```sh
|
|
95
163
|
npm install
|
|
96
164
|
npm run check # typecheck + build + test
|