@ai-react-markdown/engine 2.7.0 → 2.8.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.
package/dist/index.cjs CHANGED
@@ -519,11 +519,80 @@ var RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set([
519
519
  // (oracle review of the r2 batch; regression caught before release).
520
520
  "plaintext"
521
521
  ]);
522
+ var P5_MARKUP_RE = /<[!/?A-Za-z]/;
522
523
  var TYPE6_START_RE = /^<\/?([A-Za-z][A-Za-z0-9-]*)(?:[ \t\r]|\/?>|$)/;
523
524
  var TYPE1_START_RE = /^<(script|pre|style|textarea)(?:[ \t\r]|>|$)/i;
524
525
  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];
526
+ var isSpaceTab = (c) => c === 32 || c === 9;
527
+ var isAsciiAlpha = (c) => c >= 65 && c <= 90 || c >= 97 && c <= 122;
528
+ var isAlnum = (c) => isAsciiAlpha(c) || c >= 48 && c <= 57;
529
+ var isAttrNameRest = (c) => isAlnum(c) || c === 45 || c === 46 || c === 58 || c === 95;
530
+ var isUnquotedExit = (c) => Number.isNaN(c) || c === 34 || c === 39 || c === 47 || c === 60 || c === 61 || c === 62 || c === 96 || isSpaceTab(c);
531
+ var completeOpenTagRest = (t, from) => {
532
+ let i = from;
533
+ for (; ; ) {
534
+ const c = t.charCodeAt(i);
535
+ if (c === 47) {
536
+ i += 1;
537
+ break;
538
+ }
539
+ if (isSpaceTab(c)) {
540
+ i += 1;
541
+ continue;
542
+ }
543
+ if (!(c === 58 || c === 95 || isAsciiAlpha(c))) break;
544
+ i += 1;
545
+ while (isAttrNameRest(t.charCodeAt(i))) i += 1;
546
+ for (; ; ) {
547
+ while (isSpaceTab(t.charCodeAt(i))) i += 1;
548
+ if (t.charCodeAt(i) !== 61) break;
549
+ i += 1;
550
+ while (isSpaceTab(t.charCodeAt(i))) i += 1;
551
+ const v = t.charCodeAt(i);
552
+ if (Number.isNaN(v) || v === 60 || v === 61 || v === 62 || v === 96) return -1;
553
+ if (v === 34 || v === 39) {
554
+ i += 1;
555
+ while (t.charCodeAt(i) !== v) {
556
+ if (i >= t.length) return -1;
557
+ i += 1;
558
+ }
559
+ i += 1;
560
+ const a = t.charCodeAt(i);
561
+ if (!(a === 47 || a === 62 || isSpaceTab(a))) return -1;
562
+ break;
563
+ }
564
+ while (!isUnquotedExit(t.charCodeAt(i))) i += 1;
565
+ }
566
+ }
567
+ return t.charCodeAt(i) === 62 ? i + 1 : -1;
568
+ };
569
+ var isType7Line = (line) => {
570
+ const cr = line.indexOf("\r");
571
+ const t = cr === -1 ? line : line.slice(0, cr);
572
+ if (t.charCodeAt(0) !== 60) return false;
573
+ let i = 1;
574
+ const closing = t.charCodeAt(i) === 47;
575
+ if (closing) i += 1;
576
+ if (!isAsciiAlpha(t.charCodeAt(i))) return false;
577
+ const nameStart = i;
578
+ i += 1;
579
+ while (isAlnum(t.charCodeAt(i)) || t.charCodeAt(i) === 45) i += 1;
580
+ const c = t.charCodeAt(i);
581
+ if (!(Number.isNaN(c) || c === 47 || c === 62 || isSpaceTab(c))) return false;
582
+ const name = t.slice(nameStart, i).toLowerCase();
583
+ if (!closing && c !== 47 && TYPE1_NAMES.has(name)) return false;
584
+ if (TYPE6_NAMES.has(name)) return false;
585
+ if (closing) {
586
+ while (isSpaceTab(t.charCodeAt(i))) i += 1;
587
+ if (t.charCodeAt(i) !== 62) return false;
588
+ i += 1;
589
+ } else {
590
+ i = completeOpenTagRest(t, i);
591
+ if (i === -1) return false;
592
+ }
593
+ while (isSpaceTab(t.charCodeAt(i))) i += 1;
594
+ return i >= t.length;
595
+ };
527
596
  var mdHtml = (b, type) => b.kind === "html" && b.type === type;
528
597
  var mdHtml25 = (b) => b.kind === "html" && b.type >= 2 && b.type <= 5;
529
598
  var commentEitherOpen = (md, p5) => mdHtml(md, 2) || p5.kind === "comment";
@@ -546,6 +615,10 @@ var VOID_TAGS = /* @__PURE__ */ new Set([
546
615
  "wbr"
547
616
  ]);
548
617
  var LIST_MARKER_RE = /^ {0,3}(?:[-*+]|\d{1,9}[.)])(?:[ \t]|$)/;
618
+ var ATX_HEADING_RE = /^#{1,6}(?:[ \t]|$)/;
619
+ var THEMATIC_BREAK_RE = /^(?:(?:-[ \t]*){3,}|(?:\*[ \t]*){3,}|(?:_[ \t]*){3,})$/;
620
+ var BARE_MARKER_RE = /^(?:[-*+]|\d{1,9}[.)])[ \t]*$/;
621
+ var SETEXT_LEFTOVER_RE = /^(?:=+|--)[ \t]*$/;
549
622
  var DEF_LIST_DD_RE = /^ {0,3}:[ \t]/;
550
623
  var FENCE_RE = /^ {0,3}(`{3,}|~{3,})/;
551
624
  var MATH_RUN_RE = /^ {0,3}(\$\$+)/;
@@ -655,9 +728,10 @@ function freshCheckpoint(defListEnabled, mathFlow, referenceTaint) {
655
728
  // doc start counts as a block start
656
729
  prevLineWasText: false,
657
730
  prevLineWasValidDef: false,
731
+ prevLineOpenContent: false,
732
+ tableMaybeOpen: false,
658
733
  paragraphHasUnpairedRun: false,
659
734
  openBracket: null,
660
- mayBeRawToMicromark: false,
661
735
  p5SealPending: false,
662
736
  phasePoisonedAt: Infinity,
663
737
  pendingTruncatedTags: [],
@@ -785,7 +859,7 @@ function processConfirmedLine(cp, ln, text) {
785
859
  newest.defListSettled = ln.blank ? true : !canBecomeDdLine(ln.text, true);
786
860
  }
787
861
  const isBlockStart = cp.prevLineBlank;
788
- if (cp.p5SealPending && !ln.blank && !cp.mayBeRawToMicromark && !(mdHtml25(cp.mdBlock) || cp.p5Tok.kind === "comment" || cp.p5Tok.kind === "bogus")) {
862
+ if (cp.p5SealPending && !ln.blank && cp.mdBlock.kind !== "html" && !(cp.p5Tok.kind === "comment" || cp.p5Tok.kind === "bogus")) {
789
863
  const defShapedLine = DEF_RE.test(ln.text) || FOOTNOTE_DEF_RE.test(ln.text);
790
864
  const commentOnly = ln.text.replace(/<!--[\s\S]*?-->/g, " ").replace(/<!--[\s\S]*$/, " ").replace(/[ \t\r]/g, "") === "";
791
865
  if (!defShapedLine && !commentOnly) {
@@ -798,9 +872,13 @@ function processConfirmedLine(cp, ln, text) {
798
872
  const applyTag = (tag, closing) => {
799
873
  if (inRawTextTok(cp.p5Tok)) {
800
874
  if (cp.p5Tok.kind === "script" && cp.p5Tok.escaped && !closing && tag === "script") {
801
- cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
875
+ cp.p5Tok = { ...cp.p5Tok, double: true };
802
876
  }
803
877
  if (!(closing && tag === rawTextElement(cp.p5Tok))) return;
878
+ if (cp.p5Tok.kind === "script" && cp.p5Tok.double) {
879
+ cp.p5Tok = { ...cp.p5Tok, double: false };
880
+ return;
881
+ }
804
882
  cp.p5Tok = { kind: "data" };
805
883
  } else {
806
884
  if (!closing && RAW_TEXT_ELEMENTS.has(tag) && foreignRawTextSwitchUnknowable()) {
@@ -808,7 +886,7 @@ function processConfirmedLine(cp, ln, text) {
808
886
  } else if (!closing && RAW_TEXT_ELEMENTS.has(tag)) {
809
887
  if (cp.p5Tok.kind !== "data") cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
810
888
  const openedInline = cp.mdBlock.kind !== "html";
811
- cp.p5Tok = tag === "script" ? { kind: "script", escaped: false, openedInline } : { kind: "rawText", element: tag, openedInline };
889
+ cp.p5Tok = tag === "script" ? { kind: "script", escaped: false, double: false, openedInline } : { kind: "rawText", element: tag, openedInline };
812
890
  if (tag === "plaintext") cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
813
891
  }
814
892
  }
@@ -839,6 +917,11 @@ function processConfirmedLine(cp, ln, text) {
839
917
  const definitelyInsideTable = () => (cp.tagBalance.get("table") ?? 0) > 0;
840
918
  const strayTablePart = (tag) => TABLE_PART_NAMES.has(tag) && !definitelyInsideTable();
841
919
  const commentOpenAtLineStart = commentEitherOpen(cp.mdBlock, cp.p5Tok);
920
+ const bothCommentsOpenAtLineStart = mdHtml(cp.mdBlock, 2) && cp.p5Tok.kind === "comment";
921
+ const inDivergenceWindow = mdHtml(cp.mdBlock, 2) && cp.p5Tok.kind !== "comment" || (mdHtml(cp.mdBlock, 3) || mdHtml(cp.mdBlock, 5)) && cp.p5Tok.kind !== "bogus";
922
+ if (inDivergenceWindow && P5_MARKUP_RE.test(ln.text)) {
923
+ cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
924
+ }
842
925
  const rawOpenAtLineStart = mdHtml25(cp.mdBlock) || cp.p5Tok.kind === "comment" || cp.p5Tok.kind === "bogus";
843
926
  if (cp.mdBlock.kind === "fence") {
844
927
  const close = FENCE_RE.exec(ln.text);
@@ -851,12 +934,14 @@ function processConfirmedLine(cp, ln, text) {
851
934
  cp.prevLineBlank = false;
852
935
  cp.prevLineWasText = false;
853
936
  cp.prevLineWasValidDef = false;
937
+ cp.prevLineOpenContent = false;
938
+ cp.tableMaybeOpen = false;
854
939
  return;
855
940
  }
856
941
  if (cp.mdBlock.kind !== "math" && !rawOpenAtLineStart) {
857
942
  const open = FENCE_RE.exec(ln.text);
858
943
  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) {
944
+ if (open && !bogusInfo && cp.mdBlock.kind === "html") {
860
945
  cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
861
946
  } else if (open && !bogusInfo) {
862
947
  if (isBlockStart) {
@@ -870,6 +955,8 @@ function processConfirmedLine(cp, ln, text) {
870
955
  cp.prevLineBlank = false;
871
956
  cp.prevLineWasText = false;
872
957
  cp.prevLineWasValidDef = false;
958
+ cp.prevLineOpenContent = false;
959
+ cp.tableMaybeOpen = false;
873
960
  return;
874
961
  }
875
962
  }
@@ -884,13 +971,15 @@ function processConfirmedLine(cp, ln, text) {
884
971
  cp.prevLineBlank = false;
885
972
  cp.prevLineWasText = false;
886
973
  cp.prevLineWasValidDef = false;
974
+ cp.prevLineOpenContent = false;
975
+ cp.tableMaybeOpen = false;
887
976
  return;
888
977
  }
889
978
  const mathRun = cp.mathFlow && !rawOpenAtLineStart ? MATH_RUN_RE.exec(ln.text) : null;
890
979
  if (mathRun) {
891
980
  const rest = ln.text.slice(ln.text.indexOf(mathRun[1]) + mathRun[1].length);
892
981
  if (!rest.includes("$")) {
893
- if (cp.mayBeRawToMicromark) {
982
+ if (cp.mdBlock.kind === "html") {
894
983
  cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
895
984
  } else {
896
985
  if (isBlockStart) {
@@ -904,6 +993,8 @@ function processConfirmedLine(cp, ln, text) {
904
993
  cp.prevLineBlank = false;
905
994
  cp.prevLineWasText = false;
906
995
  cp.prevLineWasValidDef = false;
996
+ cp.prevLineOpenContent = false;
997
+ cp.tableMaybeOpen = false;
907
998
  return;
908
999
  }
909
1000
  }
@@ -919,8 +1010,11 @@ function processConfirmedLine(cp, ln, text) {
919
1010
  }
920
1011
  cp.pendingTag = null;
921
1012
  if (cp.p5Tok.kind === "bogus") {
922
- cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
923
- cp.p5Tok = { kind: "data" };
1013
+ if (mdHtml25(cp.mdBlock)) {
1014
+ } else {
1015
+ cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
1016
+ cp.p5Tok = { kind: "data" };
1017
+ }
924
1018
  }
925
1019
  if (cp.mdBlock.kind === "html" && cp.mdBlock.type >= 6) cp.mdBlock = { kind: "none" };
926
1020
  cp.blankRun += 1;
@@ -942,7 +1036,6 @@ function processConfirmedLine(cp, ln, text) {
942
1036
  cp.paragraphHasUnpairedRun = false;
943
1037
  cp.openBracket = null;
944
1038
  if (!mdHtml(cp.mdBlock, 1)) {
945
- cp.mayBeRawToMicromark = false;
946
1039
  if (inRawTextTok(cp.p5Tok) && !cp.p5Tok.openedInline) {
947
1040
  cp.phasePoisonedAt = 0;
948
1041
  }
@@ -950,6 +1043,8 @@ function processConfirmedLine(cp, ln, text) {
950
1043
  cp.prevLineBlank = true;
951
1044
  cp.prevLineWasText = false;
952
1045
  cp.prevLineWasValidDef = false;
1046
+ cp.prevLineOpenContent = false;
1047
+ cp.tableMaybeOpen = false;
953
1048
  return;
954
1049
  }
955
1050
  if (isBlockStart) {
@@ -961,24 +1056,29 @@ function processConfirmedLine(cp, ln, text) {
961
1056
  const tagStart = ln.indent <= 3 ? /^<\/?([A-Za-z][A-Za-z0-9-]*)/.exec(mdTrimStart(ln.text)) : null;
962
1057
  if (tagStart) {
963
1058
  const noRealBlockOpen = cp.mdBlock.kind !== "html";
964
- cp.mayBeRawToMicromark = true;
965
1059
  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;
967
1060
  if (cp.mdBlock.kind !== "html") {
968
1061
  const t = mdTrimStart(ln.text);
969
1062
  const t6 = TYPE6_START_RE.exec(t);
970
- const t7 = TYPE7_LINE_RE.exec(t);
971
1063
  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())) {
1064
+ if (realT6 || TYPE1_START_RE.test(t) || // Type 7 cannot interrupt CONTENT (micromark's paragraph/definition
1065
+ // construct `prevLineOpenContent`, the exact interrupt input; the
1066
+ // old `prevLineWasText` gate refused after headings, terminator
1067
+ // lines and fence closes, where micromark measurably opens). The
1068
+ // classifier itself is exact too (isType7Line) — including closing
1069
+ // raw-text names (`</style>` alone is type 7, measured) and
1070
+ // quoted-`>` attribute values.
1071
+ !cp.prevLineOpenContent && isType7Line(t)) {
975
1072
  if (cp.mdBlock.kind === "none") cp.mdBlock = { kind: "html", type: realT6 ? 6 : 7 };
1073
+ } else if (cp.prevLineOpenContent && cp.tableMaybeOpen && isType7Line(t)) {
1074
+ cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
976
1075
  }
977
1076
  }
978
1077
  }
979
- const inRawText = cp.mayBeRawToMicromark || rawOpenAtLineStart;
980
1078
  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);
1079
+ const htmlOwnedLine = cp.mdBlock.kind === "html" || rawOpenAtLineStart || rawFlowStart;
1080
+ const maskingSuppressed = htmlOwnedLine || inRawTextTok(cp.p5Tok);
1081
+ const { masked, unpaired } = maskingSuppressed ? { masked: null, unpaired: false } : maskIntraLineCodeSpans(ln.text, cp.paragraphHasUnpairedRun);
982
1082
  if (unpaired) cp.paragraphHasUnpairedRun = true;
983
1083
  const scanText = masked ?? ln.text;
984
1084
  const defRawToMicromark = cp.mdBlock.kind === "html" || rawOpenAtLineStart || inRawTextTok(cp.p5Tok);
@@ -990,30 +1090,27 @@ function processConfirmedLine(cp, ln, text) {
990
1090
  };
991
1091
  let inlineRawOpenerIdx = -1;
992
1092
  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;
1093
+ if (mdHtml(cp.mdBlock, 3) || mdHtml(cp.mdBlock, 5)) {
1094
+ const isPi = mdHtml(cp.mdBlock, 3);
1095
+ const term = isPi ? "?>" : "]]>";
1096
+ const c = scanText.indexOf(term, pos);
1097
+ const mdEnd = c === -1 ? scanText.length : c + term.length;
1098
+ if (cp.p5Tok.kind === "bogus") {
1099
+ const gt = scanText.indexOf(">", pos);
1100
+ if (gt !== -1 && (c === -1 || gt !== c + term.length - 1)) {
1101
+ cp.p5Tok = { kind: "data" };
1102
+ rawSpans.push([pos, gt + 1]);
1103
+ if (P5_MARKUP_RE.test(scanText.slice(gt + 1, c === -1 ? scanText.length : c))) {
1104
+ poisonRawDivergence();
1105
+ }
1106
+ } else {
1107
+ rawSpans.push([pos, mdEnd]);
1108
+ if (c !== -1) cp.p5Tok = { kind: "data" };
1109
+ }
1013
1110
  }
1014
- rawSpans.push([pos, c + 3]);
1111
+ if (c === -1) break;
1015
1112
  cp.mdBlock = { kind: "none" };
1016
- pos = c + 3;
1113
+ pos = mdEnd;
1017
1114
  continue;
1018
1115
  }
1019
1116
  if (mdHtml(cp.mdBlock, 4)) {
@@ -1056,7 +1153,8 @@ function processConfirmedLine(cp, ln, text) {
1056
1153
  pos = bogus + 2;
1057
1154
  } else if (first === cd) {
1058
1155
  rawSpans.push([cd, cd + 9]);
1059
- cp.mdBlock = { kind: "html", type: 5 };
1156
+ if (cp.mdBlock.kind === "none") cp.mdBlock = { kind: "html", type: 5 };
1157
+ if (cp.p5Tok.kind === "data") cp.p5Tok = { kind: "bogus" };
1060
1158
  if (!isMdBlank(scanText.slice(0, cd)) || ln.indent > 3) inlineRawOpenerIdx = cd;
1061
1159
  pos = cd + 9;
1062
1160
  } else if (first === pi) {
@@ -1067,13 +1165,15 @@ function processConfirmedLine(cp, ln, text) {
1067
1165
  continue;
1068
1166
  }
1069
1167
  rawSpans.push([pi, pi + 2]);
1070
- cp.mdBlock = { kind: "html", type: 3 };
1168
+ if (cp.mdBlock.kind === "none") cp.mdBlock = { kind: "html", type: 3 };
1169
+ if (cp.p5Tok.kind === "data") cp.p5Tok = { kind: "bogus" };
1071
1170
  if (!isMdBlank(scanText.slice(0, pi)) || ln.indent > 3) inlineRawOpenerIdx = pi;
1072
1171
  pos = pi + 2;
1073
1172
  } else {
1074
1173
  rawSpans.push([decl, decl + 2]);
1075
1174
  if (ln.indent <= 3 && /^doctype/i.test(scanText.slice(decl + 2))) cp.phasePoisonedAt = 0;
1076
- cp.mdBlock = { kind: "html", type: 4 };
1175
+ if (cp.mdBlock.kind === "none") cp.mdBlock = { kind: "html", type: 4 };
1176
+ if (cp.p5Tok.kind === "data") cp.p5Tok = { kind: "bogus" };
1077
1177
  if (!isMdBlank(scanText.slice(0, decl)) || ln.indent > 3) inlineRawOpenerIdx = decl;
1078
1178
  pos = decl + 2;
1079
1179
  }
@@ -1110,7 +1210,7 @@ function processConfirmedLine(cp, ln, text) {
1110
1210
  if (inRawTextTok(cp.p5Tok) && (m[0] === "<!--" || m[0] === "-->" || m[0] === "--!>")) {
1111
1211
  if (cp.p5Tok.kind === "script") {
1112
1212
  if (m[0] === "<!--") cp.p5Tok = { ...cp.p5Tok, escaped: true };
1113
- if (m[0] === "-->") cp.p5Tok = { ...cp.p5Tok, escaped: false };
1213
+ if (m[0] === "-->") cp.p5Tok = { ...cp.p5Tok, escaped: false, double: false };
1114
1214
  }
1115
1215
  continue;
1116
1216
  }
@@ -1122,7 +1222,9 @@ function processConfirmedLine(cp, ln, text) {
1122
1222
  if (cp.p5Tok.kind === "comment") cp.p5Tok = { kind: "data" };
1123
1223
  } else if (next === "!>" || next === "-!") {
1124
1224
  if (cp.p5Tok.kind === "comment") cp.p5Tok = { kind: "data" };
1125
- poisonRawDivergence();
1225
+ if (mdHtml(cp.mdBlock, 2) && P5_MARKUP_RE.test(tagText.slice(m.index + m[0].length))) {
1226
+ poisonRawDivergence();
1227
+ }
1126
1228
  }
1127
1229
  continue;
1128
1230
  }
@@ -1140,7 +1242,9 @@ function processConfirmedLine(cp, ln, text) {
1140
1242
  continue;
1141
1243
  }
1142
1244
  if (m[0] === "--!>") {
1143
- if (commentEitherOpen(cp.mdBlock, cp.p5Tok)) poisonRawDivergence();
1245
+ if (mdHtml(cp.mdBlock, 2) && P5_MARKUP_RE.test(tagText.slice(m.index + m[0].length))) {
1246
+ poisonRawDivergence();
1247
+ }
1144
1248
  if (cp.p5Tok.kind === "comment") cp.p5Tok = { kind: "data" };
1145
1249
  continue;
1146
1250
  }
@@ -1231,14 +1335,16 @@ function processConfirmedLine(cp, ln, text) {
1231
1335
  applyTag(tag, closing);
1232
1336
  const rawLastLt = ln.text.lastIndexOf("<");
1233
1337
  const rawTruncated = rawLastLt !== -1 && !ln.text.includes(">", rawLastLt);
1234
- if (!closing && !inRawText && rawTruncated) cp.pendingTruncatedTags.push(tag);
1338
+ if (!closing && !(htmlOwnedLine || inRawTextTok(cp.p5Tok)) && rawTruncated) {
1339
+ cp.pendingTruncatedTags.push(tag);
1340
+ }
1235
1341
  }
1236
1342
  }
1237
1343
  }
1238
1344
  }
1239
1345
  }
1240
1346
  const effectiveOpen = cp.openTotal - cp.pendingTruncatedTags.length;
1241
- if ((inRawText || rawFlowStart) && effectiveOpen <= 0) {
1347
+ if ((htmlOwnedLine || inRawTextTok(cp.p5Tok)) && effectiveOpen <= 0) {
1242
1348
  let masked2 = "";
1243
1349
  let cursor = 0;
1244
1350
  for (const [from, to] of rawSpans) {
@@ -1246,7 +1352,7 @@ function processConfirmedLine(cp, ln, text) {
1246
1352
  cursor = to;
1247
1353
  }
1248
1354
  masked2 += scanText.slice(cursor);
1249
- if (floatingResidue(masked2, commentOpenAtLineStart).length > 0) {
1355
+ if (floatingResidue(masked2, bothCommentsOpenAtLineStart).length > 0) {
1250
1356
  cp.p5SealPending = true;
1251
1357
  }
1252
1358
  }
@@ -1255,11 +1361,28 @@ function processConfirmedLine(cp, ln, text) {
1255
1361
  }
1256
1362
  if (mdHtml(cp.mdBlock, 1) && TYPE1_CLOSE_RE.test(ln.text)) {
1257
1363
  cp.mdBlock = { kind: "none" };
1258
- cp.mayBeRawToMicromark = false;
1364
+ if (inRawTextTok(cp.p5Tok)) cp.phasePoisonedAt = 0;
1259
1365
  }
1260
1366
  cp.blankRun = 0;
1261
1367
  cp.prevLineBlank = false;
1262
1368
  cp.prevLineWasText = true;
1369
+ {
1370
+ const tt = mdTrimStart(ln.text);
1371
+ let openContent;
1372
+ if (htmlOwnedLine) {
1373
+ openContent = false;
1374
+ } else if (ln.indent >= 4) {
1375
+ openContent = cp.prevLineOpenContent;
1376
+ } else if (ATX_HEADING_RE.test(tt) || THEMATIC_BREAK_RE.test(tt) || BARE_MARKER_RE.test(tt)) {
1377
+ openContent = false;
1378
+ } else if (SETEXT_LEFTOVER_RE.test(tt)) {
1379
+ openContent = !cp.prevLineOpenContent;
1380
+ } else {
1381
+ openContent = true;
1382
+ }
1383
+ cp.prevLineOpenContent = openContent;
1384
+ cp.tableMaybeOpen = ln.text.includes("|") || cp.tableMaybeOpen && openContent;
1385
+ }
1263
1386
  cp.prevLineWasValidDef = validLinkDef;
1264
1387
  }
1265
1388
 
@@ -1509,6 +1632,33 @@ function headRoutedCaptureUnclosed(values) {
1509
1632
  return !new RegExp(`</${name}(?=[\\s/>])`, "i").test(after);
1510
1633
  }
1511
1634
  var STRAY_SYNTHESIZED_END_TAG_RE = /<\/(?:br|p)\b/i;
1635
+ var RAW_TEXT_OPEN_RE = /<(script|style|textarea|title|xmp|iframe|noembed|noframes|plaintext)(?=[\s/>])/gi;
1636
+ function rawTextRegionCrossesOut(values) {
1637
+ for (const value of values) {
1638
+ let pos = 0;
1639
+ for (; ; ) {
1640
+ RAW_TEXT_OPEN_RE.lastIndex = pos;
1641
+ const open = RAW_TEXT_OPEN_RE.exec(value);
1642
+ if (open === null) break;
1643
+ const name = open[1].toLowerCase();
1644
+ const bodyStart = open.index + open[0].length;
1645
+ const closeRe = new RegExp(`</${name}(?=[\\s/>])`, "ig");
1646
+ closeRe.lastIndex = bodyStart;
1647
+ let close = closeRe.exec(value);
1648
+ if (name === "script") {
1649
+ while (close !== null) {
1650
+ const body = value.slice(bodyStart, close.index);
1651
+ const lastOpen = body.lastIndexOf("<!--");
1652
+ if (lastOpen === -1 || body.indexOf("-->", lastOpen + 4) !== -1) break;
1653
+ close = closeRe.exec(value);
1654
+ }
1655
+ }
1656
+ if (close === null) return true;
1657
+ pos = close.index + close[0].length;
1658
+ }
1659
+ }
1660
+ return false;
1661
+ }
1512
1662
  function spliceTrees(input) {
1513
1663
  const { prevMdast, prevHast, tailMdast, tailHast, content, boundary, injectionPrefix, injectedSegments } = input;
1514
1664
  const injectedLen = injectionPrefix.length;
@@ -1565,7 +1715,9 @@ function spliceTrees(input) {
1565
1715
  return !(start !== void 0 && start < injectedLen);
1566
1716
  });
1567
1717
  const tailWrapVisible = tailMdastChildren.some((child) => !isWrapInvisible(child));
1568
- if (hasStrayTablePart(prefixMdast.flatMap((c) => c.type === "html" ? [c.value] : []))) return null;
1718
+ const prefixHtmlValues = prefixMdast.flatMap((c) => c.type === "html" ? [c.value] : []);
1719
+ if (hasStrayTablePart(prefixHtmlValues)) return null;
1720
+ if (rawTextRegionCrossesOut(prefixHtmlValues)) return null;
1569
1721
  const leadingHtml = [];
1570
1722
  for (const child of tailMdastChildren) {
1571
1723
  if (isWrapInvisible(child)) continue;