mausmark 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 +57 -26
- package/RELEASE-NOTES.md +45 -7
- package/dist/abi.d.ts +18 -2
- package/dist/abi.js +16 -2
- package/dist/browser.d.ts +1 -1
- package/dist/browser.js +1 -1
- package/dist/core.d.ts +11 -8
- package/dist/core.js +5 -5
- package/dist/mausmark.wasm +0 -0
- package/dist/node.d.ts +1 -1
- package/dist/node.js +1 -5
- package/dom-example.js +187 -225
- package/package.json +6 -3
package/README.md
CHANGED
|
@@ -72,6 +72,40 @@ the document so that you could reproduce it exactly (minus whitespace).
|
|
|
72
72
|
Raw HTML is returned as `NodeType.HTML_BLOCK` or `NodeType.HTML_INLINE`;
|
|
73
73
|
sanitize it when parsing untrusted input.
|
|
74
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.
|
|
108
|
+
|
|
75
109
|
In the default Markdown dialect, `{text **bold** text}` is a
|
|
76
110
|
`NodeType.BRACE_SPAN` with parsed inline children. `text(index)` returns
|
|
77
111
|
the exact original interior, `text **bold** text`, excluding its outer
|
|
@@ -100,7 +134,7 @@ output images, you don't need to change anything.
|
|
|
100
134
|
description with `joinedText(index)`.
|
|
101
135
|
- CommonMark image descriptions should be read the same way; they are
|
|
102
136
|
plain text now. A reference inside one stays a LINK or IMAGE node
|
|
103
|
-
for your renderer to resolve
|
|
137
|
+
for your renderer to resolve.
|
|
104
138
|
|
|
105
139
|
For more details, see the [release notes](RELEASE-NOTES.md).
|
|
106
140
|
|
|
@@ -147,13 +181,8 @@ HTML, custom nodes, list markers, and entity decoding:
|
|
|
147
181
|
const parser = await createParser()
|
|
148
182
|
console.log(plainText(parser.parse("# Hello\n\nA **wrapped** paragraph.")))
|
|
149
183
|
|
|
150
|
-
For a
|
|
151
|
-
|
|
152
|
-
attributes through `setAttribute`, so it never has to escape HTML by
|
|
153
|
-
hand. It covers every node type, including entity decoding, reference
|
|
154
|
-
lookup, tight lists, fence info strings and image alt text, and it
|
|
155
|
-
leaves raw HTML inert. It is an example, not an export: copy it and
|
|
156
|
-
adapt it.
|
|
184
|
+
For a starting point that renders to the DOM, the package includes
|
|
185
|
+
`dom-example.js`, an incomplete renderer meant to be copied and adapted.
|
|
157
186
|
|
|
158
187
|
Things to decide when building a richer renderer:
|
|
159
188
|
|
|
@@ -165,8 +194,10 @@ Things to decide when building a richer renderer:
|
|
|
165
194
|
render arbitrary inline markup. In CommonMark, a `LITERAL_TEXT` leaf
|
|
166
195
|
must not be entity-decoded with its neighboring `TEXT`.
|
|
167
196
|
- `link(index)` works on `LINK`, `IMAGE`, and `REF_DEF` and returns
|
|
168
|
-
**raw** URL, title, label, and `ReferenceStyle`.
|
|
169
|
-
|
|
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;
|
|
170
201
|
`link()` joins them without truncation. Inline links and autolinks
|
|
171
202
|
already have destinations; `FULL`, `COLLAPSED`, and `SHORTCUT` need
|
|
172
203
|
a definition lookup. Scan all node indexes for `REF_DEF` (including
|
|
@@ -193,17 +224,17 @@ Things to decide when building a richer renderer:
|
|
|
193
224
|
|
|
194
225
|
## Performance
|
|
195
226
|
|
|
196
|
-
The SIMD WebAssembly parser build is
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
Bun 1.4.2, compiled with Clang 22.1.7 at `-Os` with LTO:
|
|
227
|
+
The SIMD WebAssembly parser build is 74,348 bytes uncompressed or 26,982
|
|
228
|
+
bytes with Brotli (quality 11, offline precompression). Source-to-AST
|
|
229
|
+
measurements on November 20, 2026, on an Apple M3 Pro, Node.js 26.3.1
|
|
230
|
+
and Bun 1.4.2, compiled with Clang 22.1.7 at `-Os` with LTO:
|
|
200
231
|
|
|
201
232
|
| Input | Node (µs) | Bun (µs) | Node (GB/s) | Bun (GB/s) |
|
|
202
233
|
| ---------------------- | --------: | -------: | ----------: | ---------: |
|
|
203
|
-
| Moby-Dick (1.2 MB) | ~
|
|
204
|
-
| Line-wrapped Moby-Dick | ~
|
|
205
|
-
| Pro Git chapters |
|
|
206
|
-
| tldr pages | 1.
|
|
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 |
|
|
207
238
|
|
|
208
239
|
These are warmed parser-export timings with preloaded UTF-8 input and a
|
|
209
240
|
reused arena, including JavaScript iteration and export-call overhead.
|
|
@@ -211,23 +242,23 @@ They exclude initialization, input encoding/copying, AST snapshot
|
|
|
211
242
|
copying, and rendering; they are not end-to-end timings of the package’s
|
|
212
243
|
`parse()` method. The corpus rows (Pro Git, tldr) report average time
|
|
213
244
|
per document; figures are medians across three alternating passes, each
|
|
214
|
-
with five timing samples.
|
|
215
|
-
|
|
216
|
-
bytes.
|
|
245
|
+
with five timing samples. Both Moby-Dick inputs and Pro Git use
|
|
246
|
+
Markdown; tldr uses CommonMark. GB/s uses decimal bytes.
|
|
217
247
|
|
|
218
248
|
## Notes
|
|
219
249
|
|
|
220
250
|
- Input is limited to 64 MiB; parser arena growth is limited to 512
|
|
221
251
|
MiB.
|
|
222
|
-
- Two input dialects. `Dialect.MARKDOWN` (the default)
|
|
223
|
-
Markdown
|
|
224
|
-
|
|
252
|
+
- Two input dialects. `Dialect.MARKDOWN` (the default) extends
|
|
253
|
+
original Markdown with brace syntax and pipe tables;
|
|
254
|
+
`Dialect.COMMONMARK` is based on CommonMark 0.31.2, currently
|
|
255
|
+
649/652 (99.5%) of its spec suite byte-exact. Three
|
|
225
256
|
definition-dependent reference-link cases are intentionally
|
|
226
257
|
unresolved.
|
|
227
258
|
- Unlike CommonMark, an image description is plain alt text, not
|
|
228
259
|
inline content.
|
|
229
|
-
- No GFM
|
|
230
|
-
|
|
260
|
+
- No full GFM dialect: strikethrough, task lists and autolink literals
|
|
261
|
+
are not supported.
|
|
231
262
|
|
|
232
263
|
The npm package contains ESM bindings, TypeScript declarations,
|
|
233
264
|
precompiled WebAssembly, and the renderer example. The implementation
|
package/RELEASE-NOTES.md
CHANGED
|
@@ -1,14 +1,52 @@
|
|
|
1
|
+
# Mausmark v0.5.0
|
|
2
|
+
|
|
3
|
+
- Both dialects now recognize pipe tables. Custom renderers can handle
|
|
4
|
+
`TABLE`, `TABLE_ROW` and `TABLE_CELL`; header cells carry column
|
|
5
|
+
alignment.
|
|
6
|
+
- The formatter pads table columns to a common width, counting wide
|
|
7
|
+
characters as two columns and combining marks as zero. Numbers in
|
|
8
|
+
right-aligned columns line up on the decimal point.
|
|
9
|
+
- WASM ABI is now 7. Deploy the matching JS bindings and WASM
|
|
10
|
+
together. Node records remain 16 bytes.
|
|
11
|
+
- Faster WASM parsing of adjacent backslash escapes and faster
|
|
12
|
+
native/WASM HTML rendering of code chunks that are contiguous in the
|
|
13
|
+
source.
|
|
14
|
+
- Fixed native HTML escaping for multiline reference titles on links
|
|
15
|
+
and images when using the CommonMark parser with default HTML
|
|
16
|
+
output.
|
|
17
|
+
- Faster parsing of deeply nested brackets and consecutive quoted
|
|
18
|
+
reference definitions in CommonMark.
|
|
19
|
+
- Faster Markdown formatting for long paragraphs where wrapping would
|
|
20
|
+
turn text into a list or another block. Existing line breaks chosen
|
|
21
|
+
by the formatter are preserved.
|
|
22
|
+
- Migration: CommonMark input now parses a pipe header followed by a
|
|
23
|
+
divider row as a table, where v0.4 produced paragraphs. To keep the
|
|
24
|
+
previous output, use `ParseMode.COMMONMARK | ParseMode.NO_TABLE`, or
|
|
25
|
+
`--cm --no-table` in the CLI. Existing `Dialect` constants still
|
|
26
|
+
work.
|
|
27
|
+
|
|
28
|
+
# Mausmark v0.4.1
|
|
29
|
+
|
|
30
|
+
- Simplified the DOM renderer example and fixed rendering of email
|
|
31
|
+
autolinks and literal-text line joins.
|
|
32
|
+
- Fixed: Node and browser bindings preserve a leading UTF-8 BOM when
|
|
33
|
+
decoding source ranges instead of silently stripping it.
|
|
34
|
+
- Faster emphasis classification in native ARM64 builds.
|
|
35
|
+
- Added real-browser regression coverage and browser benchmark
|
|
36
|
+
tooling.
|
|
37
|
+
- Package exports and WASM ABI remain unchanged.
|
|
38
|
+
|
|
1
39
|
# Mausmark v0.4.0
|
|
2
40
|
|
|
3
41
|
- Image descriptions are alt text, not inline formatting. CommonMark
|
|
4
|
-
keeps the text between the markup (`` → `a b`);
|
|
5
|
-
|
|
6
|
-
|
|
42
|
+
keeps the text between the markup (`` → `a b`); Markdown
|
|
43
|
+
keeps the literal source, one chunk per line. Reference nodes remain
|
|
44
|
+
until the renderer resolves them or restores literal brackets.
|
|
7
45
|
- Image descriptions nested deeper than 32 levels now stay literal.
|
|
8
|
-
- New `mausmark/references` export:
|
|
9
|
-
'markdown' | 'commonmark')`.
|
|
10
|
-
- DOM example: `renderToDom(doc, parent, {referenceMatching})`,
|
|
11
|
-
empty titles, entities decoded in alt text.
|
|
46
|
+
- New `mausmark/references` export:
|
|
47
|
+
`normalizeReferenceLabel(label, 'markdown' | 'commonmark')`.
|
|
48
|
+
- DOM example: `renderToDom(doc, parent, {referenceMatching})`,
|
|
49
|
+
explicit empty titles, entities decoded in alt text.
|
|
12
50
|
- Fixed: CommonMark list markers indented four or more columns no
|
|
13
51
|
longer interrupt a paragraph.
|
|
14
52
|
- Fixed: an HTML block or fence on a line without `>` ends the
|
package/dist/abi.d.ts
CHANGED
|
@@ -25,6 +25,9 @@ export declare const NodeType: Readonly<{
|
|
|
25
25
|
readonly TITLE: 23;
|
|
26
26
|
readonly LABEL: 24;
|
|
27
27
|
readonly BRACE_SPAN: 25;
|
|
28
|
+
readonly TABLE: 26;
|
|
29
|
+
readonly TABLE_ROW: 27;
|
|
30
|
+
readonly TABLE_CELL: 28;
|
|
28
31
|
}>;
|
|
29
32
|
export type NodeType = (typeof NodeType)[keyof typeof NodeType];
|
|
30
33
|
export declare const ReferenceStyle: Readonly<{
|
|
@@ -42,6 +45,13 @@ export declare const Join: Readonly<{
|
|
|
42
45
|
readonly NL: 2;
|
|
43
46
|
}>;
|
|
44
47
|
export type Join = (typeof Join)[keyof typeof Join];
|
|
48
|
+
export declare const TableAlignment: Readonly<{
|
|
49
|
+
readonly DEFAULT: 0;
|
|
50
|
+
readonly LEFT: 1;
|
|
51
|
+
readonly CENTER: 2;
|
|
52
|
+
readonly RIGHT: 3;
|
|
53
|
+
}>;
|
|
54
|
+
export type TableAlignment = (typeof TableAlignment)[keyof typeof TableAlignment];
|
|
45
55
|
export declare const ListFlags: Readonly<{
|
|
46
56
|
readonly ORDERED: 1;
|
|
47
57
|
readonly LOOSE: 2;
|
|
@@ -52,13 +62,19 @@ export declare const Dialect: Readonly<{
|
|
|
52
62
|
readonly COMMONMARK: 1;
|
|
53
63
|
}>;
|
|
54
64
|
export type Dialect = (typeof Dialect)[keyof typeof Dialect];
|
|
65
|
+
export declare const ParseMode: Readonly<{
|
|
66
|
+
readonly MARKDOWN: 0;
|
|
67
|
+
readonly COMMONMARK: 1;
|
|
68
|
+
readonly NO_TABLE: 2;
|
|
69
|
+
}>;
|
|
70
|
+
export type ParseMode = number;
|
|
55
71
|
export declare const HtmlMode: Readonly<{
|
|
56
72
|
readonly FULL: 0;
|
|
57
73
|
readonly FRAGMENT: 1;
|
|
58
74
|
readonly COMMONMARK: 2;
|
|
59
75
|
}>;
|
|
60
76
|
export type HtmlMode = (typeof HtmlMode)[keyof typeof HtmlMode];
|
|
61
|
-
export declare const NODE_TYPE_MAX:
|
|
77
|
+
export declare const NODE_TYPE_MAX: 28;
|
|
62
78
|
export declare const NODE_SIZE = 16;
|
|
63
79
|
export declare const NodeLayout: Readonly<{
|
|
64
80
|
TYPE: 0;
|
|
@@ -68,4 +84,4 @@ export declare const NodeLayout: Readonly<{
|
|
|
68
84
|
OFFSET: 8;
|
|
69
85
|
LENGTH: 12;
|
|
70
86
|
}>;
|
|
71
|
-
export declare const ABI_VERSION =
|
|
87
|
+
export declare const ABI_VERSION = 7;
|
package/dist/abi.js
CHANGED
|
@@ -26,6 +26,9 @@ export const NodeType = Object.freeze({
|
|
|
26
26
|
TITLE: 23,
|
|
27
27
|
LABEL: 24,
|
|
28
28
|
BRACE_SPAN: 25,
|
|
29
|
+
TABLE: 26,
|
|
30
|
+
TABLE_ROW: 27,
|
|
31
|
+
TABLE_CELL: 28,
|
|
29
32
|
});
|
|
30
33
|
export const ReferenceStyle = Object.freeze({
|
|
31
34
|
INLINE: 0,
|
|
@@ -40,6 +43,12 @@ export const Join = Object.freeze({
|
|
|
40
43
|
SPACE: 1,
|
|
41
44
|
NL: 2,
|
|
42
45
|
});
|
|
46
|
+
export const TableAlignment = Object.freeze({
|
|
47
|
+
DEFAULT: 0,
|
|
48
|
+
LEFT: 1,
|
|
49
|
+
CENTER: 2,
|
|
50
|
+
RIGHT: 3,
|
|
51
|
+
});
|
|
43
52
|
export const ListFlags = Object.freeze({
|
|
44
53
|
ORDERED: 1,
|
|
45
54
|
LOOSE: 2,
|
|
@@ -48,15 +57,20 @@ export const Dialect = Object.freeze({
|
|
|
48
57
|
MARKDOWN: 0,
|
|
49
58
|
COMMONMARK: 1,
|
|
50
59
|
});
|
|
60
|
+
export const ParseMode = Object.freeze({
|
|
61
|
+
MARKDOWN: 0,
|
|
62
|
+
COMMONMARK: 1,
|
|
63
|
+
NO_TABLE: 2,
|
|
64
|
+
});
|
|
51
65
|
export const HtmlMode = Object.freeze({
|
|
52
66
|
FULL: 0,
|
|
53
67
|
FRAGMENT: 1,
|
|
54
68
|
COMMONMARK: 2,
|
|
55
69
|
});
|
|
56
|
-
export const NODE_TYPE_MAX = NodeType.
|
|
70
|
+
export const NODE_TYPE_MAX = NodeType.TABLE_CELL;
|
|
57
71
|
export const NODE_SIZE = 16;
|
|
58
72
|
export const NodeLayout = Object.freeze({
|
|
59
73
|
TYPE: 0, LEVEL: 1, FLAGS: 2, SUBTREE_END: 4,
|
|
60
74
|
OFFSET: 8, LENGTH: 12,
|
|
61
75
|
});
|
|
62
|
-
export const ABI_VERSION =
|
|
76
|
+
export const ABI_VERSION = 7;
|
package/dist/browser.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { ABI_VERSION, Dialect, HtmlMode, Join, ListFlags, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ReferenceStyle, } from './core.js';
|
|
1
|
+
export { ABI_VERSION, Dialect, HtmlMode, Join, ListFlags, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ParseMode, ReferenceStyle, TableAlignment, } from './core.js';
|
|
2
2
|
export type { LinkFields, MausmarkDocument, MausmarkParser } from './core.js';
|
|
3
3
|
/** Fetch and initialize the package-relative WASM parser in a browser. */
|
|
4
4
|
export declare function createParser(): Promise<import('./core.js').MausmarkParser>;
|
package/dist/browser.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parserFromInstance } from './core.js';
|
|
2
|
-
export { ABI_VERSION, Dialect, HtmlMode, Join, ListFlags, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ReferenceStyle, } from './core.js';
|
|
2
|
+
export { ABI_VERSION, Dialect, HtmlMode, Join, ListFlags, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ParseMode, ReferenceStyle, TableAlignment, } from './core.js';
|
|
3
3
|
/** Fetch and initialize the package-relative WASM parser in a browser. */
|
|
4
4
|
export async function createParser() {
|
|
5
5
|
const response = await fetch(new URL('./mausmark.wasm', import.meta.url));
|
package/dist/core.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NodeType, ParseMode } from './abi.js';
|
|
2
2
|
import type { ReferenceStyle } from './abi.js';
|
|
3
|
-
export { ABI_VERSION, Dialect, HtmlMode, Join, ListFlags, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ReferenceStyle } from './abi.js';
|
|
4
|
-
/** Raw, unresolved LINK/IMAGE/REF_DEF fields.
|
|
5
|
-
*
|
|
3
|
+
export { ABI_VERSION, Dialect, HtmlMode, Join, ListFlags, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ParseMode, ReferenceStyle, TableAlignment } from './abi.js';
|
|
4
|
+
/** Raw, unresolved LINK/IMAGE/REF_DEF fields. In table cells the table-syntax
|
|
5
|
+
* backslash before an escaped pipe is removed, including in these fields.
|
|
6
|
+
* Absent and empty fields are `''`; a leading TITLE node distinguishes an
|
|
7
|
+
* explicit empty title from no title. */
|
|
6
8
|
export interface LinkFields {
|
|
7
|
-
/** Destination as written: backslash escapes and entities are not processed. */
|
|
9
|
+
/** Destination as written: other backslash escapes and entities are not processed. */
|
|
8
10
|
readonly url: string;
|
|
9
11
|
/** Title as written, without its quotes. */
|
|
10
12
|
readonly title: string;
|
|
@@ -26,7 +28,8 @@ export interface MausmarkDocument {
|
|
|
26
28
|
type(index: number): NodeType;
|
|
27
29
|
/**
|
|
28
30
|
* Type-specific value: the heading level on HEADING, the `ReferenceStyle` on
|
|
29
|
-
* LINK and IMAGE, the `Join` code on TEXT/LITERAL_TEXT, the marker on LIST
|
|
31
|
+
* LINK and IMAGE, the `Join` code on TEXT/LITERAL_TEXT, the marker on LIST,
|
|
32
|
+
* or `TableAlignment` on a header TABLE_CELL.
|
|
30
33
|
*/
|
|
31
34
|
level(index: number): number;
|
|
32
35
|
/** `ListFlags` bits on LIST and LIST_ITEM. Other bits and nodes are internal. */
|
|
@@ -56,8 +59,8 @@ export interface MausmarkDocument {
|
|
|
56
59
|
}
|
|
57
60
|
/** A synchronously usable parser after its WASM module has initialized. */
|
|
58
61
|
export interface MausmarkParser {
|
|
59
|
-
/** Parse `markdown
|
|
60
|
-
parse(markdown: string,
|
|
62
|
+
/** Parse `markdown` with combinable ParseMode flags (default: Markdown). */
|
|
63
|
+
parse(markdown: string, mode?: ParseMode): MausmarkDocument;
|
|
61
64
|
}
|
|
62
65
|
/** @internal Shared loader validation used by the Node and browser entry points. */
|
|
63
66
|
export declare function parserFromInstance(instance: WebAssembly.Instance, createDecoder?: SourceDecoderFactory): MausmarkParser;
|
package/dist/core.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { ABI_VERSION,
|
|
2
|
-
export { ABI_VERSION, Dialect, HtmlMode, Join, ListFlags, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ReferenceStyle } from './abi.js';
|
|
1
|
+
import { ABI_VERSION, Join, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ParseMode } from './abi.js';
|
|
2
|
+
export { ABI_VERSION, Dialect, HtmlMode, Join, ListFlags, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ParseMode, ReferenceStyle, TableAlignment } from './abi.js';
|
|
3
3
|
const PAGE_SIZE = 64 * 1024;
|
|
4
4
|
const ALIGNMENT = 16;
|
|
5
5
|
const MAX_INPUT_BYTES = 64 * 1024 * 1024;
|
|
6
6
|
const MAX_ARENA_BYTES = 512 * 1024 * 1024;
|
|
7
7
|
const MAX_WASM_ADDRESS = 0x7fffffff;
|
|
8
|
-
const decoder = new TextDecoder();
|
|
8
|
+
const decoder = new TextDecoder('utf-8', { ignoreBOM: true });
|
|
9
9
|
function align(value) {
|
|
10
10
|
if (!Number.isSafeInteger(value) || value < 0 || value > MAX_WASM_ADDRESS - (ALIGNMENT - 1)) {
|
|
11
11
|
throw new Error('mausmark: invalid WASM heap base');
|
|
@@ -159,7 +159,7 @@ class WasmParser {
|
|
|
159
159
|
this.#wasm = wasm;
|
|
160
160
|
this.#createSourceDecoder = createDecoder;
|
|
161
161
|
}
|
|
162
|
-
parse(markdown,
|
|
162
|
+
parse(markdown, mode = ParseMode.MARKDOWN) {
|
|
163
163
|
if (typeof markdown !== 'string')
|
|
164
164
|
throw new TypeError('mausmark: markdown must be a string');
|
|
165
165
|
const source = new TextEncoder().encode(markdown);
|
|
@@ -173,7 +173,7 @@ class WasmParser {
|
|
|
173
173
|
const required = checkedAdd(arenaPointer, arenaSize, 'input and arena exceed the WASM address limit');
|
|
174
174
|
this.ensureMemory(required);
|
|
175
175
|
new Uint8Array(this.#wasm.memory.buffer, inputPointer, source.byteLength).set(source);
|
|
176
|
-
const headerPointer = this.#wasm.parseDom(inputPointer, source.byteLength, arenaPointer, arenaSize,
|
|
176
|
+
const headerPointer = this.#wasm.parseDom(inputPointer, source.byteLength, arenaPointer, arenaSize, mode);
|
|
177
177
|
if (headerPointer >= 0)
|
|
178
178
|
return this.copyDocument(source, headerPointer);
|
|
179
179
|
if (arenaSize >= MAX_ARENA_BYTES) {
|
package/dist/mausmark.wasm
CHANGED
|
Binary file
|
package/dist/node.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { ABI_VERSION, Dialect, HtmlMode, Join, ListFlags, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ReferenceStyle, } from './core.js';
|
|
1
|
+
export { ABI_VERSION, Dialect, HtmlMode, Join, ListFlags, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ParseMode, ReferenceStyle, TableAlignment, } from './core.js';
|
|
2
2
|
export type { LinkFields, MausmarkDocument, MausmarkParser } from './core.js';
|
|
3
3
|
/** Load the package's WASM parser for Node.js. */
|
|
4
4
|
export declare function createParser(): Promise<import('./core.js').MausmarkParser>;
|
package/dist/node.js
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
2
|
import { Buffer } from 'node:buffer';
|
|
3
3
|
import { parserFromInstance } from './core.js';
|
|
4
|
-
export { ABI_VERSION, Dialect, HtmlMode, Join, ListFlags, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ReferenceStyle, } from './core.js';
|
|
4
|
+
export { ABI_VERSION, Dialect, HtmlMode, Join, ListFlags, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ParseMode, ReferenceStyle, TableAlignment, } from './core.js';
|
|
5
5
|
function createSourceDecoder(source) {
|
|
6
6
|
// Share the document's owned bytes, not WASM memory or the input string.
|
|
7
7
|
const buffer = Buffer.from(source.buffer, source.byteOffset, source.byteLength);
|
|
8
8
|
return (offset, length) => {
|
|
9
9
|
const end = offset + length;
|
|
10
|
-
// TextDecoder removes a leading UTF-8 BOM from each decoded range.
|
|
11
|
-
if (length >= 3 && buffer[offset] === 0xef && buffer[offset + 1] === 0xbb && buffer[offset + 2] === 0xbf) {
|
|
12
|
-
offset += 3;
|
|
13
|
-
}
|
|
14
10
|
return buffer.toString('utf8', offset, end);
|
|
15
11
|
};
|
|
16
12
|
}
|
package/dom-example.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
// A small
|
|
1
|
+
// A small DOM renderer showing how to walk the tree. It is a starting point,
|
|
2
|
+
// not a package export or a complete renderer: copy it and tweak the
|
|
3
|
+
// node-to-element switch.
|
|
2
4
|
//
|
|
3
5
|
// import {createParser} from "mausmark"
|
|
4
6
|
// import {renderToDom} from "./dom-example.js"
|
|
@@ -6,27 +8,46 @@
|
|
|
6
8
|
// const parser = await createParser()
|
|
7
9
|
// renderToDom(parser.parse(markdown), document.querySelector("article"))
|
|
8
10
|
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
11
|
+
// Code goes in through textContent, mixed content through text nodes, and
|
|
12
|
+
// link/image fields through setAttribute, so nothing is escaped by hand.
|
|
13
|
+
//
|
|
14
|
+
// Limitations:
|
|
15
|
+
// - Reference labels match with toLowerCase(), not Unicode case folding and
|
|
16
|
+
// whitespace collapse; see normalizeReferenceLabel in mausmark/references.
|
|
17
|
+
// - Image descriptions are read as plain text; a reference nested in one is
|
|
18
|
+
// not resolved.
|
|
19
|
+
// - Empty and absent titles are both omitted.
|
|
20
|
+
// - Entities and backslash escapes are not decoded.
|
|
21
|
+
// - No URL-scheme policy: javascript: URLs pass through to href/src. Add one
|
|
22
|
+
// before rendering untrusted input.
|
|
13
23
|
|
|
14
24
|
import {Join, ListFlags, NodeType, ReferenceStyle} from "mausmark";
|
|
15
|
-
|
|
25
|
+
|
|
26
|
+
const tableAlign = ["", "left", "center", "right"];
|
|
16
27
|
|
|
17
28
|
/** @import {MausmarkDocument} from "mausmark" */
|
|
18
|
-
/** @import {
|
|
29
|
+
/** @import {LinkFields} from "mausmark" */
|
|
19
30
|
|
|
20
31
|
/**
|
|
21
|
-
* Append the rendered document to `parent
|
|
32
|
+
* Append the rendered document to `parent`, leaving existing content intact.
|
|
22
33
|
* @param {MausmarkDocument} doc
|
|
23
34
|
* @param {ParentNode} parent
|
|
24
|
-
* @param {{referenceMatching?: ReferenceMatching}} options Renderer lookup policy,
|
|
25
|
-
* independent of the input dialect. Defaults to original Markdown matching.
|
|
26
35
|
*/
|
|
27
|
-
export function renderToDom(doc, parent
|
|
28
|
-
|
|
29
|
-
const
|
|
36
|
+
export function renderToDom(doc, parent) {
|
|
37
|
+
/** @type {Record<string, LinkFields>} */
|
|
38
|
+
const references = Object.create(null);
|
|
39
|
+
for (let index = 0; index < doc.nodeCount; index += 1) {
|
|
40
|
+
if (doc.type(index) !== NodeType.REF_DEF) continue;
|
|
41
|
+
const link = doc.link(index);
|
|
42
|
+
references[link.label.toLowerCase()] ??= link; // first definition wins
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** @param {string} text @param {ParentNode} into */
|
|
46
|
+
function text(text, into) {
|
|
47
|
+
if (text) {
|
|
48
|
+
into.appendChild(document.createTextNode(text));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
30
51
|
|
|
31
52
|
/** @param {number} index @param {ParentNode} into */
|
|
32
53
|
function children(index, into) {
|
|
@@ -35,156 +56,230 @@ export function renderToDom(doc, parent, {referenceMatching = "markdown"} = {})
|
|
|
35
56
|
}
|
|
36
57
|
}
|
|
37
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
|
+
|
|
38
65
|
/** @param {string} tag @param {number} index @param {ParentNode} into */
|
|
39
66
|
function element(tag, index, into) {
|
|
40
67
|
const el = document.createElement(tag);
|
|
41
68
|
children(index, el);
|
|
42
|
-
into.
|
|
69
|
+
into.appendChild(el);
|
|
43
70
|
return el;
|
|
44
71
|
}
|
|
45
72
|
|
|
46
|
-
// An undefined reference is not a link: put its brackets back as text.
|
|
47
|
-
/** @param {number} index @param {ParentNode} into */
|
|
48
|
-
function literalReference(index, into) {
|
|
49
|
-
const {label, style} = doc.link(index);
|
|
50
|
-
if (doc.type(index) === NodeType.IMAGE) {
|
|
51
|
-
into.append("![" + altText(doc, index, definitions, referenceMatching));
|
|
52
|
-
} else {
|
|
53
|
-
into.append("[");
|
|
54
|
-
children(index, into);
|
|
55
|
-
}
|
|
56
|
-
into.append("]");
|
|
57
|
-
if (style === ReferenceStyle.FULL) {
|
|
58
|
-
into.append("[" + unescapeLink(label) + "]");
|
|
59
|
-
} else if (style === ReferenceStyle.COLLAPSED) {
|
|
60
|
-
into.append("[]");
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
73
|
/** @param {number} index @param {ParentNode} into */
|
|
65
74
|
function render(index, into) {
|
|
66
75
|
switch (doc.type(index)) {
|
|
76
|
+
|
|
67
77
|
case NodeType.DOC:
|
|
68
78
|
case NodeType.SPAN:
|
|
69
79
|
children(index, into);
|
|
70
80
|
return;
|
|
81
|
+
|
|
71
82
|
case NodeType.BRACE_SPAN:
|
|
72
|
-
//
|
|
73
|
-
//
|
|
74
|
-
|
|
83
|
+
// Omit the delimiters for a transparent container, or interpret
|
|
84
|
+
// doc.text(index) yourself instead of traversing the children.
|
|
85
|
+
text("{", into);
|
|
75
86
|
children(index, into);
|
|
76
|
-
|
|
87
|
+
text("}", into);
|
|
77
88
|
return;
|
|
89
|
+
|
|
78
90
|
case NodeType.PARAGRAPH:
|
|
79
91
|
element("p", index, into);
|
|
80
92
|
return;
|
|
93
|
+
|
|
81
94
|
case NodeType.HEADING:
|
|
82
95
|
element("h" + doc.level(index), index, into);
|
|
83
96
|
return;
|
|
97
|
+
|
|
84
98
|
case NodeType.BLOCKQUOTE:
|
|
85
99
|
element("blockquote", index, into);
|
|
86
100
|
return;
|
|
101
|
+
|
|
102
|
+
case NodeType.TABLE: {
|
|
103
|
+
const table = document.createElement("table");
|
|
104
|
+
const rows = doc.children(index)[Symbol.iterator]();
|
|
105
|
+
const first = rows.next();
|
|
106
|
+
if (!first.done) {
|
|
107
|
+
const columns = Array.from(doc.children(first.value));
|
|
108
|
+
const head = document.createElement("thead");
|
|
109
|
+
const header = document.createElement("tr");
|
|
110
|
+
for (const column of columns) {
|
|
111
|
+
const cell = document.createElement("th");
|
|
112
|
+
const align = tableAlign[doc.level(column)];
|
|
113
|
+
if (align) cell.setAttribute("align", align);
|
|
114
|
+
tableCellContent(column, cell);
|
|
115
|
+
header.appendChild(cell);
|
|
116
|
+
}
|
|
117
|
+
head.appendChild(header);
|
|
118
|
+
table.appendChild(head);
|
|
119
|
+
let row = rows.next();
|
|
120
|
+
if (!row.done) {
|
|
121
|
+
const body = document.createElement("tbody");
|
|
122
|
+
do {
|
|
123
|
+
const tr = document.createElement("tr");
|
|
124
|
+
const cells = doc.children(row.value)[Symbol.iterator]();
|
|
125
|
+
for (const column of columns) {
|
|
126
|
+
const cell = document.createElement("td");
|
|
127
|
+
const align = tableAlign[doc.level(column)];
|
|
128
|
+
if (align) cell.setAttribute("align", align);
|
|
129
|
+
const source = cells.next();
|
|
130
|
+
if (!source.done) tableCellContent(source.value, cell);
|
|
131
|
+
tr.appendChild(cell);
|
|
132
|
+
}
|
|
133
|
+
body.appendChild(tr);
|
|
134
|
+
row = rows.next();
|
|
135
|
+
} while (!row.done);
|
|
136
|
+
table.appendChild(body);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
into.appendChild(table);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
87
143
|
case NodeType.LIST: {
|
|
88
|
-
const list = element(doc.flags(index) & ListFlags.ORDERED ? "ol" : "ul", index, into);
|
|
89
|
-
//
|
|
144
|
+
const list = element((doc.flags(index) & ListFlags.ORDERED) ? "ol" : "ul", index, into);
|
|
145
|
+
// CommonMark ordered lists retain their starting digits.
|
|
90
146
|
const start = doc.text(index);
|
|
91
147
|
if (start !== "" && Number(start) !== 1) {
|
|
92
148
|
list.setAttribute("start", String(Number(start)));
|
|
93
149
|
}
|
|
94
150
|
return;
|
|
95
151
|
}
|
|
152
|
+
|
|
96
153
|
case NodeType.LIST_ITEM: {
|
|
97
154
|
const item = document.createElement("li");
|
|
98
|
-
const loose = doc.flags(index) & ListFlags.LOOSE;
|
|
99
155
|
for (const child of doc.children(index)) {
|
|
100
|
-
//
|
|
101
|
-
if (!
|
|
156
|
+
// Tight lists omit paragraph wrappers inside each item.
|
|
157
|
+
if (!(doc.flags(index) & ListFlags.LOOSE) && doc.type(child) === NodeType.PARAGRAPH) {
|
|
102
158
|
children(child, item);
|
|
103
159
|
} else {
|
|
104
160
|
render(child, item);
|
|
105
161
|
}
|
|
106
162
|
}
|
|
107
|
-
into.
|
|
163
|
+
into.appendChild(item);
|
|
108
164
|
return;
|
|
109
165
|
}
|
|
166
|
+
|
|
110
167
|
case NodeType.CODE_BLOCK: {
|
|
111
168
|
const code = document.createElement("code");
|
|
112
|
-
|
|
113
|
-
//
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
169
|
+
|
|
170
|
+
// A fenced block's leading LABEL field holds its info string.
|
|
171
|
+
{
|
|
172
|
+
const hasInfo = (index + 1 < doc.subtreeEnd(index)) && (doc.type(index + 1) === NodeType.LABEL);
|
|
173
|
+
const language = (
|
|
174
|
+
hasInfo
|
|
175
|
+
? doc.joinedText(index + 1).trim().split(/\s+/)[0]
|
|
176
|
+
: ""
|
|
177
|
+
);
|
|
178
|
+
if (language) {
|
|
179
|
+
code.className = "language-" + language;
|
|
180
|
+
}
|
|
118
181
|
}
|
|
119
|
-
|
|
182
|
+
|
|
183
|
+
code.textContent = doc.joinedText(index);
|
|
120
184
|
const pre = document.createElement("pre");
|
|
121
|
-
pre.
|
|
122
|
-
into.
|
|
185
|
+
pre.appendChild(code);
|
|
186
|
+
into.appendChild(pre);
|
|
123
187
|
return;
|
|
124
188
|
}
|
|
189
|
+
|
|
190
|
+
case NodeType.CODE_SPAN: {
|
|
191
|
+
const code = document.createElement("code");
|
|
192
|
+
code.textContent = doc.joinedText(index);
|
|
193
|
+
into.appendChild(code);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
125
197
|
case NodeType.THEMATIC_BREAK:
|
|
126
|
-
into.
|
|
198
|
+
into.appendChild(document.createElement("hr"));
|
|
127
199
|
return;
|
|
200
|
+
|
|
128
201
|
case NodeType.EMPH:
|
|
129
202
|
element("em", index, into);
|
|
130
203
|
return;
|
|
204
|
+
|
|
131
205
|
case NodeType.STRONG:
|
|
132
206
|
element("strong", index, into);
|
|
133
207
|
return;
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
code.append(doc.joinedText(index));
|
|
137
|
-
into.append(code);
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
case NodeType.LINK: {
|
|
141
|
-
const target = resolve(doc, index, definitions, referenceMatching);
|
|
142
|
-
if (!target) {
|
|
143
|
-
literalReference(index, into);
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
const a = document.createElement("a");
|
|
147
|
-
a.setAttribute("href", target.url);
|
|
148
|
-
if (target.titlePresent) {
|
|
149
|
-
a.setAttribute("title", target.title);
|
|
150
|
-
}
|
|
151
|
-
children(index, a);
|
|
152
|
-
into.append(a);
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
208
|
+
|
|
209
|
+
case NodeType.LINK:
|
|
155
210
|
case NodeType.IMAGE: {
|
|
156
|
-
const
|
|
211
|
+
const image = doc.type(index) === NodeType.IMAGE;
|
|
212
|
+
const raw = doc.link(index);
|
|
213
|
+
const {style} = raw;
|
|
214
|
+
const isReference = style === ReferenceStyle.FULL || style === ReferenceStyle.COLLAPSED || style === ReferenceStyle.SHORTCUT;
|
|
215
|
+
const target = (
|
|
216
|
+
isReference
|
|
217
|
+
? references[raw.label.toLowerCase()]
|
|
218
|
+
: raw
|
|
219
|
+
);
|
|
220
|
+
|
|
157
221
|
if (!target) {
|
|
158
|
-
|
|
222
|
+
// An undefined reference is not a link: put its brackets back as text.
|
|
223
|
+
if (image) {
|
|
224
|
+
text("![" + doc.joinedText(index), into);
|
|
225
|
+
} else {
|
|
226
|
+
text("[", into);
|
|
227
|
+
children(index, into);
|
|
228
|
+
}
|
|
229
|
+
text(
|
|
230
|
+
(
|
|
231
|
+
style === ReferenceStyle.FULL
|
|
232
|
+
? "][" + raw.label + "]"
|
|
233
|
+
: (
|
|
234
|
+
style === ReferenceStyle.COLLAPSED
|
|
235
|
+
? "][]"
|
|
236
|
+
: "]"
|
|
237
|
+
)
|
|
238
|
+
),
|
|
239
|
+
into
|
|
240
|
+
);
|
|
159
241
|
return;
|
|
160
242
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
243
|
+
|
|
244
|
+
const {url, title} = target;
|
|
245
|
+
const el = document.createElement(image ? "img" : "a");
|
|
246
|
+
el.setAttribute(
|
|
247
|
+
image ? "src" : "href",
|
|
248
|
+
(
|
|
249
|
+
style === ReferenceStyle.AUTOLINK_EMAIL
|
|
250
|
+
? "mailto:" + url
|
|
251
|
+
: url
|
|
252
|
+
)
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
if (title) el.setAttribute("title", title);
|
|
256
|
+
|
|
257
|
+
if (image) {
|
|
258
|
+
el.setAttribute("alt", doc.joinedText(index));
|
|
259
|
+
} else {
|
|
260
|
+
children(index, el);
|
|
166
261
|
}
|
|
167
|
-
|
|
262
|
+
|
|
263
|
+
into.appendChild(el);
|
|
168
264
|
return;
|
|
169
265
|
}
|
|
266
|
+
|
|
170
267
|
case NodeType.HTML_BLOCK:
|
|
171
268
|
case NodeType.HTML_INLINE:
|
|
172
|
-
// Raw HTML stays inert text. To render trusted HTML instead, parse
|
|
173
|
-
// joinedText() with a <template> element, after sanitizing it.
|
|
174
|
-
into.append(doc.joinedText(index));
|
|
175
|
-
return;
|
|
176
269
|
case NodeType.CUSTOM:
|
|
177
|
-
//
|
|
178
|
-
|
|
270
|
+
// Other source-backed content stays text; customize as needed.
|
|
271
|
+
text(doc.joinedText(index), into);
|
|
179
272
|
return;
|
|
273
|
+
|
|
180
274
|
case NodeType.TEXT:
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
into.append(doc.text(index)); // must not be entity-decoded
|
|
275
|
+
case NodeType.LITERAL_TEXT: {
|
|
276
|
+
const join = doc.level(index);
|
|
277
|
+
text((join === Join.NL ? "\n" : (join === Join.SPACE ? " " : "")) + doc.text(index), into);
|
|
185
278
|
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
186
281
|
case NodeType.HARD_BREAK:
|
|
187
|
-
into.
|
|
282
|
+
into.appendChild(document.createElement("br"));
|
|
188
283
|
return;
|
|
189
284
|
// REF_DEFS and REF_DEF produce no output.
|
|
190
285
|
}
|
|
@@ -192,136 +287,3 @@ export function renderToDom(doc, parent, {referenceMatching = "markdown"} = {})
|
|
|
192
287
|
|
|
193
288
|
render(0, parent);
|
|
194
289
|
}
|
|
195
|
-
|
|
196
|
-
// A text chunk's join code is what comes before it: Join.NL is a soft line break.
|
|
197
|
-
/** @param {number} join */
|
|
198
|
-
function joinPrefix(join) {
|
|
199
|
-
return join === Join.NL ? "\n" : join === Join.SPACE ? " " : "";
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// A <textarea>'s content is RCDATA: assigning innerHTML decodes character
|
|
203
|
-
// references but never creates elements, whatever the input.
|
|
204
|
-
/** @param {string} text */
|
|
205
|
-
function decodeEntities(text) {
|
|
206
|
-
if (!text.includes("&")) {
|
|
207
|
-
return text;
|
|
208
|
-
}
|
|
209
|
-
const area = document.createElement("textarea");
|
|
210
|
-
area.innerHTML = text;
|
|
211
|
-
return area.value;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// An image description is alt text held as chunks: the literal source in
|
|
215
|
-
// Markdown, the text between the markup in CommonMark. Chunks follow the text
|
|
216
|
-
// rule above: TEXT decodes entities, LITERAL_TEXT (an escaped `&`, code, inline
|
|
217
|
-
// HTML) does not. Reference nodes remain until the renderer resolves them.
|
|
218
|
-
/**
|
|
219
|
-
* @param {MausmarkDocument} doc
|
|
220
|
-
* @param {number} index
|
|
221
|
-
* @param {Map<string, {url: string, title: string, titlePresent: boolean}>} definitions
|
|
222
|
-
* @param {ReferenceMatching} matching
|
|
223
|
-
* @returns {string}
|
|
224
|
-
*/
|
|
225
|
-
function altText(doc, index, definitions, matching) {
|
|
226
|
-
let text = "", run = "", end = -1;
|
|
227
|
-
function flush() { text += decodeEntities(run); run = ""; }
|
|
228
|
-
for (const child of doc.children(index)) {
|
|
229
|
-
const type = doc.type(child);
|
|
230
|
-
if (type === NodeType.TEXT) {
|
|
231
|
-
if (doc.level(child) !== Join.NONE || doc.offset(child) !== end) flush();
|
|
232
|
-
run += joinPrefix(doc.level(child)) + doc.text(child);
|
|
233
|
-
end = doc.offset(child) + doc.length(child);
|
|
234
|
-
} else {
|
|
235
|
-
flush(); end = -1;
|
|
236
|
-
if (type === NodeType.LINK || type === NodeType.IMAGE) {
|
|
237
|
-
const content = altText(doc, child, definitions, matching);
|
|
238
|
-
if (resolve(doc, child, definitions, matching)) text += content;
|
|
239
|
-
else {
|
|
240
|
-
const {label, style} = doc.link(child);
|
|
241
|
-
text += (type === NodeType.IMAGE ? "![" : "[") + content + "]";
|
|
242
|
-
if (style === ReferenceStyle.FULL) text += "[" + unescapeLink(label) + "]";
|
|
243
|
-
else if (style === ReferenceStyle.COLLAPSED) text += "[]";
|
|
244
|
-
}
|
|
245
|
-
} else text += joinPrefix(doc.level(child)) + doc.text(child);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
flush();
|
|
249
|
-
return text;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// link().title is '' for both absent and empty titles; the leading field run
|
|
253
|
-
// preserves that distinction. Do not inspect nested content for title fields.
|
|
254
|
-
/** @param {MausmarkDocument} doc @param {number} index */
|
|
255
|
-
function titlePresent(doc, index) {
|
|
256
|
-
for (let child = index + 1; child < doc.subtreeEnd(index); child = doc.subtreeEnd(child)) {
|
|
257
|
-
const type = doc.type(child);
|
|
258
|
-
if (type === NodeType.TITLE) return true;
|
|
259
|
-
if (type !== NodeType.URL && type !== NodeType.LABEL) break;
|
|
260
|
-
}
|
|
261
|
-
return false;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// Reference links arrive unresolved. Definitions can sit anywhere, including
|
|
265
|
-
// inside lists and quotes, so scan every node; the first definition of a label wins.
|
|
266
|
-
/** @param {MausmarkDocument} doc @param {ReferenceMatching} matching */
|
|
267
|
-
function collectDefinitions(doc, matching) {
|
|
268
|
-
/** @type {Map<string, {url: string, title: string, titlePresent: boolean}>} */
|
|
269
|
-
const definitions = new Map();
|
|
270
|
-
for (let index = 0; index < doc.nodeCount; index += 1) {
|
|
271
|
-
if (doc.type(index) !== NodeType.REF_DEF) continue;
|
|
272
|
-
|
|
273
|
-
const {label, url, title} = doc.link(index);
|
|
274
|
-
const key = normalizeReferenceLabel(label, matching);
|
|
275
|
-
if (key === null) continue;
|
|
276
|
-
if (!definitions.has(key)) {
|
|
277
|
-
definitions.set(key, {url, title, titlePresent: titlePresent(doc, index)});
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
return definitions;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// Where a LINK or IMAGE points, or null for a reference with no definition.
|
|
284
|
-
/**
|
|
285
|
-
* @param {MausmarkDocument} doc
|
|
286
|
-
* @param {number} index
|
|
287
|
-
* @param {Map<string, {url: string, title: string, titlePresent: boolean}>} definitions
|
|
288
|
-
* @param {ReferenceMatching} matching
|
|
289
|
-
*/
|
|
290
|
-
function resolve(doc, index, definitions, matching) {
|
|
291
|
-
const {url, title, label, style} = doc.link(index);
|
|
292
|
-
// Autolinks take entities but no backslash escapes.
|
|
293
|
-
if (style === ReferenceStyle.AUTOLINK_EMAIL) {
|
|
294
|
-
return {
|
|
295
|
-
url: "mailto:" + decodeEntities(url),
|
|
296
|
-
title: "", titlePresent: false
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
if (style === ReferenceStyle.AUTOLINK_URI) {
|
|
300
|
-
return {
|
|
301
|
-
url: decodeEntities(url),
|
|
302
|
-
title: "", titlePresent: false
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
if (style === ReferenceStyle.INLINE) {
|
|
306
|
-
return {
|
|
307
|
-
url: unescapeLink(url),
|
|
308
|
-
title: unescapeLink(title), titlePresent: titlePresent(doc, index)
|
|
309
|
-
};
|
|
310
|
-
}
|
|
311
|
-
const key = normalizeReferenceLabel(label, matching);
|
|
312
|
-
const definition = key === null ? undefined : definitions.get(key);
|
|
313
|
-
if (!definition) return null;
|
|
314
|
-
|
|
315
|
-
return {
|
|
316
|
-
url: unescapeLink(definition.url),
|
|
317
|
-
title: unescapeLink(definition.title), titlePresent: definition.titlePresent
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
// Fields are raw source. This renderer decodes entities before resolving
|
|
322
|
-
// backslash escapes; the browser percent-encodes URLs when it resolves them.
|
|
323
|
-
// Keeping interpretation here leaves the AST lossless for other consumers.
|
|
324
|
-
/** @param {string} text */
|
|
325
|
-
function unescapeLink(text) {
|
|
326
|
-
return decodeEntities(text).replace(/\\([!-/:-@[-`{-~])/g, "$1");
|
|
327
|
-
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mausmark",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "A fast, dependency-free Markdown parser compiled to WebAssembly",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -32,15 +32,18 @@
|
|
|
32
32
|
"build:js": "./build.sh codegen && tsc -p tsconfig.json",
|
|
33
33
|
"build": "npm run clean && npm run build:wasm && npm run build:js",
|
|
34
34
|
"check:wasm-size": "node test/wasm_size.mjs",
|
|
35
|
-
"test:package": "node --test test/npm-package.test.mjs test/wasm-size.test.mjs test/source-retention.test.mjs test/test-registration.test.mjs test/reference-matching.test.mjs",
|
|
35
|
+
"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",
|
|
36
36
|
"test:types": "tsc -p tsconfig.test.json --noEmit && tsc -p tsconfig.references.json",
|
|
37
|
+
"test:browser": "node test/browser/run.mjs",
|
|
38
|
+
"bench:browser": "npm run build && ./build.sh wasm && node benchmark/browser/run.mjs",
|
|
39
|
+
"setup:chrome": "node test/browser/setup-chrome.mjs",
|
|
37
40
|
"test:layers": "node --test test/layers.test.mjs",
|
|
38
41
|
"test:build": "node --test test/build.test.mjs",
|
|
39
42
|
"check:unicode": "node tools/unicode.mjs --check && node tools/case-fold.mjs --check",
|
|
40
43
|
"test:unicode": "node --test test/unicode.test.mjs test/case-fold.test.mjs",
|
|
41
44
|
"check:entities": "node tools/entities.mjs --check",
|
|
42
45
|
"test:entities": "node --test test/entities.test.mjs",
|
|
43
|
-
"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",
|
|
46
|
+
"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",
|
|
44
47
|
"prepack": "npm test"
|
|
45
48
|
},
|
|
46
49
|
"engines": {
|