handle entries

This commit is contained in:
lightling 2024-10-15 19:55:57 -04:00
parent 890434ce63
commit 15bf9f7557

View file

@ -6,14 +6,39 @@ const mapper = (inVal) => {
const { data } = inVal;
const outVal = {};
outVal.tags = {};
data.tags.forEach(tag => {
outVal.tags[tag.key] = {
category: tag.value.category,
displayName: tag.value.displayName,
};
});
// handle list basic fields
outVal.title = data.title;
// handle list entries
outVal.entries = {};
data.entries.forEach(entry => {
// give it an id
const id = `${(entry.date || '').replace(/ /gm,'_')}_${(entry.title || '').replace(/ /gm, '_')}_${entry.documentId}`;
// copy all values at first...
outVal.entries[id] = {...entry};
// ...then delete strapi fields (as there are more to copy than delete)
delete outVal.entries[id].id;
delete outVal.entries[id].documentId;
delete outVal.entries[id].createdAt;
delete outVal.entries[id].updatedAt;
delete outVal.entries[id].publishedAt;
delete outVal.entries[id].locale;
// ...then handle variants
outVal.entries[id].variants = entry.variants.map(variant => ({
alternativeText: variant.alternativeText,
caption: variant.caption,
// the images will currently be mapped to strapi, to be handled with file grabber
url: variant.url,
thumbnailUrl: variant.formats['thumbnail'].url, // TBD: non-image assets
}));
// ...also post-process tags into an array
outVal.entries[id].tags = (outVal.entries[id].tags || '').split(/,| |;/).filter(val => val !== '');
});
return outVal;
};
@ -63,12 +88,11 @@ const mapper = (inVal) => {
console.error('There was an error: ', err);
}
console.log(res);
console.log('Writing...');
try {
await writeFile(`${destination}/in.json`, JSON.stringify(res, null, 2));
let val = mapper(res);
await writeFile(`${destination}/out.json`, JSON.stringify(val, null, 2));
let list = mapper(res);
await writeFile(`${destination}/out.json`, JSON.stringify(list, null, 2));
} catch (err) {
console.error('There was an error: ', err)
}