@mdream/js 2.1.0 → 2.2.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.
@@ -1,258 +1,180 @@
1
1
  import "./const.mjs";
2
- const HTML_ENTITIES = {
3
- AElig: "Æ",
4
- AMP: "&",
5
- Aacute: "Á",
6
- Acirc: "Â",
7
- Agrave: "À",
8
- $Alpha: "Α",
9
- Aring: "Å",
10
- Atilde: "Ã",
11
- Auml: "Ä",
12
- $Beta: "Β",
13
- COPY: "©",
14
- Ccedil: "Ç",
15
- $Chi: "Χ",
16
- $Dagger: "‡",
17
- $Delta: "Δ",
18
- ETH: "Ð",
19
- Eacute: "É",
20
- Ecirc: "Ê",
21
- Egrave: "È",
22
- $Epsilon: "Ε",
23
- $Eta: "Η",
24
- Euml: "Ë",
25
- GT: ">",
26
- $Gamma: "Γ",
27
- Iacute: "Í",
28
- Icirc: "Î",
29
- Igrave: "Ì",
30
- $Iota: "Ι",
31
- Iuml: "Ï",
32
- $Kappa: "Κ",
33
- LT: "<",
34
- $Lambda: "Λ",
35
- $Mu: "Μ",
36
- Ntilde: "Ñ",
37
- $Nu: "Ν",
38
- $OElig: "Œ",
39
- Oacute: "Ó",
40
- Ocirc: "Ô",
41
- Ograve: "Ò",
42
- $Omega: "Ω",
43
- $Omicron: "Ο",
44
- Oslash: "Ø",
45
- Otilde: "Õ",
46
- Ouml: "Ö",
47
- $Phi: "Φ",
48
- $Pi: "Π",
49
- $Prime: "″",
50
- $Psi: "Ψ",
51
- QUOT: "\"",
52
- REG: "®",
53
- $Rho: "Ρ",
54
- $Scaron: "Š",
55
- $Sigma: "Σ",
56
- THORN: "Þ",
57
- $Tau: "Τ",
58
- $Theta: "Θ",
59
- Uacute: "Ú",
60
- Ucirc: "Û",
61
- Ugrave: "Ù",
62
- $Upsilon: "Υ",
63
- Uuml: "Ü",
64
- $Xi: "Ξ",
65
- Yacute: "Ý",
66
- $Yuml: "Ÿ",
67
- $Zeta: "Ζ",
68
- aacute: "á",
69
- acirc: "â",
70
- acute: "´",
71
- aelig: "æ",
72
- agrave: "à",
73
- $alefsym: "ℵ",
74
- $alpha: "α",
75
- amp: "&",
76
- $and: "∧",
77
- $ang: "∠",
78
- $apos: "'",
79
- aring: "å",
80
- $asymp: "≈",
81
- atilde: "ã",
82
- auml: "ä",
83
- $bdquo: "„",
84
- $beta: "β",
85
- brvbar: "¦",
86
- $bull: "•",
87
- $cap: "∩",
88
- ccedil: "ç",
89
- cedil: "¸",
90
- cent: "¢",
91
- $chi: "χ",
92
- $circ: "ˆ",
93
- $clubs: "♣",
94
- $cong: "≅",
95
- copy: "©",
96
- $crarr: "↵",
97
- $cup: "∪",
98
- curren: "¤",
99
- $dArr: "⇓",
100
- $dagger: "†",
101
- $darr: "↓",
102
- deg: "°",
103
- $delta: "δ",
104
- $diams: "♦",
105
- divide: "÷",
106
- eacute: "é",
107
- ecirc: "ê",
108
- egrave: "è",
109
- $empty: "∅",
110
- $epsilon: "ε",
111
- $equiv: "≡",
112
- $eta: "η",
113
- eth: "ð",
114
- euml: "ë",
115
- $euro: "€",
116
- $exist: "∃",
117
- $fnof: "ƒ",
118
- $forall: "∀",
119
- frac12: "½",
120
- frac14: "¼",
121
- frac34: "¾",
122
- $frasl: "⁄",
123
- $gamma: "γ",
124
- $ge: "≥",
125
- gt: ">",
126
- $hArr: "⇔",
127
- $harr: "↔",
128
- $hearts: "♥",
129
- $hellip: "…",
130
- iacute: "í",
131
- icirc: "î",
132
- iexcl: "¡",
133
- igrave: "ì",
134
- $image: "ℑ",
135
- $infin: "∞",
136
- $int: "∫",
137
- $iota: "ι",
138
- iquest: "¿",
139
- $isin: "∈",
140
- iuml: "ï",
141
- $kappa: "κ",
142
- $lArr: "⇐",
143
- $lambda: "λ",
144
- $lang: "⟨",
145
- laquo: "«",
146
- $larr: "←",
147
- $lceil: "⌈",
148
- $ldquo: "“",
149
- $le: "≤",
150
- $lfloor: "⌊",
151
- $lowast: "∗",
152
- $loz: "◊",
153
- $lsaquo: "‹",
154
- $lsquo: "‘",
155
- lt: "<",
156
- macr: "¯",
157
- $mdash: "—",
158
- micro: "µ",
159
- middot: "·",
160
- $minus: "−",
161
- $mu: "μ",
162
- $nabla: "∇",
163
- nbsp: "\xA0",
164
- $ndash: "–",
165
- $ne: "≠",
166
- $ni: "∋",
167
- not: "¬",
168
- $notin: "∉",
169
- $nsub: "⊄",
170
- ntilde: "ñ",
171
- $nu: "ν",
172
- oacute: "ó",
173
- ocirc: "ô",
174
- $oelig: "œ",
175
- ograve: "ò",
176
- $oline: "‾",
177
- $omega: "ω",
178
- $omicron: "ο",
179
- $oplus: "⊕",
180
- $or: "∨",
181
- ordf: "ª",
182
- ordm: "º",
183
- oslash: "ø",
184
- otilde: "õ",
185
- $otimes: "⊗",
186
- ouml: "ö",
187
- para: "¶",
188
- $part: "∂",
189
- $permil: "‰",
190
- $perp: "⊥",
191
- $phi: "φ",
192
- $pi: "π",
193
- $piv: "ϖ",
194
- plusmn: "±",
195
- pound: "£",
196
- $prime: "′",
197
- $prod: "∏",
198
- $prop: "∝",
199
- $psi: "ψ",
200
- quot: "\"",
201
- $rArr: "⇒",
202
- $radic: "√",
203
- $rang: "⟩",
204
- raquo: "»",
205
- $rarr: "→",
206
- $rceil: "⌉",
207
- $rdquo: "”",
208
- $real: "ℜ",
209
- reg: "®",
210
- $rfloor: "⌋",
211
- $rho: "ρ",
212
- $rsaquo: "›",
213
- $rsquo: "’",
214
- $sbquo: "‚",
215
- $scaron: "š",
216
- $sdot: "⋅",
217
- sect: "§",
218
- shy: "­",
219
- $sigma: "σ",
220
- $sigmaf: "ς",
221
- $sim: "∼",
222
- $spades: "♠",
223
- $sub: "⊂",
224
- $sube: "⊆",
225
- $sum: "∑",
226
- $sup: "⊃",
227
- sup1: "¹",
228
- sup2: "²",
229
- sup3: "³",
230
- $supe: "⊇",
231
- szlig: "ß",
232
- $tau: "τ",
233
- $there4: "∴",
234
- $theta: "θ",
235
- $thetasym: "ϑ",
236
- thorn: "þ",
237
- $tilde: "˜",
238
- times: "×",
239
- $trade: "™",
240
- $uArr: "⇑",
241
- uacute: "ú",
242
- $uarr: "↑",
243
- ucirc: "û",
244
- ugrave: "ù",
245
- uml: "¨",
246
- $upsih: "ϒ",
247
- $upsilon: "υ",
248
- uuml: "ü",
249
- $weierp: "℘",
250
- $xi: "ξ",
251
- yacute: "ý",
252
- yen: "¥",
253
- yuml: "ÿ",
254
- $zeta: "ζ"
2
+ const selfClosing = { isSelfClosing: true };
3
+ const rawText = { isNonNesting: true };
4
+ const inertRawText = {
5
+ isNonNesting: true,
6
+ excludesTextNodes: true
255
7
  };
8
+ const parserTagHandlers = {
9
+ [4]: inertRawText,
10
+ [52]: inertRawText,
11
+ [53]: inertRawText,
12
+ [5]: selfClosing,
13
+ [6]: selfClosing,
14
+ [13]: selfClosing,
15
+ [27]: selfClosing,
16
+ [29]: { excludesTextNodes: true },
17
+ [30]: { excludesTextNodes: true },
18
+ [38]: { excludesTextNodes: true },
19
+ [39]: { excludesTextNodes: true },
20
+ [54]: selfClosing,
21
+ [55]: selfClosing,
22
+ [56]: selfClosing,
23
+ [57]: selfClosing,
24
+ [58]: selfClosing,
25
+ [59]: selfClosing,
26
+ [60]: selfClosing,
27
+ [61]: selfClosing,
28
+ [62]: selfClosing,
29
+ [63]: selfClosing,
30
+ [64]: selfClosing,
31
+ [66]: rawText,
32
+ [73]: inertRawText,
33
+ [78]: { excludesTextNodes: true },
34
+ [84]: inertRawText,
35
+ [112]: inertRawText,
36
+ [108]: inertRawText,
37
+ [85]: inertRawText,
38
+ [86]: rawText,
39
+ [87]: rawText
40
+ };
41
+ function isOpenVoidTag(handler, tagId) {
42
+ return handler?.isSelfClosing === false && !!parserTagHandlers[tagId ?? -1]?.isSelfClosing;
43
+ }
44
+ const HTML_ENTITIES = /* @__PURE__ */ (() => {
45
+ const entities = Object.setPrototypeOf({
46
+ AMP: "&",
47
+ $Dagger: "‡",
48
+ GT: ">",
49
+ LT: "<",
50
+ $OElig: "Œ",
51
+ $Prime: "″",
52
+ QUOT: "\"",
53
+ $Scaron: "Š",
54
+ $Yuml: "Ÿ",
55
+ $alefsym: "ℵ",
56
+ amp: "&",
57
+ $and: "∧",
58
+ $ang: "∠",
59
+ $apos: "'",
60
+ $asymp: "≈",
61
+ $bdquo: "„",
62
+ $bull: "•",
63
+ $cap: "∩",
64
+ $circ: "ˆ",
65
+ $clubs: "♣",
66
+ $cong: "≅",
67
+ copy: "©",
68
+ $crarr: "↵",
69
+ $cup: "∪",
70
+ $dArr: "⇓",
71
+ $dagger: "†",
72
+ $darr: "↓",
73
+ $diams: "♦",
74
+ $empty: "∅",
75
+ $equiv: "≡",
76
+ $euro: "€",
77
+ $exist: "∃",
78
+ $fnof: "ƒ",
79
+ $forall: "∀",
80
+ $frasl: "⁄",
81
+ $ge: "≥",
82
+ gt: ">",
83
+ $hArr: "⇔",
84
+ $harr: "↔",
85
+ $hearts: "♥",
86
+ $hellip: "…",
87
+ $image: "ℑ",
88
+ $infin: "∞",
89
+ $int: "∫",
90
+ $isin: "∈",
91
+ $lArr: "⇐",
92
+ $lang: "⟨",
93
+ $larr: "←",
94
+ $lceil: "⌈",
95
+ $ldquo: "“",
96
+ $le: "≤",
97
+ $lfloor: "⌊",
98
+ $lowast: "∗",
99
+ $loz: "◊",
100
+ $lsaquo: "‹",
101
+ $lsquo: "‘",
102
+ lt: "<",
103
+ $mdash: "—",
104
+ $minus: "−",
105
+ $nabla: "∇",
106
+ $ndash: "–",
107
+ $ne: "≠",
108
+ $ni: "∋",
109
+ $notin: "∉",
110
+ $nsub: "⊄",
111
+ $oelig: "œ",
112
+ $oline: "‾",
113
+ $oplus: "⊕",
114
+ $or: "∨",
115
+ $otimes: "⊗",
116
+ $part: "∂",
117
+ $permil: "‰",
118
+ $perp: "⊥",
119
+ $piv: "ϖ",
120
+ $prime: "′",
121
+ $prod: "∏",
122
+ $prop: "∝",
123
+ quot: "\"",
124
+ $rArr: "⇒",
125
+ $radic: "√",
126
+ $rang: "⟩",
127
+ $rarr: "→",
128
+ $rceil: "⌉",
129
+ $rdquo: "”",
130
+ $real: "ℜ",
131
+ reg: "®",
132
+ $rfloor: "⌋",
133
+ $rsaquo: "›",
134
+ $rsquo: "’",
135
+ $sbquo: "‚",
136
+ $scaron: "š",
137
+ $sdot: "⋅",
138
+ $sigmaf: "ς",
139
+ $sim: "∼",
140
+ $spades: "♠",
141
+ $sub: "⊂",
142
+ $sube: "⊆",
143
+ $sum: "∑",
144
+ $sup: "⊃",
145
+ $supe: "⊇",
146
+ $there4: "∴",
147
+ $thetasym: "ϑ",
148
+ $tilde: "˜",
149
+ $trade: "™",
150
+ $uArr: "⇑",
151
+ $uarr: "↑",
152
+ $upsih: "ϒ",
153
+ $weierp: "℘"
154
+ }, null);
155
+ const latin1Names = "nbsp iexcl cent pound curren yen brvbar sect uml COPY ordf laquo not shy REG macr deg plusmn sup2 sup3 acute micro para middot cedil sup1 ordm raquo frac14 frac12 frac34 iquest Agrave Aacute Acirc Atilde Auml Aring AElig Ccedil Egrave Eacute Ecirc Euml Igrave Iacute Icirc Iuml ETH Ntilde Ograve Oacute Ocirc Otilde Ouml times Oslash Ugrave Uacute Ucirc Uuml Yacute THORN szlig agrave aacute acirc atilde auml aring aelig ccedil egrave eacute ecirc euml igrave iacute icirc iuml eth ntilde ograve oacute ocirc otilde ouml divide oslash ugrave uacute ucirc uuml yacute thorn yuml".split(" ");
156
+ for (const [index, name] of latin1Names.entries()) if (name) entities[name] = String.fromCharCode(160 + index);
157
+ const greekNames = "alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu nu xi omicron pi rho sigma tau upsilon phi chi psi omega".split(" ");
158
+ for (const [index, name] of greekNames.entries()) {
159
+ if (!name) continue;
160
+ entities[`$${name}`] = String.fromCharCode(945 + index);
161
+ entities[`$${name[0].toUpperCase()}${name.slice(1)}`] = String.fromCharCode(913 + index);
162
+ }
163
+ return entities;
164
+ })();
165
+ function isBlockChildOfOpenVoid(node) {
166
+ const tagId = node.tagId;
167
+ if (tagId === void 0 || tagId < 0 || node.tagHandler?.isInline) return false;
168
+ const spanExempt = tagId === 35 || tagId === 36 || tagId === 25;
169
+ for (let parent = node.parent; parent; parent = parent.parent) {
170
+ if (!parent.tagHandler?.collapsesInnerWhiteSpace) continue;
171
+ const parentId = parent.tagId;
172
+ if (parentId !== void 0 && parserTagHandlers[parentId]?.isSelfClosing) continue;
173
+ if (parentId === 37 && spanExempt) continue;
174
+ return false;
175
+ }
176
+ return true;
177
+ }
256
178
  function lowerAscii(code) {
257
179
  return code >= 65 && code <= 90 ? code + 32 : code;
258
180
  }
@@ -411,7 +333,7 @@ function parseUnsignedInteger(raw) {
411
333
  const value = Number(raw);
412
334
  return value <= 4294967295 ? value : void 0;
413
335
  }
414
- function trimOutputStart(value) {
336
+ function outputLeadLength(value) {
415
337
  let start = 0;
416
338
  while (start < value.length) {
417
339
  const code = value.charCodeAt(start);
@@ -422,19 +344,30 @@ function trimOutputStart(value) {
422
344
  if (code < 133 || !(code === 133 || code === 160 || code === 5760 || code >= 8192 && code <= 8202 || code === 8232 || code === 8233 || code === 8239 || code === 8287 || code === 12288)) break;
423
345
  start++;
424
346
  }
347
+ return start;
348
+ }
349
+ function trimOutputStart(value) {
350
+ const start = outputLeadLength(value);
425
351
  return start === 0 ? value : value.slice(start);
426
352
  }
427
- function trimTextAtLineStart(node, buffer) {
428
- const last = lastOutputChar(buffer);
429
- if (last !== -1 && last !== 10) return;
430
- const value = node.value;
353
+ function leadingWhitespaceLength(value) {
431
354
  let start = 0;
432
355
  while (start < value.length) {
433
356
  const code = value.charCodeAt(start);
434
357
  if (code !== 32 && code !== 10 && code !== 9 && code !== 13 && code !== 12) break;
435
358
  start++;
436
359
  }
437
- if (start !== 0) node.value = value.slice(start);
360
+ return start;
361
+ }
362
+ function dropSourceLead(node) {
363
+ const start = Math.min(node.sourceLead, leadingWhitespaceLength(node.value));
364
+ node.sourceLead = 0;
365
+ if (start !== 0) node.value = node.value.slice(start);
366
+ }
367
+ function trimTextAtLineStart(node, buffer) {
368
+ if (!node.sourceLead) return;
369
+ const last = lastOutputChar(buffer);
370
+ if (last === -1 || last === 10) dropSourceLead(node);
438
371
  }
439
372
  function needsSpacing(lastChar, firstChar) {
440
373
  if (!lastChar || " \n ".includes(lastChar) || " \n ".includes(firstChar)) return false;
@@ -727,7 +660,15 @@ function resolveAgainstBase(origin, url) {
727
660
  if (path.charCodeAt(0) === SLASH_CHAR) return `${baseRoot}${dot ? mergeDotSegments("/", path.slice(1)) : path}${suffix}`;
728
661
  return `${baseRoot}${dot ? mergeDotSegments(baseDirectory, path) : baseDirectory + path}${suffix}`;
729
662
  }
663
+ function trimUrl(url) {
664
+ let start = 0;
665
+ let end = url.length;
666
+ while (start < end && url.charCodeAt(start) <= 32) start++;
667
+ while (end > start && url.charCodeAt(end - 1) <= 32) end--;
668
+ return start === 0 && end === url.length ? url : url.slice(start, end);
669
+ }
730
670
  function resolveUrl(url, origin, clean) {
671
+ url = trimUrl(url);
731
672
  if (!url || url[0] === "#") return url;
732
673
  const isSlash = url.charCodeAt(0) === SLASH_CHAR;
733
674
  let resolved = url;
@@ -747,4 +688,4 @@ function safeAnchorOutput(node, options, entering, protectMarkdown = false) {
747
688
  if (!entering) return "</a>";
748
689
  return `<a href="${escapeHtml(resolved, true, protectMarkdown)}"${node.attributes?.title === void 0 ? "" : ` title="${escapeHtml(node.attributes.title, true, protectMarkdown)}"`}>`;
749
690
  }
750
- export { blockOpenPrefix, decodeHTMLEntities, endsAtHardBreak, escapeHtml, figcaptionOwnsBlockSpacing, getLanguageFromAttributes, hasNonWhitespace, isCharacterReferenceTail, isDataUrl, isEmptyLinkHref, isInsideHeading, isInsideTableCell, isSafeHtmlUrl, lastOutputChar, listMarkerLineStart, markRenderedChildContent, needsSpacing, noteBufferRewrite, orderedItemNumber, parseUnsignedInteger, pendingMarkerIndent, resolveUrl, safeAnchorOutput, suppressesFormattingInPre, traverseUpToFirstBlockNode, trimOutputStart, trimTextAtLineStart };
691
+ export { blockOpenPrefix, decodeHTMLEntities, dropSourceLead, endsAtHardBreak, escapeHtml, figcaptionOwnsBlockSpacing, getLanguageFromAttributes, hasNonWhitespace, isBlockChildOfOpenVoid, isCharacterReferenceTail, isDataUrl, isEmptyLinkHref, isInsideHeading, isInsideTableCell, isOpenVoidTag, isSafeHtmlUrl, lastOutputChar, leadingWhitespaceLength, listMarkerLineStart, markRenderedChildContent, needsSpacing, noteBufferRewrite, orderedItemNumber, outputLeadLength, parseUnsignedInteger, parserTagHandlers, pendingMarkerIndent, resolveUrl, safeAnchorOutput, suppressesFormattingInPre, traverseUpToFirstBlockNode, trimOutputStart, trimTextAtLineStart, trimUrl };
package/dist/parse.d.mts CHANGED
@@ -69,6 +69,10 @@ export interface ParseState {
69
69
  tagOverrideHandlers?: Map<string, TagHandler>;
70
70
  /** Whether emitted text should skip Markdown-only escaping */
71
71
  plainText?: boolean;
72
+ keepLeadingBlanks?: boolean;
73
+ /** Reuse ancestry for repeated retained blanks before any visible child. */
74
+ leadingBlankParent?: ElementNode;
75
+ leadingBlankAncestors?: ElementNode[];
72
76
  /** Output state whose `options.origin` the first usable `<base href>` replaces. */
73
77
  urlBaseTarget?: {
74
78
  options: EngineOptions;
@@ -83,6 +87,8 @@ export interface ParseState {
83
87
  trailingText?: string;
84
88
  /** The trailing run is an unfinished end tag of the open rawtext element. */
85
89
  rawtextEndTagPending?: boolean;
90
+ /** A raw-text body the output drops left the previous chunk on a whitespace character. */
91
+ inertBodyBlankPending?: boolean;
86
92
  /** The last token was an end tag that closed nothing; the next text joins. */
87
93
  endTagClosedNothing?: boolean;
88
94
  }
package/dist/splitter.mjs CHANGED
@@ -232,7 +232,7 @@ function* htmlToMarkdownSplitChunksStream(html, options = {}) {
232
232
  headers: new Map(headerHierarchy)
233
233
  });
234
234
  };
235
- const processResolvedEvent = opts.resolvedPlugins?.length ? (event) => processPluginsForEvent(event, opts.resolvedPlugins, processor.state, processor.processEvent) : processor.processEvent;
235
+ const processResolvedEvent = opts.resolvedPlugins?.length ? (event) => processPluginsForEvent(event, opts.resolvedPlugins, processor.state, processor.processEvent, processor.skipEvent) : processor.processEvent;
236
236
  const handleEvent = (event) => {
237
237
  const { type: eventType, node } = event;
238
238
  if (node.type === 1) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mdream/js",
3
3
  "type": "module",
4
- "version": "2.1.0",
4
+ "version": "2.2.0",
5
5
  "description": "Tree-shakable JavaScript converters and plugins for mdream.",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",