@braze/sanity-plugin-ai-translation 0.2.55 → 0.2.56

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
@@ -3902,10 +3902,14 @@ function serializeVTTCaptionText(captions) {
3902
3902
  firstCaption: captions.length > 0 ? captions[0] : null
3903
3903
  }), !Array.isArray(captions) || captions.length === 0)
3904
3904
  return "";
3905
- const result = captions.map((caption, index) => (devLog.debug(`Processing caption ${index}:`, {
3906
- text: caption.text,
3907
- hasText: typeof caption.text == "string"
3908
- }), `[${index}] ${caption.text || ""}`)).join(`
3905
+ const result = captions.map((caption, index) => {
3906
+ devLog.debug(`Processing caption ${index}:`, {
3907
+ text: caption.text,
3908
+ hasText: typeof caption.text == "string"
3909
+ });
3910
+ const escapedText = (caption.text || "").replace(/\n/g, "\\n");
3911
+ return `[${index}] ${escapedText}`;
3912
+ }).join(`
3909
3913
  `);
3910
3914
  return devLog.debug("serializeVTTCaptionText - output:", {
3911
3915
  resultLength: result.length,
@@ -3922,29 +3926,46 @@ function deserializeVTTCaptionText(translationText, originalMetadata) {
3922
3926
  return [];
3923
3927
  const result = [], lines = translationText.split(`
3924
3928
  `), lineRegex = /^\[(\d+)\]\s*(.*)$/;
3929
+ let currentIndex = null, currentTextParts = [];
3925
3930
  for (const line of lines) {
3926
3931
  const match = line.match(lineRegex);
3927
- if (!match) {
3928
- devLog.warn("Failed to parse caption line", { line });
3929
- continue;
3930
- }
3931
- const index = Number.parseInt(match[1], 10), text = match[2];
3932
- devLog.debug("Found caption:", {
3933
- index,
3934
- text
3935
- });
3936
- const metadata = originalMetadata.find((m) => m._originalIndex === index);
3932
+ if (match) {
3933
+ if (currentIndex !== null) {
3934
+ const text = currentTextParts.join(`
3935
+ `).replace(/\\n/g, `
3936
+ `), metadata = originalMetadata.find((m) => m._originalIndex === currentIndex);
3937
+ if (metadata) {
3938
+ const { _originalIndex, ...metadataWithoutIndex } = metadata;
3939
+ result.push({
3940
+ ...metadataWithoutIndex,
3941
+ text
3942
+ });
3943
+ } else
3944
+ devLog.warn("Missing metadata for caption", { index: currentIndex }), result.push({
3945
+ _type: "vtt.cue",
3946
+ _key: `caption-${currentIndex}`,
3947
+ text,
3948
+ startTime: 0,
3949
+ endTime: 0
3950
+ });
3951
+ }
3952
+ currentIndex = Number.parseInt(match[1], 10), currentTextParts = [match[2]];
3953
+ } else currentIndex !== null ? currentTextParts.push(line) : devLog.warn("Failed to parse caption line", { line });
3954
+ }
3955
+ if (currentIndex !== null) {
3956
+ const text = currentTextParts.join(`
3957
+ `).replace(/\\n/g, `
3958
+ `), metadata = originalMetadata.find((m) => m._originalIndex === currentIndex);
3937
3959
  if (metadata) {
3938
- devLog.debug(`Found matching metadata for index ${index}`);
3939
3960
  const { _originalIndex, ...metadataWithoutIndex } = metadata;
3940
3961
  result.push({
3941
3962
  ...metadataWithoutIndex,
3942
3963
  text
3943
3964
  });
3944
3965
  } else
3945
- devLog.warn("Missing metadata for caption", { index }), result.push({
3966
+ devLog.warn("Missing metadata for caption", { index: currentIndex }), result.push({
3946
3967
  _type: "vtt.cue",
3947
- _key: `caption-${index}`,
3968
+ _key: `caption-${currentIndex}`,
3948
3969
  text,
3949
3970
  startTime: 0,
3950
3971
  endTime: 0
@@ -4181,10 +4202,14 @@ function serializeWistiaCaptionText(cues) {
4181
4202
  firstCue: cues.length > 0 ? cues[0] : null
4182
4203
  }), !Array.isArray(cues) || cues.length === 0)
4183
4204
  return "";
4184
- const result = cues.map((cue) => (devLog.debug(`Processing cue ${cue.index}:`, {
4185
- text: cue.text,
4186
- hasText: typeof cue.text == "string"
4187
- }), `[${cue.index}] ${cue.text}`)).join(`
4205
+ const result = cues.map((cue) => {
4206
+ devLog.debug(`Processing cue ${cue.index}:`, {
4207
+ text: cue.text,
4208
+ hasText: typeof cue.text == "string"
4209
+ });
4210
+ const escapedText = cue.text.replace(/\n/g, "\\n");
4211
+ return `[${cue.index}] ${escapedText}`;
4212
+ }).join(`
4188
4213
  `);
4189
4214
  return devLog.debug("serializeWistiaCaptionText - output:", {
4190
4215
  resultLength: result.length,
@@ -4203,19 +4228,24 @@ function deserializeWistiaCaptionText(translationText, srtCuesMetadata) {
4203
4228
  `), lineRegex = /^\[(\d+)\]\s*(.*)$/, metadataByIndex = /* @__PURE__ */ new Map();
4204
4229
  for (const metadata of srtCuesMetadata)
4205
4230
  metadataByIndex.set(metadata.index, { startTime: metadata.startTime, endTime: metadata.endTime });
4231
+ let currentIndex = null, currentTextParts = [];
4206
4232
  for (const line of lines) {
4207
4233
  const match = line.match(lineRegex);
4208
- if (!match) {
4209
- devLog.warn("Failed to parse caption line", { line });
4210
- continue;
4211
- }
4212
- const index = Number.parseInt(match[1], 10), text = match[2];
4213
- devLog.debug("Found caption:", {
4214
- index,
4215
- text
4216
- });
4217
- const metadata = metadataByIndex.get(index);
4218
- metadata ? (devLog.debug(`Found matching metadata for index ${index}`), result.push(`${index}`), result.push(`${metadata.startTime} --> ${metadata.endTime}`), result.push(text), result.push("")) : devLog.warn("Missing metadata for caption", { index });
4234
+ if (match) {
4235
+ if (currentIndex !== null) {
4236
+ const text = currentTextParts.join(`
4237
+ `).replace(/\\n/g, `
4238
+ `), metadata = metadataByIndex.get(currentIndex);
4239
+ metadata ? (result.push(`${currentIndex}`), result.push(`${metadata.startTime} --> ${metadata.endTime}`), result.push(text), result.push("")) : devLog.warn("Missing metadata for caption", { index: currentIndex });
4240
+ }
4241
+ currentIndex = Number.parseInt(match[1], 10), currentTextParts = [match[2]];
4242
+ } else currentIndex !== null ? currentTextParts.push(line) : devLog.warn("Failed to parse caption line", { line });
4243
+ }
4244
+ if (currentIndex !== null) {
4245
+ const text = currentTextParts.join(`
4246
+ `).replace(/\\n/g, `
4247
+ `), metadata = metadataByIndex.get(currentIndex);
4248
+ metadata ? (result.push(`${currentIndex}`), result.push(`${metadata.startTime} --> ${metadata.endTime}`), result.push(text), result.push("")) : devLog.warn("Missing metadata for caption", { index: currentIndex });
4219
4249
  }
4220
4250
  result.length > 0 && result[result.length - 1] === "" && result.pop();
4221
4251
  const reconstructed = result.join(`