-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
222 lines (202 loc) · 5.42 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const _ = require('lodash')
const path = require('path')
const { createFilePath } = require('gatsby-source-filesystem')
const { paginate } = require('gatsby-awesome-pagination')
const getOnlyPublished = edges =>
_.filter(edges, ({ node }) => node.status === 'publish')
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
return graphql(`
{
allWordpressPage {
edges {
node {
id
slug
status
}
}
}
}
`)
.then(result => {
if (result.errors) {
result.errors.forEach(e => console.error(e.toString()))
return Promise.reject(result.errors)
}
const pageTemplate = path.resolve(`./src/templates/page.js`)
// Only publish pages with a `status === 'publish'` in production. This
// excludes drafts, future posts, etc. They will appear in development,
// but not in a production build.
const allPages = result.data.allWordpressPage.edges
const pages =
process.env.NODE_ENV === 'production'
? getOnlyPublished(allPages)
: allPages
// Call `createPage()` once per WordPress page
_.each(pages, ({ node: page }) => {
createPage({
path: `/${page.slug}/`,
component: pageTemplate,
context: {
id: page.id,
},
})
})
})
.then(() => {
return graphql(`
{
allWordpressPost {
edges {
node {
id
slug
status
}
}
}
}
`)
})
.then(result => {
if (result.errors) {
result.errors.forEach(e => console.error(e.toString()))
return Promise.reject(result.errors)
}
const postTemplate = path.resolve(`./src/templates/post.js`)
const blogTemplate = path.resolve(`./src/templates/blog.js`)
// In production builds, filter for only published posts.
const allPosts = result.data.allWordpressPost.edges
const posts =
process.env.NODE_ENV === 'production'
? getOnlyPublished(allPosts)
: allPosts
// Iterate over the array of posts
_.each(posts, ({ node: post }) => {
// Create the Gatsby page for this WordPress post
createPage({
path: `/${post.slug}/`,
component: postTemplate,
context: {
id: post.id,
},
})
})
// Create a paginated blog, e.g., /, /page/2, /page/3
paginate({
createPage,
items: posts,
itemsPerPage: 10,
pathPrefix: ({ pageNumber }) => (pageNumber === 0 ? `/` : `/page`),
component: blogTemplate,
})
})
.then(() => {
return graphql(`
{
allWordpressCategory(filter: { count: { gt: 0 } }) {
edges {
node {
id
name
slug
}
}
}
}
`)
})
.then(result => {
if (result.errors) {
result.errors.forEach(e => console.error(e.toString()))
return Promise.reject(result.errors)
}
const categoriesTemplate = path.resolve(`./src/templates/category.js`)
// Create a Gatsby page for each WordPress Category
_.each(result.data.allWordpressCategory.edges, ({ node: cat }) => {
createPage({
path: `/categories/${cat.slug}/`,
component: categoriesTemplate,
context: {
name: cat.name,
slug: cat.slug,
},
})
})
})
.then(() => {
return graphql(`
{
allWordpressTag(filter: { count: { gt: 0 } }) {
edges {
node {
id
name
slug
}
}
}
}
`)
})
.then(result => {
if (result.errors) {
result.errors.forEach(e => console.error(e.toString()))
return Promise.reject(result.errors)
}
const tagsTemplate = path.resolve(`./src/templates/tag.js`)
// Create a Gatsby page for each WordPress tag
_.each(result.data.allWordpressTag.edges, ({ node: tag }) => {
createPage({
path: `/tags/${tag.slug}/`,
component: tagsTemplate,
context: {
name: tag.name,
slug: tag.slug,
},
})
})
})
.then(() => {
return graphql(`
{
allWordpressWpUsers {
edges {
node {
id
slug
}
}
}
}
`)
})
.then(result => {
if (result.errors) {
result.errors.forEach(e => console.error(e.toString()))
return Promise.reject(result.errors)
}
const authorTemplate = path.resolve(`./src/templates/author.js`)
_.each(result.data.allWordpressWpUsers.edges, ({ node: author }) => {
createPage({
path: `/author/${author.slug}`,
component: authorTemplate,
context: {
id: author.id,
},
})
})
})
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}
}