forked from ethereumclassic/ethereumclassic.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
139 lines (133 loc) · 3.54 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
// ensure the date gets cast to a string for querying
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type yamlI18n implements Node {
data: Data
}
type Data {
date: String
}
`;
createTypes(typeDefs);
};
// PAGINATION
const itemsPerPage = 30;
let filters;
exports.createPages = async ({ graphql }) => {
const {
data: {
items: { nodes }
}
} = await graphql(`
query {
items: allYamlI18N(
filter: {
type: { in: ["collection", "markdown"] }
parentDirectory: { in: ["blog", "news"] }
}
sort: { fields: data___date, order: ASC }
) {
nodes {
parentDirectory
data {
date
tags
}
locale
}
}
}
`);
filters = {};
nodes.forEach(({ parentDirectory, locale, data: { tags, date } }) => {
filters[locale] = filters[locale] || {};
filters[locale][parentDirectory] = filters[locale][parentDirectory] || {
years: {},
tags: {},
total: 0
};
const branch = filters[locale][parentDirectory];
branch.total += 1;
if (date) {
const year = date.split('-')[0];
branch.years[year] = branch.years[year] ? branch.years[year] + 1 : 1;
}
(tags || []).forEach(tag => {
branch.tags[tag] = branch.tags[tag] ? branch.tags[tag] + 1 : 1;
});
});
};
exports.onCreatePage = async ({ page, actions: { createPage, deletePage } }) => {
const { relativePath, locale } = page.context;
if (['blog', 'news', 'news/media'].indexOf(relativePath) === -1) {
return;
}
deletePage(page);
const tags = {};
const years = {};
let total = 0;
// search for correct parent directory
({
blog: ['blog'],
news: ['blog', 'news'],
'news/media': ['news']
}[relativePath].forEach(p => {
const match = (filters[locale] || {})[p] || {};
total += match.total || 0;
Object.keys(match.years || {}).forEach(k => {
years[k] = years[k] ? years[k] + match.years[k] : match.years[k];
});
Object.keys(match.tags || {}).forEach(k => {
tags[k] = tags[k] ? tags[k] + match.tags[k] : match.tags[k];
});
}));
const allTags = Object.keys(tags);
const allYears = Object.keys(years);
const pageGroups = [{ path: page.path, items: total }];
const defaultTagQuery = [null, ...allTags];
allYears.forEach(year => {
pageGroups.push({
path: `${page.path}/year/${year}`,
items: years[year],
filter: year,
type: 'year'
});
});
allTags.forEach(tag => {
pageGroups.push({
path: `${page.path}/tag/${tag}`,
items: tags[tag],
filter: tag,
type: 'tag',
tagFilter: [tag]
});
});
pageGroups.forEach(({ path, items, type: filterType, filter, tagFilter }) => {
const numPages = Math.floor(items / itemsPerPage) + 1;
Array.from({ length: numPages }).forEach((_, i) => {
const currentPage = i + 1;
const isFirst = currentPage === 1;
const pageData = {
...page,
path: isFirst ? path : `${path}/page/${currentPage}`,
context: {
...page.context,
numPages,
filterBase: path,
numItems: items,
currentPage,
limit: itemsPerPage,
skip: i * itemsPerPage,
allYears,
allTags,
filter,
filterType,
yearQuery: filterType === 'year' ? `${filter}-*` : '*',
tagQuery: tagFilter || defaultTagQuery
}
};
createPage(pageData);
});
});
};