-
Notifications
You must be signed in to change notification settings - Fork 11
/
gatsby-node.js
364 lines (330 loc) · 10.3 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
const path = require('path');
const { slugify } = require('./scripts/slugify');
const { postsPerFirstPage, postsPerPage } = require('./site.config');
/** Paths to available templates. */
const template = {
blog: path.resolve('./src/templates/blog/blog.tsx'),
tag: path.resolve('./src/templates/tag.tsx'),
post: path.resolve('./src/templates/post.tsx'),
author: path.resolve('./src/templates/author.tsx'),
};
exports.sourceNodes = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type AuthorYaml implements Node {
id: String!
name: String!
bio: String!
github: String!
twitter: String
}
`;
createTypes(typeDefs);
};
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === 'MarkdownRemark') {
createNodeField({
name: 'slug',
node,
value: slugify(node.frontmatter.title),
});
createNodeField({
name: 'author',
node,
value: node.frontmatter.author,
});
}
};
exports.createPages = ({ graphql, actions }) => {
const { createPage, createRedirect } = actions;
const redirects = [
{ fromPath: '/app/projects', toPath: '/projects' },
{ fromPath: '/app/projects/create', toPath: '/projects/create' },
];
for (const { fromPath, toPath } of redirects) {
createRedirect({
fromPath,
redirectInBrowser: true,
isPermanent: true,
toPath,
});
}
/**
* **Creates the blog index page, blog post pages and paginated list of blog posts.**
*
* The first part is is a call to the `graphql` function passed with the
* query string for this build step. This string only fetches what is need to
* build all the index page, individual blog post pages and the paginated list
* of blog posts.
*
* We let this query run asynchronously. If the query fails, the promise is
* rejected and the build fails.
*
* From the query results, the first task is to create the blog index page.
* This page is different because its path does not have a page number, i.e.
* `/blog`. The other pages would have a page number in their paths, i.e.
* `/blog/23`.
*
* The last task is to create the individual blog posts. This step uses a
* different template than the paginated list of blog posts. And the _slugged_
* title of the blog post is appended to the path, i.e. `/blog/the-title`.
*/
const loadBlogPosts = new Promise((resolve, reject) => {
graphql(`
{
allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
limit: 1000
) {
nodes {
fields {
slug
}
frontmatter {
title
}
}
}
}
`).then(({ errors, data }) => {
if (errors) {
reject(errors);
}
/**
* @typedef {object} BlogPostNode
* @property {object} fields
* @property {string} fields.slug Slugged title of the post based on provided `frontmatter.title`
* @property {object} frontmatter
* @property {string} frontmatter.title Title of the blog post
*/
/** @type {BlogPostNode[]} */
const nodes = data.allMarkdownRemark.nodes;
const numPages = Math.ceil(
nodes.slice(postsPerFirstPage).length / postsPerPage,
);
createPage({
path: '/blog',
component: template.blog,
context: {
limit: postsPerFirstPage,
skip: 0,
numPages: numPages + 1,
currentPage: 1,
},
});
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: `/blog/${i + 2}/`,
component: template.blog,
context: {
limit: postsPerPage,
skip: i * postsPerPage + postsPerFirstPage,
numPages: numPages + 1,
currentPage: i + 2,
},
});
});
nodes.forEach(({ fields }, i, a) => {
const previous = i === 0 ? null : a[i - 1];
const next = i === a.length - 1 ? null : a[i + 1];
createPage({
path: `/blog/${fields.slug}/`,
component: template.post,
context: {
slug: fields.slug,
previous,
next,
},
});
});
resolve();
});
});
/**
* **Creates the paginated list of posts for every tag.**
*
* The first part is is a call to the `graphql` function passed with the
* query string for this build step. This string only fetches what is need to
* build all the tag pages.
*
* We let this query run asynchronously. If the query fails, the promise is
* rejected and the build fails.
*
* If the query is successful, we get a list of tag objects. For each item in
* the tag list, a page will be created.
*/
const loadBlogTags = new Promise((resolve, reject) => {
graphql(`
{
allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
limit: 1000
) {
nodes {
fields {
slug
}
frontmatter {
tags
}
}
}
}
`).then(({ errors, data }) => {
if (errors) {
reject(errors);
}
/**
* @typedef {object} BlogTagNode
* @property {object} fields
* @property {string} fields.slug Slugged title of the post based on provided `frontmatter.title`
* @property {BlogTag[]} fields.plainTags Tags on provided `frontmatter.tags`
* @property {object} frontmatter
* @property {string[]} frontmatter.tags A list of tags provided in `frontmatter.tags`
*/
/** @type {BlogTagNode[]} */
const nodes = data.allMarkdownRemark.nodes;
/**
* @typedef {object} TagMapItem
* @property {string} tag
* @property {string[]} posts
*/
/** @type {{ [index: string]: TagMapItem }} */
const tagMap = {};
nodes.forEach(({ frontmatter, fields }) => {
const { slug } = fields;
const { tags } = frontmatter;
tags.forEach((tag) => {
const sluggedTag = slugify(tag);
const tagObj = tagMap[sluggedTag];
if (tagObj) {
tagObj.posts.push(slug);
} else {
tagMap[sluggedTag] = {
tag,
posts: [slug],
};
}
});
});
Object.entries(tagMap).forEach(([slug, { tag, posts }]) => {
const totalPosts = posts.length;
const numPages = Math.ceil(totalPosts / postsPerPage);
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: `/blog/tag/${slug}/${i === 0 ? '' : i + 1}`,
component: template.tag,
context: {
tag,
slug,
totalPosts,
limit: postsPerPage,
skip: i * postsPerPage,
numPages: numPages,
currentPage: i + 1,
},
});
});
});
resolve();
});
});
/**
* **Creates the paginated list of author posts for every author.**
*
* The first part is is a call to the `graphql` function passed with the
* query string for this build step. This string only fetches what is need to
* build all the paginated list of author posts.
*
* We let this query run asynchronously. If the query fails, the promise is
* rejected and the build fails.
*
* If the query is successful, we get a list of author objects. For each item
* in the author list, a page will be created. The definition of an author
* entry is provided in `<rootDir>/content/author.yml`.
*/
const loadBlogAuthors = new Promise((resolve, reject) => {
graphql(`
{
allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
limit: 1000
) {
nodes {
fields {
slug
}
frontmatter {
author {
id
name
}
}
}
}
}
`).then(({ errors, data }) => {
if (errors) {
reject(errors);
}
/**
* @typedef {object} BlogAuthorNode
* @property {object} fields
* @property {string} fields.slug Slugged title of the post based on provided `frontmatter.title`
* @property {object} frontmatter
* @property {object} frontmatter.author Mapped author object
* @property {string} frontmatter.author.id Author ID
* @property {string} frontmatter.author.name Author display name
*/
/** @type {BlogAuthorNode[]} */
const nodes = data.allMarkdownRemark.nodes;
/**
* @typedef {object} Author
* @property {string} id Author ID
* @property {string} name Author display name
* @property {string[]} posts List of slugged post titles
*/
/** @type {{ [index: string]: Author }}*/
const authorMap = {};
nodes.forEach(({ frontmatter, fields }) => {
const { author } = frontmatter;
const sluggedAuthor = slugify(author.id);
const authorObj = authorMap[sluggedAuthor];
if (authorObj) {
authorObj.posts.push(fields.slug);
} else {
authorMap[sluggedAuthor] = {
name: author.name,
id: author.id,
posts: [fields.slug],
};
}
});
Object.entries(authorMap).forEach(
([slug, { id: authorId, name: authorName, posts: authorPosts }]) => {
const totalPosts = authorPosts.length;
const numPages = Math.ceil(totalPosts / postsPerPage);
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: `/blog/author/${slug}/${i === 0 ? '' : i + 1}`,
component: template.author,
context: {
authorId,
authorName,
totalPosts,
slug,
limit: postsPerPage,
skip: i * postsPerPage,
numPages: numPages,
currentPage: i + 1,
},
});
});
},
);
resolve();
});
});
return Promise.all([loadBlogPosts, loadBlogTags, loadBlogAuthors]);
};