@braze/sanity-plugin-ai-translation 0.2.54 → 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
@@ -174,6 +174,11 @@ const isLoggingEnabled = () => process.env.NEXT_PUBLIC_SANITY_STUDIO_AI_TRANSLAT
174
174
  selectedReferenceIds: action.payload
175
175
  };
176
176
  case "SET_TRANSLATION_JOB_STATUS": {
177
+ if (!action.payload._id)
178
+ return devLog.warn("SET_TRANSLATION_JOB_STATUS action missing _id", {
179
+ ...logContext,
180
+ payload: action.payload
181
+ }), context;
177
182
  const jobKey = `${undraftId(action.payload._id)}_${action.payload.language}`, newTranslationJobs = new Map(context.translationJobs), existingJob = newTranslationJobs.get(jobKey), mergedJob = defaults(action.payload, existingJob);
178
183
  return newTranslationJobs.set(jobKey, mergedJob), {
179
184
  ...context,
@@ -3897,10 +3902,14 @@ function serializeVTTCaptionText(captions) {
3897
3902
  firstCaption: captions.length > 0 ? captions[0] : null
3898
3903
  }), !Array.isArray(captions) || captions.length === 0)
3899
3904
  return "";
3900
- const result = captions.map((caption, index) => (devLog.debug(`Processing caption ${index}:`, {
3901
- text: caption.text,
3902
- hasText: typeof caption.text == "string"
3903
- }), `[${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(`
3904
3913
  `);
3905
3914
  return devLog.debug("serializeVTTCaptionText - output:", {
3906
3915
  resultLength: result.length,
@@ -3917,29 +3926,46 @@ function deserializeVTTCaptionText(translationText, originalMetadata) {
3917
3926
  return [];
3918
3927
  const result = [], lines = translationText.split(`
3919
3928
  `), lineRegex = /^\[(\d+)\]\s*(.*)$/;
3929
+ let currentIndex = null, currentTextParts = [];
3920
3930
  for (const line of lines) {
3921
3931
  const match = line.match(lineRegex);
3922
- if (!match) {
3923
- devLog.warn("Failed to parse caption line", { line });
3924
- continue;
3925
- }
3926
- const index = Number.parseInt(match[1], 10), text = match[2];
3927
- devLog.debug("Found caption:", {
3928
- index,
3929
- text
3930
- });
3931
- 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);
3932
3959
  if (metadata) {
3933
- devLog.debug(`Found matching metadata for index ${index}`);
3934
3960
  const { _originalIndex, ...metadataWithoutIndex } = metadata;
3935
3961
  result.push({
3936
3962
  ...metadataWithoutIndex,
3937
3963
  text
3938
3964
  });
3939
3965
  } else
3940
- devLog.warn("Missing metadata for caption", { index }), result.push({
3966
+ devLog.warn("Missing metadata for caption", { index: currentIndex }), result.push({
3941
3967
  _type: "vtt.cue",
3942
- _key: `caption-${index}`,
3968
+ _key: `caption-${currentIndex}`,
3943
3969
  text,
3944
3970
  startTime: 0,
3945
3971
  endTime: 0
@@ -4176,10 +4202,14 @@ function serializeWistiaCaptionText(cues) {
4176
4202
  firstCue: cues.length > 0 ? cues[0] : null
4177
4203
  }), !Array.isArray(cues) || cues.length === 0)
4178
4204
  return "";
4179
- const result = cues.map((cue) => (devLog.debug(`Processing cue ${cue.index}:`, {
4180
- text: cue.text,
4181
- hasText: typeof cue.text == "string"
4182
- }), `[${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(`
4183
4213
  `);
4184
4214
  return devLog.debug("serializeWistiaCaptionText - output:", {
4185
4215
  resultLength: result.length,
@@ -4198,19 +4228,24 @@ function deserializeWistiaCaptionText(translationText, srtCuesMetadata) {
4198
4228
  `), lineRegex = /^\[(\d+)\]\s*(.*)$/, metadataByIndex = /* @__PURE__ */ new Map();
4199
4229
  for (const metadata of srtCuesMetadata)
4200
4230
  metadataByIndex.set(metadata.index, { startTime: metadata.startTime, endTime: metadata.endTime });
4231
+ let currentIndex = null, currentTextParts = [];
4201
4232
  for (const line of lines) {
4202
4233
  const match = line.match(lineRegex);
4203
- if (!match) {
4204
- devLog.warn("Failed to parse caption line", { line });
4205
- continue;
4206
- }
4207
- const index = Number.parseInt(match[1], 10), text = match[2];
4208
- devLog.debug("Found caption:", {
4209
- index,
4210
- text
4211
- });
4212
- const metadata = metadataByIndex.get(index);
4213
- 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 });
4214
4249
  }
4215
4250
  result.length > 0 && result[result.length - 1] === "" && result.pop();
4216
4251
  const reconstructed = result.join(`