Last active
March 13, 2019 22:09
-
-
Save margueriteroth/74556046e845988b40851dc6e0f810a3 to your computer and use it in GitHub Desktop.
gatsby-node.js
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 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