markemental 0.4.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 CHANGED
@@ -19,7 +19,8 @@ Two stream factories cover the GFM and CommonMark flavors.
19
19
  - Inlines: emphasis, strong, inline code, links, images, autolinks, escapes, hard breaks.
20
20
  - GFM extras: tables (with column alignment) and strikethrough; CommonMark has no tables, so table syntax renders as plain paragraph text.
21
21
  - Targets batch DOM writes on `requestAnimationFrame` when given a scheduler.
22
- - Reusable streams: retarget a live stream or swap renderer config mid-stream.
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.
23
24
 
24
25
  ## Install
25
26
 
@@ -40,12 +41,11 @@ Render a streamed response into a live page:
40
41
  ```ts
41
42
  import { createGFMStream } from "markemental";
42
43
 
43
- const stream = createGFMStream(
44
- document.getElementById("out")!,
44
+ const stream = createGFMStream({
45
+ container: document.getElementById("out")!,
45
46
  document,
46
- {},
47
- { raf: requestAnimationFrame },
48
- );
47
+ raf: requestAnimationFrame,
48
+ });
49
49
 
50
50
  const writer = stream.getWriter();
51
51
  for await (const chunk of streamResponse()) {
@@ -58,7 +58,7 @@ writer.releaseLock();
58
58
  Or pipe a byte stream straight in (decode it to text first — the stream consumes unicode strings):
59
59
 
60
60
  ```ts
61
- await textStream.pipeTo(createGFMStream(target, document, {}));
61
+ await textStream.pipeTo(createGFMStream({ container: target, document }));
62
62
  ```
63
63
 
64
64
  Render on the server with linkedom:
@@ -69,7 +69,7 @@ import { createGFMStream } from "markemental";
69
69
 
70
70
  const { document } = parseHTML("<div></div>");
71
71
  const target = document.querySelector("div")!;
72
- const stream = createGFMStream(target, document, {});
72
+ const stream = createGFMStream({ container: target, document });
73
73
 
74
74
  const writer = stream.getWriter();
75
75
  await writer.write("# Hello\n\nWorld\n");
@@ -87,6 +87,34 @@ Pick a stream by flavor:
87
87
 
88
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
89
 
90
+ ## Architecture
91
+
92
+ Three main contracts:
93
+
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.
97
+
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:
99
+
100
+ ```ts
101
+ import { GFMRenderer, MarkdownStream, createDOMTarget } from "markemental";
102
+
103
+ const target = createDOMTarget({ document, container: document.getElementById("out")! });
104
+ const stream = new MarkdownStream(new GFMRenderer(target));
105
+ ```
106
+
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
+
90
118
  ## Custom renderers and targets
91
119
 
92
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:
@@ -97,16 +125,16 @@ target.appendChild(paragraph, target.createTextNode("hello"));
97
125
  target.appendChild(null, paragraph);
98
126
  ```
99
127
 
100
- Implement `MarkdownRenderer<YourConfig>` to bring your own flavor or wrap a third-party parser, and compose it with any target through `createMarkdownStream`:
128
+ Implement `MarkdownRenderer<YourConfig>` to bring your own flavor or wrap a third-party parser, and compose it with any target through `MarkdownStream`:
101
129
 
102
130
  ```ts
103
- import { createDOMTarget, createMarkdownStream } from "markemental";
131
+ import { MarkdownStream, createDOMTarget } from "markemental";
104
132
 
105
- const target = createDOMTarget(document, document.getElementById("out")!);
106
- const stream = createMarkdownStream(new MyRenderer(target, {}), target);
133
+ const target = createDOMTarget({ document, container: document.getElementById("out")! });
134
+ const stream = new MarkdownStream(new MyRenderer(target));
107
135
  ```
108
136
 
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.
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.
110
138
 
111
139
  ## MVP limitations
112
140
 
@@ -115,7 +143,8 @@ Single-line list items with basic indented nesting, single-paragraph quotes, and
115
143
  ## Roadmap
116
144
 
117
145
  - [x] Github repo and npm package
118
- - [ ] (in progress) project architecture and MVP
146
+ - [x] project architecture
147
+ - [ ] (in progress) MVP
119
148
  - [ ] Full CommonMark implementation
120
149
  - [ ] Full GFM implementation
121
150
  - [ ] Integrated sanitizer
@@ -123,6 +152,8 @@ Single-line list items with basic indented nesting, single-paragraph quotes, and
123
152
  - [ ] Client side rendering support
124
153
  - [ ] HTMX support
125
154
  - [ ] Extensions feature
155
+ - [ ] Parallel pipeline (separate pipeline with different contracts for efficient parallel rendering)
156
+ - [ ] Documentation webpage (GitHub Pages)
126
157
 
127
158
  ## Development
128
159