forked from anvilproject/anvil-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
496 lines (472 loc) · 12.6 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
/**
* The AnVIL
* https://www.anvilproject.org
*/
const express = require("express");
const { createFilePath } = require("gatsby-source-filesystem");
const { buildPostSlug } = require("./src/utils/node/create-node.service");
const {
buildDocumentTitleBySlug,
buildMenuItems,
buildSetOfNavItemsByMenuItem,
buildSetOfSiteSlugs,
buildSlugNavigations,
ComponentPath,
getSlugComponent,
isShouldCreatePage,
} = require("./src/utils/node/create-pages.service");
const {
buildDateBubbleField,
buildDateStartField,
buildPageCreatedField,
buildSessionsDisplayField,
} = require("./src/utils/node/schema-customization.service");
/**
* Create new node fields.
* Places nodes under the "fields" keys on the extended node object.
*
* @param actions
* @param getNode
* @param node
*/
exports.onCreateNode = ({ actions, getNode, node }) => {
const { createNodeField } = actions;
const { internal } = node,
{ type } = internal;
if (type === "MarkdownRemark") {
const { frontmatter } = node,
{ pageAlignment, privateEvent } = frontmatter || {};
/* Extended node fields. */
const filePath = createFilePath({ node, getNode, basePath: "" });
const nodeValuePrivateEvent = privateEvent ? privateEvent : false;
const nodeValueSlug = buildPostSlug(filePath);
const nodeValueStyles = { alignment: pageAlignment };
createNodeField({
node,
name: "privateEvent",
value: nodeValuePrivateEvent,
});
createNodeField({
node,
name: "slug",
value: nodeValueSlug,
});
createNodeField({
node,
name: "styles",
value: nodeValueStyles,
});
}
};
/**
* Create pages.
*
* @param actions
* @param graphql
*/
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions;
return graphql(`
{
allMarkdownRemark {
edges {
node {
fields {
privateEvent
slug
}
frontmatter {
breadcrumb {
link
name
}
date
dateStart
description
title
tutorial
}
}
}
}
allSiteMapYaml {
edges {
node {
menuItem
pageTitle
pathPartial
tabs {
name
pathPartial
navigationItems {
draft
file
name
navigationItems {
draft
file
name
pathOverride
}
pathOverride
pathPartial
}
}
}
}
}
allDashboardStudy {
edges {
node {
id
studyDescriptionShort
studyId
studySlug
}
}
}
allDashboardNcpiStudy {
edges {
node {
id
studyDescriptionShort
studyId
studySlug
}
}
}
}
`).then((result) => {
/* If there is an error, return. */
if (result.errors) {
return Promise.reject(result.errors);
}
const { data } = result,
{
allDashboardStudy,
allDashboardNcpiStudy,
allMarkdownRemark,
allSiteMapYaml,
} = data;
/* Build menuItems where each site map document associates the slug with a path. */
/* This will be used to create the correct path for each post. */
const siteMapNodes = allSiteMapYaml.edges.map((n) => n.node);
const menuItems = buildMenuItems(siteMapNodes);
/* Builds a set of navigation items for each menu item. */
/* Builds next and previous article links in order of appearance in the site map. */
const setOfNavItemsByMenuItem = buildSetOfNavItemsByMenuItem(menuItems);
/* Build the complete set of site document slugs (allowable slugs to be created into a page). */
const setOfSiteSlugs = buildSetOfSiteSlugs(menuItems);
/* Build a relationship between document slug and associated document title. */
/* This will be used to create next/previous page links for each document, when "name" is undefined in the site map for any nav Item. */
const documentTitleBySlug = buildDocumentTitleBySlug(
allMarkdownRemark,
setOfSiteSlugs
);
/* For each markdown file create a post. */
allMarkdownRemark.edges.forEach(({ node }) => {
const { id, fields } = node,
{ slug } = fields;
/* Create a page, if there is a slug and the file exists on the site map. */
if (isShouldCreatePage(slug, setOfSiteSlugs)) {
/* Grab the slug's pageTitle, tabs, navItems and path. */
const slugNavigations = buildSlugNavigations(
slug,
menuItems,
setOfNavItemsByMenuItem,
documentTitleBySlug
);
const slugComponent = getSlugComponent(ComponentPath.CONTENT);
/* Create page. */
createPage({
path: slugNavigations.path,
component: slugComponent,
context: {
id: id,
menuPath: slugNavigations.menuPath,
navItemNext: slugNavigations.navItemNext,
navItemPrevious: slugNavigations.navItemPrevious,
navItems: slugNavigations.navItems,
slug: slug,
tabPath: slugNavigations.tabPath,
tabs: slugNavigations.tabs,
title: slugNavigations.title,
},
});
}
});
/* For each dashboard study create a detailed page. */
allDashboardStudy.edges.forEach(({ node }) => {
const { id, studyDescriptionShort, studyId, studySlug } = node;
const slugComponent = getSlugComponent(ComponentPath.DATASET_STUDY);
/* Create page. */
createPage({
path: studySlug,
component: slugComponent,
context: {
description: studyDescriptionShort,
id: id,
menuPath: "/data",
slug: studySlug,
studyId: studyId,
title: `Study`,
},
});
});
/* For each dashboard NCPI study create a detailed page. */
allDashboardNcpiStudy.edges.forEach(({ node }) => {
const { id, studyDescriptionShort, studyId, studySlug } = node;
const slugComponent = getSlugComponent(ComponentPath.DATASET_STUDY);
/* Create page. */
createPage({
path: studySlug,
component: slugComponent,
context: {
description: studyDescriptionShort,
id: id,
menuPath: "/ncpi/data",
slug: studySlug,
studyId: studyId,
title: `Study`,
},
});
});
});
};
/**
* Schema customization.
*
* @param actions
* @returns {*}
*/
exports.createSchemaCustomization = ({ actions }) => {
const { createFieldExtension, createTypes } = actions;
/* Create field "dateBubble" of type string array. */
createFieldExtension({
name: "dateBubble",
extend() {
return {
resolve(source) {
return buildDateBubbleField(source);
},
};
},
});
/* Create field "dateStart" of type date. */
createFieldExtension({
name: "dateStart",
extend() {
return {
resolve(source) {
return buildDateStartField(source);
},
};
},
});
/* Create field "draft" of type boolean. */
createFieldExtension({
name: "draft",
extend() {
return {
resolve(source) {
/* Returns false when draft is undefined. */
if (source.draft === undefined) {
return false;
}
return JSON.parse(source.draft.toLowerCase());
},
};
},
});
/* Create field "featured" of type boolean. */
createFieldExtension({
name: "featured",
extend() {
return {
resolve(source) {
/* Returns false value when featured is undefined. */
if (source.featured === undefined) {
return false;
}
return source.featured;
},
};
},
});
/* Create field "hashtag" of type string. */
createFieldExtension({
name: "hashtag",
extend() {
return {
resolve(source) {
/* Returns empty string when hashtag is undefined. */
if (source.hashtag === undefined) {
return "";
}
return source.hashtag;
},
};
},
});
/* Create field "pageCreated" of type boolean. */
createFieldExtension({
name: "pageCreated",
extend() {
return {
async resolve(source, arg, context, info) {
const { entries } = await context.nodeModel.findAll({
type: "SitePage",
});
return buildPageCreatedField(source, [...entries]);
},
};
},
});
/* Create field "sessionsDisplay" of type string array. */
createFieldExtension({
name: "sessionsDisplay",
extend() {
return {
resolve(source) {
return buildSessionsDisplayField(source);
},
};
},
});
/* Create field "showOutline" of type boolean. */
createFieldExtension({
name: "showOutline",
extend() {
return {
resolve(source) {
/* Returns true value when showOutline is undefined. */
if (source.showOutline === undefined) {
return true;
}
return source.showOutline;
},
};
},
});
/* Create field "tutorial" of type boolean. */
createFieldExtension({
name: "tutorial",
extend() {
return {
resolve(source) {
/* Returns false when tutorial is undefined. */
if (source.tutorial === undefined) {
return false;
}
return source.tutorial;
},
};
},
});
createTypes(`
type Breadcrumb @dontInfer {
link: String
name: String
}
type Card {
actions: [CardAction]
cardLink: String
citation: Citation
media: Media
subTitle: String
supportingText: String
thumbnail: File @fileByRelativePath
title: String
}
extend type Card {
category: String
}
type CardAction {
label: String
url: String
}
type Citation {
authors: [String]
doi: String
journal: String
publisher: String
year: String
}
type Frontmatter {
benefits: [Card]
breadcrumb: Breadcrumb
conference: String
datasets: [Card]
dateBubble: [String] @dateBubble
dateStart: Date @dateStart
description: String
eventType: String
featured: Boolean @featured
hashtag: String @hashtag
location: String
publications: [Card]
sectionSubHeader: String
sessions: [Session]
sessionsDisplay: [String] @sessionsDisplay
showOutline: Boolean @showOutline
slides: [Card]
subTitle: String
technologies: [Card]
timezone: String
title: String
tutorial: Boolean @tutorial
workspaces: [Card]
}
type MarkdownRemark implements Node {
frontmatter: Frontmatter
pageCreated: Boolean @pageCreated
}
type Media implements Node {
landscape: File @fileByRelativePath
portrait: File @fileByRelativePath
}
type MenuItem implements Node {
name: String
path: String
subMenuItems: [MenuItem]
}
type NavigationItems implements Node {
draft: Boolean @draft
file: String
name: String
navigationItems: [NavigationItems]
pathOverride: String
pathPartial: String
}
type Session {
sessionEnd: String
sessionStart: String
}
type SiteMapYaml implements Node {
menuItem: String
pageTitle: String
pathPartial: String
tabs: [Tab]
}
type Tab implements Node {
name: String
navigationItems: [NavigationItems]
pathPartial: String
}`);
};
// Required for Edge. This function can be removed once Gatsby upgrades to @[email protected]. See:
// https://github.com/gatsbyjs/gatsby/issues/14848
exports.onCreateBabelConfig = ({ actions, stage }) => {
actions.setBabelPlugin({
name: `@babel/plugin-transform-spread`,
options: {
loose: false,
},
});
};
// See: https://github.com/gatsbyjs/gatsby/issues/18213
// See: https://github.com/gatsbyjs/gatsby/issues/13072
// To access static folder while in develop mode
exports.onCreateDevServer = ({ app }) => {
app.use(express.static("public"));
};