Skip to content

Instantly share code, notes, and snippets.

@DSchau
Created January 27, 2018 21:11
Show Gist options
  • Save DSchau/5cbfa9a66e017874fbf7eacd352ad338 to your computer and use it in GitHub Desktop.
Save DSchau/5cbfa9a66e017874fbf7eacd352ad338 to your computer and use it in GitHub Desktop.
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;
}, {});
/*
* This is a utility plugin that will enable
* linking Markdown metadata for further linking
* It's currently used to link Markdown.frontmatter.author
* to an actual person, and "joins" on id
* We could/should open source this and release it
*/
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 });
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment