-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgatsby-node.js
More file actions
280 lines (236 loc) · 8 KB
/
Copy pathgatsby-node.js
File metadata and controls
280 lines (236 loc) · 8 KB
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
/* eslint-disable max-len */
/* eslint-disable no-param-reassign */
/* eslint-disable no-console */
const fetch = require('node-fetch');
const { navItems } = require('./src/nav.json');
const { resolvePathFromSlug } = require('./src/utils/resolvePathFromSlug');
const { capitalize } = require('./build-utils/CorePackage/utils/common');
const { generateModuleHTML } = require('./build-utils/CorePackage');
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }) => {
function buildSourceNodes(kojiCoreDocs, res, rej) {
try {
// Restrict the graphql data to only modules we want to display
const moduleNames = [
'backend/base',
'backend/database',
'backend/dispatch',
'backend/iap',
'backend/identity',
'backend/secret',
'backend/utilities',
'backend/web3/providers/ethereum',
'frontend/analytics',
'frontend/dispatch',
'frontend/iap',
'frontend/identity',
'frontend/playerState',
'frontend/remix',
'frontend/ui/capture',
'frontend/ui/navigate',
'frontend/ui/present',
'frontend/ui/upload',
'frontend/web3/providers/ethereum',
];
const modules = [];
const excludedModules = [];
for (let x = 0; x < kojiCoreDocs.children.length; x += 1) {
const child = kojiCoreDocs.children[x];
const {
children,
flags,
groups,
sources,
...newModule
} = child;
// Create a list of referenceIds for each module
const referenceIds = [];
// For each group, look for Classes, Enumerations, Functions, Interfaces, TypeAliases and Variables
// and make them top level properties.
//
// Also, make sure to build out the referenceIds so they're easily available
(child.groups || []).forEach((group) => {
newModule[group.title] = child.children.filter(({ id }) => group.children.includes(id));
group.children.forEach((childId) => {
if (!referenceIds.includes(childId)) referenceIds.push(childId);
});
});
newModule.referenceIds = referenceIds;
console.log('NAME', child.name);
if (moduleNames.includes(child.name)) {
modules.push(newModule);
} else {
excludedModules.push(newModule);
}
}
const AllInterfaces = [...modules, ...excludedModules].map((m) => m.Interfaces || []).reduce((acc, cur) => [...acc, ...cur], []);
const AllTypeAliases = [...modules, ...excludedModules].map((m) => m['Type aliases'] || []).reduce((acc, cur) => [...acc, ...cur], []);
modules.forEach((m) => {
const { html, name = '', shortDescription = '' } = generateModuleHTML(m, AllInterfaces, AllTypeAliases);
const slug = `core-${m.name.replace(/\//g, '-').toLowerCase()}`;
console.log(slug);
const node = {
id: createNodeId(`Koji-Core-Package-Item-${m.name}`),
document: {
description: shortDescription,
title: `Koji Core Package: ${capitalize(name)}`,
},
internal: {
type: 'kojiCorePackageItem',
contentDigest: createContentDigest(html),
},
html,
pageAttributes: {
slug,
},
slug,
};
actions.createNode(node);
});
} catch (err) {
console.log('err', err);
rej(err);
}
for (let idx = 0; idx < navItems.length; idx += 1) {
const mappedNavItem = { ...navItems[idx], idx };
mappedNavItem.sections = mappedNavItem.sections.map((section, sectionIdx) => {
const mappedSection = { ...section, idx: sectionIdx };
mappedSection.items = mappedSection.items.map((item, itemIdx) => {
const mappedItem = { ...item, idx: itemIdx };
mappedItem.path = `${mappedNavItem.root}${section.root}/${item.slug}`;
if (mappedItem.subItems) {
mappedItem.subItems = mappedItem.subItems.map((subItem, subItemIdx) => {
const mappedSubItem = { ...subItem, idx: subItemIdx };
mappedSubItem.path = `${mappedNavItem.root}${section.root}/${subItem.slug}`;
return mappedSubItem;
});
}
return mappedItem;
});
return mappedSection;
});
const node = {
...mappedNavItem,
id: createNodeId(`Nav-Item-${mappedNavItem.name}`),
internal: {
type: 'NavItem',
contentDigest: createContentDigest(mappedNavItem),
},
};
actions.createNode(node);
}
res();
}
return new Promise((res, rej) => {
fetch('https://raw.githubusercontent.com/madewithkoji/koji-core/main/koji-core-docs.json')
.then((response) => response.json())
.then((json) => buildSourceNodes(json, res, rej));
});
};
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;
const asciidocTemplate = require.resolve('./src/templates/Asciidoc/index.js');
const result = await graphql(`
query {
allAsciidoc {
edges {
node {
id
html
document {
title
}
pageAttributes {
slug
description
banner
}
}
}
}
}
`);
// Handle errors
if (result.errors) {
reporter.panicOnBuild('Error while running GraphQL query.');
return;
}
const knownSlugs = [];
navItems.forEach((navItem) => {
navItem.sections.forEach((section) => {
section.items.forEach((item) => {
knownSlugs.push(item.slug);
if (item.subItems) {
item.subItems.forEach((subItem) => {
knownSlugs.push(subItem.slug);
});
}
});
});
});
const slugsInUse = [];
result.data.allAsciidoc.edges.forEach(({ node }) => {
// If the doc is missing a slug it won't be accessible
if (!node.pageAttributes.slug) {
console.warn(`Asciidoc missing slug. [Document Title]: ${node.document.title}`);
}
// If the doc's slug isn't in the navigation, it won't be accessible
if (node.pageAttributes.slug && !knownSlugs.includes(node.pageAttributes.slug)) {
console.warn(`An asciidoc has been indexed but is missing from navigation. [Slug]: ${node.pageAttributes.slug}`);
}
slugsInUse.push(node.pageAttributes.slug);
const path = resolvePathFromSlug(node.pageAttributes.slug);
if (!path) return;
createPage({
path,
component: asciidocTemplate,
context: {
id: node.id,
// additional data can be passed via context
slug: node.pageAttributes.slug,
},
});
// When we move back to an actual home page, just remove this
// section to prevent creation of the duplicate introduction page
if (node.pageAttributes.slug === 'introduction') {
createPage({
path: '/',
component: asciidocTemplate,
context: {
id: node.id,
// additional data can be passed via context
slug: node.pageAttributes.slug,
},
});
}
});
const corePackageData = await graphql(`
query {
allKojiCorePackageItem {
nodes {
id
html
slug
}
}
}
`);
const corePackageTemplate = require.resolve('./src/templates/CorePackage/index.js');
// For each node, create a page and use the updated corePackageTemplate
corePackageData.data.allKojiCorePackageItem.nodes.forEach((node) => {
createPage({
path: `/reference/core/${node.slug}`,
component: corePackageTemplate,
context: {
id: node.id,
// additional data can be passed via context
slug: node.slug,
},
});
slugsInUse.push(node.slug);
});
knownSlugs.forEach((knownSlug) => {
if (!slugsInUse.includes(knownSlug)) {
throw new Error(`A nav item has been created, but there is no corresponding page. [Slug]: ${knownSlug}`);
}
});
};