@@ -4,7 +4,6 @@ import path from "path";
44import { v4 as uuidv4 } from "uuid" ;
55import _ from "lodash" ;
66import axios from "axios" ;
7- import jsonpath from "jsonpath" ;
87import pLimit from 'p-limit' ;
98import { JSDOM } from "jsdom" ;
109import { jsonToHtml , jsonToMarkdown , htmlToJson } from '@contentstack/json-rte-serializer' ;
@@ -368,23 +367,45 @@ const cleanBrackets = (lang_value: any) => {
368367} ;
369368
370369// Helper function to process arrays and resolve IDs for entries and assets
370+ // Replaces jsonpath.query(array, "$..id") with direct item-level processing
371+ // (following the same pattern as Team Fury's processField for arrays)
371372const processArrayFields = ( array : any , entryId : any , assetId : any ) => {
372- const ids = jsonpath . query ( array , "$..id" ) ;
373- ids . forEach ( ( id : any , i : number ) => {
374- if ( id in entryId ) {
375- array . splice ( i , 1 , entryId [ id ] ) ;
376- } else if ( id in assetId ) {
377- array . splice ( i , 1 , assetId ?. [ id ] ) ;
373+ // Handle primitive arrays directly (strings, numbers, booleans)
374+ if ( array . every ( ( item : any ) => typeof item !== 'object' || item === null ) ) {
375+ return array ;
376+ }
377+
378+ const processedArray = array . reduce ( ( acc : any [ ] , item : any ) => {
379+ if ( item ?. sys ?. id ) {
380+ // Handle Contentful system links (Entry or Asset references)
381+ const { linkType, id } = item . sys ;
382+ if ( linkType === 'Entry' && id in entryId ) {
383+ acc . push ( entryId [ id ] ) ;
384+ } else if ( linkType === 'Asset' && id in assetId ) {
385+ acc . push ( assetId [ id ] ) ;
386+ } else {
387+ // Keep unresolved references intact — the import process has a
388+ // separate reference update step that resolves these later.
389+ acc . push ( item ) ;
390+ }
391+ } else if ( item !== null && typeof item === 'object' ) {
392+ // Keep non-reference objects as-is (nested objects, RTE fragments, etc.)
393+ acc . push ( item ) ;
394+ } else if ( item !== null && item !== undefined ) {
395+ // Keep primitive values (strings, numbers, booleans)
396+ acc . push ( item ) ;
378397 }
379- } ) ;
380- // Clean up empty objects
381- const cleanedArray = JSON . stringify ( array )
398+ return acc ;
399+ } , [ ] ) ;
400+
401+ // Clean up empty objects (matches original cleanup behavior)
402+ const cleanedArray = JSON . stringify ( processedArray )
382403 . replace ( / { } , / g, "" )
383404 . replace ( / , { } / g, "" )
384405 . replace ( / , { } ,/ g, "" )
385406 . replace ( / { } / g, "" ) ;
386407 const result = typeof cleanedArray === 'string' && JSON . parse ( cleanedArray ) ;
387- return result . length > 0 ? result : undefined ;
408+ return Array . isArray ( result ) && result . length > 0 ? result : undefined ;
388409} ;
389410
390411// Helper function to process Rich Text Editor (RTE) or nested object
0 commit comments