@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.js CHANGED
@@ -243,7 +243,28 @@ var HTML_BREAKOUT_TAGS = /* @__PURE__ */ new Set([
243
243
  "ul",
244
244
  "var"
245
245
  ]);
246
- var HTML_INTEGRATION_POINTS = ["foreignobject", "desc", "mi", "mo", "mn", "ms", "mtext", "annotation-xml"];
246
+ var HTML_INTEGRATION_POINTS = ["foreignobject", "desc", "title", "mi", "mo", "mn", "ms", "mtext", "annotation-xml"];
247
+ var SCOPE_BARRIER_NAMES = /* @__PURE__ */ new Set([
248
+ "applet",
249
+ "caption",
250
+ "html",
251
+ "table",
252
+ "td",
253
+ "th",
254
+ "marquee",
255
+ "object",
256
+ "template",
257
+ "mi",
258
+ "mo",
259
+ "mn",
260
+ "ms",
261
+ "mtext",
262
+ "annotation-xml",
263
+ "foreignobject",
264
+ "desc",
265
+ "title"
266
+ ]);
267
+ var FOREIGN_ROOT_NAMES = ["svg", "math"];
247
268
  var TYPE1_NAMES = /* @__PURE__ */ new Set(["script", "pre", "style", "textarea"]);
248
269
  var RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set([
249
270
  "script",
@@ -413,6 +434,8 @@ function freshCheckpoint(defListEnabled, mathFlow, referenceTaint) {
413
434
  piOpen: false,
414
435
  bogusOpen: false,
415
436
  rawTextOpen: null,
437
+ openStack: [],
438
+ scriptDataEscaped: false,
416
439
  declOpen: false,
417
440
  cdataOpen: false,
418
441
  inFence: false,
@@ -653,7 +676,7 @@ function processConfirmedLine(cp, ln, text) {
653
676
  cp.htmlSeamPending = false;
654
677
  }
655
678
  }
656
- const inForeignContent = () => (cp.tagBalance.get("svg") ?? 0) > 0 || (cp.tagBalance.get("math") ?? 0) > 0;
679
+ const inForeignContent = () => FOREIGN_ROOT_NAMES.some((name) => (cp.tagBalance.get(name) ?? 0) > 0);
657
680
  const honoursSelfClosing = (tag) => {
658
681
  if (tag === "svg" || tag === "math") return true;
659
682
  if (!inForeignContent() || HTML_BREAKOUT_TAGS.has(tag)) return false;
@@ -665,23 +688,57 @@ function processConfirmedLine(cp, ln, text) {
665
688
  for (const ip of HTML_INTEGRATION_POINTS) if ((cp.tagBalance.get(ip) ?? 0) > 0) return true;
666
689
  return false;
667
690
  };
691
+ const popForeignRoots = () => {
692
+ for (const name of FOREIGN_ROOT_NAMES) {
693
+ const count = cp.tagBalance.get(name) ?? 0;
694
+ if (count > 0) {
695
+ cp.tagBalance.set(name, 0);
696
+ cp.openTotal -= count;
697
+ }
698
+ }
699
+ if (cp.openStack.length > 0) cp.openStack = cp.openStack.filter((n) => !FOREIGN_ROOT_NAMES.includes(n));
700
+ };
701
+ const noteBreakout = (tag, closing) => {
702
+ if (closing || cp.rawTextOpen !== null) return;
703
+ if (HTML_BREAKOUT_TAGS.has(tag) && !htmlRulesApply()) popForeignRoots();
704
+ };
668
705
  const applyTag = (tag, closing) => {
669
706
  if (cp.rawTextOpen !== null) {
707
+ if (cp.scriptDataEscaped && !closing && tag === "script") {
708
+ cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
709
+ }
670
710
  if (!(closing && tag === cp.rawTextOpen)) return;
671
711
  cp.rawTextOpen = null;
672
- } else if (!closing && RAW_TEXT_ELEMENTS.has(tag) && htmlRulesApply()) {
673
- cp.rawTextOpen = tag;
674
- cp.rawTextInline = !cp.htmlFlowReal;
675
- if (tag === "plaintext") cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
712
+ cp.scriptDataEscaped = false;
713
+ } else {
714
+ noteBreakout(tag, closing);
715
+ if (!closing && RAW_TEXT_ELEMENTS.has(tag) && htmlRulesApply()) {
716
+ cp.rawTextOpen = tag;
717
+ cp.scriptDataEscaped = false;
718
+ cp.rawTextInline = !cp.htmlFlowReal;
719
+ if (tag === "plaintext") cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
720
+ }
676
721
  }
677
722
  if (DOCUMENT_STRUCTURE_NAMES.has(tag)) cp.phasePoisonedAt = 0;
723
+ if (tag === "template" && !closing) cp.phasePoisonedAt = 0;
678
724
  if (closing) {
725
+ let idx = -1;
726
+ for (let i = cp.openStack.length - 1; i >= 0; i--) {
727
+ if (cp.openStack[i] === tag) {
728
+ idx = i;
729
+ break;
730
+ }
731
+ if (SCOPE_BARRIER_NAMES.has(cp.openStack[i])) break;
732
+ }
733
+ if (idx === -1) return;
734
+ cp.openStack.splice(idx, 1);
679
735
  const count = cp.tagBalance.get(tag) ?? 0;
680
736
  if (count > 0) {
681
737
  cp.tagBalance.set(tag, count - 1);
682
738
  cp.openTotal -= 1;
683
739
  }
684
740
  } else {
741
+ cp.openStack.push(tag);
685
742
  cp.tagBalance.set(tag, (cp.tagBalance.get(tag) ?? 0) + 1);
686
743
  cp.openTotal += 1;
687
744
  }
@@ -800,6 +857,9 @@ function processConfirmedLine(cp, ln, text) {
800
857
  if (!cp.type1FlowOpen) {
801
858
  cp.htmlFlowSinceBlank = false;
802
859
  cp.htmlFlowReal = false;
860
+ if (cp.rawTextOpen !== null && !cp.rawTextInline) {
861
+ cp.phasePoisonedAt = 0;
862
+ }
803
863
  }
804
864
  cp.prevLineBlank = true;
805
865
  cp.prevLineWasText = false;
@@ -899,6 +959,7 @@ ${cont(scanText)}` };
899
959
  const poisonRawDivergence = () => {
900
960
  cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
901
961
  };
962
+ let inlineRawOpenerIdx = -1;
902
963
  while (pos < scanText.length) {
903
964
  if (cp.piOpen) {
904
965
  const c = scanText.indexOf("?>", pos);
@@ -954,6 +1015,7 @@ ${cont(scanText)}` };
954
1015
  } else if (first === cd) {
955
1016
  rawSpans.push([cd, cd + 9]);
956
1017
  cp.cdataOpen = true;
1018
+ if (!isMdBlank(scanText.slice(0, cd)) || ln.indent > 3) inlineRawOpenerIdx = cd;
957
1019
  pos = cd + 9;
958
1020
  } else if (first === pi) {
959
1021
  if (scanText[pi + 2] === ">") {
@@ -964,14 +1026,19 @@ ${cont(scanText)}` };
964
1026
  }
965
1027
  rawSpans.push([pi, pi + 2]);
966
1028
  cp.piOpen = true;
1029
+ if (!isMdBlank(scanText.slice(0, pi)) || ln.indent > 3) inlineRawOpenerIdx = pi;
967
1030
  pos = pi + 2;
968
1031
  } else {
969
1032
  rawSpans.push([decl, decl + 2]);
970
1033
  if (ln.indent <= 3 && /^doctype/i.test(scanText.slice(decl + 2))) cp.phasePoisonedAt = 0;
971
1034
  cp.declOpen = true;
1035
+ if (!isMdBlank(scanText.slice(0, decl)) || ln.indent > 3) inlineRawOpenerIdx = decl;
972
1036
  pos = decl + 2;
973
1037
  }
974
1038
  }
1039
+ if (inlineRawOpenerIdx !== -1 && (cp.piOpen || cp.declOpen || cp.cdataOpen)) {
1040
+ cp.phasePoisonedAt = 0;
1041
+ }
975
1042
  let tagText = scanText;
976
1043
  for (const [from, to] of rawSpans) {
977
1044
  tagText = tagText.slice(0, from) + " ".repeat(to - from) + tagText.slice(to);
@@ -999,7 +1066,10 @@ ${cont(scanText)}` };
999
1066
  let m;
1000
1067
  let lastCommentOpenerIdx = -1;
1001
1068
  while ((m = TAG_OR_COMMENT_RE.exec(tagText)) !== null) {
1002
- if (cp.rawTextOpen !== null && (m[0] === "<!--" || m[0] === "-->" || m[0] === "--!>")) continue;
1069
+ if (cp.rawTextOpen !== null && (m[0] === "<!--" || m[0] === "-->" || m[0] === "--!>")) {
1070
+ if (cp.rawTextOpen === "script" && m[0] === "<!--") cp.scriptDataEscaped = true;
1071
+ continue;
1072
+ }
1003
1073
  if (m[0] === "<!--") {
1004
1074
  const next = tagText.slice(m.index + 4, m.index + 6);
1005
1075
  if (cp.commentOpen) {
@@ -1053,12 +1123,13 @@ ${cont(scanText)}` };
1053
1123
  continue;
1054
1124
  }
1055
1125
  const selfClosing = /\/\s*$/.test(attrs);
1126
+ noteBreakout(tag, closing);
1056
1127
  if (VOID_TAGS.has(tag) || selfClosing && honoursSelfClosing(tag)) continue;
1057
1128
  applyTag(tag, closing);
1058
1129
  }
1059
- if (cp.commentOpen && lastCommentOpenerIdx !== -1 && !inRawText) {
1060
- if (!isMdBlank(tagText.slice(0, lastCommentOpenerIdx))) {
1061
- cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start + lastCommentOpenerIdx);
1130
+ if (cp.commentOpen && lastCommentOpenerIdx !== -1) {
1131
+ if (!isMdBlank(tagText.slice(0, lastCommentOpenerIdx)) || ln.indent > 3) {
1132
+ cp.phasePoisonedAt = 0;
1062
1133
  }
1063
1134
  }
1064
1135
  if (masked !== null && masked !== ln.text) {
@@ -1075,6 +1146,7 @@ ${cont(scanText)}` };
1075
1146
  if (strayTablePart(tag)) cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start + mr.index);
1076
1147
  if (closing && mr[3] !== void 0 && !/^\s*$/.test(mr[3])) continue;
1077
1148
  const selfClosing = mr[3] !== void 0 && /\/\s*$/.test(mr[3]);
1149
+ noteBreakout(tag, closing);
1078
1150
  if (VOID_TAGS.has(tag) || selfClosing && honoursSelfClosing(tag)) continue;
1079
1151
  applyTag(tag, closing);
1080
1152
  }
@@ -1357,6 +1429,40 @@ function hasStrayTablePart(values) {
1357
1429
  }
1358
1430
  return false;
1359
1431
  }
1432
+ var HEAD_ROUTED_NAMES = /* @__PURE__ */ new Set([
1433
+ "base",
1434
+ "basefont",
1435
+ "bgsound",
1436
+ "link",
1437
+ "meta",
1438
+ "noframes",
1439
+ "script",
1440
+ "style",
1441
+ "template",
1442
+ "title"
1443
+ ]);
1444
+ var HEAD_ROUTED_RAW_TEXT_RE = /<(script|style|title|noframes)(?=[\s/>])/i;
1445
+ var ANY_START_TAG_RE = /<([a-z][a-z0-9-]*)(?=[\s/>])/gi;
1446
+ function headRoutedCaptureUnclosed(values) {
1447
+ if (values.length === 0) return false;
1448
+ const joined = values.join("\n");
1449
+ const opener = HEAD_ROUTED_RAW_TEXT_RE.exec(joined);
1450
+ if (opener === null) return false;
1451
+ const before = joined.slice(0, opener.index);
1452
+ if (/<[!?]/.test(before)) return true;
1453
+ ANY_START_TAG_RE.lastIndex = 0;
1454
+ let m;
1455
+ while ((m = ANY_START_TAG_RE.exec(before)) !== null) {
1456
+ if (!HEAD_ROUTED_NAMES.has(m[1].toLowerCase())) return false;
1457
+ }
1458
+ const name = opener[1].toLowerCase();
1459
+ const after = joined.slice(opener.index + opener[0].length);
1460
+ if (name === "script") {
1461
+ const close = after.search(/<\/script(?=[\s/>])/i);
1462
+ return close === -1 || /<!--/.test(after.slice(0, close));
1463
+ }
1464
+ return !new RegExp(`</${name}(?=[\\s/>])`, "i").test(after);
1465
+ }
1360
1466
  var STRAY_SYNTHESIZED_END_TAG_RE = /<\/(?:br|p)\b/i;
1361
1467
  function spliceTrees(input) {
1362
1468
  const { prevMdast, prevHast, tailMdast, tailHast, content, boundary, injectionPrefix, injectedSegments } = input;
@@ -1415,11 +1521,14 @@ function spliceTrees(input) {
1415
1521
  });
1416
1522
  const tailWrapVisible = tailMdastChildren.some((child) => !isWrapInvisible(child));
1417
1523
  if (hasStrayTablePart(prefixMdast.flatMap((c) => c.type === "html" ? [c.value] : []))) return null;
1524
+ const leadingHtml = [];
1418
1525
  for (const child of tailMdastChildren) {
1419
1526
  if (isWrapInvisible(child)) continue;
1420
1527
  if (child.type !== "html") break;
1421
1528
  if (STRAY_SYNTHESIZED_END_TAG_RE.test(child.value) || TABLE_PART_TAG_RE.test(child.value)) return null;
1529
+ leadingHtml.push(child.value);
1422
1530
  }
1531
+ if (headRoutedCaptureUnclosed(leadingHtml)) return null;
1423
1532
  const aligned = alignPrefixCut(prefixMdast, cutRegion, tailWrapVisible);
1424
1533
  if (aligned === null) return null;
1425
1534
  const hastChildren = aligned.children;
@@ -2344,7 +2453,7 @@ function createRegistry(onEmpty) {
2344
2453
 
2345
2454
  // src/components/pluginChain.ts
2346
2455
  import rehypeKatex from "rehype-katex";
2347
- import rehypeRaw from "rehype-raw";
2456
+ import rehypeRaw from "@ai-markdown/rehype-raw";
2348
2457
  import rehypeUnwrapImages from "rehype-unwrap-images";
2349
2458
 
2350
2459
  // src/components/rehypeUnwrapCrossChunkImages.ts