-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
139 lines (124 loc) · 4.49 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
const path = require(`path`)
const { spawn } = require("child_process")
const { createFilePath } = require(`gatsby-source-filesystem`)
const getTagDataFromEdges = require(`./utils/tags/getTagDataFromEdges`)
const shared = require(`./src/constants/shared.json`)
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const blogPost = path.resolve(`./src/templates/BlogPost.tsx`)
const blogPostListing = path.resolve("./src/templates/BlogPostListing.tsx")
const blogTagListing = path.resolve("./src/templates/BlogTagListing.tsx")
const result = await graphql(
`
{
allMdx(sort: {frontmatter: {date: DESC}}, limit: 1000) {
edges {
node {
fields {
slug
}
frontmatter {
title
tags
draft
topPostOrder
cleanReactTypeScriptHooksOrder
cleanCrudApisOrder
}
internal {
contentFilePath
}
}
}
}
}
`
)
if (result.errors) {
throw result.errors
}
// get all posts - will need these for building pages
const posts = result.data.allMdx.edges
// Create blog post pages.
posts.forEach((post, index) => {
if (post.node.frontmatter.draft !== undefined && post.node.frontmatter.draft === false) {
console.log(`Post "${post.node.title}" has draft status and will not be published.`)
}
const previous =
index === posts.length - 1 ? null : posts[index + 1].node
const next = index === 0 ? null : posts[index - 1].node
console.log(post.node.fields.slug)
createPage({
path: post.node.fields.slug,
component: `${blogPost}?__contentFilePath=${post.node.internal.contentFilePath}`,
context: {
slug: post.node.fields.slug,
previous,
next,
},
})
})
// Create blog listing pages
const numPages = Math.ceil(posts.length / shared.POSTS_PER_PAGE)
Array.from({ length: numPages }).forEach((_, i) => {
const path = i === 0 ? `/` : `/blog-page-${i + 1}`
createPage({
path,
component: blogPostListing,
context: {
limit: shared.POSTS_PER_PAGE,
skip: i * shared.POSTS_PER_PAGE,
numPages,
currentPage: i + 1,
},
})
console.log(`Created blog listing page at "${path}" !`)
})
// Create tag listing pages
const tagData = getTagDataFromEdges(posts);
tagData.forEach(x => {
createPage({
path: x.link,
component: blogTagListing,
context: {
tag: x.label,
tagRegex: x.tagRegex
}
})
console.log(`Created tag listing page at "${x.link}" (Tag regex was "${x.tagRegex}", clean tag label was "${x.label}") !`)
})
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `Mdx`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}
}
exports.onPostBuild = async () => {
// build all code snippet pdfs
await spawnChild("node", ["create-snippets-pdf.js"], "All PDF exports successful!\n");
// copy cv from dev folder to the exports folder with all the other pdfs
await spawnChild("cp", ["dev/cv.pdf", "public/exports/"], "CV successfully copied to public/exports!");
}
async function spawnChild(command, arguments, successConsoleLog) {
const child = spawn(command, arguments)
for await (const chunk of child.stdout) {
console.log(chunk.toString())
}
for await (const chunk of child.stderr) {
console.error(chunk.toString())
}
const exitCode = await new Promise((resolve) => {
child.on("close", resolve)
})
if (exitCode) {
throw new Error(`Child exited with error code ${exitCode}!`)
} else {
console.log(successConsoleLog)
}
}