-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
268 lines (240 loc) · 6.9 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
const path = require(`path`);
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;
// Get the template config settings.
const {
data: {
site: {
siteMetadata: { templates }
}
}
} = await graphql(`
{
site {
siteMetadata {
templates {
home {
totalPosts
template
}
pages {
path
template
}
posts {
path
pathPrefix
template
filters {
tag {
pathPrefix
template
totalPosts
pagination {
template
resultsPerPage
}
}
}
pagination {
template
resultsPerPage
}
}
}
}
}
}
`);
/* Find all of the markdown files, sorted descending by filename.
* Newest-to-oldest with YYYY-MM-DD date file prefix.
*/
const createMarkdownPages = async ({
regex,
template,
pathPrefix = "",
paginate = false
}) => {
const result = await graphql(`
{
allMdx(
filter: { fileAbsolutePath: { regex: "${regex}" } }
sort: { order: DESC, fields: [fileAbsolutePath] }
) {
edges {
node {
body
fileAbsolutePath
frontmatter {
id
}
}
}
}
}
`);
// Report any errors if they occurred.
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
// Iterate through the query results to create individual pages.
const pages = result.data.allMdx.edges;
// Calculate the number of paginated results pages.
const totalPages = Math.ceil(
pages.length / templates.posts.pagination.resultsPerPage
);
const staticPages = pages.map(({ node }, index) => {
// Use a permalink based on the frontmatter id in each markdown file header.
const postId = node.frontmatter.id;
// Define the date based on the filename.
const postDate = path
.basename(node.fileAbsolutePath)
.split("-")
.splice(0, 3)
.join("-");
// The path to the previous page.
const previousPath =
index === pages.length - 1
? null
: `/${pathPrefix}/${pages[index + 1].node.frontmatter.id}`;
// The path to the next page.
const nextPath =
index === 0
? null
: `/${pathPrefix}/${pages[index - 1].node.frontmatter.id}`;
return createPage({
path: `${pathPrefix}/${postId}`,
component: path.resolve(`${__dirname}/src/templates/${template}.js`),
context: {
postId,
postDate,
previousPath,
nextPath
}
});
});
return !paginate
? staticPages
: Promise.all([
...staticPages,
Array.from({ length: totalPages }).map((_, i) =>
createPage({
path: `${pathPrefix}/page/${i + 1}`,
component: path.resolve(
`${__dirname}/src/templates/${templates.posts.pagination.template}.js`
),
context: {
limit: templates.posts.pagination.resultsPerPage,
skip: i * templates.posts.pagination.resultsPerPage,
totalPages,
currentPage: i + 1
}
})
)
]);
};
// Find all of the tags used in posts and create search result pages.
const createPostTagFilterPages = async ({
pathPrefix = "",
paginate = false,
template
}) => {
const result = await graphql(`
{
allMdx {
group(field: frontmatter___tags) {
tag: fieldValue
totalCount
}
}
}
`);
// Report any errors if they occurred.
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
// Create a page for each tag.
const tags = result.data.allMdx.group;
const staticTagPages = tags.map(async ({ tag }) => {
const staticTagPage = createPage({
path: `${pathPrefix}/${tag}`,
component: path.resolve(`${__dirname}/src/templates/${template}.js`),
context: {
tag,
limit: templates.posts.filters.tag.totalPosts
}
});
if (!paginate) return staticTagPage;
const postsWithTagResult = await graphql(`{
allMdx(filter: {
fileAbsolutePath: {regex: "/content/posts/"},
frontmatter: {tags: {in: ["${tag}"]}}
}
) {
totalCount
}
}`);
// Report any errors if they occurred.
if (postsWithTagResult.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
const totalPostsWithTag = postsWithTagResult.data.allMdx.totalCount;
const totalPages = Math.ceil(
totalPostsWithTag /
templates.posts.filters.tag.pagination.resultsPerPage
);
const staticTagPagination = Array.from({ length: totalPages }).map(
(_, i) =>
createPage({
path: `${pathPrefix}/${tag}/page/${i + 1}`,
component: path.resolve(
`${__dirname}/src/templates/${templates.posts.filters.tag.pagination.template}.js`
),
context: {
limit: templates.posts.filters.tag.pagination.resultsPerPage,
skip: i * templates.posts.filters.tag.pagination.resultsPerPage,
totalPages: totalPostsWithTag,
currentPage: i + 1,
tag
}
})
);
return Promise.all([staticTagPage, ...staticTagPagination]);
});
return staticTagPages;
};
return await Promise.all([
//Create the home page with paginated results views.
createPage({
path: "/",
component: path.resolve(
`${__dirname}/src/templates/${templates.home.template}.js`
),
context: {
limit: templates.home.totalPosts
}
}),
// Create the individual content pages for mdx files in src/content/pages.
createMarkdownPages({
regex: templates.pages.path,
template: templates.pages.template
}),
// Create individual blog post pages and paginated results pages for mdx files in src/content/posts.
createMarkdownPages({
regex: templates.posts.path,
pathPrefix: templates.posts.pathPrefix,
template: templates.posts.template,
paginate: true
}),
// Create pages for each frontmatter tag used in src/content/posts with paginated result pages.
createPostTagFilterPages({
regex: templates.posts.path,
pathPrefix: templates.posts.filters.tag.pathPrefix,
template: templates.posts.filters.tag.template,
paginate: true
})
]);
};