-
Notifications
You must be signed in to change notification settings - Fork 5
/
gatsby-node.js
112 lines (99 loc) · 2.57 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
const path = require(`path`)
var slugify = require("slugify")
const fs = require("fs").promises
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
// Defining types because Gatsby is making some Int values String
// which breaks sortign and computation
const typeDefs = `
type CentralProgramsJson implements Node {
staff_roles: [StaffRole]
staff_bargaining_units: [StaffBargainingUnit]
}
type StaffRole {
eoy_total_positions_for_role: Int
role_description: String
}
type StaffBargainingUnit {
abbreviation: String
description: String
eoy_total_positions_for_bu: Int
}
`
createTypes(typeDefs)
}
exports.onCreateNode = ({ node, actions, getNode }) => {
if (node.internal.type === `CentralProgramsJson`) {
const { createNodeField } = actions
// Some program names include parentheses, remove those so they don't end up in path
const slugifyOptions = {
remove: /[*+~.()'"!:@/]/g,
lower: true,
}
const slug = slugify(node.name, slugifyOptions)
// Add a slug field which can be used later to generate page slug
createNodeField({
name: `slug`,
node,
value: slug,
})
// Use slug in path and create node field so it is queryable
createNodeField({
name: `path`,
node,
value: `central-programs/${slug}`,
})
}
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
// code is the site code / OUSD identifier for program
const result = await graphql(`
query {
allCentralProgramsJson {
nodes {
name
code
fields {
path
}
}
}
}
`)
result.data.allCentralProgramsJson.nodes.forEach((node) => {
createPage({
path: node.fields.path,
component: path.resolve(`./src/components/central-program.js`),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
code: node.code,
},
})
})
}
// Generating a list of pages which is used for Percy testing
exports.onPostBuild = async ({ graphql }) => {
const { data } = await graphql(`
{
pages: allSitePage(filter: { path: { regex: "/^/en/|^/es//" } }) {
nodes {
path
}
}
}
`)
return fs.writeFile(
path.resolve(__dirname, "scripts/testing/page-paths.json"),
JSON.stringify(
data.pages.nodes.map((node) => {
return {
url: node.path,
}
}),
null,
2
)
)
}