-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
34 lines (33 loc) · 1.01 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const path=require("path")
exports.createPages = ({actions, graphql}) => {
const {createPage} = actions;
const blogTemplate = path.resolve(`src/templates/blogPost.jsx`);
return graphql(`query Posts {
allMarkdownRemark(limit:100) {
edges {
node {
frontmatter {
title
path
date(formatString: "YYYY/MM/DD")
}
excerpt(pruneLength: 5)
}
}
}
} `).then(result => {
if(result.errors)
Promise.reject(result.errors);
let posts= result.data.allMarkdownRemark.edges.sort((a,b)=> new Date(a.node.frontmatter.date.split('/')) - new Date(b.node.frontmatter.date.split('/')) );
posts.forEach(({ node },index,edges) => {
createPage({
path: node.frontmatter.path,
component: blogTemplate,
context: {
prevLink:edges[index-1]?edges[index-1].node.frontmatter.path:null,
nextLink:edges[index+1]?edges[index+1].node.frontmatter.path:null
} // additional data can be passed via context
})
})
})
}