gatsby-node.js: make onCreateNode more readable
This commit is contained in:
parent
13430a68d1
commit
1a2fbbcebb
1 changed files with 20 additions and 11 deletions
|
|
@ -49,16 +49,25 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
|
|||
// Add custom slug for blog posts to both File and MarkdownRemark nodes.
|
||||
exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
|
||||
const { createNodeField } = boundActionCreators
|
||||
if (node.internal.type === `File`) {
|
||||
|
||||
switch (node.internal.type) {
|
||||
case 'File':
|
||||
const parsedFilePath = path.parse(node.relativePath)
|
||||
const slug = `/${parsedFilePath.dir}/`
|
||||
createNodeField({ node, fieldName: `slug`, fieldValue: slug })
|
||||
} else if (node.internal.type === `MarkdownRemark`) {
|
||||
createNodeField({
|
||||
node,
|
||||
fieldName: 'slug',
|
||||
fieldValue: slug
|
||||
})
|
||||
return
|
||||
|
||||
case 'MarkdownRemark':
|
||||
const fileNode = getNode(node.parent)
|
||||
createNodeField({
|
||||
node,
|
||||
fieldName: `slug`,
|
||||
fieldName: 'slug',
|
||||
fieldValue: fileNode.fields.slug,
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue