This repository has been archived by the owner on Aug 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 966
/
gatsby-node.js
executable file
·255 lines (229 loc) · 7.14 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
const path = require('path');
const _ = require('lodash');
const webpackLodashPlugin = require('lodash-webpack-plugin');
const crypto = require('crypto');
/**
* Generate node edges
*
* @param {any} { node, actions, getNode }
*/
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
/**
* Add slug edge
*/
let slug;
if (node.internal.type === 'MarkdownRemark') {
const fileNode = getNode(node.parent);
const parsedFilePath = path.parse(fileNode.relativePath);
if (
Object.prototype.hasOwnProperty.call(node, 'frontmatter') &&
Object.prototype.hasOwnProperty.call(node.frontmatter, 'slug')
) {
slug = `/${node.frontmatter.slug}`;
} else if (parsedFilePath.name !== 'index' && parsedFilePath.dir !== '') {
slug = `/${parsedFilePath.name}/`;
} else {
slug = `/${parsedFilePath.dir}/`;
}
if (
Object.prototype.hasOwnProperty.call(node, 'frontmatter') &&
Object.prototype.hasOwnProperty.call(node.frontmatter, 'slug')
) {
slug = `/${_.kebabCase(node.frontmatter.slug)}`;
}
createNodeField({
node,
name: 'slug',
value: _.kebabCase(slug).toLowerCase(),
});
/**
* Add permalink edge
*
* If there is a path frontmatter - that overrides all.
* Otherwise, we'll use the nested directory structure to build the permalink.
*/
let permalink;
if (
Object.prototype.hasOwnProperty.call(node, 'frontmatter') &&
Object.prototype.hasOwnProperty.call(node.frontmatter, 'path')
) {
permalink = `/${node.frontmatter.path}${slug}`;
} else if (parsedFilePath.dir !== '') {
permalink = `/${parsedFilePath.dir}${slug}`;
} else {
permalink = slug;
}
createNodeField({
node,
name: 'permalink',
value: permalink.toLowerCase(),
});
/**
* Check if doc is "ui", "for developers", "glossary" or "release-notes" and add a field slug to represent this.
*/
let docType;
if (permalink.match(/ui\/[^/]+/)) {
docType = 'ui';
} else if (permalink.match(/for-developers\/[^/]+/)) {
docType = 'for-developers';
} else if (permalink.match(/glossary\/[^/]+/)) {
docType = 'glossary';
} else if (permalink.match(/release-notes\/[^/]+/)) {
docType = 'release-notes';
}
createNodeField({ node, name: 'docType', value: docType });
let cat;
if (
Object.prototype.hasOwnProperty.call(node, 'frontmatter') &&
Object.prototype.hasOwnProperty.call(node.frontmatter, 'category')
) {
cat = node.frontmatter.category;
} else {
// remove docType prefix
cat = parsedFilePath.dir.replace(`${docType}`, '');
cat = cat.split('/');
cat = cat.length > 1 && cat[1].length ? cat[1] : 'uncategorized';
}
createNodeField({ node, name: 'category', value: cat });
let group = 'ungrouped';
if (
Object.prototype.hasOwnProperty.call(node, 'frontmatter') &&
Object.prototype.hasOwnProperty.call(node.frontmatter, 'group')
) {
group = node.frontmatter.group;
}
createNodeField({ node, name: 'group', value: group });
let title;
if (
Object.prototype.hasOwnProperty.call(node, 'frontmatter') &&
Object.prototype.hasOwnProperty.call(node.frontmatter, 'title')
) {
title = node.frontmatter.title;
} else {
title = parsedFilePath.name.replace('-', '');
}
createNodeField({ node, name: 'title', value: title });
}
};
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return new Promise((resolve, reject) => {
const docsPage = path.resolve('src/templates/doc.jsx');
const categoryPage = path.resolve('src/templates/category.jsx');
resolve(
graphql(`
{
allMarkdownRemark {
edges {
node {
id
fileAbsolutePath
fields {
permalink
slug
category
docType
}
}
}
}
}
`).then((result) => {
if (result.errors) {
/* eslint no-console: "off" */
console.log(result.errors);
reject(result.errors);
}
const helpCategorySet = new Set();
const developerCategorySet = new Set();
result.data.allMarkdownRemark.edges.forEach((edge) => {
const { category, docType } = edge.node.fields;
// aggregate "ui" categories
if (docType === 'ui') {
helpCategorySet.add(category);
}
// aggregate "for-developers" categories
if (docType === 'for-developers') {
developerCategorySet.add(category);
}
// Create docs pages
const { permalink } = edge.node.fields;
createPage({
path: permalink,
component: docsPage,
context: {
slug: edge.node.fields.slug,
id: edge.node.id,
},
});
});
const categoryList = Array.from(developerCategorySet);
categoryList.forEach((category, i) => {
// Create "for-developer" category nodes.
const cat = {
id: `${i}`,
slug: category,
parent: '__SOURCE__',
children: [],
internal: {
type: 'forDeveloperCategories',
},
};
// Get content digest of node. (Required field)
const contentDigest = crypto
.createHash('md5')
.update(JSON.stringify(cat))
.digest('hex');
// add it to contentNode
cat.internal.contentDigest = contentDigest;
// Create "/for-developers/<category-slug>" pages.
createPage({
path: `/for-developers/${_.kebabCase(category)}/`,
component: categoryPage,
context: {
docType: 'for-developers',
category,
},
});
});
const helpCategoryList = Array.from(helpCategorySet);
helpCategoryList.forEach((category, i) => {
// Create "ui" category nodes.
const cat = {
id: `${i}`,
slug: category,
parent: '__SOURCE__',
children: [],
internal: {
type: 'helpSupportCategories',
},
};
// Get content digest of node. (Required field)
const contentDigest = crypto
.createHash('md5')
.update(JSON.stringify(cat))
.digest('hex');
// add it to userNode
cat.internal.contentDigest = contentDigest;
// Create "/ui/<category-slug>" pages.
createPage({
path: `/ui/${_.kebabCase(category)}/`,
component: categoryPage,
context: {
docType: 'ui',
category,
},
});
});
})
);
});
};
exports.onCreateWebpackConfig = ({ stage, actions }) => {
if (stage === 'build-javascript') {
actions.setWebpackConfig({
plugins: [webpackLodashPlugin],
});
}
};