This repository has been archived by the owner on Nov 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
128 lines (120 loc) · 3.15 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const path = require("path")
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
// let's get all of the wordpress content types
const postResult = await graphql(
`
{
allWpPost(sort: { fields: date, order: DESC }) {
nodes {
date
author {
node {
firstName
}
}
excerpt
featuredImage {
node {
altText
srcSet
localFile {
name
childImageSharp {
gatsbyImageData
}
}
}
}
title
uri
isSticky
id
}
}
wp {
readingSettings {
postsPerPage
}
}
}
`
)
if (postResult.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
const categoryResult = await graphql(
`
{
allWpCategory {
nodes {
uri
name
description
count
id
slug
}
}
wp {
readingSettings {
postsPerPage
}
}
}
`
)
if (categoryResult.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
// define templates
const archiveTemplate = path.resolve("./src/templates/archive.js")
const blogTemplate = path.resolve("./src/templates/blog.js")
// create pages for the blog
const posts = postResult.data.allWpPost.nodes
const blogPostsPerPage = postResult.data.wp.readingSettings.postsPerPage
const numBlogPages = Math.ceil(posts.length / blogPostsPerPage)
Array.from({ length: numBlogPages }).forEach((_, i) => {
createPage({
path: i === 0 ? `/blog/` : `/blog/${i + 1}/`,
component: blogTemplate,
context: {
totalPosts: posts.length,
limit: blogPostsPerPage,
skip: i * blogPostsPerPage,
numPages: numBlogPages,
currentPage: i + 1,
basePath: "/blog",
},
})
})
// create pages for each category
categoryResult.data.allWpCategory.nodes.forEach(category => {
const postsPerPage = categoryResult.data.wp.readingSettings.postsPerPage
const numberOfPosts = category.count
const numPages = Math.ceil(numberOfPosts / postsPerPage)
// some categories may be empty
// don't create archive for uncategorized
if (numberOfPosts > 0 || category.name !== "uncategorized") {
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path:
i === 0 ? `/blog${category.uri}` : `/blog${category.uri}${i + 1}`,
component: archiveTemplate,
context: {
limit: postsPerPage,
skip: i * postsPerPage,
numPages,
currentPage: i + 1,
catId: category.id,
catName: category.name,
catUri: category.uri,
categories: categoryResult.data.allWpCategory,
},
})
})
}
})
}