Skip to content

Instantly share code, notes, and snippets.

@patricoferris
Created December 3, 2018 17:37
Show Gist options
  • Save patricoferris/31b262c53cd8810b2fe68638efad6505 to your computer and use it in GitHub Desktop.
Save patricoferris/31b262c53cd8810b2fe68638efad6505 to your computer and use it in GitHub Desktop.
Gatsby Node API (1/2)
// Defining what should happen whenever we create a new node
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNode, createNodeField } = actions
// Check if the node being passed is the right type (our markdown content)
if (node.internal.type === 'MarkdownRemark') {
// Generate a new slug for this file using the createFilePath function
// Note we are setting the basePath as pages that way the slug will also include subdirectories
const slug = createFilePath({ node, getNode , basePath: 'pages'});
// Add a new field to node of name 'slug' with value slug
createNodeField({
node,
name: `slug`,
value: slug
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment