-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
180 lines (151 loc) · 3.87 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
const groupBy = require('lodash.groupby');
const map = require('lodash.map');
const fs = require('fs');
const slugify = require('slugify');
const path = require('path');
const DIR = 'public/pages/';
const timelineTemplate = path.resolve(`src/templates/timeline.js`);
function createSourcePages(articles, createPage) {
const groupedArticles = groupBy(articles, 'source');
map(groupedArticles, (group, source) => {
const path = `veiculos/${source}`;
createPage({
path,
component: timelineTemplate,
context: {
articles: group,
source,
},
});
// eslint-disable-next-line
console.log(`\nCreated ${source} page.`);
});
}
function createTopicPages(articles, createPage) {
const topicsData = {};
const topicsPage = [];
const topicsLabels = {};
articles.forEach((article) => {
article.topics = article.topics.map((topic) => {
const slug = slugify(topic).toLowerCase().trim();
if (topic && !topicsPage.includes(topic)) {
topicsPage.push(topic);
topicsLabels[slug] = topic;
}
return slug;
});
article.topics.forEach((topic) => {
if (topic) {
if (topicsData[topic]) {
topicsData[topic].push(article);
} else {
topicsData[topic] = [article];
}
}
});
});
createJSON({
path: `topics`,
context: {
pageArticles: topicsPage.sort((a, b) => a.localeCompare(b)),
},
});
map(topicsData, (group, topic) => {
const path = `topicos/${topic}`;
createPage({
path,
component: timelineTemplate,
context: {
articles: group,
topic: topicsLabels[topic],
},
});
// eslint-disable-next-line
console.log(`\nCreated ${topic} page.`);
});
}
function createArticlesPagination(articles) {
const groupedArticles = groupBy(articles, ({ publishDate }) =>
publishDate.slice(0, 7),
);
const months = Object.keys(groupedArticles);
const firstMonth = months[0];
map(groupedArticles, (group, month) => {
createJSON({
path: `/${month}`,
context: {
pageArticles: group,
},
});
// eslint-disable-next-line
console.log(`\nCreated ${month}.json.`);
if (month === firstMonth) {
createJSON({
path: `/data`,
context: {
pageArticles: group,
},
});
// eslint-disable-next-line
console.log(`\nCreated data.json with month ${month}.`);
}
});
fs.writeFile(`${DIR}/months.json`, JSON.stringify(months), function (err) {
if (err) {
// eslint-disable-next-line
return console.log(err);
}
});
}
function createJSON({
context: { pageArticles, currentPage, totalPages },
path,
}) {
if (!fs.existsSync(DIR)) {
fs.mkdirSync(DIR);
}
if (!fs.existsSync(`${DIR}sources`)) {
fs.mkdirSync(`${DIR}sources`);
}
if (!fs.existsSync(`${DIR}topics`)) {
fs.mkdirSync(`${DIR}topics`);
}
const filePath = `${DIR}${path}.json`;
const dataToSave = JSON.stringify(pageArticles);
fs.writeFile(filePath, dataToSave, function (err) {
if (err) {
// eslint-disable-next-line
return console.log(err);
}
});
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return graphql(`
query articlesListQuery {
allArticlesJson(sort: { publishDate: ASC }) {
totalCount
edges {
node {
id: articleId
title
url
publishDate
source
topics
}
}
}
}
`).then((result) => {
if (result.errors) {
throw result.errors;
}
const articles = result.data.allArticlesJson.edges.map(({ node }) => node);
return Promise.all([
createArticlesPagination(articles),
createSourcePages(articles, createPage),
createTopicPages(articles, createPage),
]);
});
};