-
Notifications
You must be signed in to change notification settings - Fork 671
/
gatsby-node.js
651 lines (591 loc) · 17.8 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
const path = require(`path`)
const crypto = require("crypto")
const matter = require("gray-matter")
var fs = require("fs-extra")
/*
For additional reference material, see
https://www.gatsbyjs.com/docs/reference/config-files/gatsby-node/
The contents of this file define how the source content is converted
into web pages. It is prime for refactoring.
*/
/** This helper function determines what slug a piece of content will use */
const calculateSlug = (node, getNode) => {
const fileName = getNode(node.parent).name // Sets the filename from GraphQL
if (node.frontmatter.permalink) {
//If the "permalink" frontmatter value is present...
return node.frontmatter.permalink // return it.
.replace(":basename", fileName) // If permalink includes ":basename", prepend the filename to the slug
.replace("docs", "") // If it includes "docs", remove it. Docs is added by the pathPrefix in gatsby-config.js
}
// I'm honestly not sure about this. If Alex isn't around and you need to know, ask Octahedroid.
if (fileName === undefined) {
return `${fileName}`
}
// This section creates the changelog slug based on the YYYY-MM-DD-MONTH.md template
if (getNode(node.parent).absolutePath.includes("changelogs")) {
// If the file is in the changelogs directory...
const split = fileName.split("-") // split the file name where hyphenated...
return `changelog/${split[0]}/${split[1]}` // and return a slug of changelog/YYYY/MM
}
// This section creates the releasenotes slug based on the YYYY-MM-DD-slug.md template
if (getNode(node.parent).absolutePath.includes("releasenotes")) {
// If the file is in the releasenotes directory...
// split the file name where hyphenated.
const split = fileName.split("-")
// set a const to remaining slug based on the keys from split that are not the date.
const remainingSlug = split.slice(3).join("-")
return `release-notes/${split[0]}/${split[1]}/${remainingSlug}` // and return a slug of releasenotes/YYYY/MM/slug
}
return `${fileName}` // Otherwise, as long as there is a filename in GraphQL, use it as the slug.
}
/* This helper function helps determine which template should be applied to a piece of content */
const calculateTemplate = (node, defaultTemplate) => {
// The functions accepts as arguments a GraphQL node and default template.
if (
node.frontmatter &&
node.frontmatter.layout &&
node.frontmatter.layout !== null
) {
// If the node has the layout value in frontmatter...
return node.frontmatter.layout // use that value.
}
return defaultTemplate // Otherwise, use the default template argument value.
}
/* These helper functions are used to provide multi-page guide pages with the previous
and next pages in the guide, to be used in the construction of the table of contents.
*/
const calculatePrevious = (guide) => {
// The function accepts as an argument the node of the guide page in question
if (!guide.previous) {
// If the page doesn't have a value for the "previous" field in GraphQL
return null // return nothing.
}
// Also return nothing if the guide_directory value doesn't exist.
if (
guide.node.fields.guide_directory !== guide.previous.fields.guide_directory
) {
return null
}
// Otherwise, return the slug of the page identified as previous by GraphQL. How it determines which one is previous, unclear to me at this time.
return guide.previous.fields.slug
}
// Same as above.
const calculateNext = (guide) => {
if (!guide.next) {
return null
}
if (guide.node.fields.guide_directory !== guide.next.fields.guide_directory) {
return null
}
return guide.next.fields.slug
}
const digest = (str) =>
str != null
? crypto.createHash("md5").update(str).digest("hex")
: crypto.createHash("md5").update(" ").digest("hex")
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
modules: [path.resolve(__dirname, "src"), "node_modules"],
},
})
}
exports.createSchemaCustomization = ({ actions, schema }) => {
actions.createFieldExtension({
name: `defaultFalse`,
extend() {
return {
resolve(source, args, context, info) {
if (source[info.fieldName] == null) {
return false
}
return source[info.fieldName]
},
}
},
})
actions.createTypes(`
type Mdx implements Node {
fileInfo: File @link(from: "parent"),
frontmatter: Frontmatter
}
type Frontmatter {
draft: Boolean @defaultFalse
}
`)
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return graphql(`
{
allDocs: allMdx(
filter: {
fileAbsolutePath: {
regex: "/content(?!/(partials|changelog|guides|releasenotes|iframeembeds)/)/"
}
frontmatter: { draft: { ne: true } }
}
) {
edges {
node {
fileAbsolutePath
frontmatter {
title
layout
permalink
draft
}
fields {
slug
}
}
}
}
allGuides: allMdx(
filter: {
fileAbsolutePath: { regex: "/guides/" }
frontmatter: { draft: { ne: true } }
}
sort: { fields: [fileAbsolutePath], order: ASC }
) {
edges {
previous {
fields {
slug
guide_directory
}
}
node {
frontmatter {
title
layout
permalink
draft
}
fields {
slug
guide_directory
}
}
next {
fields {
slug
guide_directory
}
}
}
}
allIframeEmbeds: allMdx(limit: 10, filter: {fileAbsolutePath: {regex: "/iframeembeds/"}}) {
edges {
node {
slug
body
}
}
}
allReleaseNotes: allMdx(
filter: { fileAbsolutePath: { regex: "/releasenotes/" } }
sort: { fields: [fileAbsolutePath], order: DESC }
) {
edges {
previous {
fields {
slug
}
}
node {
id
frontmatter {
title
categories
published_date
}
fields {
slug
}
}
next {
fields {
slug
}
}
}
}
allChangelogs: allMdx(
filter: {
fileAbsolutePath: { regex: "/changelogs/" }
frontmatter: { draft: { ne: true } }
}
sort: { fields: [fileAbsolutePath], order: DESC }
) {
edges {
previous {
fields {
slug
}
}
node {
id
frontmatter {
title
}
fields {
slug
}
}
next {
fields {
slug
}
}
}
}
allContributorYaml {
edges {
node {
yamlId
}
}
}
allLandingsYaml {
edges {
node {
id
path
}
}
}
dataJson {
commands {
description
help
name
usage
}
}
releasenotescategoriesJson {
categories {
slug
}
}
}
`).then((result) => {
if (result.errors) {
throw result.errors
}
// Create doc pages.
const docs = result.data.allDocs.edges
docs.forEach((doc) => {
const template = calculateTemplate(doc.node, "doc")
createPage({
path: doc.node.fields.slug,
component: path.resolve(`./src/templates/${template}.js`),
context: {
slug: doc.node.fields.slug,
},
})
})
// Create guide pages.
const guides = result.data.allGuides.edges
guides.forEach((guide) => {
if (guide.node.fields.guide_directory !== null) {
const previous = calculatePrevious(guide)
const next = calculateNext(guide)
const template = calculateTemplate(guide.node, "guide")
createPage({
path: guide.node.fields.slug,
component: path.resolve(`./src/templates/${template}.js`),
context: {
slug: guide.node.fields.slug,
guide_directory: guide.node.fields.guide_directory,
previous,
next,
},
})
} else {
const template = calculateTemplate(guide.node, "doc")
createPage({
path: guide.node.fields.slug,
component: path.resolve(`./src/templates/${template}.js`),
context: {
slug: guide.node.fields.slug,
},
})
}
})
// Create changelog pages.
const changelogs = result.data.allChangelogs.edges
changelogs.forEach((changelog) => {
const previous = changelog.previous
? changelog.previous.fields.slug || null
: null
const next = changelog.next ? changelog.next.fields.slug || null : null
const template = calculateTemplate(changelog.node, "changelog")
createPage({
path: changelog.node.fields.slug,
component: path.resolve(`./src/templates/${template}.js`),
context: {
slug: changelog.node.fields.slug,
next: previous,
previous: next,
},
})
})
// Create changelog pagination.
const postsPerPage = 6
const numPages = Math.ceil(changelogs.length / postsPerPage)
Array.from({ length: numPages }).forEach((_, i) => {
const currentPage = i + 1
const next =
currentPage === 1
? null
: currentPage === 2
? `/changelog/`
: `/changelog/page/${currentPage - 1}`
const previous =
currentPage < numPages ? `/changelog/page/${currentPage + 1}` : null
createPage({
path: i === 0 ? `/changelog/` : `/changelog/page/${i + 1}`,
component: path.resolve("./src/templates/changelogs.js"),
context: {
limit: postsPerPage,
skip: i * postsPerPage,
numPages,
currentPage,
previous,
next,
},
})
})
// Create Terminus Command pages
const terminusCommands = result.data.dataJson.commands
terminusCommands.forEach((command) => {
const slugRegExp = /:/g
const slug = command.name.replace(slugRegExp, "-")
createPage({
path: `terminus/commands/${slug}`,
component: path.resolve(`./src/templates/terminusCommand.js`),
context: {
slug: slug,
name: command.name,
},
})
})
// Create release notes without pagination. At a later date, we may want to add pagination.
// And can reused the code above.
createPage({
path: `/release-notes/`,
component: path.resolve("./src/templates/releaseNotesListing/index.js"),
})
// Create release notes pages for pagination.
const releaseNotesPostsPerPage = 8
const releaseNotesNumPages = Math.ceil(result.data.allReleaseNotes.edges.length / releaseNotesPostsPerPage)
Array.from({ length: releaseNotesNumPages }).forEach((_, i) => {
const path2 = `/release-notes/${i+1}/`;
createPage({
path: path2,
component: path.resolve("./src/templates/releaseNotesListing/index.js"),
context: {
limit: releaseNotesPostsPerPage,
skip: i * releaseNotesPostsPerPage,
numPages: releaseNotesNumPages,
i,
},
})
})
// terminusCommands.forEach(command => {
// const slugRegExp = /:/g
// const slug = command.name.replace(slugRegExp, "-")
// createPage({
// path: `terminus/commands/${slug}`,
// component: path.resolve(`./src/templates/terminusCommand.js`),
// context: {
// slug: slug,
// name: command.name
// }
// })
// })
// Create each release note page.
const releaseNotes = result.data.allReleaseNotes.edges
releaseNotes.forEach((releaseNote) => {
const template = calculateTemplate(releaseNote.node, "releaseNotePage")
createPage({
path: releaseNote.node.fields.slug,
component: path.resolve(`./src/templates/${template}.js`),
context: {
slug: releaseNote.node.fields.slug,
},
})
})
// Create iframeembed page
const iframeEmbeds = result.data.allIframeEmbeds.edges;
iframeEmbeds.forEach((iframeEmbed) => {
// There's an odd bug where the slug value is inconsistent, even for the same file
// across different gatsby builds. This is a workaround.
// if the slug does not contain "iframeembeds" the add it
if (!iframeEmbed.node.slug.includes("iframeembeds")) {
iframeEmbed.node.slug = `iframeembeds/${iframeEmbed.node.slug}`
}
createPage({
path: `${iframeEmbed.node.slug}`,
component: path.resolve(`./src/templates/iframeembed.js`),
context: {
slug: iframeEmbed.node.slug,
body: iframeEmbed.node.body,
},
})
})
// Create contributor pages.
const contributors = result.data.allContributorYaml.edges
contributors.forEach((contributor) => {
createPage({
path: `contributors/${contributor.node.yamlId}`,
component: path.resolve(`./src/templates/contributor.js`),
context: {
id: contributor.node.yamlId,
},
})
})
// Create topics pages.
const topics = result.data.allLandingsYaml.edges
topics.forEach((topic) => {
createPage({
path: topic.node.path,
component: path.resolve(`./src/templates/landing.js`),
context: {
id: topic.node.id,
},
})
})
return null
})
}
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNode, createNodeField } = actions
if (node.internal.owner === "gatsby-transformer-yaml") {
Object.keys(node).forEach((item) => {
if (!item.endsWith("Markdown")) {
return
}
const textNode = {
id: `${node.id}-${item}`,
parent: node.id,
dir: path.resolve("./"),
internal: {
type: `${node.internal.type}${item}`,
mediaType: "text/markdown",
content: node[item],
contentDigest: digest(node[item]),
},
}
createNode(textNode)
createNodeField({
node,
name: `${item}___NODE`,
value: textNode.id,
})
})
}
// MDX content
if (node.internal.type === `Mdx`) {
const sourceInstanceName = getNode(node.parent).sourceInstanceName
if (sourceInstanceName === "content") {
const editPath = `source/content/${getNode(node.parent).relativePath}`
// Add editPath field
createNodeField({
name: `editPath`,
node,
value: editPath,
})
}
// Add slug field
const slug = calculateSlug(node, getNode)
createNodeField({
name: `slug`,
node,
value: slug,
})
if (slug.includes("guides/")) {
if (getNode(node.parent).relativeDirectory !== "guides") {
// Add guide_directory field
createNodeField({
name: `guide_directory`,
node,
value: `${getNode(node.parent).relativeDirectory}`,
})
}
}
if (
sourceInstanceName === "changelogs" ||
sourceInstanceName === "releasenotes"
) {
const content = matter(node.internal.content, {
excerpt: true,
excerpt_separator: "<!-- excerpt -->",
})
const excerpt = content.excerpt || ""
createNodeField({
name: `excerpt`,
node,
value: excerpt,
})
const textNode = {
id: `${node.id}-MarkdownBody`,
parent: node.id,
dir: path.resolve("./"),
internal: {
type: `${node.internal.type}MarkdownBody`,
mediaType: "text/markdown",
content: excerpt,
contentDigest: digest(excerpt),
},
}
createNode(textNode)
// Create markdownBody___NODE field
createNodeField({
node,
name: "markdownBody___NODE",
value: textNode.id,
})
}
}
// Releases and Changelog Content
var nodeString = node.internal.type.toString()
var isReleaseJson = nodeString.includes("ReleasesJson")
if (isReleaseJson && !nodeString.includes("MarkdownBody")) {
//console.log("Creating markdownBody for ", node.internal.type.toString()) // For debugging
// Add original_id as int to filter using GraphQL
createNodeField({
name: `original_id`,
node,
value: parseInt(node.id),
})
// Add text/markdown node children to Release and Changelog nodes
const textNode = {
id: `${node.id}-MarkdownBody`,
parent: node.id,
dir: path.resolve("./"),
internal: {
type: `${node.internal.type}MarkdownBody`,
mediaType: "text/markdown",
content: node.body || "",
contentDigest: digest(node.body),
},
}
createNode(textNode)
// Create markdownBody___NODE field
createNodeField({
node,
name: "markdownBody___NODE",
value: textNode.id,
})
}
}
// This block moves script files from source to the public (build artifact) directory.
exports.onPreBootstrap = () => {
const scriptsCopyFrom = `${__dirname}//source/scripts`
const scriptsCopyTo = `${__dirname}/public/scripts`
fs.copySync(scriptsCopyFrom, scriptsCopyTo)
}
/* todo should there be an error thrown if a release note category is set that is not allowed */
/* todo, infer published date from file name. And throw an error if there are files that don't follow the pattern. */