-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgatsby-node.js
More file actions
195 lines (165 loc) · 4.25 KB
/
Copy pathgatsby-node.js
File metadata and controls
195 lines (165 loc) · 4.25 KB
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
const path = require(`path`);
const fs = require('fs');
function createQuotesPagination(quotes) {
const perPage = 20;
const totalPages = Math.ceil(quotes.length / perPage);
for (let currentPage = 1; currentPage <= totalPages; currentPage += 1) {
const pathSuffix = currentPage > 1 ? currentPage : '';
const start = perPage * (currentPage - 1);
const end = start + perPage;
const pageQuotes = quotes.slice(start, end);
const pageData = {
path: `/${pathSuffix}`,
context: {
pageQuotes,
currentPage,
totalPages,
},
};
createJSON(pageData);
}
// eslint-disable-next-line
console.log(`\nCreated ${totalPages} pages of paginated content.`);
}
function createAuthorPages(quotes, { graphql, createPage }) {
const component = path.resolve(`src/templates/author.js`);
return graphql(`
query authorsListQuery {
allAuthorsJson {
totalCount
edges {
node {
uuid
name
slug
}
}
}
}
`).then((result) => {
if (result.errors) {
throw result.errors;
}
const authors = result.data.allAuthorsJson.edges.map(({ node }) => node);
authors.forEach(({ name, slug, uuid }) => {
const pageData = {
path: `/quotes-by/${slug}/`,
component,
context: {
quotes: quotes.filter((quote) => quote.authors_uuid.includes(uuid)),
name,
slug,
},
};
createPage(pageData);
// eslint-disable-next-line
console.log(`\nCreated ${slug} author page.`);
});
});
}
function createTagPages(quotes, { graphql, createPage }) {
const component = path.resolve(`src/templates/tag.js`);
return graphql(`
query tagsListQuery {
allTagsJson {
totalCount
edges {
node {
uuid
name
slug
}
}
}
}
`).then((result) => {
if (result.errors) {
throw result.errors;
}
const tags = result.data.allTagsJson.edges.map(({ node }) => node);
tags.forEach(({ name, slug, uuid }) => {
const pageData = {
path: `/quotes-tagged-with/${slug}/`,
component,
context: {
quotes: quotes.filter((quote) => quote.tags_uuid.includes(uuid)),
name,
slug,
},
};
createPage(pageData);
// eslint-disable-next-line
console.log(`\nCreated ${slug} tag page.`);
});
});
}
function createQuotePages(quotes, { createPage }) {
const component = path.resolve(`src/templates/quote.js`);
let index = 0;
quotes.forEach((quote) => {
const { body, uuid } = quote;
const pageData = {
path: `/q/${uuid}/`,
component,
context: {
quotes: [quote],
body,
index,
},
};
createPage(pageData);
// eslint-disable-next-line
console.log(`\nCreated ${uuid} quote page.`);
index++;
});
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return graphql(`
query quotesListQuery {
allQuotesJson(sort: { fields: [publish_date], order: [DESC] }) {
totalCount
edges {
node {
uuid
body
authors
authors_uuid
tags
tags_uuid
}
}
}
}
`).then((result) => {
if (result.errors) {
throw result.errors;
}
const quotes = result.data.allQuotesJson.edges.map(({ node }) => node);
createQuotesPagination(quotes);
return createAuthorPages(quotes, { graphql, createPage })
.then(() => createTagPages(quotes, { graphql, createPage }))
.then(() => createQuotePages(quotes, { createPage }));
});
};
function createJSON({
context: { pageQuotes, currentPage, totalPages },
path,
}) {
const pathSuffix = path.substring(1);
const dir = 'public/pages/';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
const filePath = `${dir}${pathSuffix}.json`;
const dataToSave = JSON.stringify({
nextPage: currentPage < totalPages && currentPage + 1,
items: pageQuotes,
});
fs.writeFile(filePath, dataToSave, function (err) {
if (err) {
// eslint-disable-next-line
return console.log(err);
}
});
}