mausmark 0.5.1 → 0.7.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 +77 -233
- package/RELEASE-NOTES.md +42 -4
- package/dist/abi.d.ts +2 -1
- package/dist/abi.js +2 -2
- package/dist/core.d.ts +5 -0
- package/dist/core.js +74 -16
- package/dist/mausmark.wasm +0 -0
- package/dom-example.js +18 -11
- package/package.json +13 -13
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,100 @@ Browser bundlers must emit the package-relative `.wasm` asset.
|
|
|
18
17
|
|
|
19
18
|
## Usage
|
|
20
19
|
|
|
21
|
-
import {createParser,
|
|
22
|
-
|
|
20
|
+
import {createParser, NodeType} from "mausmark";
|
|
21
|
+
|
|
23
22
|
const parser = await createParser();
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (document.type(index) === NodeType.HEADING) {
|
|
30
|
-
console.log(document.joinedText(index)); // "Hello"
|
|
23
|
+
const doc = parser.parse("# Hello\n\nThis is **Mausmark**.");
|
|
24
|
+
|
|
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`.
|
|
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.
|
|
100
34
|
|
|
101
|
-
|
|
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.
|
|
35
|
+
## Dialects
|
|
108
36
|
|
|
109
|
-
|
|
110
|
-
`
|
|
111
|
-
|
|
112
|
-
|
|
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.
|
|
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.
|
|
116
41
|
|
|
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.
|
|
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.
|
|
124
46
|
|
|
125
|
-
|
|
126
|
-
|
|
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.
|
|
127
51
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
For most projects this is a drop-in update: if your renderer doesn't
|
|
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`. Table cell ranges exclude delimiter
|
|
72
|
+
pipes and surrounding whitespace but include inline Markdown syntax.
|
|
73
|
+
- `link(index)` returns the raw URL, title, label, and reference style
|
|
74
|
+
for a link, image, or reference definition.
|
|
75
|
+
- `level(index)` carries type-specific values such as heading depth,
|
|
76
|
+
text join codes, or header cell alignment.
|
|
77
|
+
- `flags(index)` reports list properties such as `ListFlags.ORDERED`
|
|
78
|
+
and `ListFlags.LOOSE`.
|
|
140
79
|
|
|
141
80
|
## Writing a renderer
|
|
142
81
|
|
|
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"
|
|
152
|
-
|
|
153
|
-
function plainText(doc: MausmarkDocument, index = 0): string {
|
|
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
|
-
}
|
|
180
|
-
|
|
181
|
-
const parser = await createParser()
|
|
182
|
-
console.log(plainText(parser.parse("# Hello\n\nA **wrapped** paragraph.")))
|
|
82
|
+
The package includes [dom-example.js](dom-example.js), a DOM renderer
|
|
83
|
+
you can copy and adapt. After copying it into your project:
|
|
183
84
|
|
|
184
|
-
|
|
185
|
-
`dom-example.js`, an incomplete renderer meant to be copied and adapted.
|
|
85
|
+
import {renderToDom} from "./dom-example.js";
|
|
186
86
|
|
|
187
|
-
|
|
87
|
+
renderToDom(parser.parse(source), document.querySelector("article"));
|
|
188
88
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
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.
|
|
89
|
+
When adapting the example, use `normalizeReferenceLabel` from
|
|
90
|
+
`mausmark/references` to match reference links against definitions. The
|
|
91
|
+
parser leaves these links unresolved. For untrusted input, check URL
|
|
92
|
+
schemes and sanitize any raw HTML you choose to emit.
|
|
224
93
|
|
|
225
94
|
## Performance
|
|
226
95
|
|
|
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
|
|
96
|
+
The v0.6.0 parser is 71 KB of WebAssembly (31 KB gzipped). On an Apple
|
|
97
|
+
M3 Pro with Node.js 26.3.1 and Bun 1.4.2, warmed source-to-AST
|
|
98
|
+
throughput was:
|
|
249
99
|
|
|
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.
|
|
100
|
+
| Input | Node (GB/s) | Bun (GB/s) |
|
|
101
|
+
| ---------------------- | ----------: | ---------: |
|
|
102
|
+
| Moby-Dick (1.2 MB) | 4.18 | 4.96 |
|
|
103
|
+
| Line-wrapped Moby-Dick | 3.05 | 4.26 |
|
|
104
|
+
| Pro Git chapters | 1.60 | 2.05 |
|
|
105
|
+
| tldr pages | 0.41 | 0.46 |
|
|
262
106
|
|
|
263
|
-
The
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
107
|
+
The two Moby-Dick inputs and Pro Git use Markdown; tldr uses CommonMark.
|
|
108
|
+
Each figure is the median of three passes with preloaded UTF-8 input and
|
|
109
|
+
a reused parser arena. These timings exclude initialization, input
|
|
110
|
+
encoding and copying, snapshot copying, and rendering.
|
|
267
111
|
|
|
268
112
|
## License
|
|
269
113
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
114
|
+
The npm package is licensed under [MIT](LICENSE). The implementation
|
|
115
|
+
source is currently private. Separate notices apply to [Unicode
|
|
116
|
+
data](UNICODE-LICENSE.txt) and [HTML entities](HTML-LICENSE.txt).
|
package/RELEASE-NOTES.md
CHANGED
|
@@ -1,8 +1,46 @@
|
|
|
1
|
-
#
|
|
1
|
+
# v0.7.0
|
|
2
|
+
|
|
3
|
+
- `blockRange(cell)` now covers the trimmed cell syntax, excluding
|
|
4
|
+
delimiter pipes. An empty cell has a zero-length range.
|
|
5
|
+
- CRLF and bare CR are parsed like LF. Source ranges still address the
|
|
6
|
+
original UTF-8 bytes.
|
|
7
|
+
- Renderer migration: code and custom block lines no longer include
|
|
8
|
+
their line endings. Join their content chunks and append a final
|
|
9
|
+
newline if there was at least one content line. Multiline HTML
|
|
10
|
+
blocks may now have empty `text(index)`; use `joinedText(index)`.
|
|
11
|
+
Multiline inline HTML uses sibling chunks with `Join.NL` on
|
|
12
|
+
continuation lines.
|
|
13
|
+
- WASM ABI is 9 now. Deploy the matching JS bindings and WASM
|
|
14
|
+
together.
|
|
15
|
+
- Native C callers: use `MmParseMode` in place of `MmDialect`, and
|
|
16
|
+
`mm_parse` followed by `mm_render_html` in place of the native
|
|
17
|
+
`mm_to_html` helper.
|
|
18
|
+
- The CLI `format` command no longer reparses its output to check for
|
|
19
|
+
changes in meaning or formatting stability before emitting it.
|
|
20
|
+
|
|
21
|
+
# v0.6.0
|
|
22
|
+
|
|
23
|
+
- `blockRange(index)` returns the complete syntax of supported blocks,
|
|
24
|
+
including lists and list items, as a half-open UTF-8 byte range.
|
|
25
|
+
`text(list)` still returns the CommonMark starting digits, including
|
|
26
|
+
leading zeroes.
|
|
27
|
+
- Migration for table renderers: walk `children(cell)` for both plain
|
|
28
|
+
and rich cells. `text(cell)` is now empty; plain text lives in a
|
|
29
|
+
`TEXT` child.
|
|
30
|
+
- Contiguous text and metadata no longer split at 65,535 bytes. Line
|
|
31
|
+
joins and discontinuous fields still use chunks.
|
|
32
|
+
- WASM ABI is **8**. Deploy the JS bindings and WASM together. Records
|
|
33
|
+
remain 16 bytes; the last word is now a 32-bit payload length or
|
|
34
|
+
exclusive block end.
|
|
35
|
+
- Fixed indented CommonMark HTML block termination and CRLF handling
|
|
36
|
+
for list markers, fences, setext headings, table delimiters and
|
|
37
|
+
Markdown custom blocks.
|
|
38
|
+
|
|
39
|
+
# v0.5.1
|
|
2
40
|
|
|
3
41
|
- Faster parsing of tab-indented CommonMark input.
|
|
4
42
|
|
|
5
|
-
#
|
|
43
|
+
# v0.5.0
|
|
6
44
|
|
|
7
45
|
- Both dialects now recognize pipe tables. Custom renderers can handle
|
|
8
46
|
`TABLE`, `TABLE_ROW` and `TABLE_CELL`; header cells carry column
|
|
@@ -29,7 +67,7 @@
|
|
|
29
67
|
`--cm --no-table` in the CLI. Existing `Dialect` constants still
|
|
30
68
|
work.
|
|
31
69
|
|
|
32
|
-
#
|
|
70
|
+
# v0.4.1
|
|
33
71
|
|
|
34
72
|
- Simplified the DOM renderer example and fixed rendering of email
|
|
35
73
|
autolinks and literal-text line joins.
|
|
@@ -40,7 +78,7 @@
|
|
|
40
78
|
tooling.
|
|
41
79
|
- Package exports and WASM ABI remain unchanged.
|
|
42
80
|
|
|
43
|
-
#
|
|
81
|
+
# v0.4.0
|
|
44
82
|
|
|
45
83
|
- Image descriptions are alt text, not inline formatting. CommonMark
|
|
46
84
|
keeps the text between the markup (`` → `a b`); Markdown
|
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 = 9;
|
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/cells; 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
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import { ABI_VERSION, Join, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ParseMode } from './abi.js';
|
|
2
2
|
export { ABI_VERSION, Dialect, HtmlMode, Join, ListFlags, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ParseMode, ReferenceStyle, TableAlignment } from './abi.js';
|
|
3
|
+
const PARSE_MODE_FLAGS = ParseMode.COMMONMARK | ParseMode.NO_TABLE;
|
|
3
4
|
const PAGE_SIZE = 64 * 1024;
|
|
4
5
|
const ALIGNMENT = 16;
|
|
5
6
|
const MAX_INPUT_BYTES = 64 * 1024 * 1024;
|
|
6
7
|
const MAX_ARENA_BYTES = 512 * 1024 * 1024;
|
|
7
8
|
const MAX_WASM_ADDRESS = 0x7fffffff;
|
|
9
|
+
const encoder = new TextEncoder();
|
|
8
10
|
const decoder = new TextDecoder('utf-8', { ignoreBOM: true });
|
|
11
|
+
function hasBlockRange(type) {
|
|
12
|
+
return (type >= NodeType.PARAGRAPH && type <= NodeType.REF_DEF) || type === NodeType.CUSTOM ||
|
|
13
|
+
type === NodeType.TABLE || type === NodeType.TABLE_ROW || type === NodeType.TABLE_CELL;
|
|
14
|
+
}
|
|
9
15
|
function align(value) {
|
|
10
16
|
if (!Number.isSafeInteger(value) || value < 0 || value > MAX_WASM_ADDRESS - (ALIGNMENT - 1)) {
|
|
11
17
|
throw new Error('mausmark: invalid WASM heap base');
|
|
@@ -28,13 +34,11 @@ function isField(type) {
|
|
|
28
34
|
class PackedDocument {
|
|
29
35
|
#source;
|
|
30
36
|
#decodeSource;
|
|
31
|
-
#nodes;
|
|
32
37
|
#view;
|
|
33
38
|
#nodeCount;
|
|
34
39
|
constructor(source, nodes, nodeCount, decodeSource) {
|
|
35
40
|
this.#source = source;
|
|
36
41
|
this.#decodeSource = decodeSource;
|
|
37
|
-
this.#nodes = nodes;
|
|
38
42
|
this.#view = new DataView(nodes.buffer, nodes.byteOffset, nodes.byteLength);
|
|
39
43
|
this.#nodeCount = nodeCount;
|
|
40
44
|
this.validateTable();
|
|
@@ -52,7 +56,9 @@ class PackedDocument {
|
|
|
52
56
|
}
|
|
53
57
|
flags(index) {
|
|
54
58
|
this.checkIndex(index);
|
|
55
|
-
|
|
59
|
+
const bits = this.#view.getInt16(index * NODE_SIZE + NodeLayout.FLAGS, true);
|
|
60
|
+
const type = this.type(index);
|
|
61
|
+
return type === NodeType.HTML_BLOCK ? 0 : type === NodeType.LIST ? bits & 3 : bits;
|
|
56
62
|
}
|
|
57
63
|
subtreeEnd(index) {
|
|
58
64
|
this.checkIndex(index);
|
|
@@ -60,11 +66,36 @@ class PackedDocument {
|
|
|
60
66
|
}
|
|
61
67
|
offset(index) {
|
|
62
68
|
this.checkIndex(index);
|
|
69
|
+
const type = this.type(index);
|
|
70
|
+
if (type === NodeType.HTML_BLOCK) {
|
|
71
|
+
const base = index * NODE_SIZE, aux = this.#view.getInt16(base + NodeLayout.FLAGS, true);
|
|
72
|
+
return aux < 0 ? 0 : this.#view.getUint32(base + NodeLayout.OFFSET, true) + aux;
|
|
73
|
+
}
|
|
74
|
+
if (hasBlockRange(type) && (type !== NodeType.LIST || !this.length(index)))
|
|
75
|
+
return 0;
|
|
63
76
|
return this.#view.getUint32(index * NODE_SIZE + NodeLayout.OFFSET, true);
|
|
64
77
|
}
|
|
65
78
|
length(index) {
|
|
66
79
|
this.checkIndex(index);
|
|
67
|
-
|
|
80
|
+
const type = this.type(index);
|
|
81
|
+
if (type === NodeType.LIST)
|
|
82
|
+
return this.#view.getUint16(index * NODE_SIZE + NodeLayout.FLAGS, true) >>> 2;
|
|
83
|
+
if (type === NodeType.HTML_BLOCK) {
|
|
84
|
+
const base = index * NODE_SIZE, aux = this.#view.getInt16(base + NodeLayout.FLAGS, true);
|
|
85
|
+
return aux < 0 ? 0 : this.#view.getUint32(base + NodeLayout.BLOCK_END, true) -
|
|
86
|
+
this.#view.getUint32(base + NodeLayout.OFFSET, true) - aux;
|
|
87
|
+
}
|
|
88
|
+
if (hasBlockRange(type))
|
|
89
|
+
return 0;
|
|
90
|
+
return this.#view.getUint32(index * NODE_SIZE + NodeLayout.LENGTH, true);
|
|
91
|
+
}
|
|
92
|
+
blockRange(index) {
|
|
93
|
+
const type = this.type(index);
|
|
94
|
+
if (!hasBlockRange(type))
|
|
95
|
+
return undefined;
|
|
96
|
+
const base = index * NODE_SIZE;
|
|
97
|
+
return { start: this.#view.getUint32(base + NodeLayout.OFFSET, true),
|
|
98
|
+
end: this.#view.getUint32(base + NodeLayout.BLOCK_END, true) };
|
|
68
99
|
}
|
|
69
100
|
text(index) {
|
|
70
101
|
this.checkIndex(index);
|
|
@@ -135,18 +166,41 @@ class PackedDocument {
|
|
|
135
166
|
return decoder.decode(this.#source.subarray(offset, offset + length));
|
|
136
167
|
}
|
|
137
168
|
validateTable() {
|
|
138
|
-
|
|
169
|
+
const count = this.#nodeCount;
|
|
170
|
+
const view = this.#view;
|
|
171
|
+
const sourceLength = this.#source.byteLength;
|
|
172
|
+
if (!Number.isInteger(count) || count < 0 || view.byteLength !== count * NODE_SIZE) {
|
|
139
173
|
throw new Error('mausmark: malformed AST node table');
|
|
140
174
|
}
|
|
141
|
-
for (let index = 0; index <
|
|
142
|
-
const
|
|
143
|
-
const
|
|
144
|
-
|
|
175
|
+
for (let index = 0; index < count; index += 1) {
|
|
176
|
+
const base = index * NODE_SIZE;
|
|
177
|
+
const type = view.getUint8(base + NodeLayout.TYPE);
|
|
178
|
+
const end = view.getInt32(base + NodeLayout.SUBTREE_END, true);
|
|
179
|
+
if (type < NodeType.DOC || type > NODE_TYPE_MAX || end <= index || end > count) {
|
|
145
180
|
throw new Error(`mausmark: malformed AST node ${index}`);
|
|
146
181
|
}
|
|
147
|
-
const offset =
|
|
148
|
-
|
|
149
|
-
|
|
182
|
+
const offset = view.getUint32(base + NodeLayout.OFFSET, true);
|
|
183
|
+
if (hasBlockRange(type)) {
|
|
184
|
+
const blockEnd = view.getUint32(base + NodeLayout.BLOCK_END, true);
|
|
185
|
+
if (blockEnd > sourceLength || offset > blockEnd || (offset === blockEnd && type !== NodeType.TABLE_CELL)) {
|
|
186
|
+
throw new Error(`mausmark: malformed block range on node ${index}`);
|
|
187
|
+
}
|
|
188
|
+
if (type === NodeType.HTML_BLOCK) {
|
|
189
|
+
const aux = view.getInt16(base + NodeLayout.FLAGS, true);
|
|
190
|
+
// A leaf's payload skips aux (0-3) indent bytes and ends with the block.
|
|
191
|
+
const valid = end === index + 1 ? aux >= 0 && aux <= 3 && offset + aux < blockEnd : 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
|
}
|
|
@@ -162,17 +216,20 @@ class WasmParser {
|
|
|
162
216
|
parse(markdown, mode = ParseMode.MARKDOWN) {
|
|
163
217
|
if (typeof markdown !== 'string')
|
|
164
218
|
throw new TypeError('mausmark: markdown must be a string');
|
|
165
|
-
|
|
219
|
+
// A typo such as 'cm' must not silently parse as Markdown.
|
|
220
|
+
if (!Number.isInteger(mode) || (mode & ~PARSE_MODE_FLAGS) !== 0) {
|
|
221
|
+
throw new TypeError('mausmark: mode must be a combination of ParseMode flags');
|
|
222
|
+
}
|
|
223
|
+
const source = encoder.encode(markdown);
|
|
166
224
|
if (source.byteLength > MAX_INPUT_BYTES) {
|
|
167
225
|
throw new RangeError(`mausmark: input exceeds the ${MAX_INPUT_BYTES / 1024 / 1024} MiB limit`);
|
|
168
226
|
}
|
|
169
227
|
const inputPointer = align(this.#wasm.heapBase);
|
|
170
228
|
const arenaPointer = align(checkedAdd(inputPointer, source.byteLength, 'input address exceeds the WASM address limit'));
|
|
171
229
|
let arenaSize = initialArenaSize(source.byteLength);
|
|
230
|
+
this.ensureMemory(checkedAdd(arenaPointer, arenaSize, 'input and arena exceed the WASM address limit'));
|
|
231
|
+
new Uint8Array(this.#wasm.memory.buffer, inputPointer, source.byteLength).set(source);
|
|
172
232
|
while (true) {
|
|
173
|
-
const required = checkedAdd(arenaPointer, arenaSize, 'input and arena exceed the WASM address limit');
|
|
174
|
-
this.ensureMemory(required);
|
|
175
|
-
new Uint8Array(this.#wasm.memory.buffer, inputPointer, source.byteLength).set(source);
|
|
176
233
|
const headerPointer = this.#wasm.parseDom(inputPointer, source.byteLength, arenaPointer, arenaSize, mode);
|
|
177
234
|
if (headerPointer >= 0)
|
|
178
235
|
return this.copyDocument(source, headerPointer);
|
|
@@ -180,6 +237,7 @@ class WasmParser {
|
|
|
180
237
|
throw new RangeError(`mausmark: parsing requires more than the ${MAX_ARENA_BYTES / 1024 / 1024} MiB arena limit`);
|
|
181
238
|
}
|
|
182
239
|
arenaSize = Math.min(MAX_ARENA_BYTES, arenaSize * 2);
|
|
240
|
+
this.ensureMemory(checkedAdd(arenaPointer, arenaSize, 'input and arena exceed the WASM address limit'));
|
|
183
241
|
}
|
|
184
242
|
}
|
|
185
243
|
ensureMemory(required) {
|
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);
|
|
@@ -180,7 +174,16 @@ export function renderToDom(doc, parent) {
|
|
|
180
174
|
}
|
|
181
175
|
}
|
|
182
176
|
|
|
183
|
-
|
|
177
|
+
// Lines carry no line ending; the last one's is implied.
|
|
178
|
+
let content = "";
|
|
179
|
+
let lines = false;
|
|
180
|
+
for (const child of doc.children(index)) {
|
|
181
|
+
if (doc.type(child) === NodeType.LABEL) continue;
|
|
182
|
+
const join = doc.level(child);
|
|
183
|
+
content += (join === Join.NL ? "\n" : (join === Join.SPACE ? " " : "")) + doc.text(child);
|
|
184
|
+
lines = true;
|
|
185
|
+
}
|
|
186
|
+
code.textContent = lines ? content + "\n" : "";
|
|
184
187
|
const pre = document.createElement("pre");
|
|
185
188
|
pre.appendChild(code);
|
|
186
189
|
into.appendChild(pre);
|
|
@@ -266,9 +269,13 @@ export function renderToDom(doc, parent) {
|
|
|
266
269
|
|
|
267
270
|
case NodeType.HTML_BLOCK:
|
|
268
271
|
case NodeType.HTML_INLINE:
|
|
272
|
+
// Other source-backed content stays text; customize as needed. A
|
|
273
|
+
// tag spanning lines is one chunk per line, later ones led by Join.NL.
|
|
274
|
+
text((doc.level(index) === Join.NL ? "\n" : "") + doc.joinedText(index), into);
|
|
275
|
+
return;
|
|
269
276
|
case NodeType.CUSTOM:
|
|
270
|
-
//
|
|
271
|
-
text(doc.joinedText(index), into);
|
|
277
|
+
// Lines carry no line ending; the last one's is implied.
|
|
278
|
+
text(doc.subtreeEnd(index) > index + 1 ? doc.joinedText(index) + "\n" : "", into);
|
|
272
279
|
return;
|
|
273
280
|
|
|
274
281
|
case NodeType.TEXT:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mausmark",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "A fast, dependency-free Markdown parser compiled to WebAssembly",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -27,23 +27,23 @@
|
|
|
27
27
|
"HTML-LICENSE.txt"
|
|
28
28
|
],
|
|
29
29
|
"scripts": {
|
|
30
|
-
"clean": "rm -rf dist",
|
|
31
|
-
"build:wasm": "./build.sh wasm --ast-only",
|
|
32
|
-
"build:js": "./build.sh codegen && tsc -p tsconfig.json",
|
|
33
30
|
"build": "npm run clean && npm run build:wasm && npm run build:js",
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"test:types": "tsc -p tsconfig.test.json --noEmit && tsc -p tsconfig.references.json",
|
|
37
|
-
"test:browser": "node test/browser/run.mjs",
|
|
31
|
+
"build:js": "./build.sh codegen && tsc -p tsconfig.json",
|
|
32
|
+
"build:wasm": "./build.sh wasm --ast-only",
|
|
38
33
|
"bench:browser": "npm run build && ./build.sh wasm && node benchmark/browser/run.mjs",
|
|
34
|
+
"check:entities": "node tools/entities.mjs --check",
|
|
35
|
+
"check:unicode": "node tools/unicode.mjs --check && node tools/case-fold.mjs --check",
|
|
36
|
+
"check:wasm-size": "node test/wasm_size.mjs",
|
|
39
37
|
"setup:chrome": "node test/browser/setup-chrome.mjs",
|
|
40
|
-
"test
|
|
38
|
+
"test": "npm run test:build && npm run test:layers && npm run build && npm run check:wasm-size && npm run test:package && npm run test:types && npm run test:browser",
|
|
39
|
+
"test:browser": "node test/browser/run.mjs",
|
|
41
40
|
"test:build": "node --test test/build.test.mjs",
|
|
42
|
-
"check:unicode": "node tools/unicode.mjs --check && node tools/case-fold.mjs --check",
|
|
43
|
-
"test:unicode": "node --test test/unicode.test.mjs test/case-fold.test.mjs",
|
|
44
|
-
"check:entities": "node tools/entities.mjs --check",
|
|
45
41
|
"test:entities": "node --test test/entities.test.mjs",
|
|
46
|
-
"test": "
|
|
42
|
+
"test:layers": "node --test test/layers.test.mjs",
|
|
43
|
+
"test:package": "node --test test/npm-package.test.mjs test/dom-example.test.mjs test/text-decoding.test.mjs test/webdriver.test.mjs test/browser-benchmark.test.mjs test/wasm-size.test.mjs test/source-retention.test.mjs test/test-registration.test.mjs test/reference-matching.test.mjs",
|
|
44
|
+
"test:types": "tsc -p tsconfig.test.json --noEmit && tsc -p tsconfig.references.json",
|
|
45
|
+
"test:unicode": "node --test test/unicode.test.mjs test/case-fold.test.mjs",
|
|
46
|
+
"clean": "rm -rf dist",
|
|
47
47
|
"prepack": "npm test"
|
|
48
48
|
},
|
|
49
49
|
"engines": {
|