-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.ts
288 lines (274 loc) · 7.36 KB
/
gatsby-node.ts
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
import path from "path";
import type { GatsbyNode } from "gatsby";
import { flattenPages } from "./src/utils/utils";
import { EventFrontmatter, NewsFrontmatter, PageFrontmatter, StrudelPage, TaskFlowFrontmatter } from "./src/types/strudel-config";
/**
* Shape of the result from the graphql query
* for the strudel config json and the markdown (MDX)
* content files.
*/
interface Result {
errors?: any;
data?: {
configJson: {
pages: StrudelPage[]
},
content: {
nodes: {
frontmatter: TaskFlowFrontmatter | PageFrontmatter,
internal: {
contentFilePath: string;
}
}[]
}
events: {
nodes: {
frontmatter: EventFrontmatter,
internal: {
contentFilePath: string;
},
fields: {
source: string;
}
}[]
}
news: {
nodes: {
frontmatter: NewsFrontmatter,
internal: {
contentFilePath: string;
},
fields: {
source: string;
}
}[]
}
}
}
/**
* Special gatsby method for creating pages programatically.
* Here we are pulling data from strudel-config.json to generate
* pages from the markdown files that correspond to each page.
*/
export const createPages: GatsbyNode["createPages"] = async ({
graphql,
actions,
reporter
}) => {
const { createPage, createRedirect } = actions;
/**
* Graphql query for the page objects in strudel-config.json
* and the mdx files in the content directory.
*/
const result: Result = await graphql(
`
{
configJson {
pages {
name
path
markdownId
layoutComponent
children {
markdownId
name
path
layoutComponent
children {
markdownId
name
path
layoutComponent
}
}
}
}
content: allMdx {
nodes {
frontmatter {
id
title
tagline
tags
intent
intentDetails
exampleUrl
codeUrl
figmaUrl
iconImage {
childImageSharp {
gatsbyImageData(width: 800)
}
}
subtitle
}
internal {
contentFilePath
}
}
}
events: allMdx(filter: {fields: {source: {eq: "events"}}}) {
nodes {
fields {
source
}
frontmatter {
title
slug
date
upcoming
speakers
format
location
registrationLink
shortDescription
image {
childImageSharp {
gatsbyImageData(width: 800)
}
}
}
internal {
contentFilePath
}
}
}
news: allMdx(filter: {fields: {source: {eq: "news"}}}) {
nodes {
fields {
source
}
frontmatter {
title
slug
date
author
thumbnail {
childImageSharp {
gatsbyImageData(width: 800)
}
}
}
internal {
contentFilePath
}
}
}
}
`
);
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
const nestedPages = result.data?.configJson.pages;
const pages = nestedPages && flattenPages(nestedPages);
const mdxPages = result.data?.content.nodes;
const eventPages = result.data?.events.nodes;
const newsPages = result.data?.news.nodes;
/**
* Create a page for each page object that has an associated markdown file.
* Pages that don't have a markdownId are assumed to have a custom page in
* the pages directory.
*
* Page objects are linked to markdown files by an id in the file's frontmatter metadata
* (markdownId === frontmatter.id).
*/
if (pages) {
pages.forEach((page) => {
if (page.markdownId) {
const mdx = mdxPages?.find((d) => d.frontmatter.id === page.markdownId);
if (mdx) {
/**
* The layoutComponent prop will fallback to PageLayout if none exists on the page object.
* Because gatsby graphql looks at the existing properties of strudel-config.json to determine
* what the queryable properties are, the layoutComponent property has been included in the json
* for each object to ensure layoutComponent remains queryable at every page level.
* Technically each property only needs to exist in one object per child-level to be queryable.
*/
page.layoutComponent = page.layoutComponent || 'PageLayout';
const pageTemplate = path.resolve(`src/components/layouts/${page.layoutComponent}.tsx`);
createPage({
path: page.path,
/**
* This will pass the formatted markdown file to the page template via the children prop
*/
component: `${pageTemplate}?__contentFilePath=${mdx.internal.contentFilePath}`,
context: {
frontmatter: mdx.frontmatter,
}
});
}
}
});
}
/**
* Create a page for each event mdx node.
*/
if (eventPages) {
const eventTemplate = path.resolve(`src/components/layouts/EventLayout.tsx`)
eventPages.forEach((eventPage) => {
createPage({
path: `/engage/events/${eventPage.frontmatter.slug}`,
component: `${eventTemplate}?__contentFilePath=${eventPage.internal.contentFilePath}`,
context: {
frontmatter: eventPage.frontmatter,
}
});
})
}
/**
* Create a page for each news mdx node.
*/
if (newsPages) {
const newsTemplate = path.resolve(`src/components/layouts/NewsLayout.tsx`)
newsPages.forEach((newsPage) => {
createPage({
path: `/engage/news/${newsPage.frontmatter.slug}`,
component: `${newsTemplate}?__contentFilePath=${newsPage.internal.contentFilePath}`,
context: {
frontmatter: newsPage.frontmatter,
}
});
})
}
/**
* Add redirects for top-level routes that
* don't have specific pages associated with them.
*/
createRedirect({
fromPath: `/design-system/`,
toPath: `/design-system/overview/`,
isPermanent: true,
force: true,
redirectInBrowser: true,
});
createRedirect({
fromPath: `/design-system/task-flows/`,
toPath: `/design-system/task-flows/overview/`,
isPermanent: true,
force: true,
redirectInBrowser: true,
});
createRedirect({
fromPath: `/planning-framework/`,
toPath: `/planning-framework/overview/`,
isPermanent: true,
force: true,
redirectInBrowser: true,
});
createRedirect({
fromPath: `/engage/`,
toPath: `/engage/contribute/`,
isPermanent: true,
force: true,
redirectInBrowser: true,
});
createRedirect({
fromPath: `/about/`,
toPath: `/about/background/`,
isPermanent: true,
force: true,
redirectInBrowser: true,
});
};