-
Notifications
You must be signed in to change notification settings - Fork 5
/
gatsby-node.js
186 lines (163 loc) · 4.13 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
const fs = require('fs-extra');
const path = require('path');
exports.createSchemaCustomization = ({ actions, schema }) => {
const { createTypes } = actions;
const typeDefs = [
`
type DisplayDataEntry {
value: String
label: String
valueProp: String
labelProp: String
}
type LayerLegend {
type: String
min: String
max: String
stops: [String]
color: String
icon: String
dashed: Boolean
}
type PanelLayerSource {
name: String
url: String
}
type PanelLayer {
id: String
name: String
visible: Boolean
category: String
mbLayer: String
info: String
source: PanelLayerSource
legendData: LayerLegend
displayData: [DisplayDataEntry]
}
type ChartDefinition {
name: String
type: String
data: JSON
datum: JSON
}
type Platform {
title: String
url: String
}
type StudyInfo {
consultant: String
period: String
content: File @fileByRelativePath
}
`,
schema.buildObjectType({
name: 'PostsYaml',
fields: {
title: 'String',
external: 'String',
bbox: 'JSON',
zoomExtent: 'JSON',
country: 'String',
platform: 'Platform',
study: 'StudyInfo',
layers: '[PanelLayer]',
charts: '[ChartDefinition]',
mapConfig: {
type: 'JSON',
resolve: async (source, args, context) => {
if (!source.mapConfig) return null;
// Find the parent File which we'll use to get the directory.
const parentFileNode = context.nodeModel.findRootNodeAncestor(
source,
(node) => node.internal && node.internal.type === `File`
);
// Resolve the full path to the map config.
const mapConfigPath = path.resolve(
parentFileNode.dir,
source.mapConfig
);
// Check if the file is inside the current directory. This prevents
// access to other system paths.
const relative = path.relative(__dirname, mapConfigPath);
const isSafe =
relative &&
!relative.startsWith('..') &&
!path.isAbsolute(relative);
if (!isSafe) {
return null;
}
try {
return fs.readJSON(mapConfigPath);
} catch (error) {
/* eslint-disable-next-line no-console */
console.log('Error reading map config', error);
return null;
}
}
}
},
interfaces: ['Node']
})
];
createTypes(typeDefs);
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const result = await graphql(
`
{
allPostsYaml(
sort: { fields: title }
filter: { external: { eq: null } }
) {
nodes {
id
fields {
slug
}
}
}
}
`
);
if (result.errors) {
throw result.errors;
}
const allEntries = result.data.allPostsYaml.nodes;
const singleTemplate = path.resolve(`./src/templates/study-single/index.js`);
// Create page for single content.
allEntries.forEach((node) => {
const {
fields: { slug },
id
} = node;
createPage({
path: `studies/${slug}`,
component: singleTemplate,
context: {
id
}
});
});
};
exports.onCreateNode = async ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (
node.internal.type === 'PostsYaml' ||
node.internal.type === `MarkdownRemark`
) {
const parentNode = getNode(node.parent);
const collection = parentNode.sourceInstanceName;
const fileName = getNode(node.parent).name;
createNodeField({
node,
name: 'collection',
value: collection
});
createNodeField({
node,
name: 'slug',
value: fileName
});
}
};