Skip to content

Instantly share code, notes, and snippets.

@haxzie-xx
Created January 19, 2019 18:29
Show Gist options
  • Select an option

  • Save haxzie-xx/e0da6c695454dd8f22c2d938cf7e3b53 to your computer and use it in GitHub Desktop.

Select an option

Save haxzie-xx/e0da6c695454dd8f22c2d938cf7e3b53 to your computer and use it in GitHub Desktop.
Interacting with Gatsby's node APIs to generate pages from Markdown files
const path = require("path")
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
const blogPostTemplate = path.resolve(`src/templates/blog_template.js`)
return graphql(`
{
allMarkdownRemark(
limit: 100,
sort: { order: DESC, fields: [frontmatter___date] }
) {
edges {
node {
frontmatter {
slug
}
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors)
}
result.data.allMarkdownRemark.edges.forEach(({node}) => {
createPage({
path: node.frontmatter.slug,
component: blogPostTemplate,
})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment