@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.js CHANGED
@@ -426,11 +426,80 @@ var RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set([
426
426
  // (oracle review of the r2 batch; regression caught before release).
427
427
  "plaintext"
428
428
  ]);
429
+ var P5_MARKUP_RE = /<[!/?A-Za-z]/;
429
430
  var TYPE6_START_RE = /^<\/?([A-Za-z][A-Za-z0-9-]*)(?:[ \t\r]|\/?>|$)/;
430
431
  var TYPE1_START_RE = /^<(script|pre|style|textarea)(?:[ \t\r]|>|$)/i;
431
432
  var TYPE1_CLOSE_RE = /<\/(?:script|pre|style|textarea)>/i;
432
- var TYPE7_LINE_RE = /^(?:<[A-Za-z][A-Za-z0-9-]*(?:[ \t\r][^>]*|\/)?>|<\/[A-Za-z][A-Za-z0-9-]*[ \t\r]*>)[ \t\r]*$/;
433
- var t7Name = (line) => /^<\/?([A-Za-z][A-Za-z0-9-]*)/.exec(line)[1];
433
+ var isSpaceTab = (c) => c === 32 || c === 9;
434
+ var isAsciiAlpha = (c) => c >= 65 && c <= 90 || c >= 97 && c <= 122;
435
+ var isAlnum = (c) => isAsciiAlpha(c) || c >= 48 && c <= 57;
436
+ var isAttrNameRest = (c) => isAlnum(c) || c === 45 || c === 46 || c === 58 || c === 95;
437
+ var isUnquotedExit = (c) => Number.isNaN(c) || c === 34 || c === 39 || c === 47 || c === 60 || c === 61 || c === 62 || c === 96 || isSpaceTab(c);
438
+ var completeOpenTagRest = (t, from) => {
439
+ let i = from;
440
+ for (; ; ) {
441
+ const c = t.charCodeAt(i);
442
+ if (c === 47) {
443
+ i += 1;
444
+ break;
445
+ }
446
+ if (isSpaceTab(c)) {
447
+ i += 1;
448
+ continue;
449
+ }
450
+ if (!(c === 58 || c === 95 || isAsciiAlpha(c))) break;
451
+ i += 1;
452
+ while (isAttrNameRest(t.charCodeAt(i))) i += 1;
453
+ for (; ; ) {
454
+ while (isSpaceTab(t.charCodeAt(i))) i += 1;
455
+ if (t.charCodeAt(i) !== 61) break;
456
+ i += 1;
457
+ while (isSpaceTab(t.charCodeAt(i))) i += 1;
458
+ const v = t.charCodeAt(i);
459
+ if (Number.isNaN(v) || v === 60 || v === 61 || v === 62 || v === 96) return -1;
460
+ if (v === 34 || v === 39) {
461
+ i += 1;
462
+ while (t.charCodeAt(i) !== v) {
463
+ if (i >= t.length) return -1;
464
+ i += 1;
465
+ }
466
+ i += 1;
467
+ const a = t.charCodeAt(i);
468
+ if (!(a === 47 || a === 62 || isSpaceTab(a))) return -1;
469
+ break;
470
+ }
471
+ while (!isUnquotedExit(t.charCodeAt(i))) i += 1;
472
+ }
473
+ }
474
+ return t.charCodeAt(i) === 62 ? i + 1 : -1;
475
+ };
476
+ var isType7Line = (line) => {
477
+ const cr = line.indexOf("\r");
478
+ const t = cr === -1 ? line : line.slice(0, cr);
479
+ if (t.charCodeAt(0) !== 60) return false;
480
+ let i = 1;
481
+ const closing = t.charCodeAt(i) === 47;
482
+ if (closing) i += 1;
483
+ if (!isAsciiAlpha(t.charCodeAt(i))) return false;
484
+ const nameStart = i;
485
+ i += 1;
486
+ while (isAlnum(t.charCodeAt(i)) || t.charCodeAt(i) === 45) i += 1;
487
+ const c = t.charCodeAt(i);
488
+ if (!(Number.isNaN(c) || c === 47 || c === 62 || isSpaceTab(c))) return false;
489
+ const name = t.slice(nameStart, i).toLowerCase();
490
+ if (!closing && c !== 47 && TYPE1_NAMES.has(name)) return false;
491
+ if (TYPE6_NAMES.has(name)) return false;
492
+ if (closing) {
493
+ while (isSpaceTab(t.charCodeAt(i))) i += 1;
494
+ if (t.charCodeAt(i) !== 62) return false;
495
+ i += 1;
496
+ } else {
497
+ i = completeOpenTagRest(t, i);
498
+ if (i === -1) return false;
499
+ }
500
+ while (isSpaceTab(t.charCodeAt(i))) i += 1;
501
+ return i >= t.length;
502
+ };
434
503
  var mdHtml = (b, type) => b.kind === "html" && b.type === type;
435
504
  var mdHtml25 = (b) => b.kind === "html" && b.type >= 2 && b.type <= 5;
436
505
  var commentEitherOpen = (md, p5) => mdHtml(md, 2) || p5.kind === "comment";
@@ -453,6 +522,10 @@ var VOID_TAGS = /* @__PURE__ */ new Set([
453
522
  "wbr"
454
523
  ]);
455
524
  var LIST_MARKER_RE = /^ {0,3}(?:[-*+]|\d{1,9}[.)])(?:[ \t]|$)/;
525
+ var ATX_HEADING_RE = /^#{1,6}(?:[ \t]|$)/;
526
+ var THEMATIC_BREAK_RE = /^(?:(?:-[ \t]*){3,}|(?:\*[ \t]*){3,}|(?:_[ \t]*){3,})$/;
527
+ var BARE_MARKER_RE = /^(?:[-*+]|\d{1,9}[.)])[ \t]*$/;
528
+ var SETEXT_LEFTOVER_RE = /^(?:=+|--)[ \t]*$/;
456
529
  var DEF_LIST_DD_RE = /^ {0,3}:[ \t]/;
457
530
  var FENCE_RE = /^ {0,3}(`{3,}|~{3,})/;
458
531
  var MATH_RUN_RE = /^ {0,3}(\$\$+)/;
@@ -562,9 +635,10 @@ function freshCheckpoint(defListEnabled, mathFlow, referenceTaint) {
562
635
  // doc start counts as a block start
563
636
  prevLineWasText: false,
564
637
  prevLineWasValidDef: false,
638
+ prevLineOpenContent: false,
639
+ tableMaybeOpen: false,
565
640
  paragraphHasUnpairedRun: false,
566
641
  openBracket: null,
567
- mayBeRawToMicromark: false,
568
642
  p5SealPending: false,
569
643
  phasePoisonedAt: Infinity,
570
644
  pendingTruncatedTags: [],
@@ -692,7 +766,7 @@ function processConfirmedLine(cp, ln, text) {
692
766
  newest.defListSettled = ln.blank ? true : !canBecomeDdLine(ln.text, true);
693
767
  }
694
768
  const isBlockStart = cp.prevLineBlank;
695
- if (cp.p5SealPending && !ln.blank && !cp.mayBeRawToMicromark && !(mdHtml25(cp.mdBlock) || cp.p5Tok.kind === "comment" || cp.p5Tok.kind === "bogus")) {
769
+ if (cp.p5SealPending && !ln.blank && cp.mdBlock.kind !== "html" && !(cp.p5Tok.kind === "comment" || cp.p5Tok.kind === "bogus")) {
696
770
  const defShapedLine = DEF_RE.test(ln.text) || FOOTNOTE_DEF_RE.test(ln.text);
697
771
  const commentOnly = ln.text.replace(/<!--[\s\S]*?-->/g, " ").replace(/<!--[\s\S]*$/, " ").replace(/[ \t\r]/g, "") === "";
698
772
  if (!defShapedLine && !commentOnly) {
@@ -705,9 +779,13 @@ function processConfirmedLine(cp, ln, text) {
705
779
  const applyTag = (tag, closing) => {
706
780
  if (inRawTextTok(cp.p5Tok)) {
707
781
  if (cp.p5Tok.kind === "script" && cp.p5Tok.escaped && !closing && tag === "script") {
708
- cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
782
+ cp.p5Tok = { ...cp.p5Tok, double: true };
709
783
  }
710
784
  if (!(closing && tag === rawTextElement(cp.p5Tok))) return;
785
+ if (cp.p5Tok.kind === "script" && cp.p5Tok.double) {
786
+ cp.p5Tok = { ...cp.p5Tok, double: false };
787
+ return;
788
+ }
711
789
  cp.p5Tok = { kind: "data" };
712
790
  } else {
713
791
  if (!closing && RAW_TEXT_ELEMENTS.has(tag) && foreignRawTextSwitchUnknowable()) {
@@ -715,7 +793,7 @@ function processConfirmedLine(cp, ln, text) {
715
793
  } else if (!closing && RAW_TEXT_ELEMENTS.has(tag)) {
716
794
  if (cp.p5Tok.kind !== "data") cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
717
795
  const openedInline = cp.mdBlock.kind !== "html";
718
- cp.p5Tok = tag === "script" ? { kind: "script", escaped: false, openedInline } : { kind: "rawText", element: tag, openedInline };
796
+ cp.p5Tok = tag === "script" ? { kind: "script", escaped: false, double: false, openedInline } : { kind: "rawText", element: tag, openedInline };
719
797
  if (tag === "plaintext") cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
720
798
  }
721
799
  }
@@ -746,6 +824,11 @@ function processConfirmedLine(cp, ln, text) {
746
824
  const definitelyInsideTable = () => (cp.tagBalance.get("table") ?? 0) > 0;
747
825
  const strayTablePart = (tag) => TABLE_PART_NAMES.has(tag) && !definitelyInsideTable();
748
826
  const commentOpenAtLineStart = commentEitherOpen(cp.mdBlock, cp.p5Tok);
827
+ const bothCommentsOpenAtLineStart = mdHtml(cp.mdBlock, 2) && cp.p5Tok.kind === "comment";
828
+ const inDivergenceWindow = mdHtml(cp.mdBlock, 2) && cp.p5Tok.kind !== "comment" || (mdHtml(cp.mdBlock, 3) || mdHtml(cp.mdBlock, 5)) && cp.p5Tok.kind !== "bogus";
829
+ if (inDivergenceWindow && P5_MARKUP_RE.test(ln.text)) {
830
+ cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
831
+ }
749
832
  const rawOpenAtLineStart = mdHtml25(cp.mdBlock) || cp.p5Tok.kind === "comment" || cp.p5Tok.kind === "bogus";
750
833
  if (cp.mdBlock.kind === "fence") {
751
834
  const close = FENCE_RE.exec(ln.text);
@@ -758,12 +841,14 @@ function processConfirmedLine(cp, ln, text) {
758
841
  cp.prevLineBlank = false;
759
842
  cp.prevLineWasText = false;
760
843
  cp.prevLineWasValidDef = false;
844
+ cp.prevLineOpenContent = false;
845
+ cp.tableMaybeOpen = false;
761
846
  return;
762
847
  }
763
848
  if (cp.mdBlock.kind !== "math" && !rawOpenAtLineStart) {
764
849
  const open = FENCE_RE.exec(ln.text);
765
850
  const bogusInfo = open !== null && open[1][0] === "`" && ln.text.slice(ln.text.indexOf(open[1]) + open[1].length).includes("`");
766
- if (open && !bogusInfo && cp.mayBeRawToMicromark) {
851
+ if (open && !bogusInfo && cp.mdBlock.kind === "html") {
767
852
  cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
768
853
  } else if (open && !bogusInfo) {
769
854
  if (isBlockStart) {
@@ -777,6 +862,8 @@ function processConfirmedLine(cp, ln, text) {
777
862
  cp.prevLineBlank = false;
778
863
  cp.prevLineWasText = false;
779
864
  cp.prevLineWasValidDef = false;
865
+ cp.prevLineOpenContent = false;
866
+ cp.tableMaybeOpen = false;
780
867
  return;
781
868
  }
782
869
  }
@@ -791,13 +878,15 @@ function processConfirmedLine(cp, ln, text) {
791
878
  cp.prevLineBlank = false;
792
879
  cp.prevLineWasText = false;
793
880
  cp.prevLineWasValidDef = false;
881
+ cp.prevLineOpenContent = false;
882
+ cp.tableMaybeOpen = false;
794
883
  return;
795
884
  }
796
885
  const mathRun = cp.mathFlow && !rawOpenAtLineStart ? MATH_RUN_RE.exec(ln.text) : null;
797
886
  if (mathRun) {
798
887
  const rest = ln.text.slice(ln.text.indexOf(mathRun[1]) + mathRun[1].length);
799
888
  if (!rest.includes("$")) {
800
- if (cp.mayBeRawToMicromark) {
889
+ if (cp.mdBlock.kind === "html") {
801
890
  cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
802
891
  } else {
803
892
  if (isBlockStart) {
@@ -811,6 +900,8 @@ function processConfirmedLine(cp, ln, text) {
811
900
  cp.prevLineBlank = false;
812
901
  cp.prevLineWasText = false;
813
902
  cp.prevLineWasValidDef = false;
903
+ cp.prevLineOpenContent = false;
904
+ cp.tableMaybeOpen = false;
814
905
  return;
815
906
  }
816
907
  }
@@ -826,8 +917,11 @@ function processConfirmedLine(cp, ln, text) {
826
917
  }
827
918
  cp.pendingTag = null;
828
919
  if (cp.p5Tok.kind === "bogus") {
829
- cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
830
- cp.p5Tok = { kind: "data" };
920
+ if (mdHtml25(cp.mdBlock)) {
921
+ } else {
922
+ cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
923
+ cp.p5Tok = { kind: "data" };
924
+ }
831
925
  }
832
926
  if (cp.mdBlock.kind === "html" && cp.mdBlock.type >= 6) cp.mdBlock = { kind: "none" };
833
927
  cp.blankRun += 1;
@@ -849,7 +943,6 @@ function processConfirmedLine(cp, ln, text) {
849
943
  cp.paragraphHasUnpairedRun = false;
850
944
  cp.openBracket = null;
851
945
  if (!mdHtml(cp.mdBlock, 1)) {
852
- cp.mayBeRawToMicromark = false;
853
946
  if (inRawTextTok(cp.p5Tok) && !cp.p5Tok.openedInline) {
854
947
  cp.phasePoisonedAt = 0;
855
948
  }
@@ -857,6 +950,8 @@ function processConfirmedLine(cp, ln, text) {
857
950
  cp.prevLineBlank = true;
858
951
  cp.prevLineWasText = false;
859
952
  cp.prevLineWasValidDef = false;
953
+ cp.prevLineOpenContent = false;
954
+ cp.tableMaybeOpen = false;
860
955
  return;
861
956
  }
862
957
  if (isBlockStart) {
@@ -868,24 +963,29 @@ function processConfirmedLine(cp, ln, text) {
868
963
  const tagStart = ln.indent <= 3 ? /^<\/?([A-Za-z][A-Za-z0-9-]*)/.exec(mdTrimStart(ln.text)) : null;
869
964
  if (tagStart) {
870
965
  const noRealBlockOpen = cp.mdBlock.kind !== "html";
871
- cp.mayBeRawToMicromark = true;
872
966
  if (noRealBlockOpen && TYPE1_START_RE.test(mdTrimStart(ln.text))) cp.mdBlock = { kind: "html", type: 1 };
873
- if (!TYPE6_NAMES.has(tagStart[1].toLowerCase())) cp.hazardVerdict = true;
874
967
  if (cp.mdBlock.kind !== "html") {
875
968
  const t = mdTrimStart(ln.text);
876
969
  const t6 = TYPE6_START_RE.exec(t);
877
- const t7 = TYPE7_LINE_RE.exec(t);
878
970
  const realT6 = t6 !== null && TYPE6_NAMES.has(t6[1].toLowerCase());
879
- if (realT6 || TYPE1_START_RE.test(t) || // Type 7 cannot interrupt a paragraph, and excludes the raw-text
880
- // names (those are type 1 as start tags, paragraph as end tags).
881
- t7 !== null && !cp.prevLineWasText && !TYPE1_NAMES.has(t7Name(t).toLowerCase())) {
971
+ if (realT6 || TYPE1_START_RE.test(t) || // Type 7 cannot interrupt CONTENT (micromark's paragraph/definition
972
+ // construct `prevLineOpenContent`, the exact interrupt input; the
973
+ // old `prevLineWasText` gate refused after headings, terminator
974
+ // lines and fence closes, where micromark measurably opens). The
975
+ // classifier itself is exact too (isType7Line) — including closing
976
+ // raw-text names (`</style>` alone is type 7, measured) and
977
+ // quoted-`>` attribute values.
978
+ !cp.prevLineOpenContent && isType7Line(t)) {
882
979
  if (cp.mdBlock.kind === "none") cp.mdBlock = { kind: "html", type: realT6 ? 6 : 7 };
980
+ } else if (cp.prevLineOpenContent && cp.tableMaybeOpen && isType7Line(t)) {
981
+ cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
883
982
  }
884
983
  }
885
984
  }
886
- const inRawText = cp.mayBeRawToMicromark || rawOpenAtLineStart;
887
985
  const rawFlowStart = ln.indent <= 3 && /^<(?:!--|\?|![A-Za-z]|!\[CDATA\[)/.test(mdTrimStart(ln.text));
888
- const { masked, unpaired } = inRawText ? { masked: null, unpaired: false } : maskIntraLineCodeSpans(ln.text, cp.paragraphHasUnpairedRun);
986
+ const htmlOwnedLine = cp.mdBlock.kind === "html" || rawOpenAtLineStart || rawFlowStart;
987
+ const maskingSuppressed = htmlOwnedLine || inRawTextTok(cp.p5Tok);
988
+ const { masked, unpaired } = maskingSuppressed ? { masked: null, unpaired: false } : maskIntraLineCodeSpans(ln.text, cp.paragraphHasUnpairedRun);
889
989
  if (unpaired) cp.paragraphHasUnpairedRun = true;
890
990
  const scanText = masked ?? ln.text;
891
991
  const defRawToMicromark = cp.mdBlock.kind === "html" || rawOpenAtLineStart || inRawTextTok(cp.p5Tok);
@@ -897,30 +997,27 @@ function processConfirmedLine(cp, ln, text) {
897
997
  };
898
998
  let inlineRawOpenerIdx = -1;
899
999
  while (pos < scanText.length) {
900
- if (mdHtml(cp.mdBlock, 3)) {
901
- const c = scanText.indexOf("?>", pos);
902
- const gt = scanText.indexOf(">", pos);
903
- if (gt !== -1 && (c === -1 || gt !== c + 1)) poisonRawDivergence();
904
- if (c === -1) {
905
- rawSpans.push([pos, scanText.length]);
906
- break;
907
- }
908
- rawSpans.push([pos, c + 2]);
909
- cp.mdBlock = { kind: "none" };
910
- pos = c + 2;
911
- continue;
912
- }
913
- if (mdHtml(cp.mdBlock, 5)) {
914
- const c = scanText.indexOf("]]>", pos);
915
- const gt = scanText.indexOf(">", pos);
916
- if (gt !== -1 && (c === -1 || gt !== c + 2)) poisonRawDivergence();
917
- if (c === -1) {
918
- rawSpans.push([pos, scanText.length]);
919
- break;
1000
+ if (mdHtml(cp.mdBlock, 3) || mdHtml(cp.mdBlock, 5)) {
1001
+ const isPi = mdHtml(cp.mdBlock, 3);
1002
+ const term = isPi ? "?>" : "]]>";
1003
+ const c = scanText.indexOf(term, pos);
1004
+ const mdEnd = c === -1 ? scanText.length : c + term.length;
1005
+ if (cp.p5Tok.kind === "bogus") {
1006
+ const gt = scanText.indexOf(">", pos);
1007
+ if (gt !== -1 && (c === -1 || gt !== c + term.length - 1)) {
1008
+ cp.p5Tok = { kind: "data" };
1009
+ rawSpans.push([pos, gt + 1]);
1010
+ if (P5_MARKUP_RE.test(scanText.slice(gt + 1, c === -1 ? scanText.length : c))) {
1011
+ poisonRawDivergence();
1012
+ }
1013
+ } else {
1014
+ rawSpans.push([pos, mdEnd]);
1015
+ if (c !== -1) cp.p5Tok = { kind: "data" };
1016
+ }
920
1017
  }
921
- rawSpans.push([pos, c + 3]);
1018
+ if (c === -1) break;
922
1019
  cp.mdBlock = { kind: "none" };
923
- pos = c + 3;
1020
+ pos = mdEnd;
924
1021
  continue;
925
1022
  }
926
1023
  if (mdHtml(cp.mdBlock, 4)) {
@@ -963,7 +1060,8 @@ function processConfirmedLine(cp, ln, text) {
963
1060
  pos = bogus + 2;
964
1061
  } else if (first === cd) {
965
1062
  rawSpans.push([cd, cd + 9]);
966
- cp.mdBlock = { kind: "html", type: 5 };
1063
+ if (cp.mdBlock.kind === "none") cp.mdBlock = { kind: "html", type: 5 };
1064
+ if (cp.p5Tok.kind === "data") cp.p5Tok = { kind: "bogus" };
967
1065
  if (!isMdBlank(scanText.slice(0, cd)) || ln.indent > 3) inlineRawOpenerIdx = cd;
968
1066
  pos = cd + 9;
969
1067
  } else if (first === pi) {
@@ -974,13 +1072,15 @@ function processConfirmedLine(cp, ln, text) {
974
1072
  continue;
975
1073
  }
976
1074
  rawSpans.push([pi, pi + 2]);
977
- cp.mdBlock = { kind: "html", type: 3 };
1075
+ if (cp.mdBlock.kind === "none") cp.mdBlock = { kind: "html", type: 3 };
1076
+ if (cp.p5Tok.kind === "data") cp.p5Tok = { kind: "bogus" };
978
1077
  if (!isMdBlank(scanText.slice(0, pi)) || ln.indent > 3) inlineRawOpenerIdx = pi;
979
1078
  pos = pi + 2;
980
1079
  } else {
981
1080
  rawSpans.push([decl, decl + 2]);
982
1081
  if (ln.indent <= 3 && /^doctype/i.test(scanText.slice(decl + 2))) cp.phasePoisonedAt = 0;
983
- cp.mdBlock = { kind: "html", type: 4 };
1082
+ if (cp.mdBlock.kind === "none") cp.mdBlock = { kind: "html", type: 4 };
1083
+ if (cp.p5Tok.kind === "data") cp.p5Tok = { kind: "bogus" };
984
1084
  if (!isMdBlank(scanText.slice(0, decl)) || ln.indent > 3) inlineRawOpenerIdx = decl;
985
1085
  pos = decl + 2;
986
1086
  }
@@ -1017,7 +1117,7 @@ function processConfirmedLine(cp, ln, text) {
1017
1117
  if (inRawTextTok(cp.p5Tok) && (m[0] === "<!--" || m[0] === "-->" || m[0] === "--!>")) {
1018
1118
  if (cp.p5Tok.kind === "script") {
1019
1119
  if (m[0] === "<!--") cp.p5Tok = { ...cp.p5Tok, escaped: true };
1020
- if (m[0] === "-->") cp.p5Tok = { ...cp.p5Tok, escaped: false };
1120
+ if (m[0] === "-->") cp.p5Tok = { ...cp.p5Tok, escaped: false, double: false };
1021
1121
  }
1022
1122
  continue;
1023
1123
  }
@@ -1029,7 +1129,9 @@ function processConfirmedLine(cp, ln, text) {
1029
1129
  if (cp.p5Tok.kind === "comment") cp.p5Tok = { kind: "data" };
1030
1130
  } else if (next === "!>" || next === "-!") {
1031
1131
  if (cp.p5Tok.kind === "comment") cp.p5Tok = { kind: "data" };
1032
- poisonRawDivergence();
1132
+ if (mdHtml(cp.mdBlock, 2) && P5_MARKUP_RE.test(tagText.slice(m.index + m[0].length))) {
1133
+ poisonRawDivergence();
1134
+ }
1033
1135
  }
1034
1136
  continue;
1035
1137
  }
@@ -1047,7 +1149,9 @@ function processConfirmedLine(cp, ln, text) {
1047
1149
  continue;
1048
1150
  }
1049
1151
  if (m[0] === "--!>") {
1050
- if (commentEitherOpen(cp.mdBlock, cp.p5Tok)) poisonRawDivergence();
1152
+ if (mdHtml(cp.mdBlock, 2) && P5_MARKUP_RE.test(tagText.slice(m.index + m[0].length))) {
1153
+ poisonRawDivergence();
1154
+ }
1051
1155
  if (cp.p5Tok.kind === "comment") cp.p5Tok = { kind: "data" };
1052
1156
  continue;
1053
1157
  }
@@ -1138,14 +1242,16 @@ function processConfirmedLine(cp, ln, text) {
1138
1242
  applyTag(tag, closing);
1139
1243
  const rawLastLt = ln.text.lastIndexOf("<");
1140
1244
  const rawTruncated = rawLastLt !== -1 && !ln.text.includes(">", rawLastLt);
1141
- if (!closing && !inRawText && rawTruncated) cp.pendingTruncatedTags.push(tag);
1245
+ if (!closing && !(htmlOwnedLine || inRawTextTok(cp.p5Tok)) && rawTruncated) {
1246
+ cp.pendingTruncatedTags.push(tag);
1247
+ }
1142
1248
  }
1143
1249
  }
1144
1250
  }
1145
1251
  }
1146
1252
  }
1147
1253
  const effectiveOpen = cp.openTotal - cp.pendingTruncatedTags.length;
1148
- if ((inRawText || rawFlowStart) && effectiveOpen <= 0) {
1254
+ if ((htmlOwnedLine || inRawTextTok(cp.p5Tok)) && effectiveOpen <= 0) {
1149
1255
  let masked2 = "";
1150
1256
  let cursor = 0;
1151
1257
  for (const [from, to] of rawSpans) {
@@ -1153,7 +1259,7 @@ function processConfirmedLine(cp, ln, text) {
1153
1259
  cursor = to;
1154
1260
  }
1155
1261
  masked2 += scanText.slice(cursor);
1156
- if (floatingResidue(masked2, commentOpenAtLineStart).length > 0) {
1262
+ if (floatingResidue(masked2, bothCommentsOpenAtLineStart).length > 0) {
1157
1263
  cp.p5SealPending = true;
1158
1264
  }
1159
1265
  }
@@ -1162,11 +1268,28 @@ function processConfirmedLine(cp, ln, text) {
1162
1268
  }
1163
1269
  if (mdHtml(cp.mdBlock, 1) && TYPE1_CLOSE_RE.test(ln.text)) {
1164
1270
  cp.mdBlock = { kind: "none" };
1165
- cp.mayBeRawToMicromark = false;
1271
+ if (inRawTextTok(cp.p5Tok)) cp.phasePoisonedAt = 0;
1166
1272
  }
1167
1273
  cp.blankRun = 0;
1168
1274
  cp.prevLineBlank = false;
1169
1275
  cp.prevLineWasText = true;
1276
+ {
1277
+ const tt = mdTrimStart(ln.text);
1278
+ let openContent;
1279
+ if (htmlOwnedLine) {
1280
+ openContent = false;
1281
+ } else if (ln.indent >= 4) {
1282
+ openContent = cp.prevLineOpenContent;
1283
+ } else if (ATX_HEADING_RE.test(tt) || THEMATIC_BREAK_RE.test(tt) || BARE_MARKER_RE.test(tt)) {
1284
+ openContent = false;
1285
+ } else if (SETEXT_LEFTOVER_RE.test(tt)) {
1286
+ openContent = !cp.prevLineOpenContent;
1287
+ } else {
1288
+ openContent = true;
1289
+ }
1290
+ cp.prevLineOpenContent = openContent;
1291
+ cp.tableMaybeOpen = ln.text.includes("|") || cp.tableMaybeOpen && openContent;
1292
+ }
1170
1293
  cp.prevLineWasValidDef = validLinkDef;
1171
1294
  }
1172
1295
 
@@ -1416,6 +1539,33 @@ function headRoutedCaptureUnclosed(values) {
1416
1539
  return !new RegExp(`</${name}(?=[\\s/>])`, "i").test(after);
1417
1540
  }
1418
1541
  var STRAY_SYNTHESIZED_END_TAG_RE = /<\/(?:br|p)\b/i;
1542
+ var RAW_TEXT_OPEN_RE = /<(script|style|textarea|title|xmp|iframe|noembed|noframes|plaintext)(?=[\s/>])/gi;
1543
+ function rawTextRegionCrossesOut(values) {
1544
+ for (const value of values) {
1545
+ let pos = 0;
1546
+ for (; ; ) {
1547
+ RAW_TEXT_OPEN_RE.lastIndex = pos;
1548
+ const open = RAW_TEXT_OPEN_RE.exec(value);
1549
+ if (open === null) break;
1550
+ const name = open[1].toLowerCase();
1551
+ const bodyStart = open.index + open[0].length;
1552
+ const closeRe = new RegExp(`</${name}(?=[\\s/>])`, "ig");
1553
+ closeRe.lastIndex = bodyStart;
1554
+ let close = closeRe.exec(value);
1555
+ if (name === "script") {
1556
+ while (close !== null) {
1557
+ const body = value.slice(bodyStart, close.index);
1558
+ const lastOpen = body.lastIndexOf("<!--");
1559
+ if (lastOpen === -1 || body.indexOf("-->", lastOpen + 4) !== -1) break;
1560
+ close = closeRe.exec(value);
1561
+ }
1562
+ }
1563
+ if (close === null) return true;
1564
+ pos = close.index + close[0].length;
1565
+ }
1566
+ }
1567
+ return false;
1568
+ }
1419
1569
  function spliceTrees(input) {
1420
1570
  const { prevMdast, prevHast, tailMdast, tailHast, content, boundary, injectionPrefix, injectedSegments } = input;
1421
1571
  const injectedLen = injectionPrefix.length;
@@ -1472,7 +1622,9 @@ function spliceTrees(input) {
1472
1622
  return !(start !== void 0 && start < injectedLen);
1473
1623
  });
1474
1624
  const tailWrapVisible = tailMdastChildren.some((child) => !isWrapInvisible(child));
1475
- if (hasStrayTablePart(prefixMdast.flatMap((c) => c.type === "html" ? [c.value] : []))) return null;
1625
+ const prefixHtmlValues = prefixMdast.flatMap((c) => c.type === "html" ? [c.value] : []);
1626
+ if (hasStrayTablePart(prefixHtmlValues)) return null;
1627
+ if (rawTextRegionCrossesOut(prefixHtmlValues)) return null;
1476
1628
  const leadingHtml = [];
1477
1629
  for (const child of tailMdastChildren) {
1478
1630
  if (isWrapInvisible(child)) continue;