@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.js CHANGED
@@ -1533,11 +1533,20 @@ var AIMarkdownRenderStateProvider = ({
1533
1533
  var context_default = AIMarkdownRenderStateProvider;
1534
1534
 
1535
1535
  // src/preprocessors/latex.ts
1536
+ function getRepeatedMarkerLength(content, start, marker) {
1537
+ let end = start;
1538
+ while (end < content.length && content[end] === marker) {
1539
+ end += 1;
1540
+ }
1541
+ return end - start;
1542
+ }
1536
1543
  function splitByProtectedRegions(content) {
1537
1544
  const segments = [];
1538
1545
  let lastIndex = 0;
1539
1546
  let inlineStart = -1;
1540
1547
  let multilineStart = -1;
1548
+ let multilineFenceMarker = null;
1549
+ let multilineFenceLength = 0;
1541
1550
  function pushProtected(start, end) {
1542
1551
  if (start > lastIndex) {
1543
1552
  segments.push({ text: content.substring(lastIndex, start), isCode: false });
@@ -1547,15 +1556,22 @@ function splitByProtectedRegions(content) {
1547
1556
  }
1548
1557
  for (let i = 0; i < content.length; i++) {
1549
1558
  const char = content[i];
1550
- if (char === "`" && i + 2 < content.length && content[i + 1] === "`" && content[i + 2] === "`") {
1559
+ const fenceLength = char === "`" || char === "~" ? getRepeatedMarkerLength(content, i, char) : 0;
1560
+ if ((char === "`" || char === "~") && fenceLength >= 3) {
1551
1561
  if (multilineStart === -1) {
1552
1562
  inlineStart = -1;
1553
1563
  multilineStart = i;
1554
- i += 2;
1555
- } else {
1556
- pushProtected(multilineStart, i + 3);
1564
+ multilineFenceMarker = char;
1565
+ multilineFenceLength = fenceLength;
1566
+ i += fenceLength - 1;
1567
+ } else if (char === multilineFenceMarker && fenceLength >= multilineFenceLength) {
1568
+ pushProtected(multilineStart, i + fenceLength);
1557
1569
  multilineStart = -1;
1558
- i += 2;
1570
+ multilineFenceMarker = null;
1571
+ multilineFenceLength = 0;
1572
+ i += fenceLength - 1;
1573
+ } else {
1574
+ i += fenceLength - 1;
1559
1575
  }
1560
1576
  } else if (char === "`" && multilineStart === -1) {
1561
1577
  if (inlineStart === -1) {
@@ -1575,6 +1591,9 @@ function splitByProtectedRegions(content) {
1575
1591
  }
1576
1592
  }
1577
1593
  }
1594
+ if (multilineStart !== -1) {
1595
+ pushProtected(multilineStart, content.length);
1596
+ }
1578
1597
  if (lastIndex < content.length) {
1579
1598
  segments.push({ text: content.substring(lastIndex), isCode: false });
1580
1599
  }
@@ -1650,6 +1669,54 @@ function escapeLatexPipes(text) {
1650
1669
  return match;
1651
1670
  });
1652
1671
  }
1672
+ function escapeLatexPipesInUnclosed(text) {
1673
+ let unclosedStart = -1;
1674
+ let i = 0;
1675
+ while (i < text.length) {
1676
+ if (text[i] === "$" && i + 1 < text.length && text[i + 1] === "$") {
1677
+ if (unclosedStart === -1) {
1678
+ unclosedStart = i;
1679
+ i += 2;
1680
+ } else {
1681
+ unclosedStart = -1;
1682
+ i += 2;
1683
+ }
1684
+ } else if (text[i] === "$" && (i === 0 || text[i - 1] !== "\\") && (i + 1 >= text.length || text[i + 1] !== "$")) {
1685
+ if (unclosedStart === -1) {
1686
+ unclosedStart = i;
1687
+ } else {
1688
+ unclosedStart = -1;
1689
+ }
1690
+ i += 1;
1691
+ } else {
1692
+ i += 1;
1693
+ }
1694
+ }
1695
+ if (unclosedStart === -1) return text;
1696
+ const before = text.substring(0, unclosedStart);
1697
+ const delimLen = text[unclosedStart + 1] === "$" ? 2 : 1;
1698
+ const delim = text.substring(unclosedStart, unclosedStart + delimLen);
1699
+ const tail = text.substring(unclosedStart + delimLen);
1700
+ return before + delim + replaceUnescapedPipes(tail);
1701
+ }
1702
+ function truncateUnclosedLatexBlock(text) {
1703
+ let unclosedStart = -1;
1704
+ let i = 0;
1705
+ while (i < text.length) {
1706
+ if (text[i] === "$" && i + 1 < text.length && text[i + 1] === "$") {
1707
+ if (unclosedStart === -1) {
1708
+ unclosedStart = i;
1709
+ } else {
1710
+ unclosedStart = -1;
1711
+ }
1712
+ i += 2;
1713
+ } else {
1714
+ i += 1;
1715
+ }
1716
+ }
1717
+ if (unclosedStart === -1) return text;
1718
+ return text.substring(0, unclosedStart).trimEnd();
1719
+ }
1653
1720
  function escapeTextUnderscores(text) {
1654
1721
  return text.replaceAll(ESCAPE_TEXT_UNDERSCORES_REGEX, (_match, textContent) => {
1655
1722
  const escapedTextContent = textContent.replaceAll(/(?<!\\)_/g, "\\_");
@@ -1669,8 +1736,10 @@ function preprocessLaTeX(str) {
1669
1736
  text = escapeCurrencyDollarSigns(text);
1670
1737
  text = convertLatexDelimiters(text);
1671
1738
  text = escapeLatexPipes(text);
1739
+ text = escapeLatexPipesInUnclosed(text);
1672
1740
  text = escapeTextUnderscores(text);
1673
1741
  text = convertSingleToDoubleDollar(text);
1742
+ text = truncateUnclosedLatexBlock(text);
1674
1743
  return text;
1675
1744
  });
1676
1745
  return result.join("");