@braze/sanity-plugin-ai-translation 0.2.59 → 0.2.62

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
@@ -1700,8 +1700,23 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
1700
1700
  stack.push(node);
1701
1701
  continue;
1702
1702
  }
1703
- if (trimmed.match(/^<\/(field|block|span)>$/) && stack.length > 0) {
1704
- matchedPatternCount++;
1703
+ const closeMatch = trimmed.match(/^<\/(field|block|span)>/);
1704
+ if (closeMatch) {
1705
+ if (stack.length === 0) {
1706
+ devLog.warn("Closing tag found but stack is empty", {
1707
+ line: i,
1708
+ tagName: closeMatch[1],
1709
+ trimmed
1710
+ });
1711
+ continue;
1712
+ }
1713
+ matchedPatternCount++, devLog.debug("Matched closing tag", {
1714
+ line: i,
1715
+ tagName: closeMatch[1],
1716
+ trimmed,
1717
+ stackLength: stack.length,
1718
+ stackTopName: stack[stack.length - 1]?.name
1719
+ });
1705
1720
  const node = stack.pop();
1706
1721
  if (node)
1707
1722
  if (stack.length > 0) {
@@ -1709,16 +1724,84 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
1709
1724
  parent && parent.children.push(node);
1710
1725
  } else
1711
1726
  root.push(node);
1727
+ let afterClosingTag = trimmed.substring(closeMatch[0].length).trim();
1728
+ if (afterClosingTag = afterClosingTag.replace(/<!--[\s\S]*?-->/g, "").trim(), afterClosingTag) {
1729
+ const remainingOpenMatch = afterClosingTag.match(/^<(field|block|span)([^>]*)>(.*)$/);
1730
+ if (remainingOpenMatch) {
1731
+ const [, tagName, attrsStr, content] = remainingOpenMatch, attributes = parseAttributes(attrsStr), remainingNode = {
1732
+ type: tagName,
1733
+ name: attributes.name,
1734
+ text: content ? unescapeXml(content) : void 0,
1735
+ attributes,
1736
+ children: []
1737
+ }, remainingClosingTag = `</${tagName}>`;
1738
+ if (content.includes(remainingClosingTag))
1739
+ if (remainingNode.text = unescapeXml(content.replace(remainingClosingTag, "")), stack.length > 0) {
1740
+ const parent = stack[stack.length - 1];
1741
+ parent && parent.children.push(remainingNode);
1742
+ } else
1743
+ root.push(remainingNode);
1744
+ else if (attributes.type === "string" && content)
1745
+ if (remainingNode.text = unescapeXml(content), stack.length > 0) {
1746
+ const parent = stack[stack.length - 1];
1747
+ parent && parent.children.push(remainingNode);
1748
+ } else
1749
+ root.push(remainingNode);
1750
+ else if (attributes.type === "string" && !content) {
1751
+ const contentLines = [];
1752
+ for (i++; i < lines.length; ) {
1753
+ const currentLine = lines[i];
1754
+ if (currentLine.trim() === `</${tagName}>` || currentLine.trim().startsWith(`</${tagName}>`))
1755
+ break;
1756
+ contentLines.push(currentLine), i++;
1757
+ }
1758
+ if (remainingNode.text = contentLines.join(`
1759
+ `).trim(), stack.length > 0) {
1760
+ const parent = stack[stack.length - 1];
1761
+ parent && parent.children.push(remainingNode);
1762
+ } else
1763
+ root.push(remainingNode);
1764
+ } else
1765
+ stack.push(remainingNode);
1766
+ }
1767
+ }
1712
1768
  continue;
1713
1769
  }
1770
+ if (trimmed.startsWith("</")) {
1771
+ const attemptedCloseMatch = trimmed.match(/^<\/(field|block|span)>/);
1772
+ attemptedCloseMatch ? stack.length === 0 && devLog.warn("Closing tag matched but stack is empty (unmatched closing tag)", {
1773
+ line: i,
1774
+ tagName: attemptedCloseMatch[1],
1775
+ trimmed
1776
+ }) : devLog.warn("Line starts with </ but did not match closing tag pattern", {
1777
+ line: i,
1778
+ trimmed,
1779
+ stackLength: stack.length,
1780
+ stackTopName: stack[stack.length - 1]?.name,
1781
+ stackTopType: stack[stack.length - 1]?.type
1782
+ });
1783
+ }
1714
1784
  processedLineCount <= 20 && devLog.debug("Line did not match any pattern", {
1715
1785
  line: i,
1716
1786
  trimmed,
1717
1787
  startsWithLessThan: trimmed.startsWith("<"),
1718
1788
  startsWithField: trimmed.startsWith("<field"),
1719
- startsWithBacktick: trimmed.startsWith("`")
1789
+ startsWithBacktick: trimmed.startsWith("`"),
1790
+ startsWithClosingTag: trimmed.startsWith("</")
1720
1791
  });
1721
1792
  }
1793
+ if (stack.length > 0)
1794
+ for (devLog.warn("Nodes remaining in stack after parsing complete", {
1795
+ stackLength: stack.length,
1796
+ remainingNodes: stack.map((n) => ({
1797
+ type: n.type,
1798
+ name: n.name,
1799
+ attributes: n.attributes
1800
+ }))
1801
+ }); stack.length > 0; ) {
1802
+ const node = stack.pop();
1803
+ node && root.push(node);
1804
+ }
1722
1805
  return devLog.debug("Parse complete", {
1723
1806
  totalLines: lines.length,
1724
1807
  processedLines: processedLineCount,