Skip to content

Commit 54318ed

Browse files
feat: implement block name resolution for media blocks
1 parent 9453164 commit 54318ed

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

upload-api/migration-wordpress/libs/extractItems.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ import config from '../config/index.json';
1111
import extractTaxonomy from './extractTaxonomy';
1212
import { 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+
1423
const { contentTypes: contentTypesConfig } = config.modules;
1524

1625
const 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 || '');

upload-api/migration-wordpress/libs/schemaMapper.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { Field, WordPressBlock } from '../interface/interface';
22
import restrictedUid from '../utils/index';
33
import GenerateSchema from 'generate-schema';
44

5+
const MEDIA_BLOCK_NAMES = ['core/image', 'core/video', 'core/audio', 'core/file'];
6+
7+
function resolveBlockName(key: any): string {
8+
if (key?.attributes?.metadata?.name) return key.attributes.metadata.name;
9+
if (key?.name === 'core/missing') return 'body';
10+
if (MEDIA_BLOCK_NAMES.includes(key?.name)) return 'media';
11+
return key?.name;
12+
}
13+
514
const getFieldName = (key: string ) => {
615
if(key?.includes('/')){
716
return key?.split('/')?.[1];
@@ -152,13 +161,16 @@ async function schemaMapper (key: WordPressBlock | WordPressBlock[], parentUid:
152161
const schemas: Field[] = [];
153162
for (const item of key) {
154163
const result = await schemaMapper(item, parentUid, parentFieldName, affix);
155-
const existingBlock: Field | undefined = schemas.find((schemaItem: Field) =>
156-
result?.otherCmsField === schemaItem?.otherCmsField &&
157-
schemaItem?.contentstackFieldType === result?.contentstackFieldType &&
158-
schemaItem?.contentstackField === result?.contentstackField &&
159-
result?.contentstackFieldUid?.includes(parentUid)
160-
);
161-
item?.contentstackFieldUid?.includes(parentUid) ;
164+
165+
const compareField = Array.isArray(result) ? result[0] : result;
166+
const existingBlock: Field | undefined = compareField ? schemas.find((schemaItem: Field) =>
167+
compareField?.otherCmsField === schemaItem?.otherCmsField &&
168+
schemaItem?.contentstackFieldType === compareField?.contentstackFieldType &&
169+
schemaItem?.contentstackField === compareField?.contentstackField &&
170+
parentUid && compareField?.contentstackFieldUid?.includes(parentUid) &&
171+
parentUid && schemaItem?.contentstackFieldUid?.includes(parentUid)
172+
) : undefined;
173+
162174
if (existingBlock && typeof existingBlock === 'object' && 'advanced' in existingBlock) {
163175
existingBlock.advanced = {
164176
...(existingBlock.advanced as object),
@@ -175,7 +187,7 @@ async function schemaMapper (key: WordPressBlock | WordPressBlock[], parentUid:
175187
return schemas;
176188
}
177189

178-
const fieldName = parentFieldName ? `${parentFieldName} > ${getFieldName(key?.attributes?.metadata?.name ?? (key?.name === 'core/missing' ? 'body' : key?.name))}` : getFieldName(key?.attributes?.metadata?.name ?? (key?.name === 'core/missing' ? 'body' : key?.name));
190+
const fieldName = parentFieldName ? `${parentFieldName} > ${getFieldName(resolveBlockName(key))}` : getFieldName(resolveBlockName(key));
179191

180192
switch (key?.name) {
181193
case 'core/paragraph':
@@ -184,6 +196,7 @@ async function schemaMapper (key: WordPressBlock | WordPressBlock[], parentUid:
184196
case 'core/table':
185197
case 'core/columns':
186198
case 'core/missing':
199+
case 'core/verse':
187200
case 'core/code': {
188201
const rteUid = parentUid ?
189202
`${parentUid}.${getFieldUid(`${key?.name}_${key?.clientId}`, affix)}`
@@ -209,9 +222,9 @@ async function schemaMapper (key: WordPressBlock | WordPressBlock[], parentUid:
209222

210223
return {
211224
uid: fileUid,
212-
otherCmsField: getFieldName(key?.name),
225+
otherCmsField: 'media',
213226
otherCmsType: getFieldName(key?.attributes?.metadata?.name ?? key?.name),
214-
contentstackField: fieldName,
227+
contentstackField: 'media',
215228
contentstackFieldUid: fileUid,
216229
contentstackFieldType: 'file',
217230
backupFieldType: 'file',
@@ -222,6 +235,7 @@ async function schemaMapper (key: WordPressBlock | WordPressBlock[], parentUid:
222235
break;
223236

224237
case 'core/heading':
238+
case 'core/accordion-heading':
225239
case 'core/list-item': {
226240
const textUid = parentUid ? `${parentUid}.${getFieldUid(`${key?.name}_${key?.clientId}`, affix)}` : getFieldUid(`${key?.name}_${key?.clientId}`, affix);
227241
return {
@@ -260,6 +274,8 @@ async function schemaMapper (key: WordPressBlock | WordPressBlock[], parentUid:
260274
case 'core/social-links':
261275
case 'core/details':
262276
case 'core/group':
277+
case 'core/accordion-item':
278+
case 'core/accordion-panel':
263279
case 'core/navigation': {
264280
const groupSchema: Field[] = [];
265281
const groupUid = parentUid ? `${parentUid}.${getFieldUid(`${key?.name}_${key?.clientId}`, affix)}` : getFieldUid(`${key?.name}_${key?.clientId}`, affix);

0 commit comments

Comments
 (0)