forked from the-programmers-hangout/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
217 lines (186 loc) · 5.16 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
const { graphql } = require("gatsby")
const { createFilePath } = require("gatsby-source-filesystem")
const helper = require("./scripts/buildHelpers")
const path = require(`path`)
const fs = require("fs")
const requiredArticleFrontmatter = ["authors", "date"]
const LAYOUT_RESOURCES = "resources"
const LAYOUT_ARCHIVES = "archives"
const LAYOUT_REGULAR = "regular"
const LAYOUT_HOME = "home"
function resolveLayout(path) {
if (path === "/") {
return LAYOUT_HOME
}
if (path.match(/resources/)) {
return LAYOUT_RESOURCES
}
if (path.match(/archives/)) {
return LAYOUT_ARCHIVES
}
return LAYOUT_REGULAR
}
let users = []
try {
users = JSON.parse(fs.readFileSync("./users.json", "utf-8"))
console.log("(◕◡◕✿) Users list loaded!")
} catch (e) {
console.log("(◕‸ ◕✿) Users list not present, skipping user resolution...")
}
const validateResourceArticle = node => {
const missingField = requiredArticleFrontmatter.find(
required => node.frontmatter[required]
)
if (missingField) {
console.error(
`Article ${node.fileAbsolutePath} is missing field '${missingField}'`
)
process.exit(1)
}
}
const createResources = async ({ createPage, graphql }) => {
const resourcePage = path.resolve(`src/templates/resourcePage.tsx`)
const resourceHome = path.resolve(`src/templates/resourceHome.tsx`)
const languagesQuery = await graphql(
`
query FetchLanguages {
allFile(filter: { sourceInstanceName: { eq: "languages" } }) {
edges {
node {
relativePath
relativeDirectory
sourceInstanceName
}
}
}
}
`
)
const topicsQuery = await graphql(
`
query FetchTopics {
allFile(filter: { sourceInstanceName: { eq: "topics" } }) {
edges {
node {
relativePath
relativeDirectory
sourceInstanceName
}
}
}
}
`
)
const resources = [
{ key: "topics", query: topicsQuery },
{ key: "languages", query: languagesQuery },
]
for ({ key, query } of resources) {
if (query.errors) {
return Promise.reject(query.errors)
}
const resources = query.data.allFile.edges.reduce((acc, { node }) => {
const [resourceCategory] = node.relativePath.split("/")
if (!acc.includes(resourceCategory)) {
acc.push(resourceCategory)
}
return acc
}, [])
// create home page for each resources
resources.forEach((resource) => {
createPage({
path: path.posix.join("resources", resource),
component: resourceHome,
context: {
resourceType: key,
entry: resource,
layout: LAYOUT_RESOURCES,
},
})
})
query.data.allFile.edges.forEach(({ node }) => {
createPage({
path: path.posix.join("resources", node.relativePath),
component: resourcePage,
context: {
file: node.relativePath,
directory: node.relativeDirectory,
layout: LAYOUT_RESOURCES,
},
})
})
}
}
const createArchives = async ({ createPage, graphql }) => {
const archive = path.resolve(`src/templates/archive.tsx`)
const result = await graphql(`
query FetchArchives {
allFile(filter: { sourceInstanceName: { eq: "what-is-archive" } }) {
edges {
node {
relativePath
sourceInstanceName
}
}
}
}
`)
if (result.errors) {
return Promise.reject(result.errors)
}
return result.data.allFile.edges.forEach(({ node }) => {
createPage({
path: path.posix.join("archives", node.relativePath),
component: archive,
context: {
file: node.relativePath,
layout: LAYOUT_ARCHIVES,
},
})
})
}
exports.onCreateNode = async ({ node, getNode, actions }) => {
const { createPage, createNodeField } = actions
// TODO: find a better way to avoid handling non-resources
// or better, attach authors to what-is archives
if (node.internal.type === "Mdx" && node.frontmatter.authors) {
const { frontmatter } = node
const isDoc = Boolean(!frontmatter.path)
const githubLink = helper.findMarkdownLink(node.fileAbsolutePath)
createNodeField({
node,
name: "githubLink",
value: githubLink,
})
if (!isDoc) {
return
}
const authors = helper.resolveAuthors(users, frontmatter.authors)
createNodeField({
node,
name: "authors",
value: authors,
})
}
}
exports.onCreatePage = async ({ page, actions }) => {
const { createPage, deletePage } = actions
if (page.path.match(/^\/resources\/404/)) {
const oldPage = { ...page }
page.matchPath = `/resources/*`
deletePage(oldPage)
} else if (page.path.match(/^\/archives\/404/)) {
const oldPage = { ...page }
page.matchPath = `/archives/*`
deletePage(oldPage)
}
page.context.layout = resolveLayout(page.path)
createPage(page)
}
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
return Promise.all([
createArchives({ createPage, graphql }),
createResources({ createPage, graphql }),
])
}