@ai-react-markdown/core 1.2.5 → 1.2.7

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
@@ -1572,11 +1572,20 @@ var AIMarkdownRenderStateProvider = ({
1572
1572
  var context_default = AIMarkdownRenderStateProvider;
1573
1573
 
1574
1574
  // src/preprocessors/latex.ts
1575
+ function getRepeatedMarkerLength(content, start, marker) {
1576
+ let end = start;
1577
+ while (end < content.length && content[end] === marker) {
1578
+ end += 1;
1579
+ }
1580
+ return end - start;
1581
+ }
1575
1582
  function splitByProtectedRegions(content) {
1576
1583
  const segments = [];
1577
1584
  let lastIndex = 0;
1578
1585
  let inlineStart = -1;
1579
1586
  let multilineStart = -1;
1587
+ let multilineFenceMarker = null;
1588
+ let multilineFenceLength = 0;
1580
1589
  function pushProtected(start, end) {
1581
1590
  if (start > lastIndex) {
1582
1591
  segments.push({ text: content.substring(lastIndex, start), isCode: false });
@@ -1586,15 +1595,22 @@ function splitByProtectedRegions(content) {
1586
1595
  }
1587
1596
  for (let i = 0; i < content.length; i++) {
1588
1597
  const char = content[i];
1589
- if (char === "`" && i + 2 < content.length && content[i + 1] === "`" && content[i + 2] === "`") {
1598
+ const fenceLength = char === "`" || char === "~" ? getRepeatedMarkerLength(content, i, char) : 0;
1599
+ if ((char === "`" || char === "~") && fenceLength >= 3) {
1590
1600
  if (multilineStart === -1) {
1591
1601
  inlineStart = -1;
1592
1602
  multilineStart = i;
1593
- i += 2;
1594
- } else {
1595
- pushProtected(multilineStart, i + 3);
1603
+ multilineFenceMarker = char;
1604
+ multilineFenceLength = fenceLength;
1605
+ i += fenceLength - 1;
1606
+ } else if (char === multilineFenceMarker && fenceLength >= multilineFenceLength) {
1607
+ pushProtected(multilineStart, i + fenceLength);
1596
1608
  multilineStart = -1;
1597
- i += 2;
1609
+ multilineFenceMarker = null;
1610
+ multilineFenceLength = 0;
1611
+ i += fenceLength - 1;
1612
+ } else {
1613
+ i += fenceLength - 1;
1598
1614
  }
1599
1615
  } else if (char === "`" && multilineStart === -1) {
1600
1616
  if (inlineStart === -1) {
@@ -1614,6 +1630,9 @@ function splitByProtectedRegions(content) {
1614
1630
  }
1615
1631
  }
1616
1632
  }
1633
+ if (multilineStart !== -1) {
1634
+ pushProtected(multilineStart, content.length);
1635
+ }
1617
1636
  if (lastIndex < content.length) {
1618
1637
  segments.push({ text: content.substring(lastIndex), isCode: false });
1619
1638
  }
@@ -1689,6 +1708,54 @@ function escapeLatexPipes(text) {
1689
1708
  return match;
1690
1709
  });
1691
1710
  }
1711
+ function escapeLatexPipesInUnclosed(text) {
1712
+ let unclosedStart = -1;
1713
+ let i = 0;
1714
+ while (i < text.length) {
1715
+ if (text[i] === "$" && i + 1 < text.length && text[i + 1] === "$") {
1716
+ if (unclosedStart === -1) {
1717
+ unclosedStart = i;
1718
+ i += 2;
1719
+ } else {
1720
+ unclosedStart = -1;
1721
+ i += 2;
1722
+ }
1723
+ } else if (text[i] === "$" && (i === 0 || text[i - 1] !== "\\") && (i + 1 >= text.length || text[i + 1] !== "$")) {
1724
+ if (unclosedStart === -1) {
1725
+ unclosedStart = i;
1726
+ } else {
1727
+ unclosedStart = -1;
1728
+ }
1729
+ i += 1;
1730
+ } else {
1731
+ i += 1;
1732
+ }
1733
+ }
1734
+ if (unclosedStart === -1) return text;
1735
+ const before = text.substring(0, unclosedStart);
1736
+ const delimLen = text[unclosedStart + 1] === "$" ? 2 : 1;
1737
+ const delim = text.substring(unclosedStart, unclosedStart + delimLen);
1738
+ const tail = text.substring(unclosedStart + delimLen);
1739
+ return before + delim + replaceUnescapedPipes(tail);
1740
+ }
1741
+ function truncateUnclosedLatexBlock(text) {
1742
+ let unclosedStart = -1;
1743
+ let i = 0;
1744
+ while (i < text.length) {
1745
+ if (text[i] === "$" && i + 1 < text.length && text[i + 1] === "$") {
1746
+ if (unclosedStart === -1) {
1747
+ unclosedStart = i;
1748
+ } else {
1749
+ unclosedStart = -1;
1750
+ }
1751
+ i += 2;
1752
+ } else {
1753
+ i += 1;
1754
+ }
1755
+ }
1756
+ if (unclosedStart === -1) return text;
1757
+ return text.substring(0, unclosedStart).trimEnd();
1758
+ }
1692
1759
  function escapeTextUnderscores(text) {
1693
1760
  return text.replaceAll(ESCAPE_TEXT_UNDERSCORES_REGEX, (_match, textContent) => {
1694
1761
  const escapedTextContent = textContent.replaceAll(/(?<!\\)_/g, "\\_");
@@ -1708,8 +1775,10 @@ function preprocessLaTeX(str) {
1708
1775
  text = escapeCurrencyDollarSigns(text);
1709
1776
  text = convertLatexDelimiters(text);
1710
1777
  text = escapeLatexPipes(text);
1778
+ text = escapeLatexPipesInUnclosed(text);
1711
1779
  text = escapeTextUnderscores(text);
1712
1780
  text = convertSingleToDoubleDollar(text);
1781
+ text = truncateUnclosedLatexBlock(text);
1713
1782
  return text;
1714
1783
  });
1715
1784
  return result.join("");