1.调解gatsby-node
这个就简朴了,翻开gatsby-node.js,增添代码以下:
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,
//这里是新增添的
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.调解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;
翻开首页,点击页面跳转到对应的页面功德圆满。
总结
到此,经由过程gatsby就疾速的搭建了一个博客网站,我们只需誊写markdown文件就能够天生对应的网页了。至于网页美化,那是切图的事儿,我就不在这里墨迹了。
固然了你不想切图能够运用种种现成的UI库,比方antdesign。我的网站就是直接用的antdesign.
假如你以为深切进修gatsby太贫苦,你能够直接用我写好的模板就行,
开源库地点,直接克隆就能够够用了:
https://github.com/leolau2012…
然则基本照样要会的,不然失足或许要根据你们公司需求修改功用,就没办法了。