@mdream/js 1.5.4 → 1.5.6

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.
@@ -253,7 +253,20 @@ const HTML_ENTITIES = {
253
253
  yuml: "ÿ",
254
254
  $zeta: "ζ"
255
255
  };
256
- function continuationPrefix(node, listIndentWidths) {
256
+ function equalsAsciiCaseInsensitive(value, expected) {
257
+ for (let index = 0; index < expected.length; index++) if ((value.charCodeAt(index) | 32) !== expected.charCodeAt(index)) return false;
258
+ return true;
259
+ }
260
+ function isEmptyLinkHref(href) {
261
+ if (href === "#") return true;
262
+ switch (href.indexOf(":")) {
263
+ case 4: return equalsAsciiCaseInsensitive(href, "data");
264
+ case 8: return equalsAsciiCaseInsensitive(href, "vbscript");
265
+ case 10: return equalsAsciiCaseInsensitive(href, "javascript");
266
+ default: return false;
267
+ }
268
+ }
269
+ function continuationPrefix(node, listIndentWidths, includeBlockquotes = true) {
257
270
  const chain = [];
258
271
  let current = node.parent;
259
272
  while (current) {
@@ -264,7 +277,7 @@ function continuationPrefix(node, listIndentWidths) {
264
277
  let listIndex = 0;
265
278
  for (let i = chain.length - 1; i >= 0; i--) {
266
279
  const tagId = chain[i].tagId;
267
- if (tagId === 22) prefix += "> ";
280
+ if (tagId === 22 && includeBlockquotes) prefix += "> ";
268
281
  else if (tagId === 25) {
269
282
  prefix += " ".repeat(listIndentWidths[listIndex] ?? 2);
270
283
  listIndex++;
@@ -281,7 +294,28 @@ function numericReplacement(codePoint) {
281
294
  if (codePoint >= 128 && codePoint <= 159) return C1_REPLACEMENTS[codePoint - 128];
282
295
  return String.fromCodePoint(codePoint);
283
296
  }
284
- function decodeHTMLEntities(text, inAttribute = false) {
297
+ function isCharacterReferenceTail(text, start) {
298
+ let index = start;
299
+ if (text.charCodeAt(index) === 35) {
300
+ index++;
301
+ const hex = text.charCodeAt(index) === 120 || text.charCodeAt(index) === 88;
302
+ if (hex) index++;
303
+ const digitStart = index;
304
+ while (index < text.length) {
305
+ const code = text.charCodeAt(index);
306
+ if (!(code >= 48 && code <= 57) && !(hex && (code >= 65 && code <= 70 || code >= 97 && code <= 102))) break;
307
+ index++;
308
+ }
309
+ return index > digitStart && text.charCodeAt(index) === 59;
310
+ }
311
+ const nameStart = index;
312
+ while (index < text.length && isAsciiAlphaNumeric(text.charCodeAt(index))) index++;
313
+ return index > nameStart && text.charCodeAt(index) === 59;
314
+ }
315
+ function decodedReference(replacement, text, next, protectDecodedEntityReferences) {
316
+ return replacement === "&" && protectDecodedEntityReferences && isCharacterReferenceTail(text, next) ? "\\&" : replacement;
317
+ }
318
+ function decodeHTMLEntities(text, inAttribute = false, protectDecodedEntityReferences = false) {
285
319
  let result = "";
286
320
  let i = 0;
287
321
  const len = text.length;
@@ -309,7 +343,7 @@ function decodeHTMLEntities(text, inAttribute = false) {
309
343
  end++;
310
344
  }
311
345
  if (end > digitStart) {
312
- result += numericReplacement(codePoint);
346
+ result += decodedReference(numericReplacement(codePoint), text, text.charCodeAt(end) === 59 ? end + 1 : end, protectDecodedEntityReferences);
313
347
  if (text.charCodeAt(end) === 59) end++;
314
348
  i = end;
315
349
  continue;
@@ -324,7 +358,7 @@ function decodeHTMLEntities(text, inAttribute = false) {
324
358
  const direct = HTML_ENTITIES[name];
325
359
  const replacement = typeof direct === "string" ? direct : HTML_ENTITIES[`$${name}`];
326
360
  if (replacement !== void 0) {
327
- result += replacement;
361
+ result += decodedReference(replacement, text, nameEnd + 1, protectDecodedEntityReferences);
328
362
  i = nameEnd + 1;
329
363
  continue;
330
364
  }
@@ -336,7 +370,7 @@ function decodeHTMLEntities(text, inAttribute = false) {
336
370
  if (typeof replacement === "string") {
337
371
  const next = text.charCodeAt(legacyEnd);
338
372
  if (replacement !== void 0 && !(inAttribute && (next === 61 || isAsciiAlphaNumeric(next)))) {
339
- result += replacement;
373
+ result += decodedReference(replacement, text, legacyEnd, protectDecodedEntityReferences);
340
374
  i = legacyEnd;
341
375
  decodedLegacy = true;
342
376
  }
@@ -360,16 +394,45 @@ function traverseUpToFirstBlockNode(node) {
360
394
  }
361
395
  return parentsToIncrement;
362
396
  }
363
- function resolveUrl(url, origin) {
364
- if (!url) return url;
365
- if (url.startsWith("//")) return `https:${url}`;
366
- if (url.startsWith("#")) return url;
367
- if (origin) {
368
- if (url.startsWith("/") && origin) return `${origin.endsWith("/") ? origin.slice(0, -1) : origin}${url}`;
369
- if (url.startsWith("./")) return `${origin}/${url.slice(2)}`;
370
- if (!url.startsWith("http")) return `${origin}/${url.startsWith("/") ? url.slice(1) : url}`;
397
+ const TRACKING_PARAM_RE = /^(?:utm_|fbclid|gclid|mc_eid|msclkid|oly_)/;
398
+ const URL_SCHEME_RE = /^[\dA-Z+.-]+:/i;
399
+ function stripTrackingParams(url) {
400
+ const queryStart = url.indexOf("?");
401
+ const fragmentStart = url.indexOf("#");
402
+ if (queryStart === -1 || fragmentStart !== -1 && fragmentStart < queryStart) return url;
403
+ const queryEnd = fragmentStart === -1 ? url.length : fragmentStart;
404
+ const query = url.slice(queryStart + 1, queryEnd).split("&").filter((parameter) => !TRACKING_PARAM_RE.test(parameter)).join("&");
405
+ return `${url.slice(0, queryStart)}${query ? `?${query}` : ""}${url.slice(queryEnd)}`;
406
+ }
407
+ function resolveUrl(url, origin, clean) {
408
+ if (!url || url[0] === "#") return url;
409
+ let resolved = url;
410
+ if (url.startsWith("//")) resolved = `https:${url}`;
411
+ else if (origin && !URL_SCHEME_RE.test(url)) {
412
+ const path = url.startsWith("./") ? url.slice(2) : url;
413
+ while (origin.endsWith("/")) origin = origin.slice(0, -1);
414
+ resolved = `${origin}${path[0] === "/" ? "" : "/"}${path}`;
371
415
  }
372
- return url;
416
+ return (clean === true || !!clean && clean.urls === true) && resolved.includes("?") ? stripTrackingParams(resolved) : resolved;
417
+ }
418
+ function serializeMarkdownDestination(destination) {
419
+ if (!/[\t\n\f\r ()\\<>]/.test(destination)) return destination;
420
+ return `<${/[\\<>]/.test(destination) ? destination.replace(/[\\<>]/g, "\\$&") : destination}>`;
421
+ }
422
+ function stripsEmptyLink(state, href) {
423
+ const clean = state.options?.clean;
424
+ if (!(clean === true || typeof clean === "object" && clean.emptyLinks)) return false;
425
+ return isEmptyLinkHref(href);
426
+ }
427
+ function serializeMarkdownTitle(title) {
428
+ return /[\\"]/.test(title) ? title.replace(/[\\"]/g, "\\$&") : title;
429
+ }
430
+ function serializeMarkdownResource(destination, title) {
431
+ const serializedTitle = title ? ` "${serializeMarkdownTitle(title)}"` : "";
432
+ return `(${serializeMarkdownDestination(destination)}${serializedTitle})`;
433
+ }
434
+ function serializeImageDescription(alt) {
435
+ return /[\\[\]*_`~<&]/.test(alt) ? alt.replace(/[\\[\]*_`~<&]/g, "\\$&") : alt;
373
436
  }
374
437
  function isAutolinkUri(s) {
375
438
  if (!(s.startsWith("http://") || s.startsWith("https://") || s.startsWith("ftp://") || s.startsWith("mailto:"))) return false;
@@ -387,6 +450,19 @@ function isInsideRawHtmlBlock(state) {
387
450
  const depthMap = state.depthMap;
388
451
  return Boolean(depthMap[2] || depthMap[3] || depthMap[100] || depthMap[101] || depthMap[99] || depthMap[98]);
389
452
  }
453
+ function renderBreak(node, state) {
454
+ const depthMap = state.depthMap;
455
+ if (isInsideTableCell(state) || isInsideRawHtmlBlock(state) || depthMap[7] || depthMap[8] || depthMap[9] || depthMap[10] || depthMap[11] || depthMap[12]) return "<br>";
456
+ if (depthMap[34] || depthMap[23]) return "\n";
457
+ return `\\\n${continuationPrefix(node, state.listIndentWidths || [], !state.bufferedBlockquoteDepth)}`;
458
+ }
459
+ const breakHandler = {
460
+ enter: ({ node, state }) => renderBreak(node, state),
461
+ isSelfClosing: true,
462
+ spacing: NO_SPACING,
463
+ collapsesInnerWhiteSpace: true,
464
+ isInline: true
465
+ };
390
466
  function getLanguageFromClass(className) {
391
467
  if (!className) return "";
392
468
  const langParts = className.split(" ").map((c) => c.split("language-")[1]).filter(Boolean);
@@ -420,16 +496,23 @@ const Strong = {
420
496
  const Emphasis = {
421
497
  enter: ({ state }) => {
422
498
  if ((state.depthMap?.[17] || 0) > 1) return "";
423
- return "_";
499
+ return "*";
424
500
  },
425
501
  exit: ({ node, state }) => {
426
502
  if ((state.depthMap?.[17] || 0) + (node.tagId === 17 ? 1 : 0) > 1) return "";
427
- return "_";
503
+ return "*";
428
504
  },
429
505
  collapsesInnerWhiteSpace: true,
430
506
  spacing: NO_SPACING,
431
507
  isInline: true
432
508
  };
509
+ const Strikethrough = {
510
+ enter: () => "~~",
511
+ exit: () => "~~",
512
+ collapsesInnerWhiteSpace: true,
513
+ spacing: NO_SPACING,
514
+ isInline: true
515
+ };
433
516
  const tagHandlers = {
434
517
  [1]: {
435
518
  spacing: NO_SPACING,
@@ -461,17 +544,7 @@ const tagHandlers = {
461
544
  isSelfClosing: true,
462
545
  spacing: NO_SPACING
463
546
  },
464
- [6]: {
465
- enter: ({ node, state }) => {
466
- const depthMap = state.depthMap;
467
- if (isInsideTableCell(state) || isInsideRawHtmlBlock(state) || depthMap[7] || depthMap[8] || depthMap[9] || depthMap[10] || depthMap[11] || depthMap[12]) return "<br>";
468
- return `\n${continuationPrefix(node, state.listIndentWidths || [])}`;
469
- },
470
- isSelfClosing: true,
471
- spacing: NO_SPACING,
472
- collapsesInnerWhiteSpace: true,
473
- isInline: true
474
- },
547
+ [6]: breakHandler,
475
548
  [7]: handleHeading(1),
476
549
  [8]: handleHeading(2),
477
550
  [9]: handleHeading(3),
@@ -486,13 +559,9 @@ const tagHandlers = {
486
559
  [15]: Strong,
487
560
  [16]: Emphasis,
488
561
  [17]: Emphasis,
489
- [18]: {
490
- enter: () => "~~",
491
- exit: () => "~~",
492
- collapsesInnerWhiteSpace: true,
493
- spacing: NO_SPACING,
494
- isInline: true
495
- },
562
+ [18]: Strikethrough,
563
+ [110]: Strikethrough,
564
+ [111]: Strikethrough,
496
565
  [19]: {
497
566
  enter: () => "<sub>",
498
567
  exit: () => "</sub>",
@@ -516,31 +585,25 @@ const tagHandlers = {
516
585
  },
517
586
  [22]: {
518
587
  enter: ({ state }) => {
519
- const depth = state.depthMap?.[22] || 1;
520
- let prefix = "> ".repeat(depth);
521
- if ((state.depthMap?.[25] || 0) > 0) prefix = `\n${state.listIndent}${prefix}`;
522
- return prefix;
588
+ return {
589
+ _tag: "BlockquoteEnter",
590
+ output: (state.depthMap?.[25] || 0) > 0 ? "\n" : void 0
591
+ };
523
592
  },
593
+ exit: () => ({ _tag: "BlockquoteExit" }),
524
594
  spacing: BLOCKQUOTE_SPACING
525
595
  },
526
596
  [34]: {
527
597
  enter: ({ node, state }) => {
528
598
  if (isInsideTableCell(state)) return "<pre>";
529
- state.preFencePending = true;
530
- state.preOwnFence = false;
531
- state.preFenceLang = getLanguageFromClass(node.attributes?.class);
599
+ return {
600
+ _tag: "PreEnter",
601
+ language: getLanguageFromClass(node.attributes?.class)
602
+ };
532
603
  },
533
604
  exit: ({ state }) => {
534
605
  if (isInsideTableCell(state)) return "</pre>";
535
- const ownFence = state.preOwnFence;
536
- state.preFencePending = false;
537
- state.preOwnFence = false;
538
- if (!ownFence) return;
539
- if ((state.depthMap?.[25] || 0) > 0) {
540
- const indent = state.listIndent;
541
- return `\n${indent}\`\`\`\n\n${indent}`;
542
- }
543
- return `\n\`\`\``;
606
+ return { _tag: "PreExit" };
544
607
  }
545
608
  },
546
609
  [23]: {
@@ -548,17 +611,30 @@ const tagHandlers = {
548
611
  if ((state.depthMap?.[34] || 0) > 0) {
549
612
  if (isInsideTableCell(state)) return "<code>";
550
613
  if (state.preOwnFence) return;
551
- state.preFencePending = false;
552
614
  const language = getLanguageFromClass(node.attributes?.class);
553
- if ((state.depthMap?.[25] || 0) > 0) return `\n\n${state.listIndent}\`\`\`${language}\n`;
554
- return `\`\`\`${language}\n`;
615
+ if ((state.depthMap?.[25] || 0) > 0) return {
616
+ _tag: "CodeFenceEnter",
617
+ language,
618
+ output: `\n\n${state.listIndent}\`\`\`${language}\n`
619
+ };
620
+ return {
621
+ _tag: "CodeFenceEnter",
622
+ language,
623
+ output: `\`\`\`${language}\n`
624
+ };
555
625
  }
556
626
  if ((state.depthMap?.[25] || 0) > 0) {
557
627
  const lastEntry = state.buffer.at(-1);
558
628
  const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
559
- if (lastChar && lastChar !== " " && lastChar !== "\n" && lastChar !== " " && lastChar !== "*" && lastChar !== "_" && lastChar !== "~" && lastChar !== "[" && lastChar !== ">") return ` \``;
629
+ if (lastChar && lastChar !== " " && lastChar !== "\n" && lastChar !== " " && lastChar !== "*" && lastChar !== "_" && lastChar !== "~" && lastChar !== "[" && lastChar !== ">") return {
630
+ _tag: "CodeSpanEnter",
631
+ output: ` \``
632
+ };
560
633
  }
561
- return "`";
634
+ return {
635
+ _tag: "CodeSpanEnter",
636
+ output: "`"
637
+ };
562
638
  },
563
639
  exit: ({ state }) => {
564
640
  if ((state.depthMap?.[34] || 0) > 0) {
@@ -566,11 +642,17 @@ const tagHandlers = {
566
642
  if (state.preOwnFence) return;
567
643
  if ((state.depthMap?.[25] || 0) > 0) {
568
644
  const indent = state.listIndent;
569
- return `\n${indent}\`\`\`\n\n${indent}`;
645
+ return {
646
+ _tag: "CodeFenceExit",
647
+ output: `\n${indent}\`\`\`\n\n${indent}`
648
+ };
570
649
  }
571
- return `\n\`\`\``;
650
+ return {
651
+ _tag: "CodeFenceExit",
652
+ output: `\n\`\`\``
653
+ };
572
654
  }
573
- return "`";
655
+ return { _tag: "CodeSpanExit" };
574
656
  },
575
657
  collapsesInnerWhiteSpace: true,
576
658
  spacing: NO_SPACING,
@@ -594,12 +676,16 @@ const tagHandlers = {
594
676
  spacing: LIST_ITEM_SPACING
595
677
  },
596
678
  [26]: {
597
- enter: ({ node }) => {
598
- if (node.attributes?.href) return "[";
679
+ enter: ({ node, state }) => {
680
+ if (node.attributes?.href !== void 0) {
681
+ if (stripsEmptyLink(state, node.attributes.href)) return;
682
+ return "[";
683
+ }
599
684
  },
600
685
  exit: ({ node, state }) => {
601
- if (!node.attributes?.href) return "";
602
- const href = resolveUrl(node.attributes?.href || "", state.options?.origin);
686
+ if (node.attributes?.href === void 0) return "";
687
+ if (stripsEmptyLink(state, node.attributes.href)) return "";
688
+ const href = resolveUrl(node.attributes.href, state.options?.origin, state.options?.clean);
603
689
  let title = node.attributes?.title;
604
690
  if (state.lastContentCache === title) title = "";
605
691
  if (!title && isAutolinkUri(href)) {
@@ -620,7 +706,7 @@ const tagHandlers = {
620
706
  return "";
621
707
  }
622
708
  }
623
- return title ? `](${href} "${title}")` : `](${href})`;
709
+ return `]${serializeMarkdownResource(href, title)}`;
624
710
  },
625
711
  collapsesInnerWhiteSpace: true,
626
712
  spacing: NO_SPACING,
@@ -628,7 +714,9 @@ const tagHandlers = {
628
714
  },
629
715
  [27]: {
630
716
  enter: ({ node, state }) => {
631
- return `![${node.attributes?.alt || ""}](${resolveUrl(node.attributes?.src || "", state.options?.origin)})`;
717
+ const alt = node.attributes?.alt || "";
718
+ const src = resolveUrl(node.attributes?.src || "", state.options?.origin, state.options?.clean);
719
+ return `![${serializeImageDescription(alt)}]${serializeMarkdownResource(src, node.attributes?.title)}`;
632
720
  },
633
721
  collapsesInnerWhiteSpace: true,
634
722
  isSelfClosing: true,
@@ -705,15 +793,6 @@ const tagHandlers = {
705
793
  spacing: NO_SPACING
706
794
  },
707
795
  [35]: { enter: ({ state }) => {
708
- const bqDepth = state.depthMap?.[22] || 0;
709
- if (bqDepth > 0) {
710
- const lastEntry = state.buffer.at(-1);
711
- const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
712
- if (lastChar && lastChar !== "\n" && lastChar !== " " && lastChar !== ">") {
713
- const prefix = "> ".repeat(bqDepth);
714
- return `\n${prefix.trimEnd()}\n${prefix}`;
715
- }
716
- }
717
796
  if ((state.depthMap?.[25] || 0) > 0 && !isInsideTableCell(state)) {
718
797
  const lastEntry = state.buffer.at(-1);
719
798
  const lastChar = lastEntry?.charAt(lastEntry.length - 1) || "";
@@ -997,8 +1076,8 @@ const tagHandlers = {
997
1076
  [104]: {},
998
1077
  [102]: {},
999
1078
  [106]: {
1000
- enter: () => "_",
1001
- exit: () => "_",
1079
+ enter: () => "*",
1080
+ exit: () => "*",
1002
1081
  collapsesInnerWhiteSpace: true,
1003
1082
  spacing: NO_SPACING,
1004
1083
  isInline: true
@@ -1056,10 +1135,13 @@ const TAB_CHAR = 9;
1056
1135
  const NEWLINE_CHAR = 10;
1057
1136
  const FORM_FEED_CHAR = 12;
1058
1137
  const CARRIAGE_RETURN_CHAR = 13;
1059
- const BACKTICK_CHAR = 96;
1060
1138
  const PIPE_CHAR = 124;
1061
1139
  const OPEN_BRACKET_CHAR = 91;
1062
1140
  const CLOSE_BRACKET_CHAR = 93;
1141
+ function shouldProtectDecodedEntityReferences(state) {
1142
+ const depthMap = state.depthMap;
1143
+ return !state.plainText && !depthMap[34] && !depthMap[23] && !depthMap[2] && !depthMap[3] && !depthMap[100] && !depthMap[101] && !depthMap[99] && !depthMap[98];
1144
+ }
1063
1145
  const SCRIPT_DATA = 0;
1064
1146
  const SCRIPT_DATA_ESCAPED = 1;
1065
1147
  const SCRIPT_DATA_ESCAPED_DASH = 2;
@@ -1082,7 +1164,7 @@ const HEAD_CONTENT_TAGS = /* @__PURE__ */ new Set([
1082
1164
  78
1083
1165
  ]);
1084
1166
  const CLOSES_P = (() => {
1085
- const t = /* @__PURE__ */ new Uint8Array(110);
1167
+ const t = /* @__PURE__ */ new Uint8Array(112);
1086
1168
  for (const id of [
1087
1169
  36,
1088
1170
  35,
@@ -1318,12 +1400,12 @@ var ParsedElementNode = class {
1318
1400
  }
1319
1401
  get depthMap() {
1320
1402
  if (this.cachedDepthMap) return this.cachedDepthMap;
1321
- const depthMap = /* @__PURE__ */ new Uint8Array(110);
1322
- if (this.tagId >= 0 && this.tagId < 110) depthMap[this.tagId] = 1;
1403
+ const depthMap = /* @__PURE__ */ new Uint8Array(112);
1404
+ if (this.tagId >= 0 && this.tagId < 112) depthMap[this.tagId] = 1;
1323
1405
  let node = this.parent;
1324
1406
  while (node) {
1325
1407
  const tagId = node.tagId;
1326
- if (tagId !== void 0 && tagId >= 0 && tagId < 110) depthMap[tagId] = (depthMap[tagId] || 0) + 1;
1408
+ if (tagId !== void 0 && tagId >= 0 && tagId < 112) depthMap[tagId] = (depthMap[tagId] || 0) + 1;
1327
1409
  node = node.parent;
1328
1410
  }
1329
1411
  this.cachedDepthMap = depthMap;
@@ -1441,7 +1523,7 @@ function matchesClosingTag(node, tagName, tagId, closingIsAlias) {
1441
1523
  function parseHtml(html, options = {}) {
1442
1524
  const events = [];
1443
1525
  const state = {
1444
- depthMap: /* @__PURE__ */ new Uint8Array(110),
1526
+ depthMap: /* @__PURE__ */ new Uint8Array(112),
1445
1527
  depth: 0,
1446
1528
  resolvedPlugins: options.resolvedPlugins || []
1447
1529
  };
@@ -1460,7 +1542,8 @@ function parseHtmlStream(html, state, onEvent) {
1460
1542
  function parseHtmlInternal(htmlChunk, state, handleEvent) {
1461
1543
  if (state.depthLimitReached) return "";
1462
1544
  let textBuffer = "";
1463
- state.depthMap ??= /* @__PURE__ */ new Uint8Array(110);
1545
+ let runStart = 0;
1546
+ state.depthMap ??= /* @__PURE__ */ new Uint8Array(112);
1464
1547
  state.depth ??= 0;
1465
1548
  state.lastCharWasWhitespace ??= true;
1466
1549
  state.justClosedTag ??= false;
@@ -1485,6 +1568,7 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1485
1568
  state.lastCharWasWhitespace = false;
1486
1569
  }
1487
1570
  if (!inPreTag && state.lastCharWasWhitespace) {
1571
+ if (textBuffer.length === 0) runStart = i + 1;
1488
1572
  i++;
1489
1573
  continue;
1490
1574
  }
@@ -1497,7 +1581,6 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1497
1581
  state.lastCharWasWhitespace = false;
1498
1582
  state.justClosedTag = false;
1499
1583
  if (!state.plainText && currentCharCode === PIPE_CHAR && state.depthMap[28]) textBuffer += "\\|";
1500
- else if (!state.plainText && currentCharCode === BACKTICK_CHAR && (state.depthMap[23] || state.depthMap[34])) textBuffer += "\\`";
1501
1584
  else if (!state.plainText && currentCharCode === OPEN_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\[";
1502
1585
  else if (!state.plainText && currentCharCode === CLOSE_BRACKET_CHAR && state.depthMap[26]) textBuffer += "\\]";
1503
1586
  else if (!state.plainText && currentCharCode === GT_CHAR && state.depthMap[22]) textBuffer += "\\>";
@@ -1522,9 +1605,11 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1522
1605
  if (textBuffer.length > 0) {
1523
1606
  processTextBuffer(textBuffer, state, handleEvent);
1524
1607
  textBuffer = "";
1608
+ runStart = i;
1525
1609
  }
1526
1610
  processCdataSection(htmlChunk.substring(i + 9, end), state, handleEvent);
1527
1611
  i = end + 3;
1612
+ runStart = i;
1528
1613
  continue;
1529
1614
  }
1530
1615
  if (chunkLength - i < 9 && "<![CDATA[".startsWith(htmlChunk.substring(i))) {
@@ -1535,10 +1620,13 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1535
1620
  if (textBuffer.length > 0) {
1536
1621
  processTextBuffer(textBuffer, state, handleEvent);
1537
1622
  textBuffer = "";
1623
+ runStart = i;
1538
1624
  }
1539
1625
  const result = processCommentOrDoctype(htmlChunk, i);
1540
- if (result.complete) i = result.newPosition;
1541
- else {
1626
+ if (result.complete) {
1627
+ i = result.newPosition;
1628
+ runStart = i;
1629
+ } else {
1542
1630
  textBuffer += result.remainingText;
1543
1631
  break;
1544
1632
  }
@@ -1561,10 +1649,13 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1561
1649
  if (textBuffer.length > 0) {
1562
1650
  processTextBuffer(textBuffer, state, handleEvent);
1563
1651
  textBuffer = "";
1652
+ runStart = i;
1564
1653
  }
1565
1654
  const result = processClosingTag(htmlChunk, i, state, handleEvent);
1566
- if (result.complete) i = result.newPosition;
1567
- else {
1655
+ if (result.complete) {
1656
+ i = result.newPosition;
1657
+ runStart = i;
1658
+ } else {
1568
1659
  textBuffer += result.remainingText;
1569
1660
  break;
1570
1661
  }
@@ -1595,37 +1686,48 @@ function parseHtmlInternal(htmlChunk, state, handleEvent) {
1595
1686
  continue;
1596
1687
  }
1597
1688
  if (!tagName) {
1689
+ state.textBufferContainsNonWhitespace = true;
1690
+ state.lastCharWasWhitespace = false;
1691
+ state.justClosedTag = false;
1598
1692
  textBuffer += htmlChunk[i++];
1599
1693
  continue;
1600
1694
  }
1601
1695
  if (textBuffer.length > 0) {
1602
1696
  processTextBuffer(textBuffer, state, handleEvent);
1603
1697
  textBuffer = "";
1698
+ runStart = i;
1604
1699
  }
1605
1700
  const result = processOpeningTag(tagName, tagId, htmlChunk, i2, state, handleEvent);
1606
- if (result.skip) i = result.newPosition;
1607
- else if (result.complete) {
1701
+ if (result.skip) {
1702
+ i = result.newPosition;
1703
+ runStart = i;
1704
+ } else if (result.complete) {
1608
1705
  i = result.newPosition;
1609
1706
  if (!result.selfClosing) {
1610
1707
  state.isFirstTextInElement = true;
1611
1708
  if (tagId === 52 && tagName === "script") {
1612
1709
  const scanResult = scanScriptChunk(htmlChunk, i, state);
1613
1710
  if (scanResult < 0) {
1614
- textBuffer = scanResult <= SCRIPT_SEQUENCE_INCOMPLETE ? htmlChunk.substring(-scanResult - 2) : "";
1711
+ if (scanResult <= SCRIPT_SEQUENCE_INCOMPLETE) {
1712
+ runStart = -scanResult - 2;
1713
+ textBuffer = htmlChunk.substring(runStart);
1714
+ } else textBuffer = "";
1615
1715
  break;
1616
1716
  }
1617
1717
  textBuffer = takeScriptText(state);
1618
1718
  i = scanResult;
1619
1719
  }
1620
1720
  }
1721
+ if (textBuffer.length === 0) runStart = i;
1621
1722
  } else {
1622
1723
  textBuffer += result.remainingText;
1623
1724
  break;
1624
1725
  }
1625
1726
  }
1626
1727
  }
1627
- if (textBuffer.length > 0 && isWhitespace(textBuffer.charCodeAt(0))) state.lastCharWasWhitespace = false;
1628
- return textBuffer;
1728
+ const remainingHtml = textBuffer.length > 0 ? htmlChunk.substring(runStart) : "";
1729
+ if (remainingHtml.length > 0 && isWhitespace(remainingHtml.charCodeAt(0))) state.lastCharWasWhitespace = false;
1730
+ return remainingHtml;
1629
1731
  }
1630
1732
  function processTextBuffer(textBuffer, state, handleEvent) {
1631
1733
  const containsNonWhitespace = state.textBufferContainsNonWhitespace;
@@ -1636,7 +1738,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
1636
1738
  let rootText = textBuffer;
1637
1739
  if (rootText.length === 0) return;
1638
1740
  if (state.hasEncodedHtmlEntity) {
1639
- rootText = decodeHTMLEntities(String(rootText));
1741
+ rootText = decodeHTMLEntities(String(rootText), false, shouldProtectDecodedEntityReferences(state));
1640
1742
  state.hasEncodedHtmlEntity = false;
1641
1743
  }
1642
1744
  const rootTextNode = {
@@ -1668,7 +1770,7 @@ function processTextBuffer(textBuffer, state, handleEvent) {
1668
1770
  if (start > 0) text = text.substring(start);
1669
1771
  }
1670
1772
  if (state.hasEncodedHtmlEntity) {
1671
- text = decodeHTMLEntities(String(text));
1773
+ text = decodeHTMLEntities(String(text), false, shouldProtectDecodedEntityReferences(state));
1672
1774
  state.hasEncodedHtmlEntity = false;
1673
1775
  }
1674
1776
  const textNode = {
@@ -1772,7 +1874,7 @@ function closeNode(node, state, handleEvent) {
1772
1874
  }
1773
1875
  }
1774
1876
  const tagId = node.tagId;
1775
- if (tagId !== void 0 && tagId >= 0 && tagId < 110) state.depthMap[tagId] = Math.max(0, (state.depthMap[tagId] || 0) - 1);
1877
+ if (tagId !== void 0 && tagId >= 0 && tagId < 112) state.depthMap[tagId] = Math.max(0, (state.depthMap[tagId] || 0) - 1);
1776
1878
  state.depth--;
1777
1879
  handleEvent({
1778
1880
  type: 1,
@@ -1862,7 +1964,7 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1862
1964
  const headNode = state.currentNode;
1863
1965
  if (headNode && headNode.tagId === 1) closeNode(headNode, state, handleEvent);
1864
1966
  }
1865
- if (tagId >= 0 && tagId < 110 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 65 && (state.depthMap[65] || 0) > 0) {
1967
+ if (tagId >= 0 && tagId < 112 && NEEDS_IMPLIED_END_RECOVERY[tagId] === 1) if (tagId === 65 && (state.depthMap[65] || 0) > 0) {
1866
1968
  if (closeSelectTo(state, 65, handleEvent)) return {
1867
1969
  complete: true,
1868
1970
  newPosition: result.newPosition,
@@ -1905,7 +2007,7 @@ function processOpeningTag(tagName, tagId, htmlChunk, i, state, handleEvent) {
1905
2007
  skip: true
1906
2008
  };
1907
2009
  }
1908
- if (tagId >= 0 && tagId < 110) state.depthMap[tagId] = (state.depthMap[tagId] || 0) + 1;
2010
+ if (tagId >= 0 && tagId < 112) state.depthMap[tagId] = (state.depthMap[tagId] || 0) + 1;
1909
2011
  state.depth++;
1910
2012
  i = result.newPosition;
1911
2013
  if (state.currentNode) state.currentNode.currentWalkIndex = state.currentNode.currentWalkIndex || 0;
@@ -2065,4 +2167,4 @@ function parseAttributes(attrStr) {
2065
2167
  }
2066
2168
  return result;
2067
2169
  }
2068
- export { buildTagOverrideHandlers, continuationPrefix, finalizeParse, parseAttributes, parseHtml, parseHtmlStream };
2170
+ export { breakHandler, buildTagOverrideHandlers, continuationPrefix, finalizeParse, isEmptyLinkHref, parseAttributes, parseHtml, parseHtmlStream, renderBreak, resolveUrl };
@@ -546,15 +546,6 @@ function groupByFormattingType(classes) {
546
546
  }
547
547
  return groups;
548
548
  }
549
- function fixRedundantDelimiters(content) {
550
- content = content.replaceAll("****", "**");
551
- content = content.replaceAll("~~~~", "~~");
552
- if (content.includes("***") && content.split("***").length > 3) {
553
- const parts = content.split("***");
554
- if (parts.length >= 4) content = `${parts[0]}***${parts[1]} ${parts[2]}***${parts.slice(3).join("***")}`;
555
- }
556
- return content;
557
- }
558
549
  function normalizeClasses(classes) {
559
550
  const result = [];
560
551
  const mobileClasses = classes.filter((cls) => !hasBreakpoint(cls));
@@ -648,16 +639,11 @@ function tailwindPlugin() {
648
639
  content: "",
649
640
  skip: true
650
641
  };
651
- let content = node.value;
652
642
  const prefix = state.options?.format === "text" ? "" : tailwindData?.prefix || "";
653
643
  const suffix = state.options?.format === "text" ? "" : tailwindData?.suffix || "";
654
- if (prefix || suffix) {
655
- content = prefix + content + suffix;
656
- content = fixRedundantDelimiters(content);
657
- }
658
- return {
659
- content,
660
- skip: false
644
+ if (prefix || suffix) node.generatedMarkdown = {
645
+ prefix,
646
+ suffix
661
647
  };
662
648
  },
663
649
  beforeNodeProcess({ node }) {
@@ -1,5 +1,5 @@
1
1
  import "./const.mjs";
2
- import { buildTagOverrideHandlers } from "./parse.mjs";
2
+ import { buildTagOverrideHandlers, isEmptyLinkHref } from "./parse.mjs";
3
3
  import { createMarkdownProcessor } from "./markdown-processor.mjs";
4
4
  import "./plugins.mjs";
5
5
  import { resolvePlugins } from "./resolve-plugins.mjs";
@@ -154,7 +154,7 @@ function cleanEmptyLinks(md) {
154
154
  const link = parseLink(md, i);
155
155
  if (link) {
156
156
  const url = link.url;
157
- if (url === "#" || url.startsWith("javascript:") || url.startsWith("data:") || url.startsWith("vbscript:")) {
157
+ if (isEmptyLinkHref(url)) {
158
158
  result += link.text;
159
159
  i = link.end;
160
160
  continue;