mausmark 0.5.0 → 0.6.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 +74 -231
- package/RELEASE-NOTES.md +18 -0
- package/dist/abi.d.ts +2 -1
- package/dist/abi.js +2 -2
- package/dist/core.d.ts +5 -0
- package/dist/core.js +66 -12
- package/dist/mausmark.wasm +0 -0
- package/dom-example.js +2 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
# Mausmark
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Mausmark is a renderer-agnostic Markdown parser compiled to WebAssembly.
|
|
4
|
+
It parses source into a syntax tree you can inspect or render yourself.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
[Project page and demo](https://myshkin.eu/mausmark) · [Release
|
|
7
|
+
notes](RELEASE-NOTES.md)
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
> Pre-1.0: minor versions may change the API.
|
|
9
|
+
Pre-1.0: minor versions may change the API.
|
|
11
10
|
|
|
12
11
|
## Install
|
|
13
12
|
|
|
@@ -18,255 +17,99 @@ Browser bundlers must emit the package-relative `.wasm` asset.
|
|
|
18
17
|
|
|
19
18
|
## Usage
|
|
20
19
|
|
|
21
|
-
import {createParser,
|
|
20
|
+
import {createParser, NodeType} from "mausmark";
|
|
22
21
|
|
|
23
22
|
const parser = await createParser();
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
// ...or parser.parse(source, Dialect.COMMONMARK) for CommonMark input.
|
|
23
|
+
const doc = parser.parse("# Hello\n\nThis is **Mausmark**.");
|
|
27
24
|
|
|
28
|
-
for (let
|
|
29
|
-
if (
|
|
30
|
-
console.log(
|
|
25
|
+
for (let i = 0; i < doc.nodeCount; i++) {
|
|
26
|
+
if (doc.type(i) === NodeType.HEADING) {
|
|
27
|
+
console.log(doc.joinedText(i)); // "Hello"
|
|
31
28
|
}
|
|
32
29
|
}
|
|
33
30
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
asynchronous; `parse()` is synchronous. Returned documents are
|
|
38
|
-
independent snapshots and remain valid after later parses.
|
|
39
|
-
|
|
40
|
-
## AST
|
|
41
|
-
|
|
42
|
-
The root is node `0`. Nodes use integer indexes into a compact preorder
|
|
43
|
-
table. TypeScript declarations and runtime constants are included.
|
|
44
|
-
|
|
45
|
-
- `type(index)` returns a `NodeType`.
|
|
46
|
-
- `children(index)` iterates direct content children.
|
|
47
|
-
- `text(index)` decodes one node’s source slice.
|
|
48
|
-
- `joinedText(index)` joins a node’s text chunks back into one string.
|
|
49
|
-
- `link(index)` returns raw URL, title, label, and `ReferenceStyle`.
|
|
50
|
-
- `level(index)` carries heading levels and other type-specific
|
|
51
|
-
values.
|
|
52
|
-
- `flags(index)` carries `ListFlags.ORDERED` and `ListFlags.LOOSE` on
|
|
53
|
-
lists.
|
|
54
|
-
- `offset(index)` and `length(index)` are UTF-8 byte offsets.
|
|
55
|
-
- `subtreeEnd(index)` supports lower-level preorder traversal.
|
|
56
|
-
|
|
57
|
-
Text arrives in chunks, one per source line: a soft line break is not a
|
|
58
|
-
byte inside a slice but the next chunk’s join code, so line structure is
|
|
59
|
-
read off the tree rather than by scanning for newlines. `joinedText()`
|
|
60
|
-
reassembles a run when you just want the string; `children()` walks the
|
|
61
|
-
chunks when you care about source lines.
|
|
62
|
-
|
|
63
|
-
In the CommonMark dialect, an escaped ampersand is
|
|
64
|
-
`NodeType.LITERAL_TEXT` rather than `NodeType.TEXT`: its text is literal
|
|
65
|
-
and must not be entity-decoded together with the following chunk. Both
|
|
66
|
-
types contain source-backed text slices.
|
|
67
|
-
|
|
68
|
-
The tree also holds internal URL, title, and label nodes; `children()`
|
|
69
|
-
skips them. Reference links remain unresolved so renderers can choose
|
|
70
|
-
their own lookup approach. The tree is meant to carry enough info about
|
|
71
|
-
the document so that you could reproduce it exactly (minus whitespace).
|
|
72
|
-
Raw HTML is returned as `NodeType.HTML_BLOCK` or `NodeType.HTML_INLINE`;
|
|
73
|
-
sanitize it when parsing untrusted input.
|
|
74
|
-
|
|
75
|
-
Both Markdown and CommonMark support pipe tables starting at a block
|
|
76
|
-
boundary with a header and a matching GFM-style divider row. Leave a
|
|
77
|
-
blank line before a table that follows a paragraph. Colons on the
|
|
78
|
-
divider set left, center, or right column alignment. Body rows must
|
|
79
|
-
contain a structural pipe; HTML rendering treats missing cells as empty
|
|
80
|
-
and ignores excess cells; the tree and formatter retain excess source
|
|
81
|
-
cells. Escape a literal pipe within a table as `\|`, including inside
|
|
82
|
-
code spans. Outside tables the Markdown dialect preserves the backslash
|
|
83
|
-
in `\|`. A following line without a structural pipe ends the table and
|
|
84
|
-
is parsed as ordinary block content. Valid Markdown block openers,
|
|
85
|
-
including reference definitions, also take precedence. Use a leading `|`
|
|
86
|
-
to keep definition-shaped text in a cell. Backtick fences open blocks in
|
|
87
|
-
CommonMark; in the Markdown dialect a custom block opens with `{` alone
|
|
88
|
-
on its line. Native HTML rendering emits alignment as `align`
|
|
89
|
-
attributes. In right-aligned columns, the formatter lines up numeric
|
|
90
|
-
body cells by decimal point, treating integers as having an implied
|
|
91
|
-
point after the last digit. It measures the Markdown source, counting
|
|
92
|
-
combining marks as zero columns and wide/fullwidth characters as two.
|
|
93
|
-
Widths are per Unicode scalar; an editor that displays a multi-character
|
|
94
|
-
emoji as one glyph can differ. This is a bounded subset, not full GFM or
|
|
95
|
-
MultiMarkdown table support. Pass `--no-table` to disable recognition in
|
|
96
|
-
either dialect (`--cm --no-table` for plain CommonMark). In JS use
|
|
97
|
-
`ParseMode.NO_TABLE` alone for Markdown, or
|
|
98
|
-
`ParseMode.COMMONMARK | ParseMode.NO_TABLE` for CommonMark. C uses the
|
|
99
|
-
same bitmask: `MM_PARSE_COMMONMARK | MM_PARSE_NO_TABLE`.
|
|
100
|
-
|
|
101
|
-
In the tree, a `TABLE` contains `TABLE_ROW` nodes, header first; rows
|
|
102
|
-
contain `TABLE_CELL` nodes. Header cell `level()` records a
|
|
103
|
-
`TableAlignment`: `DEFAULT` (0), `LEFT` (1), `CENTER` (2), or `RIGHT`
|
|
104
|
-
(3). Body rows preserve their original cell counts, so renderers should
|
|
105
|
-
apply the header width when presenting a rectangular table. A short
|
|
106
|
-
plain cell stores its text directly on the cell node. For rich content,
|
|
107
|
-
walk the cell's inline children.
|
|
31
|
+
Initialize the parser once. Each `parse()` call is synchronous and
|
|
32
|
+
returns an independent snapshot. Input is limited to 64 MiB, and the
|
|
33
|
+
parser arena is capped at 512 MiB.
|
|
108
34
|
|
|
109
|
-
|
|
110
|
-
`NodeType.BRACE_SPAN` with parsed inline children. `text(index)` returns
|
|
111
|
-
the exact original interior, `text **bold** text`, excluding its outer
|
|
112
|
-
braces; `offset()` and `length()` locate that interior in the UTF-8
|
|
113
|
-
source. Choose raw interpretation or child traversal, rather than
|
|
114
|
-
emitting both. The example renderer shows braces around the rendered
|
|
115
|
-
children; remove the delimiter output to make the container transparent.
|
|
35
|
+
## Dialects
|
|
116
36
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
beyond these limits braces stay literal. The Markdown formatter
|
|
122
|
-
preserves interiors verbatim. Multiline `NodeType.CUSTOM` blocks remain
|
|
123
|
-
raw, and CommonMark treats braces as ordinary text.
|
|
37
|
+
Pipe tables are enabled by default in both Markdown and CommonMark.
|
|
38
|
+
Disable them with the `ParseMode.NO_TABLE` flag or `--no-table` in the
|
|
39
|
+
CLI. Other GFM features such as task lists and strikethrough are not
|
|
40
|
+
supported.
|
|
124
41
|
|
|
125
|
-
|
|
126
|
-
|
|
42
|
+
The default Markdown dialect also supports brace syntax. A single-line
|
|
43
|
+
`BRACE_SPAN` such as `{text **bold**}` exposes both its raw interior and
|
|
44
|
+
parsed inline children. A `{` alone on a line opens a raw custom block,
|
|
45
|
+
closed by a `}` line.
|
|
127
46
|
|
|
128
|
-
|
|
47
|
+
Pass `Dialect.COMMONMARK` as the second argument to `parse()` for
|
|
48
|
+
CommonMark 0.31.2. Braces are ordinary text in this dialect. Mausmark
|
|
49
|
+
matches 649 of 652 spec examples; the remaining three depend on
|
|
50
|
+
resolving reference links during parsing.
|
|
129
51
|
|
|
130
|
-
|
|
131
|
-
output images, you don't need to change anything.
|
|
52
|
+
## AST
|
|
132
53
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
54
|
+
The root is node `0`. In index order, each node is followed by all its
|
|
55
|
+
descendants before the next sibling. Source offsets and lengths are
|
|
56
|
+
measured in UTF-8 bytes. The package includes TypeScript declarations
|
|
57
|
+
and named constants; use the constants rather than relying on their
|
|
58
|
+
numeric values.
|
|
138
59
|
|
|
139
|
-
|
|
60
|
+
- `type(index)` returns a `NodeType`.
|
|
61
|
+
- `children(index)` iterates direct content children, skipping link
|
|
62
|
+
metadata.
|
|
63
|
+
- `subtreeEnd(index)` gives the first index after a node and its
|
|
64
|
+
descendants.
|
|
65
|
+
- `text(index)` decodes the node’s own source slice. `offset(index)`
|
|
66
|
+
and `length(index)` locate that slice.
|
|
67
|
+
- `joinedText(index)` assembles immediate text chunks, including line
|
|
68
|
+
breaks. For nested markup, walk the children instead.
|
|
69
|
+
- `blockRange(index)` gives the source range of the whole block,
|
|
70
|
+
including its Markdown syntax. The end is exclusive. Nodes without a
|
|
71
|
+
block range return `undefined`.
|
|
72
|
+
- `link(index)` returns the raw URL, title, label, and reference style
|
|
73
|
+
for a link, image, or reference definition.
|
|
74
|
+
- `level(index)` carries type-specific values such as heading depth,
|
|
75
|
+
text join codes, or header cell alignment.
|
|
76
|
+
- `flags(index)` reports list properties such as `ListFlags.ORDERED`
|
|
77
|
+
and `ListFlags.LOOSE`.
|
|
140
78
|
|
|
141
79
|
## Writing a renderer
|
|
142
80
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
iterate every node index as though each were a sibling:
|
|
146
|
-
`subtreeEnd(index)` is the next sibling in the preorder table. For
|
|
147
|
-
example, this deliberately simple plain-text renderer omits URLs, raw
|
|
148
|
-
HTML, custom nodes, list markers, and entity decoding:
|
|
149
|
-
|
|
150
|
-
import {createParser, Join, NodeType} from "mausmark"
|
|
151
|
-
import type {MausmarkDocument} from "mausmark"
|
|
81
|
+
The package includes [dom-example.js](dom-example.js), a DOM renderer
|
|
82
|
+
you can copy and adapt. After copying it into your project:
|
|
152
83
|
|
|
153
|
-
|
|
154
|
-
const type = doc.type(index)
|
|
155
|
-
switch (type) {
|
|
156
|
-
case NodeType.TEXT: {
|
|
157
|
-
const join = doc.level(index)
|
|
158
|
-
return (join === Join.SPACE ? " " : join === Join.NL ? "\n" : "") + doc.text(index)
|
|
159
|
-
}
|
|
160
|
-
case NodeType.LITERAL_TEXT:
|
|
161
|
-
return doc.text(index)
|
|
162
|
-
case NodeType.HARD_BREAK:
|
|
163
|
-
return "\n"
|
|
164
|
-
case NodeType.CODE_SPAN:
|
|
165
|
-
case NodeType.CODE_BLOCK:
|
|
166
|
-
return doc.joinedText(index)
|
|
167
|
-
case NodeType.HTML_INLINE:
|
|
168
|
-
case NodeType.HTML_BLOCK:
|
|
169
|
-
case NodeType.CUSTOM:
|
|
170
|
-
case NodeType.REF_DEFS:
|
|
171
|
-
case NodeType.REF_DEF:
|
|
172
|
-
return ""
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
let text = ""
|
|
176
|
-
for (const child of doc.children(index)) text += plainText(doc, child)
|
|
177
|
-
if (type === NodeType.PARAGRAPH || type === NodeType.HEADING || type === NodeType.LIST_ITEM) text += "\n"
|
|
178
|
-
return text
|
|
179
|
-
}
|
|
84
|
+
import {renderToDom} from "./dom-example.js";
|
|
180
85
|
|
|
181
|
-
|
|
182
|
-
console.log(plainText(parser.parse("# Hello\n\nA **wrapped** paragraph.")))
|
|
86
|
+
renderToDom(parser.parse(source), document.querySelector("article"));
|
|
183
87
|
|
|
184
|
-
|
|
185
|
-
`
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
- Text is source-backed and split into chunks, often by line or by the
|
|
190
|
-
65,535-byte slice limit. For `TEXT` chunks, `level()` is a `Join`
|
|
191
|
-
code to emit *before* `text()` (`Join.NL` is a soft break).
|
|
192
|
-
`joinedText()` assembles a node’s immediate text chunks, useful for
|
|
193
|
-
code spans/blocks and metadata fields; it does not recursively
|
|
194
|
-
render arbitrary inline markup. In CommonMark, a `LITERAL_TEXT` leaf
|
|
195
|
-
must not be entity-decoded with its neighboring `TEXT`.
|
|
196
|
-
- `link(index)` works on `LINK`, `IMAGE`, and `REF_DEF` and returns
|
|
197
|
-
**raw** URL, title, label, and `ReferenceStyle`. In table cells the
|
|
198
|
-
table-syntax backslash before an escaped pipe is removed, including
|
|
199
|
-
in these fields; other escapes and entities remain raw. Destinations
|
|
200
|
-
and titles over 65,535 bytes are kept as field-child text chunks;
|
|
201
|
-
`link()` joins them without truncation. Inline links and autolinks
|
|
202
|
-
already have destinations; `FULL`, `COLLAPSED`, and `SHORTCUT` need
|
|
203
|
-
a definition lookup. Scan all node indexes for `REF_DEF` (including
|
|
204
|
-
definitions in containers), store the first definition for each
|
|
205
|
-
normalized label, and decide how to render unresolved references.
|
|
206
|
-
CommonMark label matching uses Unicode case folding and whitespace
|
|
207
|
-
collapse, not just `toLowerCase()`;
|
|
208
|
-
`normalizeReferenceLabel(label, "markdown" | "commonmark")` from
|
|
209
|
-
`mausmark/references` does either. An `AUTOLINK_EMAIL` URL is the
|
|
210
|
-
bare address; add `mailto:` when emitting it. `title === ""` alone
|
|
211
|
-
does not distinguish an absent title from an explicit empty title;
|
|
212
|
-
inspect the leading `TITLE` field if that matters to your output.
|
|
213
|
-
- For lists, `flags(index)` has `ListFlags.ORDERED` and
|
|
214
|
-
`ListFlags.LOOSE` on `LIST` / `LIST_ITEM`. `level()` on a list
|
|
215
|
-
records its marker character; `text()` on a CommonMark ordered list
|
|
216
|
-
holds its starting digits. Tight list items normally omit paragraph
|
|
217
|
-
wrappers. A fenced code block’s info string is its leading `LABEL`
|
|
218
|
-
field: when node `index + 1` is inside the block and is a `LABEL`,
|
|
219
|
-
read it with `joinedText(index + 1)`. `link(index)` covers link-like
|
|
220
|
-
nodes only.
|
|
221
|
-
- `HTML_BLOCK` and `HTML_INLINE` are raw input, not sanitized HTML.
|
|
222
|
-
Escape output for your target format and apply an explicit policy
|
|
223
|
-
for untrusted input.
|
|
88
|
+
When adapting the example, use `normalizeReferenceLabel` from
|
|
89
|
+
`mausmark/references` to match reference links against definitions. The
|
|
90
|
+
parser leaves these links unresolved. For untrusted input, check URL
|
|
91
|
+
schemes and sanitize any raw HTML you choose to emit.
|
|
224
92
|
|
|
225
93
|
## Performance
|
|
226
94
|
|
|
227
|
-
The
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
and Bun 1.4.2, compiled with Clang 22.1.7 at `-Os` with LTO:
|
|
231
|
-
|
|
232
|
-
| Input | Node (µs) | Bun (µs) | Node (GB/s) | Bun (GB/s) |
|
|
233
|
-
| ---------------------- | --------: | -------: | ----------: | ---------: |
|
|
234
|
-
| Moby-Dick (1.2 MB) | ~307 | ~245 | 3.9 | 4.9 |
|
|
235
|
-
| Line-wrapped Moby-Dick | ~404 | ~287 | 3 | 4.2 |
|
|
236
|
-
| Pro Git chapters | 34.2 | 27.3 | 1.63 | 2.04 |
|
|
237
|
-
| tldr pages | 1.4 | 1.23 | 0.4 | 0.46 |
|
|
238
|
-
|
|
239
|
-
These are warmed parser-export timings with preloaded UTF-8 input and a
|
|
240
|
-
reused arena, including JavaScript iteration and export-call overhead.
|
|
241
|
-
They exclude initialization, input encoding/copying, AST snapshot
|
|
242
|
-
copying, and rendering; they are not end-to-end timings of the package’s
|
|
243
|
-
`parse()` method. The corpus rows (Pro Git, tldr) report average time
|
|
244
|
-
per document; figures are medians across three alternating passes, each
|
|
245
|
-
with five timing samples. Both Moby-Dick inputs and Pro Git use
|
|
246
|
-
Markdown; tldr uses CommonMark. GB/s uses decimal bytes.
|
|
247
|
-
|
|
248
|
-
## Notes
|
|
95
|
+
The v0.6.0 parser is 71 KB of WebAssembly (31 KB gzipped). On an Apple
|
|
96
|
+
M3 Pro with Node.js 26.3.1 and Bun 1.4.2, warmed source-to-AST
|
|
97
|
+
throughput was:
|
|
249
98
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
definition-dependent reference-link cases are intentionally
|
|
257
|
-
unresolved.
|
|
258
|
-
- Unlike CommonMark, an image description is plain alt text, not
|
|
259
|
-
inline content.
|
|
260
|
-
- No full GFM dialect: strikethrough, task lists and autolink literals
|
|
261
|
-
are not supported.
|
|
99
|
+
| Input | Node (GB/s) | Bun (GB/s) |
|
|
100
|
+
| ---------------------- | ----------: | ---------: |
|
|
101
|
+
| Moby-Dick (1.2 MB) | 4.18 | 4.96 |
|
|
102
|
+
| Line-wrapped Moby-Dick | 3.05 | 4.26 |
|
|
103
|
+
| Pro Git chapters | 1.60 | 2.05 |
|
|
104
|
+
| tldr pages | 0.41 | 0.46 |
|
|
262
105
|
|
|
263
|
-
The
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
106
|
+
The two Moby-Dick inputs and Pro Git use Markdown; tldr uses CommonMark.
|
|
107
|
+
Each figure is the median of three passes with preloaded UTF-8 input and
|
|
108
|
+
a reused parser arena. These timings exclude initialization, input
|
|
109
|
+
encoding and copying, snapshot copying, and rendering.
|
|
267
110
|
|
|
268
111
|
## License
|
|
269
112
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
113
|
+
The npm package is licensed under [MIT](LICENSE). The implementation
|
|
114
|
+
source is currently private. Separate notices apply to [Unicode
|
|
115
|
+
data](UNICODE-LICENSE.txt) and [HTML entities](HTML-LICENSE.txt).
|
package/RELEASE-NOTES.md
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
# Mausmark v0.6.0
|
|
2
|
+
|
|
3
|
+
- `blockRange(index)` returns the complete syntax of supported blocks, including
|
|
4
|
+
lists and list items, as a half-open UTF-8 byte range. `text(list)` still
|
|
5
|
+
returns the CommonMark starting digits, including leading zeroes.
|
|
6
|
+
- Migration for table renderers: walk `children(cell)` for both plain and rich
|
|
7
|
+
cells. `text(cell)` is now empty; plain text lives in a `TEXT` child.
|
|
8
|
+
- Contiguous text and metadata no longer split at 65,535 bytes. Line joins and
|
|
9
|
+
discontinuous fields still use chunks.
|
|
10
|
+
- WASM ABI is **8**. Deploy the JS bindings and WASM together. Records remain
|
|
11
|
+
16 bytes; the last word is now a 32-bit payload length or exclusive block end.
|
|
12
|
+
- Fixed indented CommonMark HTML block termination and CRLF handling for list
|
|
13
|
+
markers, fences, setext headings, table delimiters and Markdown custom blocks.
|
|
14
|
+
|
|
15
|
+
# Mausmark v0.5.1
|
|
16
|
+
|
|
17
|
+
- Faster parsing of tab-indented CommonMark input.
|
|
18
|
+
|
|
1
19
|
# Mausmark v0.5.0
|
|
2
20
|
|
|
3
21
|
- Both dialects now recognize pipe tables. Custom renderers can handle
|
package/dist/abi.d.ts
CHANGED
package/dist/abi.js
CHANGED
|
@@ -71,6 +71,6 @@ export const NODE_TYPE_MAX = NodeType.TABLE_CELL;
|
|
|
71
71
|
export const NODE_SIZE = 16;
|
|
72
72
|
export const NodeLayout = Object.freeze({
|
|
73
73
|
TYPE: 0, LEVEL: 1, FLAGS: 2, SUBTREE_END: 4,
|
|
74
|
-
OFFSET: 8, LENGTH: 12,
|
|
74
|
+
OFFSET: 8, LENGTH: 12, BLOCK_END: 12,
|
|
75
75
|
});
|
|
76
|
-
export const ABI_VERSION =
|
|
76
|
+
export const ABI_VERSION = 8;
|
package/dist/core.d.ts
CHANGED
|
@@ -40,6 +40,11 @@ export interface MausmarkDocument {
|
|
|
40
40
|
offset(index: number): number;
|
|
41
41
|
/** UTF-8 byte length of the node's text slice. */
|
|
42
42
|
length(index: number): number;
|
|
43
|
+
/** Complete syntax for paragraphs, headings, thematic breaks, quotes, lists/items, code, HTML, custom blocks, reference definitions/groups and tables/rows; UTF-8 bytes, end exclusive. */
|
|
44
|
+
blockRange(index: number): {
|
|
45
|
+
start: number;
|
|
46
|
+
end: number;
|
|
47
|
+
} | undefined;
|
|
43
48
|
/**
|
|
44
49
|
* The node's own source slice. On BRACE_SPAN this is the exact raw interior,
|
|
45
50
|
* excluding its braces and overlapping its parsed children's source ranges.
|
package/dist/core.js
CHANGED
|
@@ -6,6 +6,10 @@ const MAX_INPUT_BYTES = 64 * 1024 * 1024;
|
|
|
6
6
|
const MAX_ARENA_BYTES = 512 * 1024 * 1024;
|
|
7
7
|
const MAX_WASM_ADDRESS = 0x7fffffff;
|
|
8
8
|
const decoder = new TextDecoder('utf-8', { ignoreBOM: true });
|
|
9
|
+
function hasBlockRange(type) {
|
|
10
|
+
return (type >= NodeType.PARAGRAPH && type <= NodeType.REF_DEF) || type === NodeType.CUSTOM ||
|
|
11
|
+
type === NodeType.TABLE || type === NodeType.TABLE_ROW;
|
|
12
|
+
}
|
|
9
13
|
function align(value) {
|
|
10
14
|
if (!Number.isSafeInteger(value) || value < 0 || value > MAX_WASM_ADDRESS - (ALIGNMENT - 1)) {
|
|
11
15
|
throw new Error('mausmark: invalid WASM heap base');
|
|
@@ -28,13 +32,11 @@ function isField(type) {
|
|
|
28
32
|
class PackedDocument {
|
|
29
33
|
#source;
|
|
30
34
|
#decodeSource;
|
|
31
|
-
#nodes;
|
|
32
35
|
#view;
|
|
33
36
|
#nodeCount;
|
|
34
37
|
constructor(source, nodes, nodeCount, decodeSource) {
|
|
35
38
|
this.#source = source;
|
|
36
39
|
this.#decodeSource = decodeSource;
|
|
37
|
-
this.#nodes = nodes;
|
|
38
40
|
this.#view = new DataView(nodes.buffer, nodes.byteOffset, nodes.byteLength);
|
|
39
41
|
this.#nodeCount = nodeCount;
|
|
40
42
|
this.validateTable();
|
|
@@ -52,7 +54,9 @@ class PackedDocument {
|
|
|
52
54
|
}
|
|
53
55
|
flags(index) {
|
|
54
56
|
this.checkIndex(index);
|
|
55
|
-
|
|
57
|
+
const bits = this.#view.getInt16(index * NODE_SIZE + NodeLayout.FLAGS, true);
|
|
58
|
+
const type = this.type(index);
|
|
59
|
+
return type === NodeType.HTML_BLOCK ? 0 : type === NodeType.LIST ? bits & 3 : bits;
|
|
56
60
|
}
|
|
57
61
|
subtreeEnd(index) {
|
|
58
62
|
this.checkIndex(index);
|
|
@@ -60,11 +64,36 @@ class PackedDocument {
|
|
|
60
64
|
}
|
|
61
65
|
offset(index) {
|
|
62
66
|
this.checkIndex(index);
|
|
67
|
+
const type = this.type(index);
|
|
68
|
+
if (type === NodeType.HTML_BLOCK) {
|
|
69
|
+
const base = index * NODE_SIZE, aux = this.#view.getInt16(base + NodeLayout.FLAGS, true);
|
|
70
|
+
return aux < 0 ? 0 : this.#view.getUint32(base + NodeLayout.OFFSET, true) + (aux & 3);
|
|
71
|
+
}
|
|
72
|
+
if (hasBlockRange(type) && (type !== NodeType.LIST || !this.length(index)))
|
|
73
|
+
return 0;
|
|
63
74
|
return this.#view.getUint32(index * NODE_SIZE + NodeLayout.OFFSET, true);
|
|
64
75
|
}
|
|
65
76
|
length(index) {
|
|
66
77
|
this.checkIndex(index);
|
|
67
|
-
|
|
78
|
+
const type = this.type(index);
|
|
79
|
+
if (type === NodeType.LIST)
|
|
80
|
+
return this.#view.getUint16(index * NODE_SIZE + NodeLayout.FLAGS, true) >>> 2;
|
|
81
|
+
if (type === NodeType.HTML_BLOCK) {
|
|
82
|
+
const base = index * NODE_SIZE, aux = this.#view.getInt16(base + NodeLayout.FLAGS, true);
|
|
83
|
+
return aux < 0 ? 0 : this.#view.getUint32(base + NodeLayout.BLOCK_END, true) -
|
|
84
|
+
this.#view.getUint32(base + NodeLayout.OFFSET, true) - (aux & 3) + (aux >> 2) - 1;
|
|
85
|
+
}
|
|
86
|
+
if (hasBlockRange(type))
|
|
87
|
+
return 0;
|
|
88
|
+
return this.#view.getUint32(index * NODE_SIZE + NodeLayout.LENGTH, true);
|
|
89
|
+
}
|
|
90
|
+
blockRange(index) {
|
|
91
|
+
const type = this.type(index);
|
|
92
|
+
if (!hasBlockRange(type))
|
|
93
|
+
return undefined;
|
|
94
|
+
const base = index * NODE_SIZE;
|
|
95
|
+
return { start: this.#view.getUint32(base + NodeLayout.OFFSET, true),
|
|
96
|
+
end: this.#view.getUint32(base + NodeLayout.BLOCK_END, true) };
|
|
68
97
|
}
|
|
69
98
|
text(index) {
|
|
70
99
|
this.checkIndex(index);
|
|
@@ -135,18 +164,43 @@ class PackedDocument {
|
|
|
135
164
|
return decoder.decode(this.#source.subarray(offset, offset + length));
|
|
136
165
|
}
|
|
137
166
|
validateTable() {
|
|
138
|
-
|
|
167
|
+
const count = this.#nodeCount;
|
|
168
|
+
const view = this.#view;
|
|
169
|
+
const sourceLength = this.#source.byteLength;
|
|
170
|
+
if (!Number.isInteger(count) || count < 0 || view.byteLength !== count * NODE_SIZE) {
|
|
139
171
|
throw new Error('mausmark: malformed AST node table');
|
|
140
172
|
}
|
|
141
|
-
for (let index = 0; index <
|
|
142
|
-
const
|
|
143
|
-
const
|
|
144
|
-
|
|
173
|
+
for (let index = 0; index < count; index += 1) {
|
|
174
|
+
const base = index * NODE_SIZE;
|
|
175
|
+
const type = view.getUint8(base + NodeLayout.TYPE);
|
|
176
|
+
const end = view.getInt32(base + NodeLayout.SUBTREE_END, true);
|
|
177
|
+
if (type < NodeType.DOC || type > NODE_TYPE_MAX || end <= index || end > count) {
|
|
145
178
|
throw new Error(`mausmark: malformed AST node ${index}`);
|
|
146
179
|
}
|
|
147
|
-
const offset =
|
|
148
|
-
|
|
149
|
-
|
|
180
|
+
const offset = view.getUint32(base + NodeLayout.OFFSET, true);
|
|
181
|
+
if (hasBlockRange(type)) {
|
|
182
|
+
const blockEnd = view.getUint32(base + NodeLayout.BLOCK_END, true);
|
|
183
|
+
if (blockEnd > sourceLength || offset >= blockEnd) {
|
|
184
|
+
throw new Error(`mausmark: malformed block range on node ${index}`);
|
|
185
|
+
}
|
|
186
|
+
if (type === NodeType.HTML_BLOCK) {
|
|
187
|
+
const aux = view.getInt16(base + NodeLayout.FLAGS, true);
|
|
188
|
+
// Most leaves have exactly the syntax extent; only adjusted slices need bounds checks.
|
|
189
|
+
const payloadEnd = blockEnd + (aux >> 2) - 1;
|
|
190
|
+
const valid = end === index + 1 ? aux === 4 || (aux >= 0 && aux <= 15 &&
|
|
191
|
+
offset + (aux & 3) < payloadEnd && payloadEnd <= sourceLength) : aux === -1;
|
|
192
|
+
if (!valid)
|
|
193
|
+
throw new Error(`mausmark: malformed HTML payload on node ${index}`);
|
|
194
|
+
}
|
|
195
|
+
else if (type === NodeType.LIST) {
|
|
196
|
+
const digits = view.getUint16(base + NodeLayout.FLAGS, true) >>> 2;
|
|
197
|
+
if (digits > 9 || digits > blockEnd - offset)
|
|
198
|
+
throw new Error(`mausmark: malformed block range on node ${index}`);
|
|
199
|
+
}
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const length = view.getUint32(base + NodeLayout.LENGTH, true);
|
|
203
|
+
if (offset > sourceLength || length > sourceLength - offset) {
|
|
150
204
|
throw new Error(`mausmark: malformed text range on node ${index}`);
|
|
151
205
|
}
|
|
152
206
|
}
|
package/dist/mausmark.wasm
CHANGED
|
Binary file
|
package/dom-example.js
CHANGED
|
@@ -56,12 +56,6 @@ export function renderToDom(doc, parent) {
|
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
/** @param {number} index @param {ParentNode} into */
|
|
60
|
-
function tableCellContent(index, into) {
|
|
61
|
-
if (doc.subtreeEnd(index) > index + 1) children(index, into);
|
|
62
|
-
else text(doc.text(index), into);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
59
|
/** @param {string} tag @param {number} index @param {ParentNode} into */
|
|
66
60
|
function element(tag, index, into) {
|
|
67
61
|
const el = document.createElement(tag);
|
|
@@ -111,7 +105,7 @@ export function renderToDom(doc, parent) {
|
|
|
111
105
|
const cell = document.createElement("th");
|
|
112
106
|
const align = tableAlign[doc.level(column)];
|
|
113
107
|
if (align) cell.setAttribute("align", align);
|
|
114
|
-
|
|
108
|
+
children(column, cell);
|
|
115
109
|
header.appendChild(cell);
|
|
116
110
|
}
|
|
117
111
|
head.appendChild(header);
|
|
@@ -127,7 +121,7 @@ export function renderToDom(doc, parent) {
|
|
|
127
121
|
const align = tableAlign[doc.level(column)];
|
|
128
122
|
if (align) cell.setAttribute("align", align);
|
|
129
123
|
const source = cells.next();
|
|
130
|
-
if (!source.done)
|
|
124
|
+
if (!source.done) children(source.value, cell);
|
|
131
125
|
tr.appendChild(cell);
|
|
132
126
|
}
|
|
133
127
|
body.appendChild(tr);
|