-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgatsby-node.js
More file actions
258 lines (227 loc) Β· 8.7 KB
/
Copy pathgatsby-node.js
File metadata and controls
258 lines (227 loc) Β· 8.7 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
const path = require(`path`)
const chunk = require(`lodash/chunk`)
// This is a simple debugging tool
// dd() will prettily dump to the terminal and kill the process
const { dd } = require(`dumper.js`)
/**
* exports.createPages is a built-in Gatsby Node API.
* It's purpose is to allow you to create pages for your site! π‘
*
* See https://www.gatsbyjs.com/docs/node-apis/#createPages for more info.
*/
exports.createPages = async gatsbyUtilities => {
// Query our posts from the GraphQL server
const posts = await getPosts(gatsbyUtilities)
console.log(`Found ${posts.length} posts`)
// Query our pages from the GraphQL server
const pages = await getPages(gatsbyUtilities)
console.log(`Found ${pages.length} pages`)
// If there are no posts in WordPress, don't do anything
// if (!posts.length) {
// return
// }
// If there are posts and pages, create Gatsby pages for them
console.log(`createSinglePosts firing with ${posts.length} items on posts.`)
//console.log(posts)
await createSinglePosts({ posts, gatsbyUtilities })
console.log(`createSinglePages firing with ${pages.length} items on pages.`)
// console.log(pages)
await createSinglePages({ pages, gatsbyUtilities })
// And a paginated archive
await createBlogPostArchive({ posts, gatsbyUtilities })
}
/**
* This function creates all the individual blog pages in this site
* @param posts An array of pages
* @param gatsbyUtilities
* @returns None. Pages are created
*/
const createSinglePages = async ({ pages, gatsbyUtilities }) =>
// dd(pages)
Promise.all(
pages.map((page) =>
// dd(page.page)
// console.log(`id is ${page.page.id}\nuri is ${page.page.uri}`)
// createPage is an action passed to createPages
// See https://www.gatsbyjs.com/docs/actions#createPage for more info
gatsbyUtilities.actions.createPage({
// Use the WordPress uri as the Gatsby page path
// This is a good idea so that internal links and menus work π
path: page.page.uri,
// use the blog post template as the page component
component: path.resolve(
`./src/templates/Page.js`
),
// `context` is available in the template as a prop and
// as a variable in GraphQL.
context: {
// we need to add the post id here
// so our blog post template knows which blog post
// the current page is (when you open it in a browser)
id: page.page.id,
// We also use the next and previous id's to query them and add links!
// previousPostId: previous ? previous.id : null,
// nextPostId: next ? next.id : null,
},
})
)
)
/**
* This function creates all the individual blog posts in this site
* @param posts An array of posts
* @param gatsbyUtilities
* @returns None. Pages are Created
*/
const createSinglePosts = async ({ posts, gatsbyUtilities }) =>
Promise.all(
posts.map(({ previous, post, next }) =>
// createPage is an action passed to createPages
// See https://www.gatsbyjs.com/docs/actions#createPage for more info
gatsbyUtilities.actions.createPage({
// Use the WordPress uri as the Gatsby page path
// This is a good idea so that internal links and menus work π
path: post.uri,
// use the blog post template as the page component
component: path.resolve(
`./src/templates/${post.__typename.replace(`Wp`, ``)}.js`
),
// `context` is available in the template as a prop and
// as a variable in GraphQL.
context: {
// we need to add the post id here
// so our blog post template knows which blog post
// the current page is (when you open it in a browser)
id: post.id,
// We also use the next and previous id's to query them and add links!
previousPostId: previous ? previous.id : null,
nextPostId: next ? next.id : null,
},
})
)
)
/**
* This function creates all the individual blog pages in this site
*/
async function createBlogPostArchive({ posts, gatsbyUtilities }) {
const graphqlResult = await gatsbyUtilities.graphql(/* GraphQL */ `
{
wp {
readingSettings {
postsPerPage
}
}
}
`)
const { postsPerPage } = graphqlResult.data.wp.readingSettings
const postsChunkedIntoArchivePages = chunk(posts, postsPerPage)
const totalPages = postsChunkedIntoArchivePages.length
return Promise.all(
postsChunkedIntoArchivePages.map(async (_posts, index) => {
const pageNumber = index + 1
const getPagePath = page => {
if (page > 0 && page <= totalPages) {
// We want the first page to be "/news/" and any additional pages
// to be numbered.
// "/blog/2" for example
return page === 1 ? `/news/` : `/news/${page}/`
}
return null
}
// createPage is an action passed to createPages
// See https://www.gatsbyjs.com/docs/actions#createPage for more info
await gatsbyUtilities.actions.createPage({
path: getPagePath(pageNumber),
// use the blog post archive template as the page component
component: path.resolve(`./src/templates/blog-post-archive.js`),
// `context` is available in the template as a prop and
// as a variable in GraphQL.
context: {
// the index of our loop is the offset of which posts we want to display
// so for page 1, 0 * 10 = 0 offset, for page 2, 1 * 10 = 10 posts offset,
// etc
offset: index * postsPerPage,
// We need to tell the template how many posts to display too
postsPerPage,
nextPagePath: getPagePath(pageNumber + 1),
previousPagePath: getPagePath(pageNumber - 1),
},
})
})
)
}
/**
* This function queries Gatsby's GraphQL server and asks for
* All WordPress blog posts. If there are any GraphQL error it throws an error
* Otherwise it will return the posts π
*
* We're passing in the utilities we got from createPages.
* So see https://www.gatsbyjs.com/docs/node-apis/#createPages for more info!
*/
async function getPages({ graphql, reporter }) {
const graphqlResult = await graphql(/* GraphQL */ `
query WpPage {
allWpPage(sort: { date: DESC }) {
edges {
# note: this is a GraphQL alias. It renames "node" to "post" for this query
# We're doing this because this "node" is a post! It makes our code more readable further down the line.
page: node {
__typename
id
uri
}
}
}
}
`)
if (graphqlResult.errors) {
reporter.panicOnBuild(
`There was an error loading your blog posts`,
graphqlResult.errors
)
return
}
return [
...graphqlResult.data.allWpPage.edges,
]
}
/**
* This function queries Gatsby's GraphQL server and asks for
* All WordPress blog posts. If there are any GraphQL error it throws an error
* Otherwise it will return the posts π
*
* We're passing in the utilities we got from createPages.
* So see https://www.gatsbyjs.com/docs/node-apis/#createPages for more info!
*/
async function getPosts({ graphql, reporter }) {
const graphqlResult = await graphql(/* GraphQL */ `
query WpPosts {
allWpPost(sort: { date: DESC }) {
edges {
previous {
id
}
# note: this is a GraphQL alias. It renames "node" to "post" for this query
# We're doing this because this "node" is a post! It makes our code more readable further down the line.
post: node {
__typename
id
uri
}
next {
id
}
}
}
}
`)
if (graphqlResult.errors) {
reporter.panicOnBuild(
`There was an error loading your blog posts`,
graphqlResult.errors
)
return
}
return [
...graphqlResult.data.allWpPost.edges,
]
}