mausmark 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HTML-LICENSE.txt +38 -0
- package/README.md +78 -41
- package/RELEASE-NOTES.md +27 -0
- package/UNICODE-LICENSE.txt +39 -0
- package/dist/abi.d.ts +3 -2
- package/dist/abi.js +3 -2
- package/dist/core.d.ts +16 -5
- package/dist/core.js +14 -6
- package/dist/mausmark.wasm +0 -0
- package/dist/node.js +14 -1
- package/dom-example.js +82 -43
- package/package.json +21 -6
- package/reference-matching.d.ts +7 -0
- package/reference-matching.js +32 -0
package/HTML-LICENSE.txt
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
WHATWG HTML named character reference data
|
|
2
|
+
Source: https://html.spec.whatwg.org/entities.json
|
|
3
|
+
License: https://github.com/whatwg/html/blob/main/LICENSE
|
|
4
|
+
|
|
5
|
+
Copyright © WHATWG (Apple, Google, Mozilla, Microsoft).
|
|
6
|
+
|
|
7
|
+
This work is licensed under a Creative Commons Attribution 4.0 International
|
|
8
|
+
License. To the extent portions of it are incorporated into source code,
|
|
9
|
+
such portions in the source code are licensed under the BSD 3-Clause License instead.
|
|
10
|
+
|
|
11
|
+
The named character reference data in src/html_entities.inc is incorporated
|
|
12
|
+
into source code under the following BSD 3-Clause License. Mausmark selects
|
|
13
|
+
semicolon-terminated spellings and rearranges them into its own lookup layout.
|
|
14
|
+
|
|
15
|
+
Redistribution and use in source and binary forms, with or without
|
|
16
|
+
modification, are permitted provided that the following conditions are met:
|
|
17
|
+
|
|
18
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
19
|
+
list of conditions and the following disclaimer.
|
|
20
|
+
|
|
21
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
22
|
+
this list of conditions and the following disclaimer in the documentation
|
|
23
|
+
and/or other materials provided with the distribution.
|
|
24
|
+
|
|
25
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
26
|
+
contributors may be used to endorse or promote products derived from
|
|
27
|
+
this software without specific prior written permission.
|
|
28
|
+
|
|
29
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
30
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
31
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
32
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
33
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
34
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
35
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
36
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
37
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
38
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
CHANGED
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
A fast, renderer-agnostic Markdown parser.
|
|
4
4
|
|
|
5
|
-
It aims to support and extend Markdown.
|
|
6
|
-
|
|
7
|
-
Distributed as a WASM build.
|
|
5
|
+
It aims to support and extend Markdown. It is also practical, so it
|
|
6
|
+
parses CommonMark too. Distributed as a WASM build.
|
|
8
7
|
|
|
9
8
|
Project page and demo: https://myshkin.eu/mausmark
|
|
10
9
|
|
|
@@ -14,6 +13,9 @@ Project page and demo: https://myshkin.eu/mausmark
|
|
|
14
13
|
|
|
15
14
|
npm install mausmark
|
|
16
15
|
|
|
16
|
+
Requires Node.js 22.14+ or a modern browser with WebAssembly SIMD.
|
|
17
|
+
Browser bundlers must emit the package-relative `.wasm` asset.
|
|
18
|
+
|
|
17
19
|
## Usage
|
|
18
20
|
|
|
19
21
|
import {createParser, Dialect, NodeType} from "mausmark";
|
|
@@ -30,8 +32,8 @@ Project page and demo: https://myshkin.eu/mausmark
|
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
The document is a flat array of nodes, so finding every node of one type
|
|
33
|
-
is a single loop; [Writing a renderer](#writing-a-renderer) walks it as
|
|
34
|
-
tree. Initialize the parser once and reuse it. Initialization is
|
|
35
|
+
is a single loop; [Writing a renderer](#writing-a-renderer) walks it as
|
|
36
|
+
a tree. Initialize the parser once and reuse it. Initialization is
|
|
35
37
|
asynchronous; `parse()` is synchronous. Returned documents are
|
|
36
38
|
independent snapshots and remain valid after later parses.
|
|
37
39
|
|
|
@@ -70,9 +72,38 @@ the document so that you could reproduce it exactly (minus whitespace).
|
|
|
70
72
|
Raw HTML is returned as `NodeType.HTML_BLOCK` or `NodeType.HTML_INLINE`;
|
|
71
73
|
sanitize it when parsing untrusted input.
|
|
72
74
|
|
|
75
|
+
In the default Markdown dialect, `{text **bold** text}` is a
|
|
76
|
+
`NodeType.BRACE_SPAN` with parsed inline children. `text(index)` returns
|
|
77
|
+
the exact original interior, `text **bold** text`, excluding its outer
|
|
78
|
+
braces; `offset()` and `length()` locate that interior in the UTF-8
|
|
79
|
+
source. Choose raw interpretation or child traversal, rather than
|
|
80
|
+
emitting both. The example renderer shows braces around the rendered
|
|
81
|
+
children; remove the delimiter output to make the container transparent.
|
|
82
|
+
|
|
83
|
+
Brace spans nest, support escaped braces, and keep code, HTML tokens,
|
|
84
|
+
and link metadata intact. Markdown delimiters cannot pair across span
|
|
85
|
+
boundaries. Unclosed spans stay literal. Spans are single-line, with a
|
|
86
|
+
maximum interior of 65,535 UTF-8 bytes and 32 levels of brace nesting;
|
|
87
|
+
beyond these limits braces stay literal. The Markdown formatter
|
|
88
|
+
preserves interiors verbatim. Multiline `NodeType.CUSTOM` blocks remain
|
|
89
|
+
raw, and CommonMark treats braces as ordinary text.
|
|
90
|
+
|
|
73
91
|
Compare node types against the exported constants: their numeric values
|
|
74
92
|
can change between versions.
|
|
75
93
|
|
|
94
|
+
### Upgrading from 0.3.x to 0.4.0
|
|
95
|
+
|
|
96
|
+
For most projects this is a drop-in update: if your renderer doesn't
|
|
97
|
+
output images, you don't need to change anything.
|
|
98
|
+
|
|
99
|
+
- To output images in the Markdown dialect, make sure you read the
|
|
100
|
+
description with `joinedText(index)`.
|
|
101
|
+
- CommonMark image descriptions should be read the same way; they are
|
|
102
|
+
plain text now. A reference inside one stays a LINK or IMAGE node
|
|
103
|
+
for your renderer to resolve; see `altText()` in `dom-example.js`.
|
|
104
|
+
|
|
105
|
+
For more details, see the [release notes](RELEASE-NOTES.md).
|
|
106
|
+
|
|
76
107
|
## Writing a renderer
|
|
77
108
|
|
|
78
109
|
Walk from node `0` with `children(index)`, which yields direct content
|
|
@@ -134,17 +165,20 @@ Things to decide when building a richer renderer:
|
|
|
134
165
|
render arbitrary inline markup. In CommonMark, a `LITERAL_TEXT` leaf
|
|
135
166
|
must not be entity-decoded with its neighboring `TEXT`.
|
|
136
167
|
- `link(index)` works on `LINK`, `IMAGE`, and `REF_DEF` and returns
|
|
137
|
-
**raw** URL, title, label, and `ReferenceStyle`.
|
|
138
|
-
|
|
139
|
-
`
|
|
140
|
-
|
|
141
|
-
definition
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
168
|
+
**raw** URL, title, label, and `ReferenceStyle`. Destinations and
|
|
169
|
+
titles over 65,535 bytes are kept as field-child text chunks;
|
|
170
|
+
`link()` joins them without truncation. Inline links and autolinks
|
|
171
|
+
already have destinations; `FULL`, `COLLAPSED`, and `SHORTCUT` need
|
|
172
|
+
a definition lookup. Scan all node indexes for `REF_DEF` (including
|
|
173
|
+
definitions in containers), store the first definition for each
|
|
174
|
+
normalized label, and decide how to render unresolved references.
|
|
175
|
+
CommonMark label matching uses Unicode case folding and whitespace
|
|
176
|
+
collapse, not just `toLowerCase()`;
|
|
177
|
+
`normalizeReferenceLabel(label, "markdown" | "commonmark")` from
|
|
178
|
+
`mausmark/references` does either. An `AUTOLINK_EMAIL` URL is the
|
|
179
|
+
bare address; add `mailto:` when emitting it. `title === ""` alone
|
|
180
|
+
does not distinguish an absent title from an explicit empty title;
|
|
181
|
+
inspect the leading `TITLE` field if that matters to your output.
|
|
148
182
|
- For lists, `flags(index)` has `ListFlags.ORDERED` and
|
|
149
183
|
`ListFlags.LOOSE` on `LIST` / `LIST_ITEM`. `level()` on a list
|
|
150
184
|
records its marker character; `text()` on a CommonMark ordered list
|
|
@@ -159,38 +193,39 @@ Things to decide when building a richer renderer:
|
|
|
159
193
|
|
|
160
194
|
## Performance
|
|
161
195
|
|
|
162
|
-
The SIMD WebAssembly parser build is **
|
|
163
|
-
Brotli** (quality 11, offline precompression).
|
|
164
|
-
an Apple M3 Pro, Node.js 26.3.1 and
|
|
165
|
-
`-Os` with LTO:
|
|
196
|
+
The SIMD WebAssembly parser build is **78.0 KB**, uncompressed, or
|
|
197
|
+
**26.1 KB with Brotli** (quality 11, offline precompression).
|
|
198
|
+
Source-to-AST measurements on an Apple M3 Pro, Node.js 26.3.1 and
|
|
199
|
+
Bun 1.4.2, compiled with Clang 22.1.7 at `-Os` with LTO:
|
|
166
200
|
|
|
167
201
|
| Input | Node (µs) | Bun (µs) | Node (GB/s) | Bun (GB/s) |
|
|
168
202
|
| ---------------------- | --------: | -------: | ----------: | ---------: |
|
|
169
|
-
| Moby-Dick (1.2 MB) | ~
|
|
170
|
-
| Line-wrapped Moby-Dick | ~
|
|
171
|
-
| Pro Git chapters |
|
|
172
|
-
| tldr pages | 1.
|
|
173
|
-
|
|
174
|
-
These are warmed parser-export timings with preloaded UTF-8 input and a
|
|
175
|
-
arena, including JavaScript iteration and export-call overhead.
|
|
176
|
-
initialization, input encoding/copying, AST snapshot
|
|
177
|
-
they are not end-to-end timings of the package’s
|
|
178
|
-
(
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
uses decimal
|
|
203
|
+
| Moby-Dick (1.2 MB) | ~287 | ~242 | ~4.2 | ~5.0 |
|
|
204
|
+
| Line-wrapped Moby-Dick | ~413 | ~287 | ~2.9 | ~4.2 |
|
|
205
|
+
| Pro Git chapters | 37.2 | 30.0 | 1.50 | 1.86 |
|
|
206
|
+
| tldr pages | 1.6 | 1.3 | 0.36 | 0.43 |
|
|
207
|
+
|
|
208
|
+
These are warmed parser-export timings with preloaded UTF-8 input and a
|
|
209
|
+
reused arena, including JavaScript iteration and export-call overhead.
|
|
210
|
+
They exclude initialization, input encoding/copying, AST snapshot
|
|
211
|
+
copying, and rendering; they are not end-to-end timings of the package’s
|
|
212
|
+
`parse()` method. The corpus rows (Pro Git, tldr) report average time
|
|
213
|
+
per document; figures are medians across three alternating passes, each
|
|
214
|
+
with five timing samples. Pro Git uses Markdown, tldr uses CommonMark;
|
|
215
|
+
Moby-Dick rounds the close results from both dialects. GB/s uses decimal
|
|
216
|
+
bytes.
|
|
182
217
|
|
|
183
218
|
## Notes
|
|
184
219
|
|
|
185
|
-
- Node.js 22.14+ or a modern browser with WebAssembly SIMD.
|
|
186
|
-
- Browser bundlers must emit the package-relative `.wasm` asset.
|
|
187
220
|
- Input is limited to 64 MiB; parser arena growth is limited to 512
|
|
188
221
|
MiB.
|
|
189
|
-
- Two input dialects
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
222
|
+
- Two input dialects. `Dialect.MARKDOWN` (the default) is original
|
|
223
|
+
Markdown; `Dialect.COMMONMARK` is based on CommonMark 0.31.2,
|
|
224
|
+
currently 649/652 (99.5%) of its spec suite byte-exact. Three
|
|
225
|
+
definition-dependent reference-link cases are intentionally
|
|
226
|
+
unresolved.
|
|
227
|
+
- Unlike CommonMark, an image description is plain alt text, not
|
|
228
|
+
inline content.
|
|
194
229
|
- No GFM yet: no tables, strikethrough, task lists or autolink
|
|
195
230
|
literals.
|
|
196
231
|
|
|
@@ -201,4 +236,6 @@ MIT.
|
|
|
201
236
|
|
|
202
237
|
## License
|
|
203
238
|
|
|
204
|
-
MIT. See [LICENSE](LICENSE).
|
|
239
|
+
MIT. See [LICENSE](LICENSE). Unicode data carries the notice in
|
|
240
|
+
[UNICODE-LICENSE.txt](UNICODE-LICENSE.txt); HTML entity data carries the
|
|
241
|
+
notice in [HTML-LICENSE.txt](HTML-LICENSE.txt).
|
package/RELEASE-NOTES.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Mausmark v0.4.0
|
|
2
|
+
|
|
3
|
+
- Image descriptions are alt text, not inline formatting. CommonMark
|
|
4
|
+
keeps the text between the markup (`` → `a b`);
|
|
5
|
+
Markdown keeps the literal source, one chunk per line. Reference nodes
|
|
6
|
+
remain until the renderer resolves them or restores literal brackets.
|
|
7
|
+
- Image descriptions nested deeper than 32 levels now stay literal.
|
|
8
|
+
- New `mausmark/references` export: `normalizeReferenceLabel(label,
|
|
9
|
+
'markdown' | 'commonmark')`.
|
|
10
|
+
- DOM example: `renderToDom(doc, parent, {referenceMatching})`, explicit
|
|
11
|
+
empty titles, entities decoded in alt text.
|
|
12
|
+
- Fixed: CommonMark list markers indented four or more columns no
|
|
13
|
+
longer interrupt a paragraph.
|
|
14
|
+
- Fixed: an HTML block or fence on a line without `>` ends the
|
|
15
|
+
blockquote instead of continuing its paragraph.
|
|
16
|
+
- Fixed: in CommonMark, code inside an indented fence keeps the right
|
|
17
|
+
indentation when its lines start with tabs.
|
|
18
|
+
- Fixed: link titles and labels continuing onto a following `> ` line
|
|
19
|
+
keep their content.
|
|
20
|
+
- Faster source-to-AST in WASM: Moby-Dick 4–11%, tldr pages 10–24%;
|
|
21
|
+
articles and docs unchanged.
|
|
22
|
+
- WASM build 78.0 KB (26.1 KB Brotli), up from 75.8 KB.
|
|
23
|
+
- `joinedText()` applies line joins to `LITERAL_TEXT` chunks too.
|
|
24
|
+
- Package includes `HTML-LICENSE.txt`.
|
|
25
|
+
- Migration: CommonMark image children are text chunks plus reference
|
|
26
|
+
containers; use the DOM example's alt-text traversal for references,
|
|
27
|
+
or `joinedText()` for chunk-only descriptions. WASM ABI stays 5.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
UNICODE LICENSE V3
|
|
2
|
+
|
|
3
|
+
COPYRIGHT AND PERMISSION NOTICE
|
|
4
|
+
|
|
5
|
+
Copyright © 1991-2026 Unicode, Inc.
|
|
6
|
+
|
|
7
|
+
NOTICE TO USER: Carefully read the following legal agreement. BY
|
|
8
|
+
DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR
|
|
9
|
+
SOFTWARE, YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE
|
|
10
|
+
TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT
|
|
11
|
+
DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE.
|
|
12
|
+
|
|
13
|
+
Permission is hereby granted, free of charge, to any person obtaining a
|
|
14
|
+
copy of data files and any associated documentation (the "Data Files") or
|
|
15
|
+
software and any associated documentation (the "Software") to deal in the
|
|
16
|
+
Data Files or Software without restriction, including without limitation
|
|
17
|
+
the rights to use, copy, modify, merge, publish, distribute, and/or sell
|
|
18
|
+
copies of the Data Files or Software, and to permit persons to whom the
|
|
19
|
+
Data Files or Software are furnished to do so, provided that either (a)
|
|
20
|
+
this copyright and permission notice appear with all copies of the Data
|
|
21
|
+
Files or Software, or (b) this copyright and permission notice appear in
|
|
22
|
+
associated Documentation.
|
|
23
|
+
|
|
24
|
+
THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
|
25
|
+
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
26
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
|
|
27
|
+
THIRD PARTY RIGHTS.
|
|
28
|
+
|
|
29
|
+
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE
|
|
30
|
+
BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES,
|
|
31
|
+
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
32
|
+
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
|
33
|
+
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA
|
|
34
|
+
FILES OR SOFTWARE.
|
|
35
|
+
|
|
36
|
+
Except as contained in this notice, the name of a copyright holder shall
|
|
37
|
+
not be used in advertising or otherwise to promote the sale, use or other
|
|
38
|
+
dealings in these Data Files or Software without prior written
|
|
39
|
+
authorization of the copyright holder.
|
package/dist/abi.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export declare const NodeType: Readonly<{
|
|
|
24
24
|
readonly URL: 22;
|
|
25
25
|
readonly TITLE: 23;
|
|
26
26
|
readonly LABEL: 24;
|
|
27
|
+
readonly BRACE_SPAN: 25;
|
|
27
28
|
}>;
|
|
28
29
|
export type NodeType = (typeof NodeType)[keyof typeof NodeType];
|
|
29
30
|
export declare const ReferenceStyle: Readonly<{
|
|
@@ -57,7 +58,7 @@ export declare const HtmlMode: Readonly<{
|
|
|
57
58
|
readonly COMMONMARK: 2;
|
|
58
59
|
}>;
|
|
59
60
|
export type HtmlMode = (typeof HtmlMode)[keyof typeof HtmlMode];
|
|
60
|
-
export declare const NODE_TYPE_MAX:
|
|
61
|
+
export declare const NODE_TYPE_MAX: 25;
|
|
61
62
|
export declare const NODE_SIZE = 16;
|
|
62
63
|
export declare const NodeLayout: Readonly<{
|
|
63
64
|
TYPE: 0;
|
|
@@ -67,4 +68,4 @@ export declare const NodeLayout: Readonly<{
|
|
|
67
68
|
OFFSET: 8;
|
|
68
69
|
LENGTH: 12;
|
|
69
70
|
}>;
|
|
70
|
-
export declare const ABI_VERSION =
|
|
71
|
+
export declare const ABI_VERSION = 5;
|
package/dist/abi.js
CHANGED
|
@@ -25,6 +25,7 @@ export const NodeType = Object.freeze({
|
|
|
25
25
|
URL: 22,
|
|
26
26
|
TITLE: 23,
|
|
27
27
|
LABEL: 24,
|
|
28
|
+
BRACE_SPAN: 25,
|
|
28
29
|
});
|
|
29
30
|
export const ReferenceStyle = Object.freeze({
|
|
30
31
|
INLINE: 0,
|
|
@@ -52,10 +53,10 @@ export const HtmlMode = Object.freeze({
|
|
|
52
53
|
FRAGMENT: 1,
|
|
53
54
|
COMMONMARK: 2,
|
|
54
55
|
});
|
|
55
|
-
export const NODE_TYPE_MAX = NodeType.
|
|
56
|
+
export const NODE_TYPE_MAX = NodeType.BRACE_SPAN;
|
|
56
57
|
export const NODE_SIZE = 16;
|
|
57
58
|
export const NodeLayout = Object.freeze({
|
|
58
59
|
TYPE: 0, LEVEL: 1, FLAGS: 2, SUBTREE_END: 4,
|
|
59
60
|
OFFSET: 8, LENGTH: 12,
|
|
60
61
|
});
|
|
61
|
-
export const ABI_VERSION =
|
|
62
|
+
export const ABI_VERSION = 5;
|
package/dist/core.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Dialect, NodeType } from './abi.js';
|
|
2
2
|
import type { ReferenceStyle } from './abi.js';
|
|
3
3
|
export { ABI_VERSION, Dialect, HtmlMode, Join, ListFlags, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ReferenceStyle } from './abi.js';
|
|
4
|
-
/**
|
|
4
|
+
/** Raw, unresolved LINK/IMAGE/REF_DEF fields. Absent and empty fields are `''`;
|
|
5
|
+
* a leading TITLE node distinguishes an explicit empty title from no title. */
|
|
5
6
|
export interface LinkFields {
|
|
6
7
|
/** Destination as written: backslash escapes and entities are not processed. */
|
|
7
8
|
readonly url: string;
|
|
@@ -12,6 +13,8 @@ export interface LinkFields {
|
|
|
12
13
|
/** How the link was written, and whether `label` needs a definition lookup. */
|
|
13
14
|
readonly style: ReferenceStyle;
|
|
14
15
|
}
|
|
16
|
+
type SourceDecoder = (offset: number, length: number) => string;
|
|
17
|
+
type SourceDecoderFactory = (source: Uint8Array) => SourceDecoder;
|
|
15
18
|
/**
|
|
16
19
|
* An immutable JS-owned snapshot of a parsed mausmark AST. Nodes are integer
|
|
17
20
|
* indexes into a preorder table; the root is node `0`.
|
|
@@ -23,7 +26,7 @@ export interface MausmarkDocument {
|
|
|
23
26
|
type(index: number): NodeType;
|
|
24
27
|
/**
|
|
25
28
|
* Type-specific value: the heading level on HEADING, the `ReferenceStyle` on
|
|
26
|
-
* LINK and IMAGE, the `Join` code on TEXT, the marker
|
|
29
|
+
* LINK and IMAGE, the `Join` code on TEXT/LITERAL_TEXT, the marker on LIST.
|
|
27
30
|
*/
|
|
28
31
|
level(index: number): number;
|
|
29
32
|
/** `ListFlags` bits on LIST and LIST_ITEM. Other bits and nodes are internal. */
|
|
@@ -34,11 +37,19 @@ export interface MausmarkDocument {
|
|
|
34
37
|
offset(index: number): number;
|
|
35
38
|
/** UTF-8 byte length of the node's text slice. */
|
|
36
39
|
length(index: number): number;
|
|
37
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* The node's own source slice. On BRACE_SPAN this is the exact raw interior,
|
|
42
|
+
* excluding its braces and overlapping its parsed children's source ranges.
|
|
43
|
+
* Choose either this raw view or children(); do not concatenate both.
|
|
44
|
+
*/
|
|
38
45
|
text(index: number): string;
|
|
39
46
|
/** Direct content children, skipping the leading URL, TITLE and LABEL fields. */
|
|
40
47
|
children(index: number): Iterable<number>;
|
|
41
|
-
/**
|
|
48
|
+
/**
|
|
49
|
+
* The node's text chunks joined with their `Join` codes; not recursive.
|
|
50
|
+
* Use text() for a BRACE_SPAN's raw interior. Image descriptions with
|
|
51
|
+
* reference containers need renderer-side traversal and resolution instead.
|
|
52
|
+
*/
|
|
42
53
|
joinedText(index: number): string;
|
|
43
54
|
/** Raw link fields of a LINK, IMAGE or REF_DEF; throws for other nodes. */
|
|
44
55
|
link(index: number): LinkFields;
|
|
@@ -49,4 +60,4 @@ export interface MausmarkParser {
|
|
|
49
60
|
parse(markdown: string, dialect?: Dialect): MausmarkDocument;
|
|
50
61
|
}
|
|
51
62
|
/** @internal Shared loader validation used by the Node and browser entry points. */
|
|
52
|
-
export declare function parserFromInstance(instance: WebAssembly.Instance): MausmarkParser;
|
|
63
|
+
export declare function parserFromInstance(instance: WebAssembly.Instance, createDecoder?: SourceDecoderFactory): MausmarkParser;
|
package/dist/core.js
CHANGED
|
@@ -27,11 +27,13 @@ function isField(type) {
|
|
|
27
27
|
}
|
|
28
28
|
class PackedDocument {
|
|
29
29
|
#source;
|
|
30
|
+
#decodeSource;
|
|
30
31
|
#nodes;
|
|
31
32
|
#view;
|
|
32
33
|
#nodeCount;
|
|
33
|
-
constructor(source, nodes, nodeCount) {
|
|
34
|
+
constructor(source, nodes, nodeCount, decodeSource) {
|
|
34
35
|
this.#source = source;
|
|
36
|
+
this.#decodeSource = decodeSource;
|
|
35
37
|
this.#nodes = nodes;
|
|
36
38
|
this.#view = new DataView(nodes.buffer, nodes.byteOffset, nodes.byteLength);
|
|
37
39
|
this.#nodeCount = nodeCount;
|
|
@@ -86,7 +88,7 @@ class PackedDocument {
|
|
|
86
88
|
let result = '';
|
|
87
89
|
for (const child of this.children(index)) {
|
|
88
90
|
foundChild = true;
|
|
89
|
-
if (this.type(child) === NodeType.TEXT) {
|
|
91
|
+
if (this.type(child) === NodeType.TEXT || this.type(child) === NodeType.LITERAL_TEXT) {
|
|
90
92
|
const join = this.level(child);
|
|
91
93
|
if (join === Join.SPACE)
|
|
92
94
|
result += ' ';
|
|
@@ -126,6 +128,10 @@ class PackedDocument {
|
|
|
126
128
|
if (offset > this.#source.byteLength || length > this.#source.byteLength - offset) {
|
|
127
129
|
throw new Error('mausmark: malformed AST text range');
|
|
128
130
|
}
|
|
131
|
+
// Decode the owned bytes: slicing a cached source string can keep that
|
|
132
|
+
// entire string (or its larger parent) alive through a small text result.
|
|
133
|
+
if (this.#decodeSource !== undefined)
|
|
134
|
+
return this.#decodeSource(offset, length);
|
|
129
135
|
return decoder.decode(this.#source.subarray(offset, offset + length));
|
|
130
136
|
}
|
|
131
137
|
validateTable() {
|
|
@@ -148,8 +154,10 @@ class PackedDocument {
|
|
|
148
154
|
}
|
|
149
155
|
class WasmParser {
|
|
150
156
|
#wasm;
|
|
151
|
-
|
|
157
|
+
#createSourceDecoder;
|
|
158
|
+
constructor(wasm, createDecoder) {
|
|
152
159
|
this.#wasm = wasm;
|
|
160
|
+
this.#createSourceDecoder = createDecoder;
|
|
153
161
|
}
|
|
154
162
|
parse(markdown, dialect = Dialect.MARKDOWN) {
|
|
155
163
|
if (typeof markdown !== 'string')
|
|
@@ -197,11 +205,11 @@ class WasmParser {
|
|
|
197
205
|
throw new Error('mausmark: malformed AST header');
|
|
198
206
|
}
|
|
199
207
|
const nodeBytes = nodeCount * NODE_SIZE;
|
|
200
|
-
return new PackedDocument(source, new Uint8Array(memory, nodesPointer, nodeBytes).slice(), nodeCount);
|
|
208
|
+
return new PackedDocument(source, new Uint8Array(memory, nodesPointer, nodeBytes).slice(), nodeCount, this.#createSourceDecoder?.(source));
|
|
201
209
|
}
|
|
202
210
|
}
|
|
203
211
|
/** @internal Shared loader validation used by the Node and browser entry points. */
|
|
204
|
-
export function parserFromInstance(instance) {
|
|
212
|
+
export function parserFromInstance(instance, createDecoder) {
|
|
205
213
|
const exports = instance.exports;
|
|
206
214
|
const memory = exports.memory;
|
|
207
215
|
const heapBase = exports.__heap_base;
|
|
@@ -220,5 +228,5 @@ export function parserFromInstance(instance) {
|
|
|
220
228
|
memory,
|
|
221
229
|
heapBase: heapBase.value,
|
|
222
230
|
parseDom: parseDom,
|
|
223
|
-
});
|
|
231
|
+
}, createDecoder);
|
|
224
232
|
}
|
package/dist/mausmark.wasm
CHANGED
|
Binary file
|
package/dist/node.js
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { Buffer } from 'node:buffer';
|
|
2
3
|
import { parserFromInstance } from './core.js';
|
|
3
4
|
export { ABI_VERSION, Dialect, HtmlMode, Join, ListFlags, NODE_SIZE, NODE_TYPE_MAX, NodeLayout, NodeType, ReferenceStyle, } from './core.js';
|
|
5
|
+
function createSourceDecoder(source) {
|
|
6
|
+
// Share the document's owned bytes, not WASM memory or the input string.
|
|
7
|
+
const buffer = Buffer.from(source.buffer, source.byteOffset, source.byteLength);
|
|
8
|
+
return (offset, length) => {
|
|
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
|
+
return buffer.toString('utf8', offset, end);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
4
17
|
/** Load the package's WASM parser for Node.js. */
|
|
5
18
|
export async function createParser() {
|
|
6
19
|
const wasm = await readFile(new URL('./mausmark.wasm', import.meta.url));
|
|
7
20
|
const result = await WebAssembly.instantiate(wasm, {});
|
|
8
|
-
return parserFromInstance(result.instance);
|
|
21
|
+
return parserFromInstance(result.instance, createSourceDecoder);
|
|
9
22
|
}
|
package/dom-example.js
CHANGED
|
@@ -12,16 +12,21 @@
|
|
|
12
12
|
// passes through as written) and what to do with raw HTML (shown as text here).
|
|
13
13
|
|
|
14
14
|
import {Join, ListFlags, NodeType, ReferenceStyle} from "mausmark";
|
|
15
|
+
import {normalizeReferenceLabel} from "mausmark/references";
|
|
15
16
|
|
|
16
17
|
/** @import {MausmarkDocument} from "mausmark" */
|
|
18
|
+
/** @import {ReferenceMatching} from "mausmark/references" */
|
|
17
19
|
|
|
18
20
|
/**
|
|
19
21
|
* Append the rendered document to `parent`.
|
|
20
22
|
* @param {MausmarkDocument} doc
|
|
21
23
|
* @param {ParentNode} parent
|
|
24
|
+
* @param {{referenceMatching?: ReferenceMatching}} options Renderer lookup policy,
|
|
25
|
+
* independent of the input dialect. Defaults to original Markdown matching.
|
|
22
26
|
*/
|
|
23
|
-
export function renderToDom(doc, parent) {
|
|
24
|
-
|
|
27
|
+
export function renderToDom(doc, parent, {referenceMatching = "markdown"} = {}) {
|
|
28
|
+
normalizeReferenceLabel("", referenceMatching); // validate even a reference-free document
|
|
29
|
+
const definitions = collectDefinitions(doc, referenceMatching);
|
|
25
30
|
|
|
26
31
|
/** @param {number} index @param {ParentNode} into */
|
|
27
32
|
function children(index, into) {
|
|
@@ -43,14 +48,14 @@ export function renderToDom(doc, parent) {
|
|
|
43
48
|
function literalReference(index, into) {
|
|
44
49
|
const {label, style} = doc.link(index);
|
|
45
50
|
if (doc.type(index) === NodeType.IMAGE) {
|
|
46
|
-
into.append("![" +
|
|
51
|
+
into.append("![" + altText(doc, index, definitions, referenceMatching));
|
|
47
52
|
} else {
|
|
48
53
|
into.append("[");
|
|
49
54
|
children(index, into);
|
|
50
55
|
}
|
|
51
56
|
into.append("]");
|
|
52
57
|
if (style === ReferenceStyle.FULL) {
|
|
53
|
-
into.append("[" +
|
|
58
|
+
into.append("[" + unescapeLink(label) + "]");
|
|
54
59
|
} else if (style === ReferenceStyle.COLLAPSED) {
|
|
55
60
|
into.append("[]");
|
|
56
61
|
}
|
|
@@ -63,6 +68,13 @@ export function renderToDom(doc, parent) {
|
|
|
63
68
|
case NodeType.SPAN:
|
|
64
69
|
children(index, into);
|
|
65
70
|
return;
|
|
71
|
+
case NodeType.BRACE_SPAN:
|
|
72
|
+
// Remove these two append calls to omit delimiters. Alternatively,
|
|
73
|
+
// interpret doc.text(index) yourself and skip children entirely.
|
|
74
|
+
into.append("{");
|
|
75
|
+
children(index, into);
|
|
76
|
+
into.append("}");
|
|
77
|
+
return;
|
|
66
78
|
case NodeType.PARAGRAPH:
|
|
67
79
|
element("p", index, into);
|
|
68
80
|
return;
|
|
@@ -126,14 +138,14 @@ export function renderToDom(doc, parent) {
|
|
|
126
138
|
return;
|
|
127
139
|
}
|
|
128
140
|
case NodeType.LINK: {
|
|
129
|
-
const target = resolve(doc, index, definitions);
|
|
141
|
+
const target = resolve(doc, index, definitions, referenceMatching);
|
|
130
142
|
if (!target) {
|
|
131
143
|
literalReference(index, into);
|
|
132
144
|
return;
|
|
133
145
|
}
|
|
134
146
|
const a = document.createElement("a");
|
|
135
147
|
a.setAttribute("href", target.url);
|
|
136
|
-
if (target.
|
|
148
|
+
if (target.titlePresent) {
|
|
137
149
|
a.setAttribute("title", target.title);
|
|
138
150
|
}
|
|
139
151
|
children(index, a);
|
|
@@ -141,15 +153,15 @@ export function renderToDom(doc, parent) {
|
|
|
141
153
|
return;
|
|
142
154
|
}
|
|
143
155
|
case NodeType.IMAGE: {
|
|
144
|
-
const target = resolve(doc, index, definitions);
|
|
156
|
+
const target = resolve(doc, index, definitions, referenceMatching);
|
|
145
157
|
if (!target) {
|
|
146
158
|
literalReference(index, into);
|
|
147
159
|
return;
|
|
148
160
|
}
|
|
149
161
|
const img = document.createElement("img");
|
|
150
162
|
img.setAttribute("src", target.url);
|
|
151
|
-
img.setAttribute("alt",
|
|
152
|
-
if (target.
|
|
163
|
+
img.setAttribute("alt", altText(doc, index, definitions, referenceMatching));
|
|
164
|
+
if (target.titlePresent) {
|
|
153
165
|
img.setAttribute("title", target.title);
|
|
154
166
|
}
|
|
155
167
|
into.append(img);
|
|
@@ -162,7 +174,7 @@ export function renderToDom(doc, parent) {
|
|
|
162
174
|
into.append(doc.joinedText(index));
|
|
163
175
|
return;
|
|
164
176
|
case NodeType.CUSTOM:
|
|
165
|
-
// A
|
|
177
|
+
// A raw multiline custom block: yours to interpret.
|
|
166
178
|
into.append(doc.joinedText(index));
|
|
167
179
|
return;
|
|
168
180
|
case NodeType.TEXT:
|
|
@@ -199,90 +211,117 @@ function decodeEntities(text) {
|
|
|
199
211
|
return area.value;
|
|
200
212
|
}
|
|
201
213
|
|
|
202
|
-
//
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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 = ""; }
|
|
206
228
|
for (const child of doc.children(index)) {
|
|
207
229
|
const type = doc.type(child);
|
|
208
230
|
if (type === NodeType.TEXT) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
} else if (type === NodeType.CODE_SPAN) {
|
|
213
|
-
text += doc.joinedText(child);
|
|
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);
|
|
214
234
|
} else {
|
|
215
|
-
|
|
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);
|
|
216
246
|
}
|
|
217
247
|
}
|
|
248
|
+
flush();
|
|
218
249
|
return text;
|
|
219
250
|
}
|
|
220
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
|
+
|
|
221
264
|
// Reference links arrive unresolved. Definitions can sit anywhere, including
|
|
222
265
|
// inside lists and quotes, so scan every node; the first definition of a label wins.
|
|
223
|
-
/** @param {MausmarkDocument} doc */
|
|
224
|
-
function collectDefinitions(doc) {
|
|
225
|
-
/** @type {Map<string, {url: string, title: string}>} */
|
|
266
|
+
/** @param {MausmarkDocument} doc @param {ReferenceMatching} matching */
|
|
267
|
+
function collectDefinitions(doc, matching) {
|
|
268
|
+
/** @type {Map<string, {url: string, title: string, titlePresent: boolean}>} */
|
|
226
269
|
const definitions = new Map();
|
|
227
270
|
for (let index = 0; index < doc.nodeCount; index += 1) {
|
|
228
271
|
if (doc.type(index) !== NodeType.REF_DEF) continue;
|
|
229
272
|
|
|
230
273
|
const {label, url, title} = doc.link(index);
|
|
231
|
-
const key =
|
|
274
|
+
const key = normalizeReferenceLabel(label, matching);
|
|
275
|
+
if (key === null) continue;
|
|
232
276
|
if (!definitions.has(key)) {
|
|
233
|
-
definitions.set(key, {url, title});
|
|
277
|
+
definitions.set(key, {url, title, titlePresent: titlePresent(doc, index)});
|
|
234
278
|
}
|
|
235
279
|
}
|
|
236
280
|
return definitions;
|
|
237
281
|
}
|
|
238
282
|
|
|
239
|
-
// CommonMark matches labels under full Unicode case folding; toLowerCase() is
|
|
240
|
-
// the approximation.
|
|
241
|
-
/** @param {string} label */
|
|
242
|
-
function normalizeLabel(label) {
|
|
243
|
-
return label.trim().replace(/\s+/g, " ").toLowerCase();
|
|
244
|
-
}
|
|
245
|
-
|
|
246
283
|
// Where a LINK or IMAGE points, or null for a reference with no definition.
|
|
247
284
|
/**
|
|
248
285
|
* @param {MausmarkDocument} doc
|
|
249
286
|
* @param {number} index
|
|
250
|
-
* @param {Map<string, {url: string, title: string}>} definitions
|
|
287
|
+
* @param {Map<string, {url: string, title: string, titlePresent: boolean}>} definitions
|
|
288
|
+
* @param {ReferenceMatching} matching
|
|
251
289
|
*/
|
|
252
|
-
function resolve(doc, index, definitions) {
|
|
290
|
+
function resolve(doc, index, definitions, matching) {
|
|
253
291
|
const {url, title, label, style} = doc.link(index);
|
|
254
292
|
// Autolinks take entities but no backslash escapes.
|
|
255
293
|
if (style === ReferenceStyle.AUTOLINK_EMAIL) {
|
|
256
294
|
return {
|
|
257
295
|
url: "mailto:" + decodeEntities(url),
|
|
258
|
-
title: ""
|
|
296
|
+
title: "", titlePresent: false
|
|
259
297
|
};
|
|
260
298
|
}
|
|
261
299
|
if (style === ReferenceStyle.AUTOLINK_URI) {
|
|
262
300
|
return {
|
|
263
301
|
url: decodeEntities(url),
|
|
264
|
-
title: ""
|
|
302
|
+
title: "", titlePresent: false
|
|
265
303
|
};
|
|
266
304
|
}
|
|
267
305
|
if (style === ReferenceStyle.INLINE) {
|
|
268
306
|
return {
|
|
269
307
|
url: unescapeLink(url),
|
|
270
|
-
title: unescapeLink(title)
|
|
308
|
+
title: unescapeLink(title), titlePresent: titlePresent(doc, index)
|
|
271
309
|
};
|
|
272
310
|
}
|
|
273
|
-
const
|
|
311
|
+
const key = normalizeReferenceLabel(label, matching);
|
|
312
|
+
const definition = key === null ? undefined : definitions.get(key);
|
|
274
313
|
if (!definition) return null;
|
|
275
314
|
|
|
276
315
|
return {
|
|
277
316
|
url: unescapeLink(definition.url),
|
|
278
|
-
title: unescapeLink(definition.title)
|
|
317
|
+
title: unescapeLink(definition.title), titlePresent: definition.titlePresent
|
|
279
318
|
};
|
|
280
319
|
}
|
|
281
320
|
|
|
282
|
-
//
|
|
283
|
-
//
|
|
284
|
-
//
|
|
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.
|
|
285
324
|
/** @param {string} text */
|
|
286
325
|
function unescapeLink(text) {
|
|
287
|
-
return decodeEntities(text.replace(/\\([!-/:-@[-`{-~])/g, "$1")
|
|
326
|
+
return decodeEntities(text).replace(/\\([!-/:-@[-`{-~])/g, "$1");
|
|
288
327
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mausmark",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "A fast, dependency-free Markdown parser compiled to WebAssembly",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -9,23 +9,38 @@
|
|
|
9
9
|
"browser": "./dist/browser.js",
|
|
10
10
|
"node": "./dist/node.js",
|
|
11
11
|
"default": "./dist/browser.js"
|
|
12
|
+
},
|
|
13
|
+
"./references": {
|
|
14
|
+
"types": "./reference-matching.d.ts",
|
|
15
|
+
"default": "./reference-matching.js"
|
|
12
16
|
}
|
|
13
17
|
},
|
|
14
18
|
"files": [
|
|
15
19
|
"dist",
|
|
16
20
|
"dom-example.js",
|
|
21
|
+
"reference-matching.js",
|
|
22
|
+
"reference-matching.d.ts",
|
|
17
23
|
"README.md",
|
|
18
|
-
"
|
|
24
|
+
"RELEASE-NOTES.md",
|
|
25
|
+
"LICENSE",
|
|
26
|
+
"UNICODE-LICENSE.txt",
|
|
27
|
+
"HTML-LICENSE.txt"
|
|
19
28
|
],
|
|
20
29
|
"scripts": {
|
|
21
30
|
"clean": "rm -rf dist",
|
|
22
|
-
"build:wasm": "./build.sh wasm-
|
|
31
|
+
"build:wasm": "./build.sh wasm --ast-only",
|
|
23
32
|
"build:js": "./build.sh codegen && tsc -p tsconfig.json",
|
|
24
33
|
"build": "npm run clean && npm run build:wasm && npm run build:js",
|
|
25
34
|
"check:wasm-size": "node test/wasm_size.mjs",
|
|
26
|
-
"test:package": "node --test test/npm-package.test.mjs test/wasm-size.test.mjs",
|
|
27
|
-
"test:types": "tsc -p tsconfig.test.json --noEmit",
|
|
28
|
-
"test": "
|
|
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",
|
|
36
|
+
"test:types": "tsc -p tsconfig.test.json --noEmit && tsc -p tsconfig.references.json",
|
|
37
|
+
"test:layers": "node --test test/layers.test.mjs",
|
|
38
|
+
"test:build": "node --test test/build.test.mjs",
|
|
39
|
+
"check:unicode": "node tools/unicode.mjs --check && node tools/case-fold.mjs --check",
|
|
40
|
+
"test:unicode": "node --test test/unicode.test.mjs test/case-fold.test.mjs",
|
|
41
|
+
"check:entities": "node tools/entities.mjs --check",
|
|
42
|
+
"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",
|
|
29
44
|
"prepack": "npm test"
|
|
30
45
|
},
|
|
31
46
|
"engines": {
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Reference lookup policy, independent of the parser's input dialect. */
|
|
2
|
+
export type ReferenceMatching = 'markdown' | 'commonmark';
|
|
3
|
+
|
|
4
|
+
/** Normalize raw label spelling; no entity/escape decoding or NFC.
|
|
5
|
+
* An empty CommonMark key returns null and must never match a definition.
|
|
6
|
+
*/
|
|
7
|
+
export function normalizeReferenceLabel(label: string, matching?: ReferenceMatching): string | null;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** @typedef {'markdown' | 'commonmark'} ReferenceMatching */
|
|
2
|
+
|
|
3
|
+
// Fold each scalar independently: whole-string lowercasing has contextual sigma
|
|
4
|
+
// rules. Upper/lower conversion expands ligatures and sharp s; these exceptions
|
|
5
|
+
// preserve dotless i, expand capital sharp s, and keep Cherokee in uppercase.
|
|
6
|
+
/** @param {string} char */
|
|
7
|
+
function foldCharacter(char) {
|
|
8
|
+
const cp = char.codePointAt(0) ?? 0;
|
|
9
|
+
if (cp === 0x131) return char;
|
|
10
|
+
if (cp === 0x1E9E) return 'ss';
|
|
11
|
+
if ((cp >= 0x13A0 && cp <= 0x13F5) || (cp >= 0x13F8 && cp <= 0x13FD) ||
|
|
12
|
+
(cp >= 0xAB70 && cp <= 0xABBF)) return char.toUpperCase();
|
|
13
|
+
return char.toUpperCase().toLowerCase();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Normalize raw label spelling. Does not decode entities/escapes or apply NFC.
|
|
17
|
+
* Markdown matches ASCII case only, preserving whitespace. CommonMark trims and
|
|
18
|
+
* collapses ASCII whitespace and folds Unicode using runtime case conversion.
|
|
19
|
+
* Returns null for an empty CommonMark key, which must never match a definition.
|
|
20
|
+
* @param {string} label
|
|
21
|
+
* @param {ReferenceMatching} matching
|
|
22
|
+
* @returns {string | null}
|
|
23
|
+
*/
|
|
24
|
+
export function normalizeReferenceLabel(label, matching = 'markdown') {
|
|
25
|
+
if (matching === 'markdown') return label.replace(/[A-Z]/g, char => String.fromCharCode(char.charCodeAt(0) + 32));
|
|
26
|
+
if (matching !== 'commonmark') throw new RangeError(`Unknown reference matching policy: ${matching}`);
|
|
27
|
+
const text = label.replace(/[ \t\n\v\f\r]+/g, ' ').replace(/^ | $/g, '');
|
|
28
|
+
if (!text) return null;
|
|
29
|
+
let result = '';
|
|
30
|
+
for (const char of text) result += foldCharacter(char);
|
|
31
|
+
return result;
|
|
32
|
+
}
|