Gatsby Getting Started Guide - add features from previous page to next page (end)

1. Adjust Gatsby node

This is simple. Open gatsby-node.js and add the following code:

const path = require("path");
exports.createPages = ({ actions, graphql }) => {
  const { createPage } = actions
  const blogPostTemplate = path.resolve(`src/templates/blogPost.js`)
  return graphql(`
    {
      allMarkdownRemark {
        edges {
          node {
            frontmatter {
              path,
              title,
              tags
            }
          }
        }
      }
    }
  `).then(result => {
    if (result.errors) {
      return Promise.reject(result.errors)
    }
    const posts = result.data.allMarkdownRemark.edges;
    createTagPages(createPage, posts);
    posts.forEach(({ node }, index) => {
      const path = node.frontmatter.path;
      const title = node.frontmatter.title;
      createPage({
        title,
        path,
        component: blogPostTemplate,
        context: {
          pathSlug: path,
          //This is a new addition
          prev: index === 0 ? null : posts[index - 1].node,
          next: index === (posts.length - 1) ? null : posts[index + 1].node
        }, // additional data can be passed via context
      })
    })
  })
}

2. Adjust blogPost.js

import React from "react"
import { graphql,Link } from 'gatsby'
const Template = ({ data, pageContext }) => {
  const {next,prev} = pageContext;
  const {markdownRemark} = data;
  const title = markdownRemark.frontmatter.title;
  const html = markdownRemark.html;
  return (
​
    <div style={{
      display: 'flex',
      flexDirection: 'column',
      alignItems: 'center'
    }}>
      <h1>{title}</h1>
      <div dangerouslySetInnerHTML={{ __html: html }} />
      
      {next&&<Link to={next.frontmatter.path}>Next</Link>}
      {prev&&<Link to={prev.frontmatter.path}>Prev</Link>}
    </div>
  )
}
​
export const query = graphql`
  query($pathSlug: String!) {
    markdownRemark(frontmatter: { path: { eq: $pathSlug } }) {
      html
      frontmatter {
        date(formatString: "MMMM DD, YYYY")
        path
        title
      }
    }
  }
`
export default Template;
Open the home page and click the page to jump to the corresponding page.

Conclusion:

So far, through gatsby, we have built a blog website quickly. We just need to write the markdown file to generate the corresponding web page. As for webpage beautification, it's a matter of cutting pictures, so I won't write here.

Of course, you don't want to use all kinds of existing UI libraries, such as ant design. My website is ant design

If you think it's too troublesome to study gatsby in depth, you can just use the template I wrote,

Open source library address, which can be directly cloned:

https://github.com/leolau2012/gatsby-teach

But the foundation still needs to be met. Otherwise, there is no way to make mistakes or change functions according to your company's requirements.

 

 

 

 

 

Tags: React github

Posted on Sat, 02 Nov 2019 23:50:43 -0400 by robvan75