const path = require('path') const { createFilePath } = require('gatsby-source-filesystem') exports.onCreateNode = ({ node, getNode, actions }) => { const { createNodeField } = actions if (node.internal.type === `MarkdownRemark`) { const fileNode = getNode(node.parent) const parsedFilePath = path.parse(fileNode.relativePath) const slug = createFilePath({ node, getNode }) const section = parsedFilePath.dir createNodeField({ node, name: 'slug', value: slug }) createNodeField({ node, name: 'section', value: section }) } } exports.createPages = ({ graphql, actions }) => { const { createPage } = actions return new Promise((resolve, reject) => { resolve( graphql( ` { allMarkdownRemark { edges { node { fields { slug section } } } } } ` ).then(result => { if (result.errors) { /* eslint-disable-next-line no-console */ console.log(result.errors) reject(result.errors) } const posts = result.data.allMarkdownRemark.edges const docTemplate = path.resolve('./src/templates/Doc.jsx') // Create Doc pages posts.forEach(post => { createPage({ path: `${post.node.fields.slug}`, component: docTemplate, context: { slug: post.node.fields.slug, section: post.node.fields.section } }) }) resolve() }) ) }) }