@ai-react-markdown/core 1.2.6 → 1.2.8

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
@@ -1708,29 +1708,24 @@ function escapeLatexPipes(text) {
1708
1708
  return match;
1709
1709
  });
1710
1710
  }
1711
- function escapeLatexPipesInUnclosed(text) {
1711
+ function findUnclosedDelimiterStart(text, mode) {
1712
1712
  let unclosedStart = -1;
1713
1713
  let i = 0;
1714
1714
  while (i < text.length) {
1715
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
- }
1716
+ unclosedStart = unclosedStart === -1 ? i : -1;
1717
+ i += 2;
1718
+ } else if (mode === "both" && text[i] === "$" && (i === 0 || text[i - 1] !== "\\") && (i + 1 >= text.length || text[i + 1] !== "$")) {
1719
+ unclosedStart = unclosedStart === -1 ? i : -1;
1729
1720
  i += 1;
1730
1721
  } else {
1731
1722
  i += 1;
1732
1723
  }
1733
1724
  }
1725
+ return unclosedStart;
1726
+ }
1727
+ function escapeLatexPipesInUnclosed(text) {
1728
+ const unclosedStart = findUnclosedDelimiterStart(text, "both");
1734
1729
  if (unclosedStart === -1) return text;
1735
1730
  const before = text.substring(0, unclosedStart);
1736
1731
  const delimLen = text[unclosedStart + 1] === "$" ? 2 : 1;
@@ -1738,6 +1733,11 @@ function escapeLatexPipesInUnclosed(text) {
1738
1733
  const tail = text.substring(unclosedStart + delimLen);
1739
1734
  return before + delim + replaceUnescapedPipes(tail);
1740
1735
  }
1736
+ function truncateUnclosedLatexBlock(text) {
1737
+ const unclosedStart = findUnclosedDelimiterStart(text, "double-only");
1738
+ if (unclosedStart === -1) return text;
1739
+ return text.substring(0, unclosedStart).trimEnd();
1740
+ }
1741
1741
  function escapeTextUnderscores(text) {
1742
1742
  return text.replaceAll(ESCAPE_TEXT_UNDERSCORES_REGEX, (_match, textContent) => {
1743
1743
  const escapedTextContent = textContent.replaceAll(/(?<!\\)_/g, "\\_");
@@ -1760,6 +1760,7 @@ function preprocessLaTeX(str) {
1760
1760
  text = escapeLatexPipesInUnclosed(text);
1761
1761
  text = escapeTextUnderscores(text);
1762
1762
  text = convertSingleToDoubleDollar(text);
1763
+ text = truncateUnclosedLatexBlock(text);
1763
1764
  return text;
1764
1765
  });
1765
1766
  return result.join("");