-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
313 lines (287 loc) · 8.49 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
const { LANGUAGES, DEFAULT_LANGUAGE_CODE } = require(`./src/constants`);
const path = require(`path`);
const { createFilePath } = require(`gatsby-source-filesystem`);
const slug = require(`slug`);
const sharp = require('sharp');
const fsExtra = require('fs-extra');
exports.onCreateNode = ({ node, getNode, boundActionCreators }) => {
if (node.internal.type === `MarkdownRemark`) {
const { createNodeField, deleteNode } = boundActionCreators;
const parent = getNode(node.parent);
createNodeField({
node,
name: `slug`,
value: slug(parent.name),
});
createNodeField({
node,
name: `collection`,
value: parent.name === 'noop' ? 'noop' : parent.sourceInstanceName,
});
if (node.frontmatter.image) {
let url = `/thumbnails${node.frontmatter.image}`;
const filePath = path.resolve(`./public${url}`);
fsExtra.ensureFileSync(filePath);
sharp(path.resolve(`./static${node.frontmatter.image}`))
.resize(320, 240)
.jpeg({
quality: 95,
})
.toFile(filePath, (err, info) => {
if (err) {
console.error(err)
}
});
createNodeField({
node,
name: `image_thumbnail`,
value: url,
});
}
}
};
exports.onCreatePage= ({ page, boundActionCreators }) => {
const { createPage, deletePage } = boundActionCreators;
// Delete page and make it again on all languages
deletePage(page);
LANGUAGES.forEach(({ code }) => {
createPage({
...page,
path: `${code === DEFAULT_LANGUAGE_CODE ? '' : `/${code}`}${page.path}`,
context: {
...page.context,
language: code,
}
})
})
};
const articleTopicFilter = (topic) => (article) => article.frontmatter.topics.split(',').some((topicName) => topicName === topic.frontmatter.name);
const articlePlaceFilter = (place) => (article) => article.frontmatter.places.split(',').some((placeName) => placeName === place.frontmatter.name);
exports.createPages = ({ graphql, boundActionCreators }) => {
const { createPage } = boundActionCreators;
const blogAndVacanciesPromise = new Promise((resolve, reject) => {
graphql(`
{
allMarkdownRemark(
filter: {
frontmatter: {
is_hidden: {ne: true}
}
}
) {
edges {
node {
fields {
slug
collection
}
frontmatter {
language
version
}
}
}
}
}
`).then(result => {
result.data.allMarkdownRemark.edges.forEach((edge) => {
const { node } = edge;
if (['blog', 'vacancies'].some((x) => x === node.fields.collection)) {
let pagePath = `${node.fields.collection}/${node.fields.slug}`;
let language = node.frontmatter.language;
let version = node.frontmatter.version;
let versionSuffix = version ? `-${version}` : '';
if (language !== DEFAULT_LANGUAGE_CODE) {
pagePath = `${language}/` + pagePath;
}
createPage({
path: pagePath,
component: path.resolve(`./src/templates/${node.fields.collection}${versionSuffix}.js`),
context: {
// Data passed to context is available in page queries as GraphQL variables.
slug: node.fields.slug,
},
});
}
});
resolve()
})
})
const volunteerPromise = new Promise((resolve, reject) => {
const placesP = graphql(`
{
allMarkdownRemark(
filter: {
fields: {
collection: {eq: "volunteer-places"}
}
}
) {
edges {
node {
id
frontmatter {
name
title
title_ru
title_es
}
fields {
slug,
}
}
}
}
}
`).then(result => {
if (result.data.allMarkdownRemark) {
return result.data.allMarkdownRemark.edges.map(({ node }) => node);
}
return [];
});
const topicsP = graphql(`
{
allMarkdownRemark(
filter: {
fields: {
collection: {eq: "volunteer-topics"}
}
}
sort: {fields: [frontmatter___order], order: ASC},
) {
edges {
node {
id
frontmatter {
name
title
title_ru
title_es
}
fields {
slug,
}
}
}
}
}
`).then(result => {
if (result.data.allMarkdownRemark) {
return result.data.allMarkdownRemark.edges.map(({ node }) => node);
}
return [];
});
const articlesP = graphql(`
{
allMarkdownRemark(
filter: {
fields: {
collection: {eq: "volunteer-articles"}
}
frontmatter: {
is_hidden: {ne: true}
}
}
) {
edges {
node {
id
frontmatter {
title
language
title
image
topics
places
date
is_pinned
}
fields {
slug
image_thumbnail
}
}
}
}
}
`).then(result => {
if (result.data.allMarkdownRemark) {
let nodes = result.data.allMarkdownRemark.edges.map(({ node }) => node);
return nodes;
}
return [];
}).then((nodes) => {
const sortedNodes = [...nodes];
sortedNodes.sort((x, y) => {
const xIsPinned = x.frontmatter.is_pinned;
const yIsPinned = y.frontmatter.is_pinned;
const xDate = new Date(x.frontmatter.date);
const yDate = new Date(y.frontmatter.date);
if (xIsPinned !== yIsPinned) {
return xIsPinned ? -1 : 1;
}
if (xDate !== yDate) {
return xDate > yDate ? -1 : 1;
}
return 0;
});
return sortedNodes;
});
Promise.all([placesP, topicsP, articlesP])
.then(([places, topics, articles]) => {
LANGUAGES.forEach(({ code }) => {
let baseLanguagePath = `/`;
if (code !== DEFAULT_LANGUAGE_CODE) {
baseLanguagePath = `/${code}` + baseLanguagePath;
}
const languageArticles = articles.filter(({ frontmatter }) => frontmatter.language === code);
places.forEach((place) => {
const basePlacePath = baseLanguagePath + `${place.frontmatter.name}/`;
const placeArticles = languageArticles.filter(articlePlaceFilter(place));
// Filter out topics without articles
const placeTopics = topics.filter((topic) => {
return placeArticles.some(articleTopicFilter(topic)) ;
});
// Create page for place with no topic selected
createPage({
path: basePlacePath,
component: path.resolve(`./src/templates/volunteer-topic.js`),
context: {
topic: null,
place,
topics: placeTopics,
articles: placeArticles,
},
});
// Create page for place for each topic
placeTopics.forEach((topic) => {
const placeAndTopicArticles = placeArticles.filter(articleTopicFilter(topic));
let baseTopicPath = basePlacePath + `topic/${topic.frontmatter.name}/`;
createPage({
path: baseTopicPath,
component: path.resolve(`./src/templates/volunteer-topic.js`),
context: {
topic,
place,
topics: placeTopics,
articles: placeAndTopicArticles,
},
});
});
// Create page for each article in topic
articles.forEach((article) => {
createPage({
path: basePlacePath + `${article.fields.slug}/`,
component: path.resolve(`./src/templates/volunteer-article.js`),
context: {
slug: article.fields.slug,
},
});
})
})
});
resolve();
})
});
return Promise.all([blogAndVacanciesPromise, volunteerPromise]);
};