-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
382 lines (358 loc) · 11.7 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
const path = require('path');
const _ = require('lodash');
const fs = require('fs');
// const webpackLodashPlugin = require('lodash-webpack-plugin');
const siteConfig = require('./data/SiteConfig');
const { createPaginationPages, createLinkedPages, prefixPathFormatter } = require('gatsby-pagination');
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
let slug;
if (node.internal.type === 'MarkdownRemark') {
const fileNode = getNode(node.parent);
const parsedFilePath = path.parse(fileNode.relativePath);
if (
Object.prototype.hasOwnProperty.call(node, 'frontmatter') &&
Object.prototype.hasOwnProperty.call(node.frontmatter, 'slug')
) {
slug = `/${_.kebabCase(node.frontmatter.slug)}`;
} else if (
Object.prototype.hasOwnProperty.call(node, 'frontmatter') &&
Object.prototype.hasOwnProperty.call(node.frontmatter, 'title')
) {
slug = `/${_.kebabCase(node.frontmatter.title)}`;
} else if (parsedFilePath.name !== 'index' && parsedFilePath.dir !== '') {
slug = `/${parsedFilePath.dir}/${parsedFilePath.name}/`;
} else if (parsedFilePath.dir === '') {
slug = `/${parsedFilePath.name}/`;
} else {
slug = `/${parsedFilePath.dir}/`;
}
createNodeField({ node, name: 'slug', value: slug });
}
};
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return new Promise((resolve, reject) => {
const indexPage = path.resolve('src/templates/index.jsx');
const postPage = path.resolve('src/templates/post.jsx');
const tagPage = path.resolve('src/templates/tag.jsx');
const categoryPage = path.resolve('src/templates/category.jsx');
const authorPage = path.resolve('src/templates/author.jsx');
const guidesPage = path.resolve('src/templates/trail-guides.js');
const fundamentalsPage = path.resolve('src/templates/fundamentals.jsx');
const hikeInPage = path.resolve('src/templates/hike-in.jsx');
const driveInPage = path.resolve('src/templates/drive-in.jsx');
if (!fs.existsSync(path.resolve(`content/${siteConfig.blogAuthorDir}/authors/`))) {
reject("The 'authors' folder is missing within the 'blogAuthorDir' folder.");
}
resolve(
graphql(
`
{
allMarkdownRemark(limit: 1000, sort: { fields: [frontmatter___date], order: DESC }) {
totalCount
edges {
node {
frontmatter {
title
tags
cover
thumbnail
date
category
author
}
fields {
slug
}
excerpt(pruneLength: 280)
timeToRead
}
}
}
fundamentalPosts: allMarkdownRemark(
limit: 1000
filter: { fileAbsolutePath: { regex: "/(\\/content\\/fundamentals)/.*\\\\.md$/" } }
sort: { fields: [frontmatter___date], order: DESC }
) {
totalCount
edges {
node {
frontmatter {
title
tags
cover
thumbnail
date
category
author
date
}
fields {
slug
}
excerpt
timeToRead
}
}
}
trailPosts: allMarkdownRemark(
limit: 1000
filter: {
fileAbsolutePath: { regex: "/(\\/content\\/trail-posts)/.*\\\\.md$/" }
}
sort: { fields: [frontmatter___date], order: DESC }
) {
totalCount
edges {
node {
frontmatter {
title
cover
thumbnail
author
category
date
difficulty
distance
elevation
location
tags
}
fields {
slug
}
excerpt(pruneLength: 280)
timeToRead
}
}
}
hikeInPosts: allMarkdownRemark(
limit: 1000
filter: {
fileAbsolutePath: { regex: "/(\\/content\\/trail-posts)/.*\\\\.md$/" }
frontmatter: { category: { eq: "hike in" } }
}
sort: { fields: [frontmatter___date], order: DESC }
) {
totalCount
edges {
node {
frontmatter {
title
campTitle
category
cover
date
difficulty
distance
elevation
location
tags
thumbnail
usage
}
fields {
slug
}
excerpt(pruneLength: 280)
timeToRead
}
}
}
driveInPosts: allMarkdownRemark(
limit: 1000
filter: {
fileAbsolutePath: { regex: "/(\\/content\\/trail-posts)/.*\\\\.md$/" }
frontmatter: { category: { eq: "drive in" } }
}
sort: { fields: [frontmatter___date], order: DESC }
) {
totalCount
edges {
node {
frontmatter {
title
campTitle
category
cover
date
difficulty
distance
elevation
location
tags
thumbnail
usage
}
fields {
slug
}
excerpt(pruneLength: 280)
timeToRead
}
}
}
}
`
).then(result => {
console.log('RESULTS', result);
if (result.errors) {
/* eslint no-console: "off" */
console.log(result.errors);
reject(result.errors);
}
/**
* IMPORTANT: For camping guides/posts separate from "Trail Guides", must
* note them here via the relative path.
*/
const excludedTrails = ['/chilao-campground', '/blue-ridge-campground', '/buckhorn-campground'];
const acceptedTrails = result.data.trailPosts.edges.filter(p => {
return !excludedTrails.includes(p.node.fields.slug);
});
// Creates Index page
createPaginationPages({
createPage,
edges: result.data.allMarkdownRemark.edges,
component: indexPage,
limit: 3
});
/* Creates Guides page */
createPaginationPages({
createPage,
edges: acceptedTrails,
component: guidesPage,
limit: siteConfig.sitePaginationLimit,
pathFormatter: prefixPathFormatter('/trail-guides')
});
createLinkedPages({
createPage,
edges: result.data.trailPosts.edges,
component: postPage,
edgeParser: edge => ({
path: edge.node.fields.slug,
context: {
slug: edge.node.fields.slug,
tag: edge.node.frontmatter.tags[0],
tags: edge.node.frontmatter.tags
}
}),
circular: true
});
/* Creates Hiking Fundamentals page */
createPaginationPages({
createPage,
edges: result.data.fundamentalPosts.edges,
component: fundamentalsPage,
limit: siteConfig.sitePaginationLimit,
pathFormatter: prefixPathFormatter('/fundamentals')
});
createLinkedPages({
createPage,
edges: result.data.fundamentalPosts.edges,
component: postPage,
edgeParser: edge => ({
path: edge.node.fields.slug,
context: {
slug: edge.node.fields.slug,
tag: edge.node.frontmatter.tags[0],
tags: edge.node.frontmatter.tags
}
}),
circular: true
});
/* Creates Camp Hike-In page */
createPaginationPages({
createPage,
edges: result.data.hikeInPosts.edges,
component: hikeInPage,
limit: siteConfig.sitePaginationLimit,
pathFormatter: prefixPathFormatter('/hike-in')
});
/* Creates Camp Drive-In page */
createPaginationPages({
createPage,
edges: result.data.driveInPosts.edges,
component: driveInPage,
limit: siteConfig.sitePaginationLimit,
pathFormatter: prefixPathFormatter('/drive-in')
});
const tagSet = new Set();
const tagMap = new Map();
const categorySet = new Set();
const authorSet = new Set();
authorSet.add(siteConfig.blogAuthorId);
result.data.allMarkdownRemark.edges.forEach(edge => {
if (edge.node.frontmatter.tags) {
edge.node.frontmatter.tags.forEach(tag => {
tagSet.add(tag);
const array = tagMap.has(tag) ? tagMap.get(tag) : [];
array.push(edge);
tagMap.set(tag, array);
});
}
if (edge.node.frontmatter.category) {
categorySet.add(edge.node.frontmatter.category);
}
if (edge.node.frontmatter.author) {
authorSet.add(edge.node.frontmatter.author);
}
});
const tagFormatter = tag => route => `/tags/${_.kebabCase(tag)}/${route !== 1 ? route : ''}`;
const tagList = Array.from(tagSet);
tagList.forEach(tag => {
// Creates tag pages
createPaginationPages({
createPage,
edges: tagMap.get(tag),
component: tagPage,
pathFormatter: tagFormatter(tag),
limit: siteConfig.sitePaginationLimit,
context: {
tag
}
});
});
const categoryList = Array.from(categorySet);
categoryList.forEach(category => {
createPage({
path: `/categories/${_.kebabCase(category)}/`,
component: categoryPage,
limit: siteConfig.sitePaginationLimit,
context: {
category
}
});
});
const authorList = Array.from(authorSet);
authorList.forEach(author => {
createPage({
path: `/author/${_.kebabCase(author)}/`,
component: authorPage,
context: {
author
}
});
});
})
);
});
};
// exports.modifyWebpackConfig = ({ config, stage }) => {
// if (stage === 'build-javascript') {
// config.plugin('Lodash', webpackLodashPlugin, null);
// }
// };
// exports.onCreateWebpackConfig = ({ stage, actions }) => {
// switch (stage) {
// case 'build-javascript':
// actions.setWebpackConfig({
// plugins: [webpackLodashPlugin],
// });
// }
// if (stage === 'build-javascript') {
// actions.setWebpackConfig({
// plugins: [webpackLodashPlugin],
// });
// }
// };