@@ -62,6 +62,124 @@ function removeUnwanted(entry, unwantedkeys) {
6262 return entry ;
6363}
6464
65+ function isLinkObject ( obj , keyName ) {
66+ if ( obj === null || typeof obj !== 'object' || Array . isArray ( obj ) ) {
67+ return false ;
68+ }
69+
70+ const linkKeyNames = [ 'link' , 'card_link' ] ;
71+ if ( linkKeyNames . includes ( keyName ) ) {
72+ return true ;
73+ }
74+
75+ const hasTitle = 'title' in obj && obj . title !== undefined ;
76+ const hasUrl = 'url' in obj && obj . url !== undefined ;
77+ const hasHref = 'href' in obj && obj . href !== undefined ;
78+
79+ return hasTitle && ( hasUrl || hasHref ) ;
80+ }
81+
82+ function ensureHrefIsString ( linkObj ) {
83+ if ( linkObj . href === undefined || linkObj . href === null ) {
84+ linkObj . href = '' ;
85+ } else if ( typeof linkObj . href !== 'string' ) {
86+ linkObj . href = String ( linkObj . href ) ;
87+ }
88+ }
89+
90+ function isValidJsonRte ( obj ) {
91+ return obj !== null &&
92+ typeof obj === 'object' &&
93+ ! Array . isArray ( obj ) &&
94+ typeof obj . type === 'string' &&
95+ obj . type !== '' ;
96+ }
97+
98+ function cleanJsonFields ( obj ) {
99+ if ( obj === null || obj === undefined || typeof obj !== 'object' ) {
100+ return obj ;
101+ }
102+
103+ if ( Array . isArray ( obj ) ) {
104+ return obj . map ( ( item ) => cleanJsonFields ( item ) ) ;
105+ }
106+
107+ const cleaned = { } ;
108+ for ( const key in obj ) {
109+ if ( ! obj . hasOwnProperty ( key ) ) {
110+ continue ;
111+ }
112+ let value = obj [ key ] ;
113+ const isJsonField = key . endsWith ( '_rte' ) || key === 'json_rte' ;
114+ const isAccessibilityField = key . endsWith ( '_accessibility' ) || key === 'image_preset_accessibility' ;
115+
116+ if ( isJsonField ) {
117+ if ( value === '' || value === null || value === undefined ) {
118+ continue ;
119+ }
120+ if ( typeof value === 'object' && ! Array . isArray ( value ) ) {
121+ const keyCount = Object . keys ( value ) . length ;
122+ if ( keyCount === 0 ) {
123+ continue ;
124+ }
125+ if ( ! isValidJsonRte ( value ) ) {
126+ continue ;
127+ }
128+ cleaned [ key ] = value ;
129+ } else {
130+ continue ;
131+ }
132+ } else if ( isAccessibilityField && value === '' ) {
133+ cleaned [ key ] = { } ;
134+ } else if ( typeof value === 'object' && value !== null && ! Array . isArray ( value ) ) {
135+ value = cleanJsonFields ( value ) ;
136+ if ( value !== null && typeof value === 'object' ) {
137+ cleaned [ key ] = value ;
138+ }
139+ } else {
140+ cleaned [ key ] = value ;
141+ }
142+ }
143+ return cleaned ;
144+ }
145+
146+ function convertUrlToHref ( obj ) {
147+ if ( obj === null || obj === undefined ) {
148+ return obj ;
149+ }
150+
151+ if ( Array . isArray ( obj ) ) {
152+ return obj . map ( ( item ) => convertUrlToHref ( item ) ) ;
153+ }
154+
155+ if ( typeof obj === 'object' ) {
156+ const converted = { } ;
157+ for ( const key in obj ) {
158+ const value = obj [ key ] ;
159+
160+ if ( isLinkObject ( value , key ) ) {
161+ converted [ key ] = { ...value } ;
162+ if ( converted [ key ] . url !== undefined && converted [ key ] . href === undefined ) {
163+ if ( typeof converted [ key ] . url === 'string' ) {
164+ converted [ key ] . href = converted [ key ] . url ;
165+ } else if ( converted [ key ] . url === null || converted [ key ] . url === undefined ) {
166+ converted [ key ] . href = '' ;
167+ } else {
168+ converted [ key ] . href = String ( converted [ key ] . url ) ;
169+ }
170+ delete converted [ key ] . url ;
171+ }
172+ ensureHrefIsString ( converted [ key ] ) ;
173+ } else {
174+ converted [ key ] = convertUrlToHref ( value ) ;
175+ }
176+ }
177+ return converted ;
178+ }
179+
180+ return obj ;
181+ }
182+
65183function fileFields ( entry , uid , multiple ) {
66184 if ( entry [ uid ] ) {
67185 if ( typeof entry [ uid ] === 'object' || Array . isArray ( entry [ uid ] ) ) {
@@ -106,6 +224,11 @@ function addFields(contentType, entry) {
106224 }
107225 } else if ( schema . enum ) {
108226 entry [ schema . uid ] = null ;
227+ } else if ( schema . data_type === 'json' ) {
228+ const isJsonRteField = schema . uid && ( schema . uid . endsWith ( '_rte' ) || schema . uid === 'json_rte' ) ;
229+ if ( ! isJsonRteField ) {
230+ entry [ schema . uid ] = { } ;
231+ }
109232 } else if ( Object . prototype . hasOwnProperty . call ( defaults , schema . data_type ) ) {
110233 entry [ schema . uid ] = defaults [ schema . data_type ] ;
111234 } else {
@@ -126,19 +249,31 @@ function addFields(contentType, entry) {
126249
127250 if ( schema . data_type === 'group' && ! schema . multiple ) {
128251 addFields ( schema . schema , entry [ schema . uid ] ) ;
252+ if ( entry [ schema . uid ] ) {
253+ entry [ schema . uid ] = convertUrlToHref ( entry [ schema . uid ] ) ;
254+ }
129255 }
130256 if ( schema . data_type === 'group' && schema . multiple ) {
131257 entry [ schema . uid ] . forEach ( ( field ) => {
132258 addFields ( schema . schema , field ) ;
133259 } ) ;
260+ if ( entry [ schema . uid ] ) {
261+ entry [ schema . uid ] = convertUrlToHref ( entry [ schema . uid ] ) ;
262+ }
134263 }
135264 if ( schema . data_type === 'global_field' && ! schema . multiple ) {
136265 addFields ( schema . schema , entry [ schema . uid ] ) ;
266+ if ( entry [ schema . uid ] ) {
267+ entry [ schema . uid ] = convertUrlToHref ( entry [ schema . uid ] ) ;
268+ }
137269 }
138270 if ( schema . data_type === 'global_field' && schema . multiple ) {
139271 entry [ schema . uid ] . forEach ( ( field ) => {
140272 addFields ( schema . schema , field ) ;
141273 } ) ;
274+ if ( entry [ schema . uid ] ) {
275+ entry [ schema . uid ] = convertUrlToHref ( entry [ schema . uid ] ) ;
276+ }
142277 }
143278 if ( schema . data_type === 'blocks' ) {
144279 if ( ! entry [ schema . uid ] && ! Array . isArray ( entry [ schema . uid ] ) ) {
@@ -156,6 +291,9 @@ function addFields(contentType, entry) {
156291 if ( filterBlockFields . length > 0 ) {
157292 filterBlockFields . forEach ( ( bfield ) => {
158293 addFields ( block . schema , bfield [ block . uid ] ) ;
294+ if ( bfield [ block . uid ] ) {
295+ bfield [ block . uid ] = convertUrlToHref ( bfield [ block . uid ] ) ;
296+ }
159297 } ) ;
160298 } else {
161299 entry [ schema . uid ] . push ( { [ block . uid ] : { } } ) ;
@@ -169,6 +307,9 @@ function addFields(contentType, entry) {
169307 if ( filterBlockFields . length > 0 ) {
170308 filterBlockFields . forEach ( ( bfield ) => {
171309 addFields ( block . schema , bfield [ block . uid ] ) ;
310+ if ( bfield [ block . uid ] ) {
311+ bfield [ block . uid ] = convertUrlToHref ( bfield [ block . uid ] ) ;
312+ }
172313 } ) ;
173314 }
174315 }
@@ -221,8 +362,14 @@ async function getEntries(
221362 for ( let index = 0 ; index < entriesResponse . items . length ; index ++ ) {
222363 let updatedEntry = addFields ( schema , entries [ index ] ) ;
223364 if ( updatedEntry . changedFlag || forceUpdate ) {
224- updatedEntry = removeUnwanted ( entries [ index ] , deleteFields ) ;
225- const flag = await updateEntry ( updatedEntry , locale ) ;
365+ let entryData = JSON . parse ( JSON . stringify ( updatedEntry . entry ) ) ;
366+ entryData = removeUnwanted ( entryData , deleteFields ) ;
367+ entryData = cleanJsonFields ( entryData ) ;
368+ entryData = convertUrlToHref ( entryData ) ;
369+ entryData = cleanJsonFields ( entryData ) ;
370+ const entry = stack . contentType ( contentType ) . entry ( entries [ index ] . uid ) ;
371+ Object . assign ( entry , entryData ) ;
372+ const flag = await updateEntry ( entry , locale ) ;
226373 if ( flag ) {
227374 if ( bulkPublish ) {
228375 if ( bulkPublishSet . length < bulkPublishLimit ) {
@@ -353,8 +500,6 @@ async function start(
353500 }
354501}
355502
356- // start()
357-
358503module . exports = {
359504 start,
360505 getContentTypeSchema,
0 commit comments