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`) {
switch (node.internal.type) {
case 'File':
const parsedFilePath = path.parse(node.relativePath) const parsedFilePath = path.parse(node.relativePath)
const slug = `/${parsedFilePath.dir}/` const slug = `/${parsedFilePath.dir}/`
createNodeField({ node, fieldName: `slug`, fieldValue: slug }) createNodeField({
} else if (node.internal.type === `MarkdownRemark`) { node,
fieldName: 'slug',
fieldValue: slug
})
return
case 'MarkdownRemark':
const fileNode = getNode(node.parent) const fileNode = getNode(node.parent)
createNodeField({ createNodeField({
node, node,
fieldName: `slug`, fieldName: 'slug',
fieldValue: fileNode.fields.slug, fieldValue: fileNode.fields.slug,
}) })
return
} }
} }