-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
421 lines (365 loc) · 11.8 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
const crypto = require('crypto');
const fsExtra = require('fs-extra');
const path = require('path');
const { createRemoteFileNode } = require('gatsby-source-filesystem');
const { fetchVimeoVideo } = require('./src/lib/vimeo');
exports.sourceNodes = ({ actions }) => {
const { createTypes } = actions;
createTypes(`
type VimeoVideo implements Node @infer {
sources: [VimeoVideoSource]!
tracks: [VimeoVideoTrack]!
pictures: [VimeoVideoPoster]!
}
type VimeoVideoSource @infer {
link: String!
type: String!
width: Int
}
type VimeoVideoTrack @infer {
publicPath: String
name: String
language: String
file: File!
}
type VimeoVideoPoster @infer {
width: Int!
link: String!
}
`);
};
exports.onCreateNode = ({ node }) => {
/* for some reason the repeater field returns false, if there wasn't any footnote.
To avoid a Schema conflict (array/boolean) always use an empty array */
if (
node.internal.type === 'WordPressAcf_text' &&
node.footnotesRepeat === false
) {
// eslint-disable-next-line no-param-reassign
node.footnotesRepeat = [];
}
};
exports.createPages = ({
actions,
graphql,
getNode,
store,
cache,
createNodeId,
}) => {
const { createPage, createNode } = actions;
return (
graphql(`
{
episodes: allWordpressWpEpisodes(
filter: { acf: { published: { ne: false } } }
) {
edges {
node {
wordpress_id
slug
title
acf {
quote
number
text
language
content_episodes {
... on WordPressAcf_vimeoVideo {
__typename
wordpress_id
}
}
}
}
}
}
protagonists: allWordpressWpProtagonists(
filter: { status: { eq: "publish" } }
) {
edges {
node {
slug
wordpress_id
acf {
language
}
}
}
}
background: allWordpressWpBackground(
filter: { status: { eq: "publish" } }
) {
edges {
node {
slug
wordpress_id
acf {
language
}
}
}
}
}
`)
// filter out published nodes
.then(
({
errors,
data: {
episodes: { edges: episodes },
protagonists: { edges: protagonists },
background: { edges: background },
},
}) => {
if (errors) {
return Promise.reject(errors);
}
return {
episodes,
protagonists,
background,
};
}
)
// fetch vimeo data
.then(({ episodes, protagonists, background }) => {
const videos = [];
episodes.forEach(({ node: { acf } }) => {
const { content_episodes: contentEpisodes } = acf;
contentEpisodes
.filter(
({ __typename: typeName }) =>
typeName === 'WordPressAcf_vimeoVideo'
)
.map(({ wordpress_id: videoID }) =>
videos.push(fetchVimeoVideo(videoID))
);
});
return Promise.all(videos)
.then((videoData) => {
const createTrackNodes = (node, video) => {
const nodes = [];
if (
video &&
video.tracks &&
video.tracks.data &&
video.tracks.data.length > 0
) {
video.tracks.data.forEach((track, index) => {
const id = `${node.id}-track-${index}`;
const { name, language, link } = track;
const trackNode = createNode({
name,
language,
id,
parent: null,
children: [],
internal: {
type: `VimeoVideoTrack`,
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(video))
.digest(`hex`),
},
});
const trackFileNode = createRemoteFileNode({
url: link,
store,
cache,
createNode,
createNodeId,
parentNodeId: node.id,
auth: {},
});
const completeNode = Promise.all([
trackNode,
trackFileNode,
]).then((resolvedNodes) => {
const createdNode = getNode(id);
const fileNode = resolvedNodes[1];
const { name: trackFileName } = fileNode;
createdNode.file = fileNode;
createdNode.publicPath = path.join(
'/',
'static',
'subtitles',
`${trackFileName}.vtt`
);
return createdNode;
});
nodes.push(completeNode);
});
}
return Promise.all(nodes);
};
const createVideoNode = (data) => {
const nodes = [];
const cached = [];
data.filter(Boolean).forEach((video) => {
const { id } = video;
if (cached.includes(id)) {
return;
}
const node = createNode({
id,
sources: video.video.files
? video.video.files.map((file) => {
const { link, type, width } = file;
return { link, type, width };
})
: [],
pictures: video.video.pictures
? video.video.pictures.sizes.map(({ width, link }) => ({
width,
link,
}))
: [],
parent: null,
children: [],
internal: {
type: `VimeoVideo`,
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(video))
.digest(`hex`),
},
}).then(() => {
const createdNode = getNode(id);
return createTrackNodes(createdNode, video).then((tracks) => {
createdNode.tracks = tracks;
tracks.forEach((track) => {
const { absolutePath } = track.file;
const { publicPath } = track;
const fullPublicPath = path.join(
process.cwd(),
'public',
publicPath
);
if (!fsExtra.existsSync(fullPublicPath)) {
fsExtra.copy(absolutePath, fullPublicPath, (err) => {
if (err) {
// eslint-disable-next-line no-console
console.error(
`Error copying file from ${absolutePath} to ${fullPublicPath}`,
err
);
}
});
}
});
return createdNode;
});
});
nodes.push(node);
cached.push(id);
});
return nodes;
};
return Promise.all([...createVideoNode(videoData)]).then(
() => videoData
);
})
.then(() => ({
protagonists,
episodes,
background,
}));
})
// create pages
.then(({ episodes, protagonists, background }) => {
protagonists.forEach(({ node }) => {
const { slug, wordpress_id: wordpressId, acf } = node;
let normalizedSlug = slug;
const languageSlug =
!acf?.language || acf.language === 'de' ? '' : `/${acf.language}`;
if (normalizedSlug.endsWith('-2')) {
normalizedSlug = normalizedSlug.replace(/-2$/g, '');
}
const pagePath = `${languageSlug}/protagonists/${normalizedSlug}/`;
// eslint-disable-next-line no-console
console.log('create page', pagePath);
const context = {
wordpressId,
language: acf?.language ?? 'de',
};
createPage({
path: pagePath,
component: path.resolve('src/templates/protagonist.jsx'),
context,
});
});
episodes.forEach(({ node }) => {
const { slug, acf, wordpress_id: wordpressId } = node;
const number = parseInt(acf.number, 10);
const languageSlug =
!acf?.language || acf.language === 'de' ? '' : `/${acf.language}`;
let pagePath = `${languageSlug}/episodes/${slug}/`;
if (number === 0 && (acf.language === 'de' || !acf.language)) {
pagePath = '/';
} else if (number === 0 && acf.language !== 'de') {
pagePath = `/${acf.language}/`;
}
const context = {
wordpressId,
language: acf?.language ?? 'de',
};
// eslint-disable-next-line no-console
console.log('create page', pagePath);
createPage({
path: pagePath,
component: path.resolve('src/templates/episode.jsx'),
context,
});
});
background.forEach(
({ node: { slug, wordpress_id: wordpressId, acf } }) => {
const languageSlug =
!acf?.language || acf.language === 'de' ? '' : `/${acf.language}`;
const pagePath = `${languageSlug}/background/${slug}/`;
// eslint-disable-next-line no-console
console.log('create background', pagePath);
const context = {
wordpressId,
language: acf?.language ?? 'de',
};
createPage({
path: pagePath,
component: path.resolve('src/templates/background.jsx'),
context,
});
}
);
['de', 'en'].forEach((language) => {
const localeEpisodes = episodes.filter((episode) => {
if (language === 'de' && !episode?.node?.acf?.language) {
return true;
}
return episode?.node?.acf?.language === language;
});
createPage({
path: `${language === 'de' ? '' : `/${language}`}/navigation/`,
component: path.resolve('src/templates/navigation.jsx'),
context: {
episodes: localeEpisodes,
language,
},
});
createPage({
path: `${language === 'de' ? '' : `/${language}`}/background/`,
component: path.resolve('src/templates/backgroundOverview.jsx'),
context: {
language: language ?? 'de',
},
});
createPage({
path: `${language === 'de' ? '' : `/${language}`}/protagonists/`,
component: path.resolve('src/templates/protagonistsOverview.jsx'),
context: {
language: language ?? 'de',
},
});
});
})
);
};