-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
81 lines (75 loc) · 2.24 KB
/
Copy pathgatsby-node.js
File metadata and controls
81 lines (75 loc) · 2.24 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require("path")
const { createFilePath } = require("gatsby-source-filesystem")
const PostTemplate = path.resolve("./src/templates/postTemplate.js")
const BlogTemplate = path.resolve("./src/templates/blogTemplate.js")
const { postsPerPage } = require("./src/constants/postsPerPage.js")
// You can delete this file if you're not using it
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === "MarkdownRemark") {
const slug = createFilePath({ node, getNode, basePath: "posts" })
createNodeField({
node,
name: "slug",
value: slug,
})
}
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const result = await graphql(`
{
allMarkdownRemark(limit: 1000) {
edges {
node {
fields {
slug
}
}
}
}
}
`)
const posts = result.data.allMarkdownRemark.edges
posts.forEach(({ node: post }) => {
createPage({
path: `posts${post.fields.slug}`,
component: PostTemplate,
context: {
slug: post.fields.slug,
},
})
})
//query for all high priority posts
const featured = await graphql(`
{
allMarkdownRemark(filter: { frontmatter: { priority: { eq: "High" } } }) {
totalCount
}
}
`)
const featuredLength = featured.data.allMarkdownRemark.totalCount
const mainPageRemainder = postsPerPage - featuredLength
//extraSkip is the number of non-featured articles to skip past the first page
const extraSkip = mainPageRemainder >= 0 ? mainPageRemainder : 0
const totalPages = Math.ceil(posts.length / postsPerPage)
//Make an array for pages after the first page, i.e. with length totalPages - 1
Array.from({ length: totalPages - 1 }).forEach((_, index) => {
const currentPage = index + 2 //0th index represents the second page
createPage({
path: `/${currentPage}`,
component: BlogTemplate,
context: {
limit: postsPerPage,
skip: extraSkip + index * postsPerPage,
currentPage,
totalPages,
},
})
})
}