gatsby-node.js: make onCreateNode more readable

This commit is contained in:
Rico Sta. Cruz 2017-06-09 10:13:06 +08:00 committed by GitHub
parent 13430a68d1
commit 1a2fbbcebb

View file

@ -49,16 +49,25 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
// Add custom slug for blog posts to both File and MarkdownRemark nodes. // Add custom slug for blog posts to both File and MarkdownRemark nodes.
exports.onCreateNode = ({ node, boundActionCreators, getNode }) => { exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
const { createNodeField } = boundActionCreators const { createNodeField } = boundActionCreators
if (node.internal.type === `File`) {
const parsedFilePath = path.parse(node.relativePath) switch (node.internal.type) {
const slug = `/${parsedFilePath.dir}/` case 'File':
createNodeField({ node, fieldName: `slug`, fieldValue: slug }) const parsedFilePath = path.parse(node.relativePath)
} else if (node.internal.type === `MarkdownRemark`) { const slug = `/${parsedFilePath.dir}/`
const fileNode = getNode(node.parent) createNodeField({
createNodeField({ node,
node, fieldName: 'slug',
fieldName: `slug`, fieldValue: slug
fieldValue: fileNode.fields.slug, })
}) return
case 'MarkdownRemark':
const fileNode = getNode(node.parent)
createNodeField({
node,
fieldName: 'slug',
fieldValue: fileNode.fields.slug,
})
return
} }
} }