@braze/sanity-plugin-ai-translation 0.2.65 → 0.2.66
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 +45 -4
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1562,7 +1562,42 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1562
1562
|
codeBlocksRemoved,
|
|
1563
1563
|
preview: cleaned.trim().substring(0, 200)
|
|
1564
1564
|
}), cleaned.trim();
|
|
1565
|
-
}, TAG_NAMES = ["field", "block", "span"], looksLikeXmlResponse = (text) => TAG_NAMES.some((tag) => text.includes(`<${tag}`))
|
|
1565
|
+
}, TAG_NAMES = ["field", "block", "span"], looksLikeXmlResponse = (text) => TAG_NAMES.some((tag) => text.includes(`<${tag}`));
|
|
1566
|
+
function parseCompactFormatSections(text, originalDoc) {
|
|
1567
|
+
const nodes = [], lines = text.split(`
|
|
1568
|
+
`), compactStartRegex = /^---\s+(\w+)\s+---$/;
|
|
1569
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1570
|
+
const match = lines[i].trim().match(compactStartRegex);
|
|
1571
|
+
if (!match) continue;
|
|
1572
|
+
const fieldName = match[1], contentLines = [];
|
|
1573
|
+
for (i++; i < lines.length; ) {
|
|
1574
|
+
const currentLine = lines[i];
|
|
1575
|
+
if (currentLine.trim().match(new RegExp(`^---\\s+/${fieldName}\\s+---$`)))
|
|
1576
|
+
break;
|
|
1577
|
+
contentLines.push(currentLine), i++;
|
|
1578
|
+
}
|
|
1579
|
+
const content = contentLines.join(`
|
|
1580
|
+
`);
|
|
1581
|
+
let contentType;
|
|
1582
|
+
try {
|
|
1583
|
+
const fieldValue = get(originalDoc, [fieldName]);
|
|
1584
|
+
if (fieldValue && typeof fieldValue == "object" && fieldValue !== null) {
|
|
1585
|
+
const fieldType = fieldValue._type;
|
|
1586
|
+
typeof fieldType == "string" && fieldType.startsWith("translatable.") && (contentType = fieldType);
|
|
1587
|
+
}
|
|
1588
|
+
} catch {
|
|
1589
|
+
}
|
|
1590
|
+
contentType || (content.includes("<block") || content.includes("<span") ? contentType = "translatable.richtext" : contentType = "translatable.vtt.captions"), nodes.push({
|
|
1591
|
+
type: "field",
|
|
1592
|
+
name: fieldName,
|
|
1593
|
+
text: content,
|
|
1594
|
+
attributes: { type: contentType },
|
|
1595
|
+
children: []
|
|
1596
|
+
});
|
|
1597
|
+
}
|
|
1598
|
+
return nodes;
|
|
1599
|
+
}
|
|
1600
|
+
const parseResponseToNodes = (text, originalDoc) => {
|
|
1566
1601
|
if (looksLikeXmlResponse(text))
|
|
1567
1602
|
try {
|
|
1568
1603
|
const wrapped = `<root>${text}</root>`, parsed = new XMLParser({
|
|
@@ -1575,9 +1610,15 @@ const globalTransformRegistry = new TransformRegistry(), callGPT5 = async (apiKe
|
|
|
1575
1610
|
isArray: (tagName) => TAG_NAMES.includes(tagName)
|
|
1576
1611
|
}).parse(wrapped);
|
|
1577
1612
|
if (parsed && typeof parsed == "object" && parsed.root) {
|
|
1578
|
-
const
|
|
1579
|
-
if (
|
|
1580
|
-
|
|
1613
|
+
const xmlNodes = convertXmlObjectToParsedNodes(parsed.root), compactNodes = parseCompactFormatSections(text, originalDoc);
|
|
1614
|
+
if (xmlNodes.length > 0 || compactNodes.length > 0) {
|
|
1615
|
+
const merged = [...xmlNodes, ...compactNodes];
|
|
1616
|
+
return devLog.debug("Parsed response with XML parser + compact sections", {
|
|
1617
|
+
xmlNodeCount: xmlNodes.length,
|
|
1618
|
+
compactNodeCount: compactNodes.length,
|
|
1619
|
+
total: merged.length
|
|
1620
|
+
}), merged;
|
|
1621
|
+
}
|
|
1581
1622
|
}
|
|
1582
1623
|
} catch (err) {
|
|
1583
1624
|
devLog.warn("XML parse failed, falling back to regex parser", {
|