Created
October 18, 2018 15:08
-
-
Save 1-800-jono/4ba1f59f5bfc92479ce8895469d9e37e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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