generated from pixel-point/gatsby-tailwind-starter
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgatsby-node.js
402 lines (358 loc) · 11.5 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* eslint-disable import/no-extraneous-dependencies */
const path = require('path');
const get = require('lodash.get');
const fetch = require('node-fetch');
const { BLOG_CATEGORIES, BLOG_POSTS_PER_PAGE } = require('./src/constants/blog');
const { CASE_STUDIES_BASE_PATH } = require('./src/constants/case-studies');
const getBlogPath = require('./src/utils/get-blog-path');
const getBlogPostPath = require('./src/utils/get-blog-post-path');
const blogPostTemplate = path.resolve('./src/templates/blog-post.jsx');
const caseStudyTemplate = path.resolve('./src/templates/case-study.jsx');
// We have this variable in order to decide whether to render draft posts or not
// We are rendering ALL posts, including draft ones in development mode
// And we are skipping draft posts in production mode
// We have an array structure here in order to use it in the filter using the "in" operator
const DRAFT_FILTER = process.env.NODE_ENV === 'production' ? [false] : [true, false];
const POST_REQUIRED_FIELDS = ['title', 'summary', 'category', 'author', 'cover'];
const CASE_STUDY_REQUIRED_FIELDS = [
'logo',
'title',
'description',
'websiteUrl',
'services',
'stack',
'keynotes',
'position',
];
const FEATURED_CASE_STUDY_REQUIRED_FIELDS = ['cover'];
const OPEN_SOURCE_CASE_STUDY_REQUIRED_FIELDS = ['githubUsername', 'githubRepoName'];
async function createBlogPages({ graphql, actions }) {
const { createPage } = actions;
const result = await graphql(
`
query ($draftFilter: [Boolean]!) {
allMdx(
filter: {
internal: { contentFilePath: { regex: "/content/posts/((?!post-authors).)*$/" } }
fields: { isDraft: { in: $draftFilter } }
}
) {
nodes {
frontmatter {
category
}
}
totalCount
}
}
`,
{ draftFilter: DRAFT_FILTER }
);
if (result.errors) throw new Error(result.errors);
const pageCount = Math.ceil(result.data.allMdx.totalCount / BLOG_POSTS_PER_PAGE);
Array.from({ length: pageCount }).forEach((_, index) => {
createPage({
path: getBlogPath({ pageNumber: index + 1 }),
component: path.resolve('./src/templates/blog.jsx'),
context: {
currentPageIndex: index,
pageCount,
limit: BLOG_POSTS_PER_PAGE,
skip: index * BLOG_POSTS_PER_PAGE,
draftFilter: DRAFT_FILTER,
},
});
});
BLOG_CATEGORIES.forEach((category) => {
const postsForCategory = result.data.allMdx.nodes.filter(
({ frontmatter }) => frontmatter.category === category
);
const pageCount = Math.ceil(postsForCategory.length / BLOG_POSTS_PER_PAGE);
Array.from({ length: pageCount }).forEach((_, index) => {
createPage({
path: getBlogPath({ pageNumber: index + 1, category }),
component: path.resolve('./src/templates/blog.jsx'),
context: {
category,
currentPageIndex: index,
pageCount,
limit: BLOG_POSTS_PER_PAGE,
skip: index * BLOG_POSTS_PER_PAGE,
draftFilter: DRAFT_FILTER,
},
});
});
});
}
async function createBlogPosts({ graphql, actions }) {
const { createPage } = actions;
const result = await graphql(`
{
allMdx(
filter: {
internal: { contentFilePath: { regex: "/content/posts/((?!post-authors).)*$/" } }
}
sort: { internal: { contentFilePath: DESC } }
) {
nodes {
id
fields {
isDraft
slug
}
frontmatter {
title
author
summary
cover {
publicURL
}
category
}
internal {
contentFilePath
}
}
}
# Query all video covers from all blog posts
allFile(
filter: { absolutePath: { regex: "/content/posts/" }, name: { regex: "/video-cover/" } }
) {
nodes {
name
ext
childImageSharp {
gatsbyImageData(width: 970)
}
relativeDirectory
}
}
}
`);
if (result.errors) throw new Error(result.errors);
result.data.allMdx.nodes.forEach(({ id, fields, frontmatter, internal: { contentFilePath } }) => {
const { slug } = fields;
// Do not create a post in production if it's draft
if (process.env.NODE_ENV === 'production' && fields.isDraft) return;
// Required fields validation
POST_REQUIRED_FIELDS.forEach((fieldName) => {
if (!get(frontmatter, fieldName)) {
throw new Error(`Post "${contentFilePath}" does not have field "${fieldName}"!`);
}
});
if (!BLOG_CATEGORIES.includes(frontmatter.category)) {
throw new Error(
`Post "${contentFilePath}" has unknown category "${
frontmatter.category
}"!\nAvailable categories: ${Object.keys(BLOG_CATEGORIES).join(
', '
)}.\nPlease check categories in "src/constants/blog.js"`
);
}
// Create an object containing all the video covers for all blog posts groped by post slug
const allVideoCovers = result.data.allFile.nodes.reduce((acc, item) => {
const { name, ext, childImageSharp, relativeDirectory } = item;
const slug = `${relativeDirectory}`;
if (!acc[slug]) {
acc[slug] = {};
}
acc[slug][name + ext] = { childImageSharp };
return acc;
}, {});
// Create a page for each blog post and pass context with video covers only for the current post
createPage({
path: getBlogPostPath(slug),
component: `${blogPostTemplate}?__contentFilePath=${contentFilePath}`,
context: { id, videoCovers: allVideoCovers[slug] },
});
});
}
async function createCaseStudiesPage({ actions }) {
const { createPage } = actions;
createPage({
path: CASE_STUDIES_BASE_PATH,
component: path.resolve('./src/templates/case-studies.jsx'),
context: {
draftFilter: DRAFT_FILTER,
},
});
}
async function createCaseStudies({ graphql, actions }) {
const { createPage } = actions;
const result = await graphql(`
{
allMdx(filter: { internal: { contentFilePath: { regex: "/content/case-studies/" } } }) {
nodes {
id
fields {
isDraft
slug
}
frontmatter {
logo {
url {
publicURL
}
}
title
description
websiteUrl
githubUsername
githubRepoName
quote {
text
authorName
authorPosition
authorPhoto {
publicURL
}
}
services
stack
keynotes
position
cover {
publicURL
}
isFeatured
isOpenSource
isHidden
}
internal {
contentFilePath
}
}
}
}
`);
if (result.errors) throw new Error(result.errors);
result.data.allMdx.nodes.forEach(({ id, fields, frontmatter, internal: { contentFilePath } }) => {
// Do not create a case study in production if it's draft
if (process.env.NODE_ENV === 'production' && fields.isDraft) return;
// Required fields validation
CASE_STUDY_REQUIRED_FIELDS.forEach((fieldName) => {
if (!get(frontmatter, fieldName)) {
throw new Error(`Case Study "${fields.slug}" does not have field "${fieldName}"!`);
}
});
if (frontmatter.isFeatured) {
FEATURED_CASE_STUDY_REQUIRED_FIELDS.forEach((fieldName) => {
if (!get(frontmatter, fieldName)) {
throw new Error(`Case Study "${fields.slug}" does not have field "${fieldName}"!`);
}
});
}
if (frontmatter.isOpenSource) {
OPEN_SOURCE_CASE_STUDY_REQUIRED_FIELDS.forEach((fieldName) => {
if (!get(frontmatter, fieldName)) {
throw new Error(`Case Study "${fields.slug}" does not have field "${fieldName}"!`);
}
});
}
createPage({
path: `${CASE_STUDIES_BASE_PATH}${fields.slug}`,
component: `${caseStudyTemplate}?__contentFilePath=${contentFilePath}`,
context: { id },
});
});
}
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions;
createNodeField({
node,
name: 'slug',
value: node.internal.contentFilePath
? node.internal.contentFilePath.split('/').slice(-2, -1)[0]
: '',
});
if (node.frontmatter) {
createNodeField({
node,
name: 'isDraft',
value: node.frontmatter.isDraft || false,
});
createNodeField({
node,
name: 'isFeatured',
value: node.frontmatter.isFeatured || false,
});
createNodeField({
node,
name: 'isOpenSource',
value: node.frontmatter.isOpenSource || false,
});
createNodeField({
node,
name: 'isHidden',
value: node.frontmatter.isHidden || false,
});
}
};
exports.createResolvers = ({ createResolvers, cache }) => {
createResolvers({
Mdx: {
githubStars: {
type: 'String',
resolve: async ({ frontmatter, internal: { contentFilePath } }) => {
const { githubUsername, githubRepoName, isOpenSource } = frontmatter;
if (
contentFilePath.includes('/case-studies/') &&
isOpenSource &&
githubUsername &&
githubRepoName
) {
try {
let stars;
// Set expiration time as 24 hours in milliseconds
const expirationTime = 24 * 60 * 60 * 1000;
const cacheKey = `stars-${githubUsername}-${githubRepoName}`;
const cacheStarsData = await cache.get(cacheKey);
// Use cache if it is not expired
if (cacheStarsData && cacheStarsData.created > Date.now() - expirationTime) {
stars = cacheStarsData.stars;
} else {
// Use setTimeout to avoid hitting GitHub API rate limit with a random delay with interval from 500ms to 1500ms
await new Promise((resolve) => setTimeout(resolve, Math.random() * 3000 + 500));
const response = await fetch(
`https://api.github.com/repos/${githubUsername}/${githubRepoName}`
);
const { stargazers_count } = await response.json();
if (!stargazers_count) {
throw new Error(
`Failed to fetch GitHub stars for case study "${githubUsername}/${githubRepoName}"`
);
}
stars = new Intl.NumberFormat('en-US').format(stargazers_count);
await cache.set(cacheKey, {
stars,
created: Date.now(),
});
}
return stars;
} catch (error) {
console.log(error);
throw new Error(
`Failed to fetch GitHub stars for case study "${githubUsername}/${githubRepoName}"`
);
}
}
return null;
},
},
},
});
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
createTypes(`
type Mdx implements Node {
author: PostAuthorsJson @link(by: "name", from: "frontmatter.author")
}
`);
};
exports.createPages = async (options) => {
await createBlogPages(options);
await createBlogPosts(options);
await createCaseStudiesPage(options);
await createCaseStudies(options);
};