Skip to content

Instantly share code, notes, and snippets.

@1-800-jono
Created October 18, 2018 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1-800-jono/4ba1f59f5bfc92479ce8895469d9e37e to your computer and use it in GitHub Desktop.
Save 1-800-jono/4ba1f59f5bfc92479ce8895469d9e37e to your computer and use it in GitHub Desktop.
const pathPrefixes = {
blog: 'blog',
features: 'features',
authors: 'blog/authors',
'blog-categories': 'blog/category'
}
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const fileNode = getNode(node.parent)
const pathPrefix = pathPrefixes[fileNode.sourceInstanceName]
const slug = createFilePath({ node, getNode })
createNodeField({ node, name: `slug`, value: `${slug.replace(/\/$/, '')}` })
createNodeField({ node, name: `path`, value: `/${pathPrefix}${slug}` })
createNodeField({ node, name: 'sourceInstanceName', value: pathPrefix })
}
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return new Promise((resolve, reject) => {
graphql(`
{
allMarkdownRemark {
edges {
node {
id
fields {
path
slug
}
frontmatter {
templateKey
category {
fields {
path
slug
}
}
}
}
}
}
}
`).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
const id = node.id
const pageUrl =
node.frontmatter.category !== null
? `/blog${node.frontmatter.category.fields.slug}${node.fields.slug}/`
: `${node.fields.path}`
console.log(pageUrl)
createPage({
path: pageUrl,
component: path.resolve(`./src/templates/${String(node.frontmatter.templateKey)}.js`),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
slug: node.fields.slug,
id
}
})
})
resolve()
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment