const path = require('path');
const PeopleMarkdownNode = 'PeopleMarkdownRemark';
const files = [
['approach', 'approach'],
['case-studies', 'case-study'],
['events', 'event'],
['careers', 'career'],
['locations', 'location'],
['people', 'person'],
['blog', 'post'],
['training', 'training'],
['services', 'service'],
['assets/images', 'images'],
['marketing', 'marketing'],
].map(([filePath, type]) => ({
resolve: 'gatsby-source-filesystem',
options: {
path: path.join(__dirname, 'content', filePath),
name: type,
},
}));
const crypto = require('crypto');
const get = require('lodash.get');
const setNullToEmpty = data => Object.keys(data).reduce((merged, key) => { if (key !== 'null') { merged[key] = data[key] === null ? '' : data[key]; } return merged; }, {});
exports.onCreateNode = function onCreateNode(
{ node, boundActionCreators, loadNodeContent, getNode },
pluginOptions
) {
if (node.internal.type !== 'MarkdownRemark') {
return;
}
const { createNode, createParentChildLink } = boundActionCreators;
const { sourceInstanceName: type } = getNode(node.parent);
if (type === 'person') {
const { frontmatter } = node;
const data = setNullToEmpty(frontmatter);
const contentDigest = crypto
.createHash('md5')
.update(JSON.stringify(data))
.digest('hex');
const id = get(node, pluginOptions.id);
const personNode = Object.assign(data, {
id,
children: [],
parent: node.id,
internal: {
contentDigest,
type: pluginOptions.type,
},
});
createNode(personNode);
createParentChildLink({ parent: node, child: personNode });
}
};