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.
|
// 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue