@ai-react-markdown/engine 2.5.3 → 2.5.5

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
@@ -336,7 +336,28 @@ var HTML_BREAKOUT_TAGS = /* @__PURE__ */ new Set([
336
336
  "ul",
337
337
  "var"
338
338
  ]);
339
- var HTML_INTEGRATION_POINTS = ["foreignobject", "desc", "mi", "mo", "mn", "ms", "mtext", "annotation-xml"];
339
+ var HTML_INTEGRATION_POINTS = ["foreignobject", "desc", "title", "mi", "mo", "mn", "ms", "mtext", "annotation-xml"];
340
+ var SCOPE_BARRIER_NAMES = /* @__PURE__ */ new Set([
341
+ "applet",
342
+ "caption",
343
+ "html",
344
+ "table",
345
+ "td",
346
+ "th",
347
+ "marquee",
348
+ "object",
349
+ "template",
350
+ "mi",
351
+ "mo",
352
+ "mn",
353
+ "ms",
354
+ "mtext",
355
+ "annotation-xml",
356
+ "foreignobject",
357
+ "desc",
358
+ "title"
359
+ ]);
360
+ var FOREIGN_ROOT_NAMES = ["svg", "math"];
340
361
  var TYPE1_NAMES = /* @__PURE__ */ new Set(["script", "pre", "style", "textarea"]);
341
362
  var RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set([
342
363
  "script",
@@ -506,6 +527,8 @@ function freshCheckpoint(defListEnabled, mathFlow, referenceTaint) {
506
527
  piOpen: false,
507
528
  bogusOpen: false,
508
529
  rawTextOpen: null,
530
+ openStack: [],
531
+ scriptDataEscaped: false,
509
532
  declOpen: false,
510
533
  cdataOpen: false,
511
534
  inFence: false,
@@ -746,7 +769,7 @@ function processConfirmedLine(cp, ln, text) {
746
769
  cp.htmlSeamPending = false;
747
770
  }
748
771
  }
749
- const inForeignContent = () => (cp.tagBalance.get("svg") ?? 0) > 0 || (cp.tagBalance.get("math") ?? 0) > 0;
772
+ const inForeignContent = () => FOREIGN_ROOT_NAMES.some((name) => (cp.tagBalance.get(name) ?? 0) > 0);
750
773
  const honoursSelfClosing = (tag) => {
751
774
  if (tag === "svg" || tag === "math") return true;
752
775
  if (!inForeignContent() || HTML_BREAKOUT_TAGS.has(tag)) return false;
@@ -758,23 +781,57 @@ function processConfirmedLine(cp, ln, text) {
758
781
  for (const ip of HTML_INTEGRATION_POINTS) if ((cp.tagBalance.get(ip) ?? 0) > 0) return true;
759
782
  return false;
760
783
  };
784
+ const popForeignRoots = () => {
785
+ for (const name of FOREIGN_ROOT_NAMES) {
786
+ const count = cp.tagBalance.get(name) ?? 0;
787
+ if (count > 0) {
788
+ cp.tagBalance.set(name, 0);
789
+ cp.openTotal -= count;
790
+ }
791
+ }
792
+ if (cp.openStack.length > 0) cp.openStack = cp.openStack.filter((n) => !FOREIGN_ROOT_NAMES.includes(n));
793
+ };
794
+ const noteBreakout = (tag, closing) => {
795
+ if (closing || cp.rawTextOpen !== null) return;
796
+ if (HTML_BREAKOUT_TAGS.has(tag) && !htmlRulesApply()) popForeignRoots();
797
+ };
761
798
  const applyTag = (tag, closing) => {
762
799
  if (cp.rawTextOpen !== null) {
800
+ if (cp.scriptDataEscaped && !closing && tag === "script") {
801
+ cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
802
+ }
763
803
  if (!(closing && tag === cp.rawTextOpen)) return;
764
804
  cp.rawTextOpen = null;
765
- } else if (!closing && RAW_TEXT_ELEMENTS.has(tag) && htmlRulesApply()) {
766
- cp.rawTextOpen = tag;
767
- cp.rawTextInline = !cp.htmlFlowReal;
768
- if (tag === "plaintext") cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
805
+ cp.scriptDataEscaped = false;
806
+ } else {
807
+ noteBreakout(tag, closing);
808
+ if (!closing && RAW_TEXT_ELEMENTS.has(tag) && htmlRulesApply()) {
809
+ cp.rawTextOpen = tag;
810
+ cp.scriptDataEscaped = false;
811
+ cp.rawTextInline = !cp.htmlFlowReal;
812
+ if (tag === "plaintext") cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
813
+ }
769
814
  }
770
815
  if (DOCUMENT_STRUCTURE_NAMES.has(tag)) cp.phasePoisonedAt = 0;
816
+ if (tag === "template" && !closing) cp.phasePoisonedAt = 0;
771
817
  if (closing) {
818
+ let idx = -1;
819
+ for (let i = cp.openStack.length - 1; i >= 0; i--) {
820
+ if (cp.openStack[i] === tag) {
821
+ idx = i;
822
+ break;
823
+ }
824
+ if (SCOPE_BARRIER_NAMES.has(cp.openStack[i])) break;
825
+ }
826
+ if (idx === -1) return;
827
+ cp.openStack.splice(idx, 1);
772
828
  const count = cp.tagBalance.get(tag) ?? 0;
773
829
  if (count > 0) {
774
830
  cp.tagBalance.set(tag, count - 1);
775
831
  cp.openTotal -= 1;
776
832
  }
777
833
  } else {
834
+ cp.openStack.push(tag);
778
835
  cp.tagBalance.set(tag, (cp.tagBalance.get(tag) ?? 0) + 1);
779
836
  cp.openTotal += 1;
780
837
  }
@@ -893,6 +950,9 @@ function processConfirmedLine(cp, ln, text) {
893
950
  if (!cp.type1FlowOpen) {
894
951
  cp.htmlFlowSinceBlank = false;
895
952
  cp.htmlFlowReal = false;
953
+ if (cp.rawTextOpen !== null && !cp.rawTextInline) {
954
+ cp.phasePoisonedAt = 0;
955
+ }
896
956
  }
897
957
  cp.prevLineBlank = true;
898
958
  cp.prevLineWasText = false;
@@ -992,6 +1052,7 @@ ${cont(scanText)}` };
992
1052
  const poisonRawDivergence = () => {
993
1053
  cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
994
1054
  };
1055
+ let inlineRawOpenerIdx = -1;
995
1056
  while (pos < scanText.length) {
996
1057
  if (cp.piOpen) {
997
1058
  const c = scanText.indexOf("?>", pos);
@@ -1047,6 +1108,7 @@ ${cont(scanText)}` };
1047
1108
  } else if (first === cd) {
1048
1109
  rawSpans.push([cd, cd + 9]);
1049
1110
  cp.cdataOpen = true;
1111
+ if (!isMdBlank(scanText.slice(0, cd)) || ln.indent > 3) inlineRawOpenerIdx = cd;
1050
1112
  pos = cd + 9;
1051
1113
  } else if (first === pi) {
1052
1114
  if (scanText[pi + 2] === ">") {
@@ -1057,14 +1119,19 @@ ${cont(scanText)}` };
1057
1119
  }
1058
1120
  rawSpans.push([pi, pi + 2]);
1059
1121
  cp.piOpen = true;
1122
+ if (!isMdBlank(scanText.slice(0, pi)) || ln.indent > 3) inlineRawOpenerIdx = pi;
1060
1123
  pos = pi + 2;
1061
1124
  } else {
1062
1125
  rawSpans.push([decl, decl + 2]);
1063
1126
  if (ln.indent <= 3 && /^doctype/i.test(scanText.slice(decl + 2))) cp.phasePoisonedAt = 0;
1064
1127
  cp.declOpen = true;
1128
+ if (!isMdBlank(scanText.slice(0, decl)) || ln.indent > 3) inlineRawOpenerIdx = decl;
1065
1129
  pos = decl + 2;
1066
1130
  }
1067
1131
  }
1132
+ if (inlineRawOpenerIdx !== -1 && (cp.piOpen || cp.declOpen || cp.cdataOpen)) {
1133
+ cp.phasePoisonedAt = 0;
1134
+ }
1068
1135
  let tagText = scanText;
1069
1136
  for (const [from, to] of rawSpans) {
1070
1137
  tagText = tagText.slice(0, from) + " ".repeat(to - from) + tagText.slice(to);
@@ -1092,7 +1159,10 @@ ${cont(scanText)}` };
1092
1159
  let m;
1093
1160
  let lastCommentOpenerIdx = -1;
1094
1161
  while ((m = TAG_OR_COMMENT_RE.exec(tagText)) !== null) {
1095
- if (cp.rawTextOpen !== null && (m[0] === "<!--" || m[0] === "-->" || m[0] === "--!>")) continue;
1162
+ if (cp.rawTextOpen !== null && (m[0] === "<!--" || m[0] === "-->" || m[0] === "--!>")) {
1163
+ if (cp.rawTextOpen === "script" && m[0] === "<!--") cp.scriptDataEscaped = true;
1164
+ continue;
1165
+ }
1096
1166
  if (m[0] === "<!--") {
1097
1167
  const next = tagText.slice(m.index + 4, m.index + 6);
1098
1168
  if (cp.commentOpen) {
@@ -1146,12 +1216,13 @@ ${cont(scanText)}` };
1146
1216
  continue;
1147
1217
  }
1148
1218
  const selfClosing = /\/\s*$/.test(attrs);
1219
+ noteBreakout(tag, closing);
1149
1220
  if (VOID_TAGS.has(tag) || selfClosing && honoursSelfClosing(tag)) continue;
1150
1221
  applyTag(tag, closing);
1151
1222
  }
1152
- if (cp.commentOpen && lastCommentOpenerIdx !== -1 && !inRawText) {
1153
- if (!isMdBlank(tagText.slice(0, lastCommentOpenerIdx))) {
1154
- cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start + lastCommentOpenerIdx);
1223
+ if (cp.commentOpen && lastCommentOpenerIdx !== -1) {
1224
+ if (!isMdBlank(tagText.slice(0, lastCommentOpenerIdx)) || ln.indent > 3) {
1225
+ cp.phasePoisonedAt = 0;
1155
1226
  }
1156
1227
  }
1157
1228
  if (masked !== null && masked !== ln.text) {
@@ -1168,6 +1239,7 @@ ${cont(scanText)}` };
1168
1239
  if (strayTablePart(tag)) cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start + mr.index);
1169
1240
  if (closing && mr[3] !== void 0 && !/^\s*$/.test(mr[3])) continue;
1170
1241
  const selfClosing = mr[3] !== void 0 && /\/\s*$/.test(mr[3]);
1242
+ noteBreakout(tag, closing);
1171
1243
  if (VOID_TAGS.has(tag) || selfClosing && honoursSelfClosing(tag)) continue;
1172
1244
  applyTag(tag, closing);
1173
1245
  }
@@ -1450,6 +1522,40 @@ function hasStrayTablePart(values) {
1450
1522
  }
1451
1523
  return false;
1452
1524
  }
1525
+ var HEAD_ROUTED_NAMES = /* @__PURE__ */ new Set([
1526
+ "base",
1527
+ "basefont",
1528
+ "bgsound",
1529
+ "link",
1530
+ "meta",
1531
+ "noframes",
1532
+ "script",
1533
+ "style",
1534
+ "template",
1535
+ "title"
1536
+ ]);
1537
+ var HEAD_ROUTED_RAW_TEXT_RE = /<(script|style|title|noframes)(?=[\s/>])/i;
1538
+ var ANY_START_TAG_RE = /<([a-z][a-z0-9-]*)(?=[\s/>])/gi;
1539
+ function headRoutedCaptureUnclosed(values) {
1540
+ if (values.length === 0) return false;
1541
+ const joined = values.join("\n");
1542
+ const opener = HEAD_ROUTED_RAW_TEXT_RE.exec(joined);
1543
+ if (opener === null) return false;
1544
+ const before = joined.slice(0, opener.index);
1545
+ if (/<[!?]/.test(before)) return true;
1546
+ ANY_START_TAG_RE.lastIndex = 0;
1547
+ let m;
1548
+ while ((m = ANY_START_TAG_RE.exec(before)) !== null) {
1549
+ if (!HEAD_ROUTED_NAMES.has(m[1].toLowerCase())) return false;
1550
+ }
1551
+ const name = opener[1].toLowerCase();
1552
+ const after = joined.slice(opener.index + opener[0].length);
1553
+ if (name === "script") {
1554
+ const close = after.search(/<\/script(?=[\s/>])/i);
1555
+ return close === -1 || /<!--/.test(after.slice(0, close));
1556
+ }
1557
+ return !new RegExp(`</${name}(?=[\\s/>])`, "i").test(after);
1558
+ }
1453
1559
  var STRAY_SYNTHESIZED_END_TAG_RE = /<\/(?:br|p)\b/i;
1454
1560
  function spliceTrees(input) {
1455
1561
  const { prevMdast, prevHast, tailMdast, tailHast, content, boundary, injectionPrefix, injectedSegments } = input;
@@ -1508,11 +1614,14 @@ function spliceTrees(input) {
1508
1614
  });
1509
1615
  const tailWrapVisible = tailMdastChildren.some((child) => !isWrapInvisible(child));
1510
1616
  if (hasStrayTablePart(prefixMdast.flatMap((c) => c.type === "html" ? [c.value] : []))) return null;
1617
+ const leadingHtml = [];
1511
1618
  for (const child of tailMdastChildren) {
1512
1619
  if (isWrapInvisible(child)) continue;
1513
1620
  if (child.type !== "html") break;
1514
1621
  if (STRAY_SYNTHESIZED_END_TAG_RE.test(child.value) || TABLE_PART_TAG_RE.test(child.value)) return null;
1622
+ leadingHtml.push(child.value);
1515
1623
  }
1624
+ if (headRoutedCaptureUnclosed(leadingHtml)) return null;
1516
1625
  const aligned = alignPrefixCut(prefixMdast, cutRegion, tailWrapVisible);
1517
1626
  if (aligned === null) return null;
1518
1627
  const hastChildren = aligned.children;
@@ -2437,7 +2546,7 @@ function createRegistry(onEmpty) {
2437
2546
 
2438
2547
  // src/components/pluginChain.ts
2439
2548
  var import_rehype_katex = __toESM(require("rehype-katex"), 1);
2440
- var import_rehype_raw = __toESM(require("rehype-raw"), 1);
2549
+ var import_rehype_raw = __toESM(require("@ai-markdown/rehype-raw"), 1);
2441
2550
  var import_rehype_unwrap_images = __toESM(require("rehype-unwrap-images"), 1);
2442
2551
 
2443
2552
  // src/components/rehypeUnwrapCrossChunkImages.ts