Merge pull request #86 from marcobiedermann/feature/previous-next

Feature: Previous & Next Post
This commit is contained in:
Kyle Mathews 2018-01-10 07:42:11 -08:00 committed by GitHub
commit dad95c97ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 4 deletions

View file

@ -12,12 +12,15 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
graphql( graphql(
` `
{ {
allMarkdownRemark(limit: 1000) { allMarkdownRemark(sort: { fields: [frontmatter___date], order: DESC }, limit: 1000) {
edges { edges {
node { node {
fields { fields {
slug slug
} }
frontmatter {
title
}
} }
} }
} }
@ -30,12 +33,19 @@ exports.createPages = ({ graphql, boundActionCreators }) => {
} }
// Create blog posts pages. // 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({ createPage({
path: edge.node.fields.slug, path: post.node.fields.slug,
component: blogPost, component: blogPost,
context: { context: {
slug: edge.node.fields.slug, slug: post.node.fields.slug,
previous,
next,
}, },
}) })
}) })

View file

@ -1,5 +1,6 @@
import React from 'react' import React from 'react'
import Helmet from 'react-helmet' import Helmet from 'react-helmet'
import Link from 'gatsby-link'
import get from 'lodash/get' import get from 'lodash/get'
import Bio from '../components/Bio' import Bio from '../components/Bio'
@ -9,6 +10,7 @@ class BlogPostTemplate extends React.Component {
render() { render() {
const post = this.props.data.markdownRemark const post = this.props.data.markdownRemark
const siteTitle = get(this.props, 'data.site.siteMetadata.title') const siteTitle = get(this.props, 'data.site.siteMetadata.title')
const { previous, next } = this.props.pathContext
return ( return (
<div> <div>
@ -31,6 +33,32 @@ class BlogPostTemplate extends React.Component {
}} }}
/> />
<Bio /> <Bio />
<ul
style={{
display: 'flex',
flexWrap: 'wrap',
justifyContent: 'space-between',
listStyle: 'none',
padding: 0,
}}
>
{previous && (
<li>
<Link to={previous.fields.slug} rel="prev">
{previous.frontmatter.title}
</Link>
</li>
)}
{next && (
<li>
<Link to={next.fields.slug} rel="next">
{next.frontmatter.title}
</Link>
</li>
)}
</ul>
</div> </div>
) )
} }