Skip to content

Instantly share code, notes, and snippets.

@jazibsawar
Created June 5, 2018 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jazibsawar/26e6a4eb34aca0d7e24389ba89b562e0 to your computer and use it in GitHub Desktop.
Save jazibsawar/26e6a4eb34aca0d7e24389ba89b562e0 to your computer and use it in GitHub Desktop.
const _ = require('lodash')
const Promise = require('bluebird')
const path = require('path')
exports.createPages = ({ graphql, boundActionCreators }) => {
const { createPage } = boundActionCreators
const indexPage = path.resolve('./src/pages/index.js')
createPage({
path: `posts`,
component: indexPage,
})
return new Promise((resolve, reject) => {
const blogPost = path.resolve('./src/templates/blog-post.js')
resolve(
graphql(
`
{
allCosmicjsPosts(sort: { fields: [created], order: DESC }, limit: 1000) {
edges {
node {
slug,
title
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
// Create blog posts pages.
const posts = result.data.allCosmicjsPosts.edges;
_.each(posts, (post, index) => {
const next = index === posts.length - 1 ? null : posts[index + 1].node;
const previous = index === 0 ? null : posts[index - 1].node;
createPage({
path: `posts/${post.node.slug}`,
component: blogPost,
context: {
slug: post.node.slug,
previous,
next,
},
})
})
})
)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment