Skip to content

Instantly share code, notes, and snippets.

@christineywang
Last active April 20, 2018 19:20
Show Gist options
  • Save christineywang/b804b054b74a6ef340e881e7a2244b97 to your computer and use it in GitHub Desktop.
Save christineywang/b804b054b74a6ef340e881e7a2244b97 to your computer and use it in GitHub Desktop.
JSON shaper
const fs = require('fs');
const moment = require('moment');
const sw = require('stopword');
const product =
'./cf-exports/contentful-export-2q1fpqwpgegt-2018-04-18T11-21-56.json';
const blog =
'./cf-exports/contentful-export-fo9twyrwpveg-2018-04-19T10-08-54.json';
const exportData = JSON.parse(fs.readFileSync(blog, 'utf8'));
const extractFields = Object.keys(exportData.entries[0].fields);
const sanitizeData = exportData.entries.map(originalEntry => {
const newEntry = {};
extractFields.forEach(fieldKey => {
newEntry[fieldKey] = originalEntry.fields[fieldKey]['en-US'];
if (newEntry.content) {
const string = newEntry.content;
const uniqueListIndex = string
.toString()
.split(/\s+/)
.sort();
const uniqueList = uniqueListIndex.filter(function(v, i, o) {
return v !== o[i - 1];
});
newEntry.content = sw.removeStopwords(uniqueList).toString();
}
});
const oldFileName = originalEntry.fields.oldFileName['en-US'];
const publishDate = originalEntry.fields.publishDate['en-US'];
const newDate = moment(publishDate, 'YYYY-MM-DDTHH:mm:ss:SSSz').format(
'YYYY/MM/DD'
);
newEntry.createdAt = originalEntry.sys.createdAt;
newEntry.publishedAt = publishDate;
newEntry.objectID = originalEntry.sys.id;
newEntry.url = `https://www.contentful.com/blog/${newDate}/${oldFileName}`;
return newEntry;
});
// console.log(sanitizeData);
const newFile = './algolia-ready/blog-index.json';
fs.writeFileSync(newFile, JSON.stringify(sanitizeData, null, 4));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment