@mdream/js 2.0.3 → 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,6 +1,5 @@
1
1
  import "./const.mjs";
2
- import { isInsideHeading } from "./utils.mjs";
3
- import { resolveUrl } from "./url.mjs";
2
+ import { isInsideHeading, resolveUrl, trimUrl } from "./url.mjs";
4
3
  const ALL_RULES = {
5
4
  urls: true,
6
5
  fragments: true,
@@ -36,19 +35,23 @@ function startPass(rules, target) {
36
35
  const depthMap = target.depthMap;
37
36
  const links = [];
38
37
  const brackets = [];
38
+ let bracketClosers;
39
39
  const spans = [];
40
40
  const spanFragments = [];
41
+ const spanClosers = [];
41
42
  let closing = -1;
43
+ let closingCloser = 0;
42
44
  const headings = [];
43
45
  let headingStart = -1;
44
46
  return {
45
47
  holdsOutput: fragments,
46
- enter(element, outputStart) {
48
+ enter(element, outputStart, closer = 0) {
47
49
  closing = -1;
48
50
  const tagId = element.tagId;
49
51
  if (tagId === 26) {
50
52
  links.push(element);
51
53
  brackets.push(outputStart < buffer.length && buffer[buffer.length - 1] === "[" ? buffer.length - 1 : -1);
54
+ if (closer) (bracketClosers ??= [])[links.length - 1] = closer;
52
55
  } else if (fragments && headingStart < 0 && tagId >= 7 && tagId <= 12 && !depthMap[26]) headingStart = buffer.length;
53
56
  },
54
57
  exit(element) {
@@ -57,8 +60,10 @@ function startPass(rules, target) {
57
60
  if (tagId === 26) {
58
61
  for (let index = links.length - 1; index >= 0; index--) if (links[index] === element) {
59
62
  closing = brackets[index];
63
+ closingCloser = bracketClosers?.[index] ?? 0;
60
64
  links.length = index;
61
65
  brackets.length = index;
66
+ if (bracketClosers && bracketClosers.length > index) bracketClosers.length = index;
62
67
  break;
63
68
  }
64
69
  } else if (headingStart >= 0 && tagId >= 7 && tagId <= 12 && !depthMap[26]) {
@@ -74,16 +79,19 @@ function startPass(rules, target) {
74
79
  let textLength = 0;
75
80
  for (let index = bracket + 1; index < buffer.length; index++) textLength += buffer[index].length;
76
81
  if (textLength === 0) return false;
77
- if (!(selfLinkHeadings && href.charCodeAt(0) === 35 && isInsideHeading(depthMap)) && !(redundantLinks && spells(buffer, bracket + 1, textLength, resolveUrl(href, target.options?.origin, rules)))) return false;
78
- const unwrapped = opener.slice(0, -1) + buffer.slice(bracket + 1).join("");
79
- 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));
80
88
  buffer.length = bracket;
81
89
  buffer.push(unwrapped);
82
90
  target.lastContentCache = unwrapped;
83
91
  closing = -1;
84
92
  return true;
85
93
  },
86
- closed(element, outputStart, close) {
94
+ closed(element, outputStart, close, closer = 0) {
87
95
  const bracket = closing;
88
96
  closing = -1;
89
97
  const code = close.charCodeAt(3);
@@ -102,13 +110,14 @@ function startPass(rules, target) {
102
110
  if (target.lastContentCache === close) target.lastContentCache = marked;
103
111
  spans.push(`﷐[${buffer.slice(bracket + 1, index).join("")}${marked}`);
104
112
  spanFragments.push(close.slice(3, destinationEnd(close, 3)));
113
+ spanClosers.push(closingCloser << 8 | closer);
105
114
  },
106
115
  held() {
107
116
  for (let index = 0; index < brackets.length; index++) if (brackets[index] >= 0) return brackets[index];
108
117
  return Infinity;
109
118
  },
110
119
  finish(markdown) {
111
- return fragments ? applyFragments(markdown, headings, spans, spanFragments) : { markdown };
120
+ return fragments ? applyFragments(markdown, headings, spans, spanFragments, spanClosers) : { markdown };
112
121
  }
113
122
  };
114
123
  }
@@ -177,7 +186,57 @@ function slugify(text) {
177
186
  if (lastWasDash) slug = slug.slice(0, -1);
178
187
  return slug;
179
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
+ }
180
236
  function stripHeadingFormatting(text) {
237
+ return stripFormatting(text, false);
238
+ }
239
+ function stripFormatting(text, closerReferences) {
181
240
  let result = "";
182
241
  const len = text.length;
183
242
  const lastGt = text.lastIndexOf(">");
@@ -243,7 +302,7 @@ function stripHeadingFormatting(text) {
243
302
  if (close !== -1 && text.charCodeAt(close + 1) === 40) {
244
303
  const parenClose = text.indexOf(")", close + 2);
245
304
  if (parenClose !== -1) {
246
- result += text.slice(i + 1, close);
305
+ result += closerReferences ? linkTextWithCloserReferences(text, i + 1, close) : text.slice(i + 1, close);
247
306
  i = parenClose + 1;
248
307
  continue;
249
308
  }
@@ -255,6 +314,14 @@ function stripHeadingFormatting(text) {
255
314
  i++;
256
315
  continue;
257
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
+ }
258
325
  result += text[i];
259
326
  i++;
260
327
  }
@@ -262,7 +329,7 @@ function stripHeadingFormatting(text) {
262
329
  }
263
330
  function headingSlug(text) {
264
331
  if (text.includes("﷐") || text.includes("﷑")) text = text.replaceAll("﷐", "").replaceAll("﷑", "");
265
- return slugify(stripHeadingFormatting(text));
332
+ return slugify(stripFormatting(text, true));
266
333
  }
267
334
  const OPEN_CODE = "﷐".charCodeAt(0);
268
335
  const CLOSE_CODE = "﷑".charCodeAt(0);
@@ -435,7 +502,7 @@ function stripQuotePrefixes(value) {
435
502
  }
436
503
  return copied ? result + value.slice(copied) : value;
437
504
  }
438
- function applyFragments(markdown, headings, spans, spanFragments) {
505
+ function applyFragments(markdown, headings, spans, spanFragments, spanClosers) {
439
506
  const count = spans.length;
440
507
  if (count === 0) return {
441
508
  markdown,
@@ -457,18 +524,26 @@ function applyFragments(markdown, headings, spans, spanFragments) {
457
524
  closes.fill(-1);
458
525
  pairSpelledMarkers(markdown, spans, opens, closes);
459
526
  }
460
- const dropAt = /* @__PURE__ */ new Map();
527
+ const edits = /* @__PURE__ */ new Map();
528
+ let closerAt;
461
529
  for (let span = 0; span < count; span++) {
462
530
  const open = opens[span];
463
531
  if (open === -1) continue;
464
532
  const close = closes[span];
465
533
  const closeEnd = linkCloseEnd(markdown, close + 1);
466
534
  const broken = closeEnd !== -1 && !slugs.has(spanFragments[span]);
467
- dropAt.set(open, broken ? 2 : 1);
468
- 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
+ }
469
543
  }
470
544
  let result = "";
471
545
  let copied = 0;
546
+ let closer = 0;
472
547
  let nextOpen = markdown.indexOf("﷐");
473
548
  let nextClose = markdown.indexOf("﷑");
474
549
  while (nextOpen !== -1 || nextClose !== -1) {
@@ -480,31 +555,51 @@ function applyFragments(markdown, headings, spans, spanFragments) {
480
555
  next = nextClose;
481
556
  nextClose = markdown.indexOf("﷑", next + 1);
482
557
  }
483
- const drop = dropAt.get(next);
558
+ const drop = edits.get(next);
484
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
+ }
485
569
  result += markdown.slice(copied, next);
486
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++;
487
578
  }
488
579
  result += markdown.slice(copied);
489
580
  let positionMap;
490
581
  return {
491
582
  markdown: result,
492
583
  mapPosition(position) {
493
- positionMap ??= createPositionMap(dropAt);
584
+ positionMap ??= createPositionMap(edits);
494
585
  return positionMap(position);
495
586
  }
496
587
  };
497
588
  }
498
- 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) {
499
594
  const starts = [];
500
595
  const ends = [];
501
596
  const totals = [];
502
597
  let removed = 0;
503
598
  let copied = 0;
504
- for (const start of [...drops.keys()].sort((a, b) => a - b)) {
599
+ for (const start of [...edits.keys()].sort((a, b) => a - b)) {
505
600
  if (start < copied) continue;
506
- const length = drops.get(start);
507
- copied = start + length;
601
+ const length = edits.get(start);
602
+ copied = start + (length < 0 ? 1 : length);
508
603
  removed += length;
509
604
  starts.push(start);
510
605
  ends.push(copied);
@@ -11,4 +11,5 @@ export interface ExtractedElement extends ElementNode {
11
11
  }
12
12
  type ExtractionCallback = (element: ExtractedElement, state: PluginState) => void;
13
13
  /** Extract matching elements through the composable JavaScript plugin interface. */
14
- export declare function extractionPlugin(selectors: Record<string, ExtractionCallback>): PluginSetup;
14
+ export declare function extractionPlugin(selectors: Record<string, ExtractionCallback>): PluginSetup;
15
+ export {};
@@ -1,8 +1,6 @@
1
1
  import { TagIdMap } from "./const.mjs";
2
- import { escapeHtml, getLanguageFromClass, isSafeHtmlUrl, markRenderedChildContent, parseUnsignedInteger } from "./utils.mjs";
3
- import { resolveUrl, safeAnchorOutput } from "./url.mjs";
2
+ import { escapeHtml, getLanguageFromAttributes, hasNonWhitespace, isSafeHtmlUrl, markRenderedChildContent, parseUnsignedInteger, parserTagHandlers, resolveUrl, safeAnchorOutput, suppressesFormattingInPre } from "./url.mjs";
4
3
  import { assertEngineOptions, buildTagOverrideHandlers, checkClean } from "./tag-overrides.mjs";
5
- import { parserTagHandlers } from "./parse.mjs";
6
4
  import { resolvePlugins } from "./plugin.mjs";
7
5
  import { processHtmlOutput, streamHtmlOutput } from "./output-runner.mjs";
8
6
  import { slugify, stripHeadingFormatting } from "./clean.mjs";
@@ -44,7 +42,7 @@ function safeAttributes(node, tagId) {
44
42
  return start === void 0 ? "" : ` start="${start}"`;
45
43
  }
46
44
  if (tagId === 23) {
47
- const language = getLanguageFromClass(attributes.class);
45
+ const language = getLanguageFromAttributes(attributes);
48
46
  return language ? ` class="language-${escapeHtml(language, true)}"` : "";
49
47
  }
50
48
  if (tagId === 31 || tagId === 32) {
@@ -83,6 +81,8 @@ function processText(node, state, output) {
83
81
  if (!value) return;
84
82
  }
85
83
  }
84
+ const top = state.frames.at(-1);
85
+ if (top?._tag === 1 && top.languagePending && hasNonWhitespace(value)) top.languagePending = false;
86
86
  for (let index = state.frames.length - 1; index >= 0; index--) {
87
87
  const frame = state.frames[index];
88
88
  if (frame._tag === 0) {
@@ -113,9 +113,17 @@ function processHtmlOutputEvent(event, state, output, options) {
113
113
  const tagId = resolveTagId(element);
114
114
  const frame = state.frames.at(-1);
115
115
  if (frame?._tag === 1) {
116
- if (type === 0 && tagId === 23 && !frame.language) frame.language = getLanguageFromClass(element.attributes.class);
117
- else if (type === 0 && tagId === 6) frame.output += "\n";
118
- else if (type !== 0 && tagId === 34 && element.depth === frame.depth) closeFrame(state, output, frame);
116
+ if (type === 0) {
117
+ if (frame.languagePending && tagId !== 34 && !suppressesFormattingInPre(tagId)) {
118
+ frame.languagePending = false;
119
+ const parent = element.parent;
120
+ if (tagId === 23 && parent?.tagId === 34 && parent.depth === frame.depth) {
121
+ const codeLanguage = getLanguageFromAttributes(element.attributes);
122
+ if (codeLanguage) frame.language = codeLanguage;
123
+ }
124
+ }
125
+ if (tagId === 6) frame.output += "\n";
126
+ } else if (tagId === 34 && element.depth === frame.depth) closeFrame(state, output, frame);
119
127
  return;
120
128
  }
121
129
  if (type === 0 && tagId !== void 0 && tagId >= 7 && tagId <= 12) {
@@ -135,7 +143,8 @@ function processHtmlOutputEvent(event, state, output, options) {
135
143
  state.frames.push({
136
144
  _tag: 1,
137
145
  depth: element.depth,
138
- language: getLanguageFromClass(element.attributes.class),
146
+ language: getLanguageFromAttributes(element.attributes),
147
+ languagePending: true,
139
148
  output: ""
140
149
  });
141
150
  return;
@@ -231,7 +240,7 @@ function createProcessor(options) {
231
240
  processEvent(event) {
232
241
  state.depth = event.node.depth;
233
242
  if (event.node.type === 1 ? event.node.excludedFromMarkdown : event.node.parent?.excludedFromMarkdown) return;
234
- processHtmlOutputEvent(event, outputState, state.buffer, options);
243
+ processHtmlOutputEvent(event, outputState, state.buffer, state.options);
235
244
  },
236
245
  takeOutput() {
237
246
  const output = state.buffer.join("");