Created
January 27, 2018 21:11
-
-
Save DSchau/5cbfa9a66e017874fbf7eacd352ad338 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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