@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,5 +1,5 @@
1
1
  import "./const.mjs";
2
- import { isInsideHeading, resolveUrl } from "./url.mjs";
2
+ import { isInsideHeading, resolveUrl, trimUrl } from "./url.mjs";
3
3
  const ALL_RULES = {
4
4
  urls: true,
5
5
  fragments: true,
@@ -35,19 +35,23 @@ function startPass(rules, target) {
35
35
  const depthMap = target.depthMap;
36
36
  const links = [];
37
37
  const brackets = [];
38
+ let bracketClosers;
38
39
  const spans = [];
39
40
  const spanFragments = [];
41
+ const spanClosers = [];
40
42
  let closing = -1;
43
+ let closingCloser = 0;
41
44
  const headings = [];
42
45
  let headingStart = -1;
43
46
  return {
44
47
  holdsOutput: fragments,
45
- enter(element, outputStart) {
48
+ enter(element, outputStart, closer = 0) {
46
49
  closing = -1;
47
50
  const tagId = element.tagId;
48
51
  if (tagId === 26) {
49
52
  links.push(element);
50
53
  brackets.push(outputStart < buffer.length && buffer[buffer.length - 1] === "[" ? buffer.length - 1 : -1);
54
+ if (closer) (bracketClosers ??= [])[links.length - 1] = closer;
51
55
  } else if (fragments && headingStart < 0 && tagId >= 7 && tagId <= 12 && !depthMap[26]) headingStart = buffer.length;
52
56
  },
53
57
  exit(element) {
@@ -56,8 +60,10 @@ function startPass(rules, target) {
56
60
  if (tagId === 26) {
57
61
  for (let index = links.length - 1; index >= 0; index--) if (links[index] === element) {
58
62
  closing = brackets[index];
63
+ closingCloser = bracketClosers?.[index] ?? 0;
59
64
  links.length = index;
60
65
  brackets.length = index;
66
+ if (bracketClosers && bracketClosers.length > index) bracketClosers.length = index;
61
67
  break;
62
68
  }
63
69
  } else if (headingStart >= 0 && tagId >= 7 && tagId <= 12 && !depthMap[26]) {
@@ -73,16 +79,19 @@ function startPass(rules, target) {
73
79
  let textLength = 0;
74
80
  for (let index = bracket + 1; index < buffer.length; index++) textLength += buffer[index].length;
75
81
  if (textLength === 0) return false;
76
- if (!(selfLinkHeadings && href.charCodeAt(0) === 35 && isInsideHeading(depthMap)) && !(redundantLinks && spells(buffer, bracket + 1, textLength, resolveUrl(href, target.options?.origin, rules)))) return false;
77
- const unwrapped = opener.slice(0, -1) + buffer.slice(bracket + 1).join("");
78
- target.outputPositions?.replace(bracket, buffer.length, [unwrapped], (offset) => offset > opener.length - 1 ? offset - 1 : offset);
82
+ if (!(selfLinkHeadings && trimUrl(href).charCodeAt(0) === 35 && isInsideHeading(depthMap)) && !(redundantLinks && spells(buffer, bracket + 1, textLength, resolveUrl(href, target.options?.origin, rules)))) return false;
83
+ const prefix = opener.slice(0, -1);
84
+ const text = buffer.slice(bracket + 1).join("");
85
+ const reference = closingCloser ? closerReference(prefix || buffer[bracket - 1] || "", text.charCodeAt(0), closingCloser) : "";
86
+ const unwrapped = prefix + (reference ? reference + text.slice(1) : text);
87
+ target.outputPositions?.replace(bracket, buffer.length, [unwrapped], (offset) => (offset > prefix.length ? offset - 1 : offset) + (reference && offset > opener.length ? reference.length - 1 : 0));
79
88
  buffer.length = bracket;
80
89
  buffer.push(unwrapped);
81
90
  target.lastContentCache = unwrapped;
82
91
  closing = -1;
83
92
  return true;
84
93
  },
85
- closed(element, outputStart, close) {
94
+ closed(element, outputStart, close, closer = 0) {
86
95
  const bracket = closing;
87
96
  closing = -1;
88
97
  const code = close.charCodeAt(3);
@@ -101,13 +110,14 @@ function startPass(rules, target) {
101
110
  if (target.lastContentCache === close) target.lastContentCache = marked;
102
111
  spans.push(`﷐[${buffer.slice(bracket + 1, index).join("")}${marked}`);
103
112
  spanFragments.push(close.slice(3, destinationEnd(close, 3)));
113
+ spanClosers.push(closingCloser << 8 | closer);
104
114
  },
105
115
  held() {
106
116
  for (let index = 0; index < brackets.length; index++) if (brackets[index] >= 0) return brackets[index];
107
117
  return Infinity;
108
118
  },
109
119
  finish(markdown) {
110
- return fragments ? applyFragments(markdown, headings, spans, spanFragments) : { markdown };
120
+ return fragments ? applyFragments(markdown, headings, spans, spanFragments, spanClosers) : { markdown };
111
121
  }
112
122
  };
113
123
  }
@@ -176,7 +186,57 @@ function slugify(text) {
176
186
  if (lastWasDash) slug = slug.slice(0, -1);
177
187
  return slug;
178
188
  }
189
+ function closerReferenceAt(text, at, end) {
190
+ const before = text.charCodeAt(at - 1);
191
+ if (before !== 42 && before !== 126 || isEscaped(text, at - 1) || text.charCodeAt(at + 1) !== 35) return 0;
192
+ let code = text.charCodeAt(at + 2);
193
+ if (!(code >= 49 && code <= 57)) return 0;
194
+ let value = 0;
195
+ let index = at + 2;
196
+ while (index < at + 5 && index < end && code >= 48 && code <= 57) {
197
+ value = value * 10 + code - 48;
198
+ code = text.charCodeAt(++index);
199
+ }
200
+ if (index >= end || code !== 59) return 0;
201
+ const folded = value | 32;
202
+ return value >= 48 && value <= 57 || folded >= 97 && folded <= 122 ? value : 0;
203
+ }
204
+ function linkTextWithCloserReferences(text, start, end) {
205
+ let result = "";
206
+ let copied = start;
207
+ let codeTicks = 0;
208
+ let index = start;
209
+ while (index < end) {
210
+ const code = text.charCodeAt(index);
211
+ if (code === 92 && codeTicks === 0) {
212
+ index += 2;
213
+ continue;
214
+ }
215
+ if (code === 96) {
216
+ const runStart = index;
217
+ while (index < end && text.charCodeAt(index) === 96) index++;
218
+ const ticks = index - runStart;
219
+ if (codeTicks === 0) codeTicks = ticks;
220
+ else if (codeTicks === ticks) codeTicks = 0;
221
+ continue;
222
+ }
223
+ if (code === 38 && codeTicks === 0) {
224
+ const decoded = closerReferenceAt(text, index, end);
225
+ if (decoded) {
226
+ result += text.slice(copied, index) + String.fromCharCode(decoded);
227
+ index = text.indexOf(";", index) + 1;
228
+ copied = index;
229
+ continue;
230
+ }
231
+ }
232
+ index++;
233
+ }
234
+ return result + text.slice(copied, end);
235
+ }
179
236
  function stripHeadingFormatting(text) {
237
+ return stripFormatting(text, false);
238
+ }
239
+ function stripFormatting(text, closerReferences) {
180
240
  let result = "";
181
241
  const len = text.length;
182
242
  const lastGt = text.lastIndexOf(">");
@@ -242,7 +302,7 @@ function stripHeadingFormatting(text) {
242
302
  if (close !== -1 && text.charCodeAt(close + 1) === 40) {
243
303
  const parenClose = text.indexOf(")", close + 2);
244
304
  if (parenClose !== -1) {
245
- result += text.slice(i + 1, close);
305
+ result += closerReferences ? linkTextWithCloserReferences(text, i + 1, close) : text.slice(i + 1, close);
246
306
  i = parenClose + 1;
247
307
  continue;
248
308
  }
@@ -254,6 +314,14 @@ function stripHeadingFormatting(text) {
254
314
  i++;
255
315
  continue;
256
316
  }
317
+ if (c === 38 && closerReferences) {
318
+ const decoded = closerReferenceAt(text, i, len);
319
+ if (decoded) {
320
+ result += String.fromCharCode(decoded);
321
+ i = text.indexOf(";", i) + 1;
322
+ continue;
323
+ }
324
+ }
257
325
  result += text[i];
258
326
  i++;
259
327
  }
@@ -261,7 +329,7 @@ function stripHeadingFormatting(text) {
261
329
  }
262
330
  function headingSlug(text) {
263
331
  if (text.includes("﷐") || text.includes("﷑")) text = text.replaceAll("﷐", "").replaceAll("﷑", "");
264
- return slugify(stripHeadingFormatting(text));
332
+ return slugify(stripFormatting(text, true));
265
333
  }
266
334
  const OPEN_CODE = "﷐".charCodeAt(0);
267
335
  const CLOSE_CODE = "﷑".charCodeAt(0);
@@ -434,7 +502,7 @@ function stripQuotePrefixes(value) {
434
502
  }
435
503
  return copied ? result + value.slice(copied) : value;
436
504
  }
437
- function applyFragments(markdown, headings, spans, spanFragments) {
505
+ function applyFragments(markdown, headings, spans, spanFragments, spanClosers) {
438
506
  const count = spans.length;
439
507
  if (count === 0) return {
440
508
  markdown,
@@ -456,18 +524,26 @@ function applyFragments(markdown, headings, spans, spanFragments) {
456
524
  closes.fill(-1);
457
525
  pairSpelledMarkers(markdown, spans, opens, closes);
458
526
  }
459
- const dropAt = /* @__PURE__ */ new Map();
527
+ const edits = /* @__PURE__ */ new Map();
528
+ let closerAt;
460
529
  for (let span = 0; span < count; span++) {
461
530
  const open = opens[span];
462
531
  if (open === -1) continue;
463
532
  const close = closes[span];
464
533
  const closeEnd = linkCloseEnd(markdown, close + 1);
465
534
  const broken = closeEnd !== -1 && !slugs.has(spanFragments[span]);
466
- dropAt.set(open, broken ? 2 : 1);
467
- dropAt.set(close, broken ? closeEnd - close : 1);
535
+ edits.set(open, broken ? 2 : 1);
536
+ edits.set(close, broken ? closeEnd - close : 1);
537
+ const closers = broken ? spanClosers[span] : 0;
538
+ if (closers) {
539
+ closerAt ??= /* @__PURE__ */ new Map();
540
+ if (closers >> 8) closerAt.set(open, closers >> 8);
541
+ if (closers & 255) closerAt.set(close, closers & 255);
542
+ }
468
543
  }
469
544
  let result = "";
470
545
  let copied = 0;
546
+ let closer = 0;
471
547
  let nextOpen = markdown.indexOf("﷐");
472
548
  let nextClose = markdown.indexOf("﷑");
473
549
  while (nextOpen !== -1 || nextClose !== -1) {
@@ -479,31 +555,51 @@ function applyFragments(markdown, headings, spans, spanFragments) {
479
555
  next = nextClose;
480
556
  nextClose = markdown.indexOf("﷑", next + 1);
481
557
  }
482
- const drop = dropAt.get(next);
558
+ const drop = edits.get(next);
483
559
  if (drop === void 0 || next < copied) continue;
560
+ if (closer && next > copied) {
561
+ const reference = closerReference(result, markdown.charCodeAt(copied), closer);
562
+ if (reference) {
563
+ edits.set(copied, 1 - reference.length);
564
+ result += reference;
565
+ copied++;
566
+ }
567
+ closer = 0;
568
+ }
484
569
  result += markdown.slice(copied, next);
485
570
  copied = next + drop;
571
+ closer = closerAt?.get(next) || closer;
572
+ }
573
+ const reference = closer && copied < markdown.length ? closerReference(result, markdown.charCodeAt(copied), closer) : "";
574
+ if (reference) {
575
+ edits.set(copied, 1 - reference.length);
576
+ result += reference;
577
+ copied++;
486
578
  }
487
579
  result += markdown.slice(copied);
488
580
  let positionMap;
489
581
  return {
490
582
  markdown: result,
491
583
  mapPosition(position) {
492
- positionMap ??= createPositionMap(dropAt);
584
+ positionMap ??= createPositionMap(edits);
493
585
  return positionMap(position);
494
586
  }
495
587
  };
496
588
  }
497
- function createPositionMap(drops) {
589
+ function closerReference(result, code, closer) {
590
+ const folded = code | 32;
591
+ return result.charCodeAt(result.length - 1) === closer && (code >= 48 && code <= 57 || folded >= 97 && folded <= 122) ? `&#${code};` : "";
592
+ }
593
+ function createPositionMap(edits) {
498
594
  const starts = [];
499
595
  const ends = [];
500
596
  const totals = [];
501
597
  let removed = 0;
502
598
  let copied = 0;
503
- for (const start of [...drops.keys()].sort((a, b) => a - b)) {
599
+ for (const start of [...edits.keys()].sort((a, b) => a - b)) {
504
600
  if (start < copied) continue;
505
- const length = drops.get(start);
506
- copied = start + length;
601
+ const length = edits.get(start);
602
+ copied = start + (length < 0 ? 1 : length);
507
603
  removed += length;
508
604
  starts.push(start);
509
605
  ends.push(copied);
@@ -1,7 +1,6 @@
1
1
  import { TagIdMap } from "./const.mjs";
2
- import { escapeHtml, getLanguageFromAttributes, hasNonWhitespace, isSafeHtmlUrl, markRenderedChildContent, parseUnsignedInteger, resolveUrl, safeAnchorOutput, suppressesFormattingInPre } from "./url.mjs";
2
+ import { escapeHtml, getLanguageFromAttributes, hasNonWhitespace, isSafeHtmlUrl, markRenderedChildContent, parseUnsignedInteger, parserTagHandlers, resolveUrl, safeAnchorOutput, suppressesFormattingInPre } from "./url.mjs";
3
3
  import { assertEngineOptions, buildTagOverrideHandlers, checkClean } from "./tag-overrides.mjs";
4
- import { parserTagHandlers } from "./parse.mjs";
5
4
  import { resolvePlugins } from "./plugin.mjs";
6
5
  import { processHtmlOutput, streamHtmlOutput } from "./output-runner.mjs";
7
6
  import { slugify, stripHeadingFormatting } from "./clean.mjs";