@ai-react-markdown/engine 2.7.0 → 2.8.1

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.
@@ -48,7 +48,6 @@ __export(src_exports, {
48
48
  collectDefLabels: () => collectDefLabels,
49
49
  computeFreezeBoundary: () => computeFreezeBoundary,
50
50
  createDefLabelScanner: () => createDefLabelScanner,
51
- createFile: () => createFile,
52
51
  createIncrementalLatexPreprocessor: () => createIncrementalLatexPreprocessor,
53
52
  createProcessor: () => createProcessor,
54
53
  createRegistry: () => createRegistry,
@@ -65,10 +64,8 @@ __export(src_exports, {
65
64
  hasLoneSurrogate: () => hasLoneSurrogate,
66
65
  highlight: () => highlight,
67
66
  isFootnoteSection: () => isFootnoteSection,
68
- isWhitespaceText: () => isWhitespaceText,
69
67
  lastMeaningfulIdx: () => lastMeaningfulIdx,
70
68
  measureStage: () => measureStage,
71
- mergeClassNameAllowlist: () => mergeClassNameAllowlist,
72
69
  normalizeForMatch: () => normalizeForMatch,
73
70
  normalizeId: () => normalizeId,
74
71
  pangu: () => pangu,
@@ -84,7 +81,6 @@ __export(src_exports, {
84
81
  shortenDocumentId: () => shortenDocumentId,
85
82
  smartypants: () => smartypants,
86
83
  sourceIdFromFootnoteLiId: () => sourceIdFromFootnoteLiId,
87
- splitByProtectedRegions: () => splitByProtectedRegions,
88
84
  subscribeStageTimings: () => subscribeStageTimings,
89
85
  transformStage: () => transformStage,
90
86
  withDefs: () => withDefs
@@ -519,12 +515,82 @@ var RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set([
519
515
  // (oracle review of the r2 batch; regression caught before release).
520
516
  "plaintext"
521
517
  ]);
518
+ var P5_MARKUP_RE = /<[!/?A-Za-z]/;
522
519
  var TYPE6_START_RE = /^<\/?([A-Za-z][A-Za-z0-9-]*)(?:[ \t\r]|\/?>|$)/;
523
520
  var TYPE1_START_RE = /^<(script|pre|style|textarea)(?:[ \t\r]|>|$)/i;
524
521
  var TYPE1_CLOSE_RE = /<\/(?:script|pre|style|textarea)>/i;
525
- var TYPE7_LINE_RE = /^(?:<[A-Za-z][A-Za-z0-9-]*(?:[ \t\r][^>]*|\/)?>|<\/[A-Za-z][A-Za-z0-9-]*[ \t\r]*>)[ \t\r]*$/;
526
- var t7Name = (line) => /^<\/?([A-Za-z][A-Za-z0-9-]*)/.exec(line)[1];
522
+ var isSpaceTab = (c) => c === 32 || c === 9;
523
+ var isAsciiAlpha = (c) => c >= 65 && c <= 90 || c >= 97 && c <= 122;
524
+ var isAlnum = (c) => isAsciiAlpha(c) || c >= 48 && c <= 57;
525
+ var isAttrNameRest = (c) => isAlnum(c) || c === 45 || c === 46 || c === 58 || c === 95;
526
+ var isUnquotedExit = (c) => Number.isNaN(c) || c === 34 || c === 39 || c === 47 || c === 60 || c === 61 || c === 62 || c === 96 || isSpaceTab(c);
527
+ var completeOpenTagRest = (t, from) => {
528
+ let i = from;
529
+ for (; ; ) {
530
+ const c = t.charCodeAt(i);
531
+ if (c === 47) {
532
+ i += 1;
533
+ break;
534
+ }
535
+ if (isSpaceTab(c)) {
536
+ i += 1;
537
+ continue;
538
+ }
539
+ if (!(c === 58 || c === 95 || isAsciiAlpha(c))) break;
540
+ i += 1;
541
+ while (isAttrNameRest(t.charCodeAt(i))) i += 1;
542
+ for (; ; ) {
543
+ while (isSpaceTab(t.charCodeAt(i))) i += 1;
544
+ if (t.charCodeAt(i) !== 61) break;
545
+ i += 1;
546
+ while (isSpaceTab(t.charCodeAt(i))) i += 1;
547
+ const v = t.charCodeAt(i);
548
+ if (Number.isNaN(v) || v === 60 || v === 61 || v === 62 || v === 96) return -1;
549
+ if (v === 34 || v === 39) {
550
+ i += 1;
551
+ while (t.charCodeAt(i) !== v) {
552
+ if (i >= t.length) return -1;
553
+ i += 1;
554
+ }
555
+ i += 1;
556
+ const a = t.charCodeAt(i);
557
+ if (!(a === 47 || a === 62 || isSpaceTab(a))) return -1;
558
+ break;
559
+ }
560
+ while (!isUnquotedExit(t.charCodeAt(i))) i += 1;
561
+ }
562
+ }
563
+ return t.charCodeAt(i) === 62 ? i + 1 : -1;
564
+ };
565
+ var isType7Line = (line) => {
566
+ const cr = line.indexOf("\r");
567
+ const t = cr === -1 ? line : line.slice(0, cr);
568
+ if (t.charCodeAt(0) !== 60) return false;
569
+ let i = 1;
570
+ const closing = t.charCodeAt(i) === 47;
571
+ if (closing) i += 1;
572
+ if (!isAsciiAlpha(t.charCodeAt(i))) return false;
573
+ const nameStart = i;
574
+ i += 1;
575
+ while (isAlnum(t.charCodeAt(i)) || t.charCodeAt(i) === 45) i += 1;
576
+ const c = t.charCodeAt(i);
577
+ if (!(Number.isNaN(c) || c === 47 || c === 62 || isSpaceTab(c))) return false;
578
+ const name = t.slice(nameStart, i).toLowerCase();
579
+ if (!closing && c !== 47 && TYPE1_NAMES.has(name)) return false;
580
+ if (TYPE6_NAMES.has(name)) return false;
581
+ if (closing) {
582
+ while (isSpaceTab(t.charCodeAt(i))) i += 1;
583
+ if (t.charCodeAt(i) !== 62) return false;
584
+ i += 1;
585
+ } else {
586
+ i = completeOpenTagRest(t, i);
587
+ if (i === -1) return false;
588
+ }
589
+ while (isSpaceTab(t.charCodeAt(i))) i += 1;
590
+ return i >= t.length;
591
+ };
527
592
  var mdHtml = (b, type) => b.kind === "html" && b.type === type;
593
+ var mdType1RawText = (b) => b.kind === "html" && b.type === 1 && b.raw;
528
594
  var mdHtml25 = (b) => b.kind === "html" && b.type >= 2 && b.type <= 5;
529
595
  var commentEitherOpen = (md, p5) => mdHtml(md, 2) || p5.kind === "comment";
530
596
  var inRawTextTok = (t) => t.kind === "rawText" || t.kind === "script";
@@ -546,7 +612,13 @@ var VOID_TAGS = /* @__PURE__ */ new Set([
546
612
  "wbr"
547
613
  ]);
548
614
  var LIST_MARKER_RE = /^ {0,3}(?:[-*+]|\d{1,9}[.)])(?:[ \t]|$)/;
615
+ var CONTAINER_MARKER_RE = /^ {0,3}(?:>|(?:[-*+]|\d{1,9}[.)])[ \t]+\S)/;
616
+ var ATX_HEADING_RE = /^#{1,6}(?:[ \t]|$)/;
617
+ var THEMATIC_BREAK_RE = /^(?:(?:-[ \t]*){3,}|(?:\*[ \t]*){3,}|(?:_[ \t]*){3,})$/;
618
+ var BARE_MARKER_RE = /^(?:[-*+]|\d{1,9}[.)])[ \t]*$/;
619
+ var SETEXT_LEFTOVER_RE = /^(?:=+|--)[ \t]*$/;
549
620
  var DEF_LIST_DD_RE = /^ {0,3}:[ \t]/;
621
+ var CLOSE_TAG_ONLY_RE = /^[ \t]*<\/[A-Za-z][A-Za-z0-9-]*[ \t]*>[ \t\r]*$/;
550
622
  var FENCE_RE = /^ {0,3}(`{3,}|~{3,})/;
551
623
  var MATH_RUN_RE = /^ {0,3}(\$\$+)/;
552
624
  var TAG_OR_COMMENT_RE = /<(\/?)([A-Za-z][A-Za-z0-9-]*)(?=[\s/>])([^>]*)>|<!--|-->|--!>/g;
@@ -655,10 +727,14 @@ function freshCheckpoint(defListEnabled, mathFlow, referenceTaint) {
655
727
  // doc start counts as a block start
656
728
  prevLineWasText: false,
657
729
  prevLineWasValidDef: false,
730
+ prevLineOpenContent: false,
731
+ tableMaybeOpen: false,
732
+ containerMaybeOpen: false,
658
733
  paragraphHasUnpairedRun: false,
659
734
  openBracket: null,
660
- mayBeRawToMicromark: false,
661
735
  p5SealPending: false,
736
+ defBlockMaybeOpen: false,
737
+ fnDefResumable: false,
662
738
  phasePoisonedAt: Infinity,
663
739
  pendingTruncatedTags: [],
664
740
  pendingTruncatedCloses: [],
@@ -785,10 +861,11 @@ function processConfirmedLine(cp, ln, text) {
785
861
  newest.defListSettled = ln.blank ? true : !canBecomeDdLine(ln.text, true);
786
862
  }
787
863
  const isBlockStart = cp.prevLineBlank;
788
- if (cp.p5SealPending && !ln.blank && !cp.mayBeRawToMicromark && !(mdHtml25(cp.mdBlock) || cp.p5Tok.kind === "comment" || cp.p5Tok.kind === "bogus")) {
789
- const defShapedLine = DEF_RE.test(ln.text) || FOOTNOTE_DEF_RE.test(ln.text);
864
+ if (cp.p5SealPending && !ln.blank && cp.mdBlock.kind !== "html" && !(cp.p5Tok.kind === "comment" || cp.p5Tok.kind === "bogus")) {
865
+ const defShapedLine = DEF_RE.test(ln.text) || FOOTNOTE_DEF_RE.test(ln.text) || cp.defBlockMaybeOpen || cp.fnDefResumable && ln.indent >= 4;
790
866
  const commentOnly = ln.text.replace(/<!--[\s\S]*?-->/g, " ").replace(/<!--[\s\S]*$/, " ").replace(/[ \t\r]/g, "") === "";
791
- if (!defShapedLine && !commentOnly) {
867
+ const closeTagOnly = CLOSE_TAG_ONLY_RE.test(ln.text);
868
+ if (!defShapedLine && !commentOnly && !closeTagOnly) {
792
869
  cp.p5SealPending = false;
793
870
  }
794
871
  }
@@ -798,9 +875,13 @@ function processConfirmedLine(cp, ln, text) {
798
875
  const applyTag = (tag, closing) => {
799
876
  if (inRawTextTok(cp.p5Tok)) {
800
877
  if (cp.p5Tok.kind === "script" && cp.p5Tok.escaped && !closing && tag === "script") {
801
- cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
878
+ cp.p5Tok = { ...cp.p5Tok, double: true };
802
879
  }
803
880
  if (!(closing && tag === rawTextElement(cp.p5Tok))) return;
881
+ if (cp.p5Tok.kind === "script" && cp.p5Tok.double) {
882
+ cp.p5Tok = { ...cp.p5Tok, double: false };
883
+ return;
884
+ }
804
885
  cp.p5Tok = { kind: "data" };
805
886
  } else {
806
887
  if (!closing && RAW_TEXT_ELEMENTS.has(tag) && foreignRawTextSwitchUnknowable()) {
@@ -808,7 +889,7 @@ function processConfirmedLine(cp, ln, text) {
808
889
  } else if (!closing && RAW_TEXT_ELEMENTS.has(tag)) {
809
890
  if (cp.p5Tok.kind !== "data") cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
810
891
  const openedInline = cp.mdBlock.kind !== "html";
811
- cp.p5Tok = tag === "script" ? { kind: "script", escaped: false, openedInline } : { kind: "rawText", element: tag, openedInline };
892
+ cp.p5Tok = tag === "script" ? { kind: "script", escaped: false, double: false, openedInline } : { kind: "rawText", element: tag, openedInline };
812
893
  if (tag === "plaintext") cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
813
894
  }
814
895
  }
@@ -836,9 +917,14 @@ function processConfirmedLine(cp, ln, text) {
836
917
  cp.openTotal += 1;
837
918
  }
838
919
  };
839
- const definitelyInsideTable = () => (cp.tagBalance.get("table") ?? 0) > 0;
920
+ const definitelyInsideTable = () => (cp.tagBalance.get("table") ?? 0) > cp.pendingTruncatedTags.filter((t) => t === "table").length;
840
921
  const strayTablePart = (tag) => TABLE_PART_NAMES.has(tag) && !definitelyInsideTable();
841
922
  const commentOpenAtLineStart = commentEitherOpen(cp.mdBlock, cp.p5Tok);
923
+ const bothCommentsOpenAtLineStart = mdHtml(cp.mdBlock, 2) && cp.p5Tok.kind === "comment";
924
+ const inDivergenceWindow = mdHtml(cp.mdBlock, 2) && cp.p5Tok.kind !== "comment" || (mdHtml(cp.mdBlock, 3) || mdHtml(cp.mdBlock, 5)) && cp.p5Tok.kind !== "bogus";
925
+ if (inDivergenceWindow && P5_MARKUP_RE.test(ln.text)) {
926
+ cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
927
+ }
842
928
  const rawOpenAtLineStart = mdHtml25(cp.mdBlock) || cp.p5Tok.kind === "comment" || cp.p5Tok.kind === "bogus";
843
929
  if (cp.mdBlock.kind === "fence") {
844
930
  const close = FENCE_RE.exec(ln.text);
@@ -851,12 +937,15 @@ function processConfirmedLine(cp, ln, text) {
851
937
  cp.prevLineBlank = false;
852
938
  cp.prevLineWasText = false;
853
939
  cp.prevLineWasValidDef = false;
940
+ cp.prevLineOpenContent = false;
941
+ cp.tableMaybeOpen = false;
942
+ cp.containerMaybeOpen = false;
854
943
  return;
855
944
  }
856
945
  if (cp.mdBlock.kind !== "math" && !rawOpenAtLineStart) {
857
946
  const open = FENCE_RE.exec(ln.text);
858
947
  const bogusInfo = open !== null && open[1][0] === "`" && ln.text.slice(ln.text.indexOf(open[1]) + open[1].length).includes("`");
859
- if (open && !bogusInfo && cp.mayBeRawToMicromark) {
948
+ if (open && !bogusInfo && cp.mdBlock.kind === "html") {
860
949
  cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
861
950
  } else if (open && !bogusInfo) {
862
951
  if (isBlockStart) {
@@ -870,6 +959,9 @@ function processConfirmedLine(cp, ln, text) {
870
959
  cp.prevLineBlank = false;
871
960
  cp.prevLineWasText = false;
872
961
  cp.prevLineWasValidDef = false;
962
+ cp.prevLineOpenContent = false;
963
+ cp.tableMaybeOpen = false;
964
+ cp.containerMaybeOpen = false;
873
965
  return;
874
966
  }
875
967
  }
@@ -884,13 +976,16 @@ function processConfirmedLine(cp, ln, text) {
884
976
  cp.prevLineBlank = false;
885
977
  cp.prevLineWasText = false;
886
978
  cp.prevLineWasValidDef = false;
979
+ cp.prevLineOpenContent = false;
980
+ cp.tableMaybeOpen = false;
981
+ cp.containerMaybeOpen = false;
887
982
  return;
888
983
  }
889
984
  const mathRun = cp.mathFlow && !rawOpenAtLineStart ? MATH_RUN_RE.exec(ln.text) : null;
890
985
  if (mathRun) {
891
986
  const rest = ln.text.slice(ln.text.indexOf(mathRun[1]) + mathRun[1].length);
892
987
  if (!rest.includes("$")) {
893
- if (cp.mayBeRawToMicromark) {
988
+ if (cp.mdBlock.kind === "html") {
894
989
  cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
895
990
  } else {
896
991
  if (isBlockStart) {
@@ -904,6 +999,9 @@ function processConfirmedLine(cp, ln, text) {
904
999
  cp.prevLineBlank = false;
905
1000
  cp.prevLineWasText = false;
906
1001
  cp.prevLineWasValidDef = false;
1002
+ cp.prevLineOpenContent = false;
1003
+ cp.tableMaybeOpen = false;
1004
+ cp.containerMaybeOpen = false;
907
1005
  return;
908
1006
  }
909
1007
  }
@@ -919,8 +1017,11 @@ function processConfirmedLine(cp, ln, text) {
919
1017
  }
920
1018
  cp.pendingTag = null;
921
1019
  if (cp.p5Tok.kind === "bogus") {
922
- cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
923
- cp.p5Tok = { kind: "data" };
1020
+ if (mdHtml25(cp.mdBlock)) {
1021
+ } else {
1022
+ cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
1023
+ cp.p5Tok = { kind: "data" };
1024
+ }
924
1025
  }
925
1026
  if (cp.mdBlock.kind === "html" && cp.mdBlock.type >= 6) cp.mdBlock = { kind: "none" };
926
1027
  cp.blankRun += 1;
@@ -942,7 +1043,6 @@ function processConfirmedLine(cp, ln, text) {
942
1043
  cp.paragraphHasUnpairedRun = false;
943
1044
  cp.openBracket = null;
944
1045
  if (!mdHtml(cp.mdBlock, 1)) {
945
- cp.mayBeRawToMicromark = false;
946
1046
  if (inRawTextTok(cp.p5Tok) && !cp.p5Tok.openedInline) {
947
1047
  cp.phasePoisonedAt = 0;
948
1048
  }
@@ -950,6 +1050,10 @@ function processConfirmedLine(cp, ln, text) {
950
1050
  cp.prevLineBlank = true;
951
1051
  cp.prevLineWasText = false;
952
1052
  cp.prevLineWasValidDef = false;
1053
+ cp.defBlockMaybeOpen = false;
1054
+ cp.prevLineOpenContent = false;
1055
+ cp.tableMaybeOpen = false;
1056
+ cp.containerMaybeOpen = false;
953
1057
  return;
954
1058
  }
955
1059
  if (isBlockStart) {
@@ -961,24 +1065,35 @@ function processConfirmedLine(cp, ln, text) {
961
1065
  const tagStart = ln.indent <= 3 ? /^<\/?([A-Za-z][A-Za-z0-9-]*)/.exec(mdTrimStart(ln.text)) : null;
962
1066
  if (tagStart) {
963
1067
  const noRealBlockOpen = cp.mdBlock.kind !== "html";
964
- cp.mayBeRawToMicromark = true;
965
- if (noRealBlockOpen && TYPE1_START_RE.test(mdTrimStart(ln.text))) cp.mdBlock = { kind: "html", type: 1 };
966
- if (!TYPE6_NAMES.has(tagStart[1].toLowerCase())) cp.hazardVerdict = true;
1068
+ const t1 = noRealBlockOpen ? TYPE1_START_RE.exec(mdTrimStart(ln.text)) : null;
1069
+ if (t1) cp.mdBlock = { kind: "html", type: 1, raw: RAW_TEXT_ELEMENTS.has(t1[1].toLowerCase()) };
967
1070
  if (cp.mdBlock.kind !== "html") {
968
1071
  const t = mdTrimStart(ln.text);
969
1072
  const t6 = TYPE6_START_RE.exec(t);
970
- const t7 = TYPE7_LINE_RE.exec(t);
971
1073
  const realT6 = t6 !== null && TYPE6_NAMES.has(t6[1].toLowerCase());
972
- if (realT6 || TYPE1_START_RE.test(t) || // Type 7 cannot interrupt a paragraph, and excludes the raw-text
973
- // names (those are type 1 as start tags, paragraph as end tags).
974
- t7 !== null && !cp.prevLineWasText && !TYPE1_NAMES.has(t7Name(t).toLowerCase())) {
1074
+ const t1Line = TYPE1_START_RE.test(t);
1075
+ const type7Shaped = !realT6 && !t1Line && isType7Line(t);
1076
+ if (realT6 || t1Line || // Type 7 cannot interrupt CONTENT (micromark's paragraph/definition
1077
+ // construct — `prevLineOpenContent`, the exact interrupt input; the
1078
+ // old `prevLineWasText` gate refused after headings, terminator
1079
+ // lines and fence closes, where micromark measurably opens). The
1080
+ // classifier itself is exact too (isType7Line) — including closing
1081
+ // raw-text names (`</style>` alone is type 7, measured) and
1082
+ // quoted-`>` attribute values.
1083
+ !cp.prevLineOpenContent && type7Shaped) {
975
1084
  if (cp.mdBlock.kind === "none") cp.mdBlock = { kind: "html", type: realT6 ? 6 : 7 };
1085
+ } else if (cp.prevLineOpenContent && cp.tableMaybeOpen && type7Shaped) {
1086
+ cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
1087
+ }
1088
+ if (type7Shaped && cp.containerMaybeOpen) {
1089
+ cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
976
1090
  }
977
1091
  }
978
1092
  }
979
- const inRawText = cp.mayBeRawToMicromark || rawOpenAtLineStart;
980
1093
  const rawFlowStart = ln.indent <= 3 && /^<(?:!--|\?|![A-Za-z]|!\[CDATA\[)/.test(mdTrimStart(ln.text));
981
- const { masked, unpaired } = inRawText ? { masked: null, unpaired: false } : maskIntraLineCodeSpans(ln.text, cp.paragraphHasUnpairedRun);
1094
+ const htmlOwnedLine = cp.mdBlock.kind === "html" || rawOpenAtLineStart || rawFlowStart;
1095
+ const maskingSuppressed = htmlOwnedLine || inRawTextTok(cp.p5Tok);
1096
+ const { masked, unpaired } = maskingSuppressed ? { masked: null, unpaired: false } : maskIntraLineCodeSpans(ln.text, cp.paragraphHasUnpairedRun);
982
1097
  if (unpaired) cp.paragraphHasUnpairedRun = true;
983
1098
  const scanText = masked ?? ln.text;
984
1099
  const defRawToMicromark = cp.mdBlock.kind === "html" || rawOpenAtLineStart || inRawTextTok(cp.p5Tok);
@@ -990,30 +1105,27 @@ function processConfirmedLine(cp, ln, text) {
990
1105
  };
991
1106
  let inlineRawOpenerIdx = -1;
992
1107
  while (pos < scanText.length) {
993
- if (mdHtml(cp.mdBlock, 3)) {
994
- const c = scanText.indexOf("?>", pos);
995
- const gt = scanText.indexOf(">", pos);
996
- if (gt !== -1 && (c === -1 || gt !== c + 1)) poisonRawDivergence();
997
- if (c === -1) {
998
- rawSpans.push([pos, scanText.length]);
999
- break;
1000
- }
1001
- rawSpans.push([pos, c + 2]);
1002
- cp.mdBlock = { kind: "none" };
1003
- pos = c + 2;
1004
- continue;
1005
- }
1006
- if (mdHtml(cp.mdBlock, 5)) {
1007
- const c = scanText.indexOf("]]>", pos);
1008
- const gt = scanText.indexOf(">", pos);
1009
- if (gt !== -1 && (c === -1 || gt !== c + 2)) poisonRawDivergence();
1010
- if (c === -1) {
1011
- rawSpans.push([pos, scanText.length]);
1012
- break;
1108
+ if (mdHtml(cp.mdBlock, 3) || mdHtml(cp.mdBlock, 5)) {
1109
+ const isPi = mdHtml(cp.mdBlock, 3);
1110
+ const term = isPi ? "?>" : "]]>";
1111
+ const c = scanText.indexOf(term, pos);
1112
+ const mdEnd = c === -1 ? scanText.length : c + term.length;
1113
+ if (cp.p5Tok.kind === "bogus") {
1114
+ const gt = scanText.indexOf(">", pos);
1115
+ if (gt !== -1 && (c === -1 || gt !== c + term.length - 1)) {
1116
+ cp.p5Tok = { kind: "data" };
1117
+ rawSpans.push([pos, gt + 1]);
1118
+ if (P5_MARKUP_RE.test(scanText.slice(gt + 1, c === -1 ? scanText.length : c))) {
1119
+ poisonRawDivergence();
1120
+ }
1121
+ } else {
1122
+ rawSpans.push([pos, mdEnd]);
1123
+ if (c !== -1) cp.p5Tok = { kind: "data" };
1124
+ }
1013
1125
  }
1014
- rawSpans.push([pos, c + 3]);
1126
+ if (c === -1) break;
1015
1127
  cp.mdBlock = { kind: "none" };
1016
- pos = c + 3;
1128
+ pos = mdEnd;
1017
1129
  continue;
1018
1130
  }
1019
1131
  if (mdHtml(cp.mdBlock, 4)) {
@@ -1039,7 +1151,12 @@ function processConfirmedLine(cp, ln, text) {
1039
1151
  pos = c + 1;
1040
1152
  continue;
1041
1153
  }
1042
- if (commentOpenAtLineStart || inRawTextTok(cp.p5Tok) || mdHtml(cp.mdBlock, 1)) break;
1154
+ if (commentOpenAtLineStart || inRawTextTok(cp.p5Tok) || mdType1RawText(cp.mdBlock)) {
1155
+ if (inRawTextTok(cp.p5Tok) && cp.p5Tok.openedInline && (tailCarriesRetroactive(ln.text) || /<template(?![a-z0-9-])/i.test(ln.text))) {
1156
+ cp.phasePoisonedAt = 0;
1157
+ }
1158
+ break;
1159
+ }
1043
1160
  const pi = scanText.indexOf("<?", pos);
1044
1161
  const cd = scanText.indexOf("<![CDATA[", pos);
1045
1162
  const dm = scanText.slice(pos).search(/<![A-Za-z]/);
@@ -1056,7 +1173,8 @@ function processConfirmedLine(cp, ln, text) {
1056
1173
  pos = bogus + 2;
1057
1174
  } else if (first === cd) {
1058
1175
  rawSpans.push([cd, cd + 9]);
1059
- cp.mdBlock = { kind: "html", type: 5 };
1176
+ if (cp.mdBlock.kind === "none") cp.mdBlock = { kind: "html", type: 5 };
1177
+ if (cp.p5Tok.kind === "data") cp.p5Tok = { kind: "bogus" };
1060
1178
  if (!isMdBlank(scanText.slice(0, cd)) || ln.indent > 3) inlineRawOpenerIdx = cd;
1061
1179
  pos = cd + 9;
1062
1180
  } else if (first === pi) {
@@ -1067,13 +1185,15 @@ function processConfirmedLine(cp, ln, text) {
1067
1185
  continue;
1068
1186
  }
1069
1187
  rawSpans.push([pi, pi + 2]);
1070
- cp.mdBlock = { kind: "html", type: 3 };
1188
+ if (cp.mdBlock.kind === "none") cp.mdBlock = { kind: "html", type: 3 };
1189
+ if (cp.p5Tok.kind === "data") cp.p5Tok = { kind: "bogus" };
1071
1190
  if (!isMdBlank(scanText.slice(0, pi)) || ln.indent > 3) inlineRawOpenerIdx = pi;
1072
1191
  pos = pi + 2;
1073
1192
  } else {
1074
1193
  rawSpans.push([decl, decl + 2]);
1075
1194
  if (ln.indent <= 3 && /^doctype/i.test(scanText.slice(decl + 2))) cp.phasePoisonedAt = 0;
1076
- cp.mdBlock = { kind: "html", type: 4 };
1195
+ if (cp.mdBlock.kind === "none") cp.mdBlock = { kind: "html", type: 4 };
1196
+ if (cp.p5Tok.kind === "data") cp.p5Tok = { kind: "bogus" };
1077
1197
  if (!isMdBlank(scanText.slice(0, decl)) || ln.indent > 3) inlineRawOpenerIdx = decl;
1078
1198
  pos = decl + 2;
1079
1199
  }
@@ -1110,7 +1230,7 @@ function processConfirmedLine(cp, ln, text) {
1110
1230
  if (inRawTextTok(cp.p5Tok) && (m[0] === "<!--" || m[0] === "-->" || m[0] === "--!>")) {
1111
1231
  if (cp.p5Tok.kind === "script") {
1112
1232
  if (m[0] === "<!--") cp.p5Tok = { ...cp.p5Tok, escaped: true };
1113
- if (m[0] === "-->") cp.p5Tok = { ...cp.p5Tok, escaped: false };
1233
+ if (m[0] === "-->") cp.p5Tok = { ...cp.p5Tok, escaped: false, double: false };
1114
1234
  }
1115
1235
  continue;
1116
1236
  }
@@ -1122,7 +1242,9 @@ function processConfirmedLine(cp, ln, text) {
1122
1242
  if (cp.p5Tok.kind === "comment") cp.p5Tok = { kind: "data" };
1123
1243
  } else if (next === "!>" || next === "-!") {
1124
1244
  if (cp.p5Tok.kind === "comment") cp.p5Tok = { kind: "data" };
1125
- poisonRawDivergence();
1245
+ if (mdHtml(cp.mdBlock, 2) && P5_MARKUP_RE.test(tagText.slice(m.index + m[0].length))) {
1246
+ poisonRawDivergence();
1247
+ }
1126
1248
  }
1127
1249
  continue;
1128
1250
  }
@@ -1140,7 +1262,9 @@ function processConfirmedLine(cp, ln, text) {
1140
1262
  continue;
1141
1263
  }
1142
1264
  if (m[0] === "--!>") {
1143
- if (commentEitherOpen(cp.mdBlock, cp.p5Tok)) poisonRawDivergence();
1265
+ if (mdHtml(cp.mdBlock, 2) && P5_MARKUP_RE.test(tagText.slice(m.index + m[0].length))) {
1266
+ poisonRawDivergence();
1267
+ }
1144
1268
  if (cp.p5Tok.kind === "comment") cp.p5Tok = { kind: "data" };
1145
1269
  continue;
1146
1270
  }
@@ -1231,14 +1355,16 @@ function processConfirmedLine(cp, ln, text) {
1231
1355
  applyTag(tag, closing);
1232
1356
  const rawLastLt = ln.text.lastIndexOf("<");
1233
1357
  const rawTruncated = rawLastLt !== -1 && !ln.text.includes(">", rawLastLt);
1234
- if (!closing && !inRawText && rawTruncated) cp.pendingTruncatedTags.push(tag);
1358
+ if (!closing && !(htmlOwnedLine || inRawTextTok(cp.p5Tok)) && rawTruncated) {
1359
+ cp.pendingTruncatedTags.push(tag);
1360
+ }
1235
1361
  }
1236
1362
  }
1237
1363
  }
1238
1364
  }
1239
1365
  }
1240
1366
  const effectiveOpen = cp.openTotal - cp.pendingTruncatedTags.length;
1241
- if ((inRawText || rawFlowStart) && effectiveOpen <= 0) {
1367
+ if ((htmlOwnedLine || inRawTextTok(cp.p5Tok)) && effectiveOpen <= 0) {
1242
1368
  let masked2 = "";
1243
1369
  let cursor = 0;
1244
1370
  for (const [from, to] of rawSpans) {
@@ -1246,7 +1372,7 @@ function processConfirmedLine(cp, ln, text) {
1246
1372
  cursor = to;
1247
1373
  }
1248
1374
  masked2 += scanText.slice(cursor);
1249
- if (floatingResidue(masked2, commentOpenAtLineStart).length > 0) {
1375
+ if (floatingResidue(masked2, bothCommentsOpenAtLineStart).length > 0) {
1250
1376
  cp.p5SealPending = true;
1251
1377
  }
1252
1378
  }
@@ -1255,12 +1381,36 @@ function processConfirmedLine(cp, ln, text) {
1255
1381
  }
1256
1382
  if (mdHtml(cp.mdBlock, 1) && TYPE1_CLOSE_RE.test(ln.text)) {
1257
1383
  cp.mdBlock = { kind: "none" };
1258
- cp.mayBeRawToMicromark = false;
1384
+ if (inRawTextTok(cp.p5Tok)) cp.phasePoisonedAt = 0;
1259
1385
  }
1260
1386
  cp.blankRun = 0;
1261
1387
  cp.prevLineBlank = false;
1262
1388
  cp.prevLineWasText = true;
1389
+ {
1390
+ const tt = mdTrimStart(ln.text);
1391
+ let openContent;
1392
+ if (htmlOwnedLine) {
1393
+ openContent = false;
1394
+ } else if (ln.indent >= 4) {
1395
+ openContent = cp.prevLineOpenContent;
1396
+ } else if (ATX_HEADING_RE.test(tt) || THEMATIC_BREAK_RE.test(tt)) {
1397
+ openContent = false;
1398
+ } else if (BARE_MARKER_RE.test(tt)) {
1399
+ openContent = tt[0] === "-" ? false : cp.prevLineOpenContent;
1400
+ } else if (SETEXT_LEFTOVER_RE.test(tt)) {
1401
+ openContent = cp.tableMaybeOpen && cp.prevLineOpenContent ? true : !cp.prevLineOpenContent;
1402
+ } else {
1403
+ openContent = true;
1404
+ }
1405
+ cp.prevLineOpenContent = openContent;
1406
+ cp.tableMaybeOpen = !htmlOwnedLine && ln.text.includes("|") || cp.tableMaybeOpen && openContent;
1407
+ const containerMarker = CONTAINER_MARKER_RE.test(ln.text) || FOOTNOTE_DEF_RE.test(ln.text) || cp.defListEnabled && DEF_LIST_DD_RE.test(ln.text);
1408
+ cp.containerMaybeOpen = openContent && (containerMarker || cp.containerMaybeOpen);
1409
+ }
1263
1410
  cp.prevLineWasValidDef = validLinkDef;
1411
+ cp.defBlockMaybeOpen = cp.defBlockMaybeOpen || DEF_RE.test(ln.text) || FOOTNOTE_DEF_RE.test(ln.text);
1412
+ if (FOOTNOTE_DEF_RE.test(ln.text)) cp.fnDefResumable = true;
1413
+ else if (isBlockStart && ln.indent <= 3) cp.fnDefResumable = false;
1264
1414
  }
1265
1415
 
1266
1416
  // src/components/incrementalParse/spliceParse.ts
@@ -1509,6 +1659,33 @@ function headRoutedCaptureUnclosed(values) {
1509
1659
  return !new RegExp(`</${name}(?=[\\s/>])`, "i").test(after);
1510
1660
  }
1511
1661
  var STRAY_SYNTHESIZED_END_TAG_RE = /<\/(?:br|p)\b/i;
1662
+ var RAW_TEXT_OPEN_RE = /<(script|style|textarea|title|xmp|iframe|noembed|noframes|plaintext)(?=[\s/>])/gi;
1663
+ function rawTextRegionCrossesOut(values) {
1664
+ for (const value of values) {
1665
+ let pos = 0;
1666
+ for (; ; ) {
1667
+ RAW_TEXT_OPEN_RE.lastIndex = pos;
1668
+ const open = RAW_TEXT_OPEN_RE.exec(value);
1669
+ if (open === null) break;
1670
+ const name = open[1].toLowerCase();
1671
+ const bodyStart = open.index + open[0].length;
1672
+ const closeRe = new RegExp(`</${name}(?=[\\s/>])`, "ig");
1673
+ closeRe.lastIndex = bodyStart;
1674
+ let close = closeRe.exec(value);
1675
+ if (name === "script") {
1676
+ while (close !== null) {
1677
+ const body = value.slice(bodyStart, close.index);
1678
+ const lastOpen = body.lastIndexOf("<!--");
1679
+ if (lastOpen === -1 || body.indexOf("-->", lastOpen + 4) !== -1) break;
1680
+ close = closeRe.exec(value);
1681
+ }
1682
+ }
1683
+ if (close === null) return true;
1684
+ pos = close.index + close[0].length;
1685
+ }
1686
+ }
1687
+ return false;
1688
+ }
1512
1689
  function spliceTrees(input) {
1513
1690
  const { prevMdast, prevHast, tailMdast, tailHast, content, boundary, injectionPrefix, injectedSegments } = input;
1514
1691
  const injectedLen = injectionPrefix.length;
@@ -1565,7 +1742,9 @@ function spliceTrees(input) {
1565
1742
  return !(start !== void 0 && start < injectedLen);
1566
1743
  });
1567
1744
  const tailWrapVisible = tailMdastChildren.some((child) => !isWrapInvisible(child));
1568
- if (hasStrayTablePart(prefixMdast.flatMap((c) => c.type === "html" ? [c.value] : []))) return null;
1745
+ const prefixHtmlValues = prefixMdast.flatMap((c) => c.type === "html" ? [c.value] : []);
1746
+ if (hasStrayTablePart(prefixHtmlValues)) return null;
1747
+ if (rawTextRegionCrossesOut(prefixHtmlValues)) return null;
1569
1748
  const leadingHtml = [];
1570
1749
  for (const child of tailMdastChildren) {
1571
1750
  if (isWrapInvisible(child)) continue;
@@ -1746,7 +1925,7 @@ function alignPrefixCut(prefixMdast, cutRegion, tailWrapVisible) {
1746
1925
  if (sepBuffer.length !== trailingGaps && sepBuffer.length !== trailingGaps + 1) return null;
1747
1926
  for (let j = pairIdx + 1; j < visibles.length; j++) {
1748
1927
  const v = visibles[j];
1749
- if (v.type === "html" && !/^\s*<[!?]/.test(v.value)) return null;
1928
+ if (v.type === "html" && !isExactSanitizeStrippedConstruct(v.value)) return null;
1750
1929
  }
1751
1930
  for (let i = 0; i < trailingGaps + seam; i++) {
1752
1931
  out.push({ type: "text", value: "\n" });
@@ -1796,7 +1975,7 @@ function tailLeadingTextIsHoist(tailMdastChildren, tailHastChildren) {
1796
1975
  if (!isSeparatorText(firstText)) {
1797
1976
  if (firstText.type === "text" && firstText.position === void 0 && firstVisible?.type === "html") {
1798
1977
  if (/^\s*<\/[A-Za-z][A-Za-z0-9-]*\s*>/.test(firstVisible.value)) return true;
1799
- if (isCompleteRawConstruct(firstVisible.value)) return false;
1978
+ if (isSanitizeStrippedConstruct(firstVisible.value)) return false;
1800
1979
  return null;
1801
1980
  }
1802
1981
  return false;
@@ -1812,12 +1991,26 @@ function tailLeadingTextIsHoist(tailMdastChildren, tailHastChildren) {
1812
1991
  }
1813
1992
  }
1814
1993
  if (firstVisible.type === "math") return false;
1815
- if (firstVisible.type === "html" && isCompleteRawConstruct(firstVisible.value)) return false;
1994
+ if (firstVisible.type === "html" && isSanitizeStrippedConstruct(firstVisible.value)) return false;
1816
1995
  return null;
1817
1996
  }
1818
- function isCompleteRawConstruct(value) {
1997
+ function isSanitizeStrippedConstruct(value) {
1819
1998
  const v = value.trim();
1820
- return v.startsWith("<!--") && v.endsWith("-->") || v.startsWith("<?") && v.endsWith("?>") || v.startsWith("<![CDATA[") && v.endsWith("]]>") || /^<![A-Za-z]/.test(v) && v.endsWith(">");
1999
+ return v.startsWith("<!--") && v.endsWith("-->") || v.startsWith("<?") && v.endsWith("?>") || v.startsWith("<![CDATA[") && v.endsWith("]]>") || /^<![A-Za-z]/.test(v) && !/^<!doctype/i.test(v) && v.endsWith(">");
2000
+ }
2001
+ function isExactSanitizeStrippedConstruct(value) {
2002
+ if (value.startsWith("<!--")) {
2003
+ if (value.length < 7 || !value.endsWith("-->")) return false;
2004
+ const close = value.indexOf("-->", 4);
2005
+ const bangClose = value.indexOf("--!>", 4);
2006
+ if (close !== value.length - 3) return false;
2007
+ return bangClose === -1 || bangClose > close;
2008
+ }
2009
+ if (/^<!doctype/i.test(value)) return false;
2010
+ if (value.startsWith("<?") || /^<!(?:[A-Za-z]|\[CDATA\[)/.test(value)) {
2011
+ return value.indexOf(">") === value.length - 1;
2012
+ }
2013
+ return false;
1821
2014
  }
1822
2015
  function isSeparatorText(node) {
1823
2016
  return node.type === "text" && node.position === void 0 && node.value.trim() === "";
@@ -4983,7 +5176,6 @@ function createRemendPreprocessor(options) {
4983
5176
  collectDefLabels,
4984
5177
  computeFreezeBoundary,
4985
5178
  createDefLabelScanner,
4986
- createFile,
4987
5179
  createIncrementalLatexPreprocessor,
4988
5180
  createProcessor,
4989
5181
  createRegistry,
@@ -5000,10 +5192,8 @@ function createRemendPreprocessor(options) {
5000
5192
  hasLoneSurrogate,
5001
5193
  highlight,
5002
5194
  isFootnoteSection,
5003
- isWhitespaceText,
5004
5195
  lastMeaningfulIdx,
5005
5196
  measureStage,
5006
- mergeClassNameAllowlist,
5007
5197
  normalizeForMatch,
5008
5198
  normalizeId,
5009
5199
  pangu,
@@ -5019,7 +5209,6 @@ function createRemendPreprocessor(options) {
5019
5209
  shortenDocumentId,
5020
5210
  smartypants,
5021
5211
  sourceIdFromFootnoteLiId,
5022
- splitByProtectedRegions,
5023
5212
  subscribeStageTimings,
5024
5213
  transformStage,
5025
5214
  withDefs