@ai-react-markdown/engine 2.5.4 → 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
@@ -244,6 +244,26 @@ var HTML_BREAKOUT_TAGS = /* @__PURE__ */ new Set([
244
244
  "var"
245
245
  ]);
246
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
+ ]);
247
267
  var FOREIGN_ROOT_NAMES = ["svg", "math"];
248
268
  var TYPE1_NAMES = /* @__PURE__ */ new Set(["script", "pre", "style", "textarea"]);
249
269
  var RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set([
@@ -414,6 +434,7 @@ function freshCheckpoint(defListEnabled, mathFlow, referenceTaint) {
414
434
  piOpen: false,
415
435
  bogusOpen: false,
416
436
  rawTextOpen: null,
437
+ openStack: [],
417
438
  scriptDataEscaped: false,
418
439
  declOpen: false,
419
440
  cdataOpen: false,
@@ -675,6 +696,7 @@ function processConfirmedLine(cp, ln, text) {
675
696
  cp.openTotal -= count;
676
697
  }
677
698
  }
699
+ if (cp.openStack.length > 0) cp.openStack = cp.openStack.filter((n) => !FOREIGN_ROOT_NAMES.includes(n));
678
700
  };
679
701
  const noteBreakout = (tag, closing) => {
680
702
  if (closing || cp.rawTextOpen !== null) return;
@@ -698,13 +720,25 @@ function processConfirmedLine(cp, ln, text) {
698
720
  }
699
721
  }
700
722
  if (DOCUMENT_STRUCTURE_NAMES.has(tag)) cp.phasePoisonedAt = 0;
723
+ if (tag === "template" && !closing) cp.phasePoisonedAt = 0;
701
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);
702
735
  const count = cp.tagBalance.get(tag) ?? 0;
703
736
  if (count > 0) {
704
737
  cp.tagBalance.set(tag, count - 1);
705
738
  cp.openTotal -= 1;
706
739
  }
707
740
  } else {
741
+ cp.openStack.push(tag);
708
742
  cp.tagBalance.set(tag, (cp.tagBalance.get(tag) ?? 0) + 1);
709
743
  cp.openTotal += 1;
710
744
  }
@@ -823,6 +857,9 @@ function processConfirmedLine(cp, ln, text) {
823
857
  if (!cp.type1FlowOpen) {
824
858
  cp.htmlFlowSinceBlank = false;
825
859
  cp.htmlFlowReal = false;
860
+ if (cp.rawTextOpen !== null && !cp.rawTextInline) {
861
+ cp.phasePoisonedAt = 0;
862
+ }
826
863
  }
827
864
  cp.prevLineBlank = true;
828
865
  cp.prevLineWasText = false;
@@ -922,6 +959,7 @@ ${cont(scanText)}` };
922
959
  const poisonRawDivergence = () => {
923
960
  cp.phasePoisonedAt = Math.min(cp.phasePoisonedAt, ln.start);
924
961
  };
962
+ let inlineRawOpenerIdx = -1;
925
963
  while (pos < scanText.length) {
926
964
  if (cp.piOpen) {
927
965
  const c = scanText.indexOf("?>", pos);
@@ -977,6 +1015,7 @@ ${cont(scanText)}` };
977
1015
  } else if (first === cd) {
978
1016
  rawSpans.push([cd, cd + 9]);
979
1017
  cp.cdataOpen = true;
1018
+ if (!isMdBlank(scanText.slice(0, cd)) || ln.indent > 3) inlineRawOpenerIdx = cd;
980
1019
  pos = cd + 9;
981
1020
  } else if (first === pi) {
982
1021
  if (scanText[pi + 2] === ">") {
@@ -987,14 +1026,19 @@ ${cont(scanText)}` };
987
1026
  }
988
1027
  rawSpans.push([pi, pi + 2]);
989
1028
  cp.piOpen = true;
1029
+ if (!isMdBlank(scanText.slice(0, pi)) || ln.indent > 3) inlineRawOpenerIdx = pi;
990
1030
  pos = pi + 2;
991
1031
  } else {
992
1032
  rawSpans.push([decl, decl + 2]);
993
1033
  if (ln.indent <= 3 && /^doctype/i.test(scanText.slice(decl + 2))) cp.phasePoisonedAt = 0;
994
1034
  cp.declOpen = true;
1035
+ if (!isMdBlank(scanText.slice(0, decl)) || ln.indent > 3) inlineRawOpenerIdx = decl;
995
1036
  pos = decl + 2;
996
1037
  }
997
1038
  }
1039
+ if (inlineRawOpenerIdx !== -1 && (cp.piOpen || cp.declOpen || cp.cdataOpen)) {
1040
+ cp.phasePoisonedAt = 0;
1041
+ }
998
1042
  let tagText = scanText;
999
1043
  for (const [from, to] of rawSpans) {
1000
1044
  tagText = tagText.slice(0, from) + " ".repeat(to - from) + tagText.slice(to);
@@ -1083,9 +1127,9 @@ ${cont(scanText)}` };
1083
1127
  if (VOID_TAGS.has(tag) || selfClosing && honoursSelfClosing(tag)) continue;
1084
1128
  applyTag(tag, closing);
1085
1129
  }
1086
- if (cp.commentOpen && lastCommentOpenerIdx !== -1 && !inRawText) {
1087
- if (!isMdBlank(tagText.slice(0, lastCommentOpenerIdx))) {
1088
- 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;
1089
1133
  }
1090
1134
  }
1091
1135
  if (masked !== null && masked !== ln.text) {
@@ -1385,6 +1429,40 @@ function hasStrayTablePart(values) {
1385
1429
  }
1386
1430
  return false;
1387
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
+ }
1388
1466
  var STRAY_SYNTHESIZED_END_TAG_RE = /<\/(?:br|p)\b/i;
1389
1467
  function spliceTrees(input) {
1390
1468
  const { prevMdast, prevHast, tailMdast, tailHast, content, boundary, injectionPrefix, injectedSegments } = input;
@@ -1443,11 +1521,14 @@ function spliceTrees(input) {
1443
1521
  });
1444
1522
  const tailWrapVisible = tailMdastChildren.some((child) => !isWrapInvisible(child));
1445
1523
  if (hasStrayTablePart(prefixMdast.flatMap((c) => c.type === "html" ? [c.value] : []))) return null;
1524
+ const leadingHtml = [];
1446
1525
  for (const child of tailMdastChildren) {
1447
1526
  if (isWrapInvisible(child)) continue;
1448
1527
  if (child.type !== "html") break;
1449
1528
  if (STRAY_SYNTHESIZED_END_TAG_RE.test(child.value) || TABLE_PART_TAG_RE.test(child.value)) return null;
1529
+ leadingHtml.push(child.value);
1450
1530
  }
1531
+ if (headRoutedCaptureUnclosed(leadingHtml)) return null;
1451
1532
  const aligned = alignPrefixCut(prefixMdast, cutRegion, tailWrapVisible);
1452
1533
  if (aligned === null) return null;
1453
1534
  const hastChildren = aligned.children;
@@ -2372,7 +2453,7 @@ function createRegistry(onEmpty) {
2372
2453
 
2373
2454
  // src/components/pluginChain.ts
2374
2455
  import rehypeKatex from "rehype-katex";
2375
- import rehypeRaw from "rehype-raw";
2456
+ import rehypeRaw from "@ai-markdown/rehype-raw";
2376
2457
  import rehypeUnwrapImages from "rehype-unwrap-images";
2377
2458
 
2378
2459
  // src/components/rehypeUnwrapCrossChunkImages.ts