@@ -11,6 +11,15 @@ import config from '../config/index.json';
1111import extractTaxonomy from './extractTaxonomy' ;
1212import { DataConfig , Field , CT } from '../interface/interface' ;
1313
14+ const MEDIA_BLOCK_NAMES = [ 'core/image' , 'core/video' , 'core/audio' , 'core/file' ] ;
15+
16+ function resolveBlockName ( field : any ) : string {
17+ if ( field ?. attributes ?. metadata ?. name ) return field . attributes . metadata . name ;
18+ if ( field ?. name === 'core/missing' ) return 'body' ;
19+ if ( MEDIA_BLOCK_NAMES . includes ( field ?. name ) ) return 'media' ;
20+ return field ?. name ;
21+ }
22+
1423const { contentTypes : contentTypesConfig } = config . modules ;
1524
1625const contentTypeFolderPath = path . resolve ( config . data , contentTypesConfig . dirName ) ;
@@ -288,7 +297,8 @@ const extractItems = async (item: any, config: DataConfig, type: string, affix:
288297
289298 const contentEncoded = targetItem ?. find ( "content\\:encoded" ) ?. text ( ) || '' ;
290299 const blocksJson = await setupWordPressBlocks ( contentEncoded ) ;
291- //await helper?.writeFileAsync(`${data?.title || 'undefined'}.json`, JSON.stringify({blocks : blocksJson, count :blocksJson?.length}, null, 4), 4);
300+ const folderName = path . basename ( localPath , path . extname ( localPath ) ) ;
301+ await helper ?. writeFileAsync ( `${ folderName } /${ data ?. title || 'undefined' } .json` , JSON . stringify ( { blocks : blocksJson , count :blocksJson ?. length } , null , 4 ) , 4 ) ;
292302
293303
294304 // Example usage
@@ -300,7 +310,7 @@ const extractItems = async (item: any, config: DataConfig, type: string, affix:
300310
301311 for ( const field of blocksJson ) {
302312 const fieldUid = getFieldUid ( `${ field ?. name } _${ field ?. clientId } ` || '' , affix || '' ) ;
303- const contentstackFieldName = getFieldName ( field ?. attributes ?. metadata ?. name ?? ( field ?. name === 'core/missing' ? 'body' : field ?. name ) ) ;
313+ const contentstackFieldName = getFieldName ( resolveBlockName ( field ) ) ;
304314
305315 const similarBlocks = findSimilarBlocks ( result , field ?. clientId ) ;
306316
@@ -310,8 +320,8 @@ const extractItems = async (item: any, config: DataConfig, type: string, affix:
310320 . filter ( ( name , index , array ) => name && array ?. indexOf ( name ) === index ) // Remove duplicates
311321 . sort ( ) ; // Sort for consistency
312322
313- const filterOutBlock = allBlockNames ?. filter ( ( item ) => item !== ( field ?. attributes ?. metadata ?. name ?? ( field ?. name === 'core/missing' ? 'body' : field ?. name ) ) ) ;
314- const fieldDisplayName = getFieldName ( field ?. attributes ?. metadata ?. name ?? ( field ?. name === 'core/missing' ? 'body' : field ?. name ) ) ;
323+ const filterOutBlock = allBlockNames ?. filter ( ( item ) => item !== resolveBlockName ( field ) ) ;
324+ const fieldDisplayName = getFieldName ( resolveBlockName ( field ) ) ;
315325 const firstFilterBlock = filterOutBlock ?. [ 0 ] ? `Modular Blocks > ${ filterOutBlock ?. [ 0 ] } ` : null ;
316326
317327 const generatedFieldName = `Modular Blocks > ${ fieldDisplayName } ` ;
@@ -334,7 +344,7 @@ const extractItems = async (item: any, config: DataConfig, type: string, affix:
334344 // If this block has similar structures
335345 if ( similarBlocks ?. length > 0 ) {
336346 // Create a unique key based on the structure/name to track processed groups
337- const groupKey = field ?. attributes ?. metadata ?. name ?? ( field ?. name === 'core/missing' ? 'body' : field ?. name ) ;
347+ const groupKey = resolveBlockName ( field ) ;
338348 // Skip if we've already processed this group of similar blocks
339349 if ( processedSimilarBlocks ?. has ?.( groupKey ) || existingBlock ) {
340350 continue ;
@@ -398,10 +408,10 @@ const extractItems = async (item: any, config: DataConfig, type: string, affix:
398408 }
399409 else {
400410 // Handle single blocks (no similar structures found)
401- const singleBlockName = getFieldName ( field ?. attributes ?. metadata ?. name ?? ( field ?. name === 'core/missing' ? 'body' : field ?. name ) ) ;
411+ const singleBlockName = getFieldName ( resolveBlockName ( field ) ) ;
402412
403- if ( ! existingBlock && ! processedSimilarBlocks ?. has ?.( field ?. attributes ?. metadata ?. name ?? getFieldName ( field ?. name === 'core/missing' ? 'body' : field ?. name ) ) ) {
404- processedSimilarBlocks ?. add ?.( field ?. attributes ?. metadata ?. name ?? ( field ?. name === 'core/missing' ? 'body' : field ?. name ) ) ;
413+ if ( ! existingBlock && ! processedSimilarBlocks ?. has ?.( resolveBlockName ( field ) ) ) {
414+ processedSimilarBlocks ?. add ?.( resolveBlockName ( field ) ) ;
405415
406416 // Generate Fieldschema first to check for duplicates
407417 const Fieldschema : Field [ ] | Field = await schemaMapper ( field ?. innerBlocks ?. length > 0 ? field ?. innerBlocks : field , `modular_blocks.${ fieldUid } ` , groupedContentstackField , affix || '' ) ;
0 commit comments