@braze/sanity-plugin-ai-translation 0.2.58 → 0.2.61

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
@@ -1564,11 +1564,26 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
1564
1564
  }, parseReviewFormat = (text, originalDoc) => {
1565
1565
  const root = [], stack = [], lines = text.split(`
1566
1566
  `);
1567
+ devLog.debug("Parsing response text", {
1568
+ lineCount: lines.length,
1569
+ firstLines: lines.slice(0, 10).map((l, idx) => ({ line: idx, content: l.substring(0, 100) })),
1570
+ hasFieldTags: text.includes("<field"),
1571
+ hasBlockTags: text.includes("<block"),
1572
+ hasSpanTags: text.includes("<span"),
1573
+ textLength: text.length,
1574
+ textPreview: text.substring(0, 500)
1575
+ });
1576
+ let processedLineCount = 0, skippedEmptyLineCount = 0, matchedPatternCount = 0;
1567
1577
  for (let i = 0; i < lines.length; i++) {
1568
1578
  const trimmed = lines[i].trim();
1569
- if (!trimmed) continue;
1579
+ if (!trimmed) {
1580
+ skippedEmptyLineCount++;
1581
+ continue;
1582
+ }
1583
+ processedLineCount++;
1570
1584
  const compactStringMatch = trimmed.match(/^(\w+):\s*(.*)$/);
1571
1585
  if (compactStringMatch && !trimmed.includes("<")) {
1586
+ matchedPatternCount++;
1572
1587
  const [, fieldName, text2] = compactStringMatch, node = {
1573
1588
  type: "field",
1574
1589
  name: fieldName,
@@ -1585,6 +1600,7 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
1585
1600
  }
1586
1601
  const compactStartMatch = trimmed.match(/^---\s+(\w+)\s+---$/);
1587
1602
  if (compactStartMatch) {
1603
+ matchedPatternCount++;
1588
1604
  const fieldName = compactStartMatch[1], contentLines = [];
1589
1605
  for (i++; i < lines.length; ) {
1590
1606
  const currentLine = lines[i];
@@ -1620,6 +1636,12 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
1620
1636
  }
1621
1637
  const openMatch = trimmed.match(/^<(field|block|span)([^>]*)>(.*)$/);
1622
1638
  if (openMatch) {
1639
+ matchedPatternCount++, devLog.debug("Matched opening tag", {
1640
+ line: i,
1641
+ tagName: openMatch[1],
1642
+ attributes: openMatch[2],
1643
+ contentPreview: openMatch[3]?.substring(0, 50)
1644
+ });
1623
1645
  const [, tagName, attrsStr, content] = openMatch, attributes = parseAttributes(attrsStr), node = {
1624
1646
  type: tagName,
1625
1647
  name: attributes.name,
@@ -1678,7 +1700,9 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
1678
1700
  stack.push(node);
1679
1701
  continue;
1680
1702
  }
1681
- if (trimmed.match(/^<\/(field|block|span)>$/) && stack.length > 0) {
1703
+ const closeMatch = trimmed.match(/^<\/(field|block|span)>/);
1704
+ if (closeMatch && stack.length > 0) {
1705
+ matchedPatternCount++;
1682
1706
  const node = stack.pop();
1683
1707
  if (node)
1684
1708
  if (stack.length > 0) {
@@ -1686,9 +1710,65 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
1686
1710
  parent && parent.children.push(node);
1687
1711
  } else
1688
1712
  root.push(node);
1713
+ let afterClosingTag = trimmed.substring(closeMatch[0].length).trim();
1714
+ if (afterClosingTag = afterClosingTag.replace(/<!--[\s\S]*?-->/g, "").trim(), afterClosingTag) {
1715
+ const remainingOpenMatch = afterClosingTag.match(/^<(field|block|span)([^>]*)>(.*)$/);
1716
+ if (remainingOpenMatch) {
1717
+ const [, tagName, attrsStr, content] = remainingOpenMatch, attributes = parseAttributes(attrsStr), remainingNode = {
1718
+ type: tagName,
1719
+ name: attributes.name,
1720
+ text: content ? unescapeXml(content) : void 0,
1721
+ attributes,
1722
+ children: []
1723
+ }, remainingClosingTag = `</${tagName}>`;
1724
+ if (content.includes(remainingClosingTag))
1725
+ if (remainingNode.text = unescapeXml(content.replace(remainingClosingTag, "")), stack.length > 0) {
1726
+ const parent = stack[stack.length - 1];
1727
+ parent && parent.children.push(remainingNode);
1728
+ } else
1729
+ root.push(remainingNode);
1730
+ else if (attributes.type === "string" && content)
1731
+ if (remainingNode.text = unescapeXml(content), stack.length > 0) {
1732
+ const parent = stack[stack.length - 1];
1733
+ parent && parent.children.push(remainingNode);
1734
+ } else
1735
+ root.push(remainingNode);
1736
+ else if (attributes.type === "string" && !content) {
1737
+ const contentLines = [];
1738
+ for (i++; i < lines.length; ) {
1739
+ const currentLine = lines[i];
1740
+ if (currentLine.trim() === `</${tagName}>` || currentLine.trim().startsWith(`</${tagName}>`))
1741
+ break;
1742
+ contentLines.push(currentLine), i++;
1743
+ }
1744
+ if (remainingNode.text = contentLines.join(`
1745
+ `).trim(), stack.length > 0) {
1746
+ const parent = stack[stack.length - 1];
1747
+ parent && parent.children.push(remainingNode);
1748
+ } else
1749
+ root.push(remainingNode);
1750
+ } else
1751
+ stack.push(remainingNode);
1752
+ }
1753
+ }
1754
+ continue;
1689
1755
  }
1756
+ processedLineCount <= 20 && devLog.debug("Line did not match any pattern", {
1757
+ line: i,
1758
+ trimmed,
1759
+ startsWithLessThan: trimmed.startsWith("<"),
1760
+ startsWithField: trimmed.startsWith("<field"),
1761
+ startsWithBacktick: trimmed.startsWith("`")
1762
+ });
1690
1763
  }
1691
- return root;
1764
+ return devLog.debug("Parse complete", {
1765
+ totalLines: lines.length,
1766
+ processedLines: processedLineCount,
1767
+ skippedEmptyLines: skippedEmptyLineCount,
1768
+ rootNodeCount: root.length,
1769
+ stackLength: stack.length,
1770
+ matchedPatterns: matchedPatternCount
1771
+ }), root;
1692
1772
  }, parseAttributes = (attrStr) => {
1693
1773
  const attrs = {}, attrRegex = /(\w+)="([^"]*)"/g;
1694
1774
  let match = attrRegex.exec(attrStr);