Skip to content

Instantly share code, notes, and snippets.

@margueriteroth
Last active March 13, 2019 22:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save margueriteroth/74556046e845988b40851dc6e0f810a3 to your computer and use it in GitHub Desktop.
Save margueriteroth/74556046e845988b40851dc6e0f810a3 to your computer and use it in GitHub Desktop.
gatsby-node.js
const path = require('path');
const { createFilePath } = require(`gatsby-source-filesystem`);
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({
node,
getNode,
basePath: `posts` })
createNodeField({
node,
name: `slug`,
value: slug,
})
}
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return graphql(`
{
allMarkdownRemark {
edges {
node {
fields {
slug
}
frontmatter {
title
type
}
}
}
}
}
`
).then(result => {
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
createPage({
path: node.fields.slug,
component: path.resolve('./src/components/postLayout.js'),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
slug: node.fields.slug,
},
})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment