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 +62 -25
- package/dist/index.cjs +1535 -973
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +582 -519
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +582 -519
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1527 -965
- package/dist/index.js.map +1 -1
- package/package.json +16 -13
package/README.md
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
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 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 {
|
|
41
|
+
import { createGFMStream } from "markemental";
|
|
36
42
|
|
|
37
|
-
const
|
|
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
|
|
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(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(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 |
|
|
71
87
|
|
|
72
|
-
The trailing partial block is held back until more input completes it or
|
|
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
|
-
|
|
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
|
-
|
|
95
|
+
const paragraph = target.createElement("p");
|
|
96
|
+
target.appendChild(paragraph, target.createTextNode("hello"));
|
|
97
|
+
target.appendChild(null, paragraph);
|
|
98
|
+
```
|
|
80
99
|
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
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
|