add linking to previous and next post

This commit is contained in:
Marco Biedermann 2018-01-09 21:04:46 +01:00
parent a8a8f64575
commit 1a862b7fab
No known key found for this signature in database
GPG key ID: 61342DB42C3F6E1E
2 changed files with 40 additions and 4 deletions

View file

@ -12,12 +12,15 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
graphql(
`
{
allMarkdownRemark(limit: 1000) {
allMarkdownRemark(sort: { fields: [frontmatter___date], order: DESC }, limit: 1000) {
edges {
node {
fields {
slug
}
frontmatter {
title
}
}
}
}
@ -30,12 +33,19 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
}
// Create blog posts pages.
_.each(result.data.allMarkdownRemark.edges, edge => {
const posts = result.data.allMarkdownRemark.edges;
_.each(posts, (post, index) => {
const previous = index === posts.length - 1 ? false : posts[index + 1].node;
const next = index === 0 ? false : posts[index - 1].node;
createPage({
path: edge.node.fields.slug,
path: post.node.fields.slug,
component: blogPost,
context: {
slug: edge.node.fields.slug,
slug: post.node.fields.slug,
previous,
next,
},
})
})