forked from DataBiosphere/data-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
240 lines (194 loc) · 5.37 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
// Imports
const path = require('path');
const {createFilePath} = require(`gatsby-source-filesystem`);
// find our template files
const contentTemplate = path.resolve(`src/templates/contentTemplate.js`);
const metadataTemplate = path.resolve(`src/templates/metadataTemplate.js`);
const systemStatusTemplate = path.resolve(`src/templates/systemStatusTemplate.js`);
function getTemplate(templateName) {
// see if there is a template matching the template name from the front matter.
if (templateName === 'metadataTemplate') {
return metadataTemplate;
}
if (templateName === 'systemStatusTemplate') {
return systemStatusTemplate;
}
// if not return the default content template.
return contentTemplate;
}
// Returns document path or key.
function getKeyOrPath(key, siteMapPathOrKey) {
const path = siteMapPathOrKey.get(key);
if (path) {
return path;
}
else {
return key
}
}
// sections -> tabs -> primary docs -> secondary docs
function keyToPath(siteMap) {
return siteMap.reduce((acc, section) => {
if (section.tabs) {
return section.tabs.reduce((acc, tab) => {
return tab.primaryLinks.reduce((acc, pLink) => {
addToMap(acc, pLink);
if (pLink.secondaryLinks) {
pLink.secondaryLinks.forEach(sLink => addToMap(acc, sLink));
}
return acc;
}, acc);
}, acc);
}
return acc;
}, new Map());
}
function addToMap(acc, doc) {
if (doc.path) {
acc.set(doc.key, doc.path);
} else {
acc.set(doc.key, doc.key);
}
}
exports.createPages = ({actions, graphql}) => {
const {createPage} = actions;
// create the markdown pages
return graphql(`
{
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
) {
edges {
node {
id
fileAbsolutePath
frontmatter {
path
template
linked {
childMarkdownRemark{
frontmatter{
title
subTitle
}
}
}
}
}
}
}
allSiteMapYaml {
edges {
node {
name
key
path
tabs {
name
key
primaryLinks {
name
key
path
secondaryLinks {
name
key
path
}
}
}
}
}
}
allMetadataSchemaEntity(filter: {schemaType: {eq: "type"}}) {
edges {
node {
id
relativeFilePath
schemaType
title
}
}
}
}
`).then(result => {
// if there is an error return
if (result.errors) {
return Promise.reject(result.errors);
}
// get siteMap from data-portal-content
let yamlSiteMap = result.data.allSiteMapYaml.edges.map(n => n.node);
let yamlSiteMapPathOrKey = keyToPath(yamlSiteMap);
// for each markdown page
result.data.allMarkdownRemark.edges.forEach(({node}) => {
// create the page
let path;
if (!node.frontmatter.path) {
path = getPath(node.fileAbsolutePath);
} else {
path = node.frontmatter.path
}
if (path) {
createPage({
path: getKeyOrPath(path, yamlSiteMapPathOrKey),
component: getTemplate(node.frontmatter.template), // extract template name from front matter and use it to retrieve the template.
context: {id: node.id, metadataCoreName: node.frontmatter.metadataCoreName, metadataPath: '', metadataTitle: ''} // additional data can be passed via context
});
}
});
// for each metadata type create a page
result.data.allMetadataSchemaEntity.edges.forEach(({node}) => {
// create the page
let path = getMetadataDictionaryPath(node.relativeFilePath).slice(0, -1);
if (path) {
createPage({
path: path,
component: metadataTemplate, // extract template name from front matter and use it to retrieve the template.
context: {id: node.id, metadataCoreName: path.split('/')[3], metadataPath: node.relativeFilePath, metadataTitle: node.title} // additional data can be passed via context
});
}
});
});
};
// Create slugs for files.
exports.onCreateNode = ({node, getNode, actions}) => {
const {createNodeField} = actions;
if (node.internal.type === 'MarkdownRemark') {
// path can come from frontmatter or... from associating a title to path
let path;
if (!node.frontmatter.path) {
path = getPath(node.fileAbsolutePath);
} else {
path = node.frontmatter.path
}
const relativeFilePath = createFilePath({node, getNode, basePath: ''});
//this adds the path under "fields"
createNodeField({node, name: 'path', value: path});
//this adds the gitHubPath under "fields"
createNodeField({node, name: 'gitHubPath', value: relativeFilePath});
}
if (node.internal.type === 'MetadataSchemaEntity' && node.relativeFilePath.includes('/type/')) {
let path = getMetadataDictionaryPath(node.relativeFilePath).slice(0, -1);
//this adds the path under "fields"
createNodeField({node, name: 'path', value: path});
}
};
function getPath(markdownId) {
if (markdownId && markdownId.includes('docs/structure.md')) {
return '/metadata/design-principles/structure';
} else if (markdownId && markdownId.includes('docs/rationale.md')) {
return '/metadata/design-principles/rationale';
}
else {
return null;
}
}
function getMetadataDictionaryPath(path) {
return path.replace('/type/', '/metadata/dictionary/')
}