@ai-react-markdown/core 1.2.5 → 1.2.6

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,36 @@ 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
+ }
1692
1741
  function escapeTextUnderscores(text) {
1693
1742
  return text.replaceAll(ESCAPE_TEXT_UNDERSCORES_REGEX, (_match, textContent) => {
1694
1743
  const escapedTextContent = textContent.replaceAll(/(?<!\\)_/g, "\\_");
@@ -1708,6 +1757,7 @@ function preprocessLaTeX(str) {
1708
1757
  text = escapeCurrencyDollarSigns(text);
1709
1758
  text = convertLatexDelimiters(text);
1710
1759
  text = escapeLatexPipes(text);
1760
+ text = escapeLatexPipesInUnclosed(text);
1711
1761
  text = escapeTextUnderscores(text);
1712
1762
  text = convertSingleToDoubleDollar(text);
1713
1763
  return text;