Skip to content

Instantly share code, notes, and snippets.

@patricoferris
Created December 4, 2018 13:22
Show Gist options
  • Save patricoferris/d3e3fa852a42fdbe0da21db3a9672845 to your computer and use it in GitHub Desktop.
Save patricoferris/d3e3fa852a42fdbe0da21db3a9672845 to your computer and use it in GitHub Desktop.
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions;
// A template for generating a new page from
const notesTemplate = path.resolve(`src/templates/notes.js`);
// A promise which collects all the MarkdownRemark nodes and then generates pages
return new Promise((resolve, reject) => {
// Our graphql query wrapped in a resolve
// Note how you can add more information in the allMarkdownRemark parameters to fine tune the query
resolve(graphql(`
{
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
) {
edges {
node {
fields{
slug
}
frontmatter {
title
}
}
}
}
}`).then(result => {
// If there are errors then report them and reject.
if (result.errors) {
console.error(results.errors);
return reject(result.errors);
}
// Otherwise generate the pages for each node we found
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
createPage({
path: node.fields.slug,
component: notesTemplate,
context: {
slug: node.fields.slug,
}
})
})
return
})
)
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment