forked from fabricjs/fabricjs.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
268 lines (248 loc) · 8.26 KB
/
gatsby-node.js
File metadata and controls
268 lines (248 loc) · 8.26 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
/* eslint-disable no-unused-expressions */
/* eslint-disable max-len */
// +------------------------------------------------------------------+
// | Fetch remote data (contributors) at build-time |
// | Later, maybe use the Github GraphQl API to source data in one go |
// +------------------------------------------------------------------+
const fetch = require('node-fetch');
// https://www.gatsbyjs.org/docs/creating-a-source-plugin/#sourcing-data-and-creating-nodes
// https://www.gatsbyjs.org/docs/data-fetching/
exports.sourceNodes = async ({
actions: { createNode },
createNodeId,
createContentDigest,
}) => {
// get data from GitHub API at build time
const result = await fetch(
'https://api.github.com/repos/fabricjs/fabric.js/contributors?per_page=10'
);
const jsonData = await result.json();
// Commented out, since we also require the 'name' which is not available until we query user-details of each contributor
/* jsonData.forEach((datum) => {
createNode({
// arbitrary fields from the data
name: datum.login,
picUrl: datum.avatar_url, // maybe later create remote file-nodes from this so as for Sharp to optimize the img delivery
url: datum.html_url,
// required fields
id: createNodeId(`fabricjs-contributor-${datum.login}`),
parent: null,
children: [],
internal: {
type: 'Contributor',
contentDigest: createContentDigest(datum),
},
});
});// */
const numUsers = jsonData.length;
// https://zellwk.com/blog/async-await-in-loops/
for (let userIndex = 0; userIndex < numUsers; userIndex++) {
/* eslint-disable-line no-plusplus */
// console.log(`Creating node for ${jsonData[userIndex].login}`);
const userData = await fetch(
`https://api.github.com/users/${jsonData[userIndex].login}`
); /* eslint-disable-line no-await-in-loop */
const user =
await userData.json(); /* eslint-disable-line no-await-in-loop */
createNode({
// arbitrary fields from the data
name: user.name || user.login,
picUrl: user.avatar_url, // maybe later create remote file-nodes from this so as for Sharp to optimize the img delivery
url: user.html_url, // url: user.blog === '' ? user.html_url : user.blog,
// required fields
id: createNodeId(`fabricjs-contributor-${user.login}`),
parent: null,
children: [],
internal: {
type: 'Contributor',
contentDigest: createContentDigest(user),
},
});
}
};
// +------------------------------------------------------------------+
// | Create slug from file-path and append it to the 'fields' section |
// | under GraphQl so that we can reference it inside pages |
// +------------------------------------------------------------------+
const { createFilePath } = require('gatsby-source-filesystem');
exports.onCreateNode = ({ node, getNode, actions }) => {
if (node.internal.type === 'Mdx') {
const { createNodeField } = actions;
const slug = createFilePath({
node,
getNode,
basePath: 'content',
trailingSlash: false,
}); /* basePath -- path inside src folder to act as base path */
createNodeField({
node,
name: 'slug',
value: slug.replace(/ +/g, '-').replace(/-+/g, '-'), // slug.replace(/\/$/,"") //also remove trailing slash if any (this is not req coz while creating the slug using createFilePath(), we have already set 'trailingSlash' as false)
});
}
};
const path = require('path');
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
// +------------------------------------------------------------------+
// | Create pages for listing 'demos' & each 'demo' |
// +------------------------------------------------------------------+
/// /fetch the 'demo' md files and create pages for them
const demoPagesQueryResult = await graphql(`
query demoPagesQueryResult {
allMdx(
filter: {
internal: {
contentFilePath: { regex: "//demo/[a-zA-Z0-9-]+/index.md$/" }
}
frontmatter: { title: { regex: "/[a-zA-Z0-9]+$/" } }
}
sort: { frontmatter: { title: ASC } }
) {
edges {
previous {
frontmatter {
title
}
fields {
slug
}
internal {
contentFilePath
}
}
node {
frontmatter {
title
}
fields {
slug
}
internal {
contentFilePath
}
}
next {
frontmatter {
title
}
fields {
slug
}
internal {
contentFilePath
}
}
}
}
}
`);
// create each 'demo' page
demoPagesQueryResult.data.allMdx &&
demoPagesQueryResult.data.allMdx.edges.forEach(
({ node, previous, next }) => {
node.fields.slug !== null &&
node.fields.slug.trim() !== '' &&
createPage({
path: node.fields.slug,
component: `${path.resolve(
'./src/templates/demo.jsx'
)}?__contentFilePath=${node.internal.contentFilePath}`,
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
title: node.frontmatter.title,
slug: node.fields.slug,
prev: previous
? {
title: previous.frontmatter.title,
slug: previous.fields.slug,
}
: null,
next: next
? { title: next.frontmatter.title, slug: next.fields.slug }
: null,
},
});
}
);
// create 'demos' page
createPage({
path: '/demos',
component: path.resolve('./src/templates/demos.jsx'),
});
// +------------------------------------------------------------------+
// | Create pages for 'docs' intro-page & each 'doc' page |
// +------------------------------------------------------------------+
/// /fetch the 'doc' md files and create pages for them
const docPagesQueryResult = await graphql(`
query docPagesQueryResult {
allDocPagesMD: allMdx(
filter: {
internal: {
contentFilePath: { regex: "//docs/[a-zA-Z0-9-]+/index.mdx?$/" }
}
frontmatter: { title: { regex: "/[a-zA-Z0-9]+$/" } }
}
sort: { frontmatter: { date: ASC } }
) {
docPages: nodes {
frontmatter {
title
}
fields {
slug
}
internal {
contentFilePath
}
}
}
}
`);
const { docPages } = docPagesQueryResult.data.allDocPagesMD;
// collect all doc titles and links in a array which we'll pass as context when creating each doc page
/* let docList = [];
docPages.forEach(({frontmatter,fields})=>{
fields.slug!==null && fields.slug.trim()!=='' && docList.push({'title':frontmatter.title,'slug':fields.slug})
}) */
const docList = docPages.map(({ frontmatter, fields }) => ({
title: frontmatter.title,
slug: fields.slug,
}));
// create each 'doc' page
docPages.forEach(({ fields, internal }, di) => {
fields.slug !== null &&
fields.slug.trim() !== '' &&
createPage({
path: fields.slug,
component: `${path.resolve(
'./src/templates/doc.jsx'
)}?__contentFilePath=${internal.contentFilePath}`,
context: {
slug: fields.slug,
prev:
di === 0
? null
: {
title: docPages[di - 1].frontmatter.title,
slug: docPages[di - 1].fields.slug,
},
next:
di === docPages.length - 1
? null
: {
title: docPages[di + 1].frontmatter.title,
slug: docPages[di + 1].fields.slug,
},
docList,
},
});
});
// create 'docs' page
createPage({
path: '/docs',
component: path.resolve('./src/templates/docs.jsx'),
context: { docList },
});
};