mausmark 0.3.0 → 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 +37 -31
- package/RELEASE-NOTES.md +27 -0
- package/dist/core.d.ts +5 -3
- package/dist/core.js +1 -1
- package/dist/mausmark.wasm +0 -0
- package/dom-example.js +74 -42
- package/package.json +20 -8
- 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
|
@@ -13,6 +13,9 @@ Project page and demo: https://myshkin.eu/mausmark
|
|
|
13
13
|
|
|
14
14
|
npm install mausmark
|
|
15
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
|
+
|
|
16
19
|
## Usage
|
|
17
20
|
|
|
18
21
|
import {createParser, Dialect, NodeType} from "mausmark";
|
|
@@ -88,19 +91,18 @@ raw, and CommonMark treats braces as ordinary text.
|
|
|
88
91
|
Compare node types against the exported constants: their numeric values
|
|
89
92
|
can change between versions.
|
|
90
93
|
|
|
91
|
-
### Upgrading from 0.
|
|
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.
|
|
92
98
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
`CUSTOM` blocks are unchanged, and CommonMark still treats braces as
|
|
99
|
-
ordinary text.
|
|
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`.
|
|
100
104
|
|
|
101
|
-
|
|
102
|
-
from the same release. The node record remains 16 bytes, and existing
|
|
103
|
-
node-type numeric values are unchanged.
|
|
105
|
+
For more details, see the [release notes](RELEASE-NOTES.md).
|
|
104
106
|
|
|
105
107
|
## Writing a renderer
|
|
106
108
|
|
|
@@ -171,7 +173,9 @@ Things to decide when building a richer renderer:
|
|
|
171
173
|
definitions in containers), store the first definition for each
|
|
172
174
|
normalized label, and decide how to render unresolved references.
|
|
173
175
|
CommonMark label matching uses Unicode case folding and whitespace
|
|
174
|
-
collapse, not just `toLowerCase()
|
|
176
|
+
collapse, not just `toLowerCase()`;
|
|
177
|
+
`normalizeReferenceLabel(label, "markdown" | "commonmark")` from
|
|
178
|
+
`mausmark/references` does either. An `AUTOLINK_EMAIL` URL is the
|
|
175
179
|
bare address; add `mailto:` when emitting it. `title === ""` alone
|
|
176
180
|
does not distinguish an absent title from an explicit empty title;
|
|
177
181
|
inspect the leading `TITLE` field if that matters to your output.
|
|
@@ -189,38 +193,39 @@ Things to decide when building a richer renderer:
|
|
|
189
193
|
|
|
190
194
|
## Performance
|
|
191
195
|
|
|
192
|
-
The SIMD WebAssembly parser build is **
|
|
193
|
-
**
|
|
196
|
+
The SIMD WebAssembly parser build is **78.0 KB**, uncompressed, or
|
|
197
|
+
**26.1 KB with Brotli** (quality 11, offline precompression).
|
|
194
198
|
Source-to-AST measurements on an Apple M3 Pro, Node.js 26.3.1 and
|
|
195
199
|
Bun 1.4.2, compiled with Clang 22.1.7 at `-Os` with LTO:
|
|
196
200
|
|
|
197
201
|
| Input | Node (µs) | Bun (µs) | Node (GB/s) | Bun (GB/s) |
|
|
198
202
|
| ---------------------- | --------: | -------: | ----------: | ---------: |
|
|
199
|
-
| Moby-Dick (1.2 MB) | ~
|
|
200
|
-
| Line-wrapped Moby-Dick | ~
|
|
201
|
-
| Pro Git chapters |
|
|
202
|
-
| tldr pages | 1.
|
|
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 |
|
|
203
207
|
|
|
204
208
|
These are warmed parser-export timings with preloaded UTF-8 input and a
|
|
205
209
|
reused arena, including JavaScript iteration and export-call overhead.
|
|
206
210
|
They exclude initialization, input encoding/copying, AST snapshot
|
|
207
211
|
copying, and rendering; they are not end-to-end timings of the package’s
|
|
208
|
-
`parse()` method. The
|
|
209
|
-
figures are medians across three alternating passes, each
|
|
210
|
-
timing samples. Pro Git uses Markdown, tldr uses CommonMark;
|
|
211
|
-
rounds the close results from both dialects. GB/s uses decimal
|
|
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.
|
|
212
217
|
|
|
213
218
|
## Notes
|
|
214
219
|
|
|
215
|
-
- Node.js 22.14+ or a modern browser with WebAssembly SIMD.
|
|
216
|
-
- Browser bundlers must emit the package-relative `.wasm` asset.
|
|
217
220
|
- Input is limited to 64 MiB; parser arena growth is limited to 512
|
|
218
221
|
MiB.
|
|
219
|
-
- Two input dialects
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
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.
|
|
224
229
|
- No GFM yet: no tables, strikethrough, task lists or autolink
|
|
225
230
|
literals.
|
|
226
231
|
|
|
@@ -231,5 +236,6 @@ MIT.
|
|
|
231
236
|
|
|
232
237
|
## License
|
|
233
238
|
|
|
234
|
-
MIT. See [LICENSE](LICENSE). Unicode
|
|
235
|
-
|
|
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.
|
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;
|
|
@@ -25,7 +26,7 @@ export interface MausmarkDocument {
|
|
|
25
26
|
type(index: number): NodeType;
|
|
26
27
|
/**
|
|
27
28
|
* Type-specific value: the heading level on HEADING, the `ReferenceStyle` on
|
|
28
|
-
* 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.
|
|
29
30
|
*/
|
|
30
31
|
level(index: number): number;
|
|
31
32
|
/** `ListFlags` bits on LIST and LIST_ITEM. Other bits and nodes are internal. */
|
|
@@ -46,7 +47,8 @@ export interface MausmarkDocument {
|
|
|
46
47
|
children(index: number): Iterable<number>;
|
|
47
48
|
/**
|
|
48
49
|
* The node's text chunks joined with their `Join` codes; not recursive.
|
|
49
|
-
* Use text() for a BRACE_SPAN's raw interior.
|
|
50
|
+
* Use text() for a BRACE_SPAN's raw interior. Image descriptions with
|
|
51
|
+
* reference containers need renderer-side traversal and resolution instead.
|
|
50
52
|
*/
|
|
51
53
|
joinedText(index: number): string;
|
|
52
54
|
/** Raw link fields of a LINK, IMAGE or REF_DEF; throws for other nodes. */
|
package/dist/core.js
CHANGED
|
@@ -88,7 +88,7 @@ class PackedDocument {
|
|
|
88
88
|
let result = '';
|
|
89
89
|
for (const child of this.children(index)) {
|
|
90
90
|
foundChild = true;
|
|
91
|
-
if (this.type(child) === NodeType.TEXT) {
|
|
91
|
+
if (this.type(child) === NodeType.TEXT || this.type(child) === NodeType.LITERAL_TEXT) {
|
|
92
92
|
const join = this.level(child);
|
|
93
93
|
if (join === Join.SPACE)
|
|
94
94
|
result += ' ';
|
package/dist/mausmark.wasm
CHANGED
|
Binary file
|
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
|
}
|
|
@@ -133,14 +138,14 @@ export function renderToDom(doc, parent) {
|
|
|
133
138
|
return;
|
|
134
139
|
}
|
|
135
140
|
case NodeType.LINK: {
|
|
136
|
-
const target = resolve(doc, index, definitions);
|
|
141
|
+
const target = resolve(doc, index, definitions, referenceMatching);
|
|
137
142
|
if (!target) {
|
|
138
143
|
literalReference(index, into);
|
|
139
144
|
return;
|
|
140
145
|
}
|
|
141
146
|
const a = document.createElement("a");
|
|
142
147
|
a.setAttribute("href", target.url);
|
|
143
|
-
if (target.
|
|
148
|
+
if (target.titlePresent) {
|
|
144
149
|
a.setAttribute("title", target.title);
|
|
145
150
|
}
|
|
146
151
|
children(index, a);
|
|
@@ -148,15 +153,15 @@ export function renderToDom(doc, parent) {
|
|
|
148
153
|
return;
|
|
149
154
|
}
|
|
150
155
|
case NodeType.IMAGE: {
|
|
151
|
-
const target = resolve(doc, index, definitions);
|
|
156
|
+
const target = resolve(doc, index, definitions, referenceMatching);
|
|
152
157
|
if (!target) {
|
|
153
158
|
literalReference(index, into);
|
|
154
159
|
return;
|
|
155
160
|
}
|
|
156
161
|
const img = document.createElement("img");
|
|
157
162
|
img.setAttribute("src", target.url);
|
|
158
|
-
img.setAttribute("alt",
|
|
159
|
-
if (target.
|
|
163
|
+
img.setAttribute("alt", altText(doc, index, definitions, referenceMatching));
|
|
164
|
+
if (target.titlePresent) {
|
|
160
165
|
img.setAttribute("title", target.title);
|
|
161
166
|
}
|
|
162
167
|
into.append(img);
|
|
@@ -206,90 +211,117 @@ function decodeEntities(text) {
|
|
|
206
211
|
return area.value;
|
|
207
212
|
}
|
|
208
213
|
|
|
209
|
-
//
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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 = ""; }
|
|
213
228
|
for (const child of doc.children(index)) {
|
|
214
229
|
const type = doc.type(child);
|
|
215
230
|
if (type === NodeType.TEXT) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
} else if (type === NodeType.CODE_SPAN) {
|
|
220
|
-
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);
|
|
221
234
|
} else {
|
|
222
|
-
|
|
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);
|
|
223
246
|
}
|
|
224
247
|
}
|
|
248
|
+
flush();
|
|
225
249
|
return text;
|
|
226
250
|
}
|
|
227
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
|
+
|
|
228
264
|
// Reference links arrive unresolved. Definitions can sit anywhere, including
|
|
229
265
|
// inside lists and quotes, so scan every node; the first definition of a label wins.
|
|
230
|
-
/** @param {MausmarkDocument} doc */
|
|
231
|
-
function collectDefinitions(doc) {
|
|
232
|
-
/** @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}>} */
|
|
233
269
|
const definitions = new Map();
|
|
234
270
|
for (let index = 0; index < doc.nodeCount; index += 1) {
|
|
235
271
|
if (doc.type(index) !== NodeType.REF_DEF) continue;
|
|
236
272
|
|
|
237
273
|
const {label, url, title} = doc.link(index);
|
|
238
|
-
const key =
|
|
274
|
+
const key = normalizeReferenceLabel(label, matching);
|
|
275
|
+
if (key === null) continue;
|
|
239
276
|
if (!definitions.has(key)) {
|
|
240
|
-
definitions.set(key, {url, title});
|
|
277
|
+
definitions.set(key, {url, title, titlePresent: titlePresent(doc, index)});
|
|
241
278
|
}
|
|
242
279
|
}
|
|
243
280
|
return definitions;
|
|
244
281
|
}
|
|
245
282
|
|
|
246
|
-
// CommonMark matches labels under full Unicode case folding; toLowerCase() is
|
|
247
|
-
// the approximation.
|
|
248
|
-
/** @param {string} label */
|
|
249
|
-
function normalizeLabel(label) {
|
|
250
|
-
return label.trim().replace(/\s+/g, " ").toLowerCase();
|
|
251
|
-
}
|
|
252
|
-
|
|
253
283
|
// Where a LINK or IMAGE points, or null for a reference with no definition.
|
|
254
284
|
/**
|
|
255
285
|
* @param {MausmarkDocument} doc
|
|
256
286
|
* @param {number} index
|
|
257
|
-
* @param {Map<string, {url: string, title: string}>} definitions
|
|
287
|
+
* @param {Map<string, {url: string, title: string, titlePresent: boolean}>} definitions
|
|
288
|
+
* @param {ReferenceMatching} matching
|
|
258
289
|
*/
|
|
259
|
-
function resolve(doc, index, definitions) {
|
|
290
|
+
function resolve(doc, index, definitions, matching) {
|
|
260
291
|
const {url, title, label, style} = doc.link(index);
|
|
261
292
|
// Autolinks take entities but no backslash escapes.
|
|
262
293
|
if (style === ReferenceStyle.AUTOLINK_EMAIL) {
|
|
263
294
|
return {
|
|
264
295
|
url: "mailto:" + decodeEntities(url),
|
|
265
|
-
title: ""
|
|
296
|
+
title: "", titlePresent: false
|
|
266
297
|
};
|
|
267
298
|
}
|
|
268
299
|
if (style === ReferenceStyle.AUTOLINK_URI) {
|
|
269
300
|
return {
|
|
270
301
|
url: decodeEntities(url),
|
|
271
|
-
title: ""
|
|
302
|
+
title: "", titlePresent: false
|
|
272
303
|
};
|
|
273
304
|
}
|
|
274
305
|
if (style === ReferenceStyle.INLINE) {
|
|
275
306
|
return {
|
|
276
307
|
url: unescapeLink(url),
|
|
277
|
-
title: unescapeLink(title)
|
|
308
|
+
title: unescapeLink(title), titlePresent: titlePresent(doc, index)
|
|
278
309
|
};
|
|
279
310
|
}
|
|
280
|
-
const
|
|
311
|
+
const key = normalizeReferenceLabel(label, matching);
|
|
312
|
+
const definition = key === null ? undefined : definitions.get(key);
|
|
281
313
|
if (!definition) return null;
|
|
282
314
|
|
|
283
315
|
return {
|
|
284
316
|
url: unescapeLink(definition.url),
|
|
285
|
-
title: unescapeLink(definition.title)
|
|
317
|
+
title: unescapeLink(definition.title), titlePresent: definition.titlePresent
|
|
286
318
|
};
|
|
287
319
|
}
|
|
288
320
|
|
|
289
|
-
//
|
|
290
|
-
//
|
|
291
|
-
//
|
|
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.
|
|
292
324
|
/** @param {string} text */
|
|
293
325
|
function unescapeLink(text) {
|
|
294
|
-
return decodeEntities(text.replace(/\\([!-/:-@[-`{-~])/g, "$1")
|
|
326
|
+
return decodeEntities(text).replace(/\\([!-/:-@[-`{-~])/g, "$1");
|
|
295
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,26 +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",
|
|
24
|
+
"RELEASE-NOTES.md",
|
|
18
25
|
"LICENSE",
|
|
19
|
-
"UNICODE-LICENSE.txt"
|
|
26
|
+
"UNICODE-LICENSE.txt",
|
|
27
|
+
"HTML-LICENSE.txt"
|
|
20
28
|
],
|
|
21
29
|
"scripts": {
|
|
22
30
|
"clean": "rm -rf dist",
|
|
23
|
-
"build:wasm": "./build.sh wasm-
|
|
31
|
+
"build:wasm": "./build.sh wasm --ast-only",
|
|
24
32
|
"build:js": "./build.sh codegen && tsc -p tsconfig.json",
|
|
25
33
|
"build": "npm run clean && npm run build:wasm && npm run build:js",
|
|
26
34
|
"check:wasm-size": "node test/wasm_size.mjs",
|
|
27
|
-
"test:package": "node --test test/npm-package.test.mjs test/wasm-size.test.mjs test/source-retention.test.mjs",
|
|
28
|
-
"test:types": "tsc -p tsconfig.test.json --noEmit",
|
|
29
|
-
"
|
|
30
|
-
"test:
|
|
31
|
-
"
|
|
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",
|
|
32
44
|
"prepack": "npm test"
|
|
33
45
|
},
|
|
34
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
|
+
}
|