-
Notifications
You must be signed in to change notification settings - Fork 41
/
gatsby-node.js
251 lines (224 loc) · 5.74 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
const path = require(`path`);
const slugify = require("slugify");
const { createFilePath } = require(`gatsby-source-filesystem`);
const { paginate } = require("gatsby-awesome-pagination");
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const parent = getNode(node.parent);
let collection = parent.sourceInstanceName;
let prepend = "";
if (collection === "blog") {
prepend = "/article";
}
const value = prepend + createFilePath({ node, getNode });
createNodeField({
name: `slug`,
node,
value,
});
createNodeField({
node,
name: "collection",
value: collection,
});
}
};
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
const pageTemplate = require.resolve(`./src/templates/page.tsx`);
const pageresults = await graphql(`
{
Pages: allMarkdownRemark(
filter: { fields: { collection: { eq: "pagesrc" } } }
) {
nodes {
fields {
slug
}
}
}
}
`);
if (pageresults.errors) {
reporter.panicOnBuild(`Error while running Pages GraphQL query.`);
return;
}
pageresults.data.Pages.nodes.forEach(page => {
createPage({
path: page.fields.slug,
component: pageTemplate,
context: {
slug: page.fields.slug,
},
});
});
const blogPostTemplate = require.resolve(`./src/templates/blog-post.tsx`);
const blogresults = await graphql(`
{
blogPosts: allMarkdownRemark(
sort: { frontmatter: { date: DESC } }
filter: { fields: { collection: { eq: "blog" } } }
limit: 1000
) {
nodes {
fields {
slug
}
frontmatter {
tags
}
}
}
}
`);
if (blogresults.errors) {
reporter.panicOnBuild(`Error while running Blog Post GraphQL query.`);
return;
}
let posts = blogresults.data.blogPosts.nodes;
posts.forEach(post => {
createPage({
path: post.fields.slug,
component: blogPostTemplate,
context: {
slug: post.fields.slug,
},
});
});
paginate({
createPage,
items: posts,
itemsPerPage: 20,
pathPrefix: ({ pageNumber, numberOfPages }) =>
pageNumber === 0 ? "/blog" : "/blog/page",
component: path.resolve("src/templates/blog-index.tsx"),
});
const tagresults = await graphql(`
query MyQuery {
blogTags: allMarkdownRemark(
filter: { fields: { collection: { eq: "blog" } } }
) {
distinct(field: { frontmatter: { tags: SELECT } })
}
}
`);
if (tagresults.errors) {
reporter.panicOnBuild(`Error while running Tags GraphQL query.`);
return;
}
let tags = tagresults.data.blogTags.distinct;
let tagSlug = "";
tags.forEach(tag => {
tagSlug = slugify(tag, { lower: true, remove: /[^\w\s$*_+~.()'"!\-@]+/g });
paginate({
createPage,
items: posts.filter(post => {
if (post.frontmatter.tags != undefined) {
if (post.frontmatter.tags.includes(tag)) {
return post;
}
}
}),
itemsPerPage: 20,
pathPrefix: ({ pageNumber, numberOfPages }) =>
pageNumber === 0 ? "/blog/tag/" + tagSlug : "/blog/tag/" + tagSlug + "/page",
component: path.resolve("src/templates/tag.tsx"),
context: { tag: tag },
});
});
// *** Begin Omega Addon Page Builds
const omegaaddonresults = await graphql(`
query MyQuery {
allOmegaAddon {
nodes {
slug
}
}
}
`);
if (omegaaddonresults.errors) {
reporter.panicOnBuild(`Error while running Omega Add-on GraphQL query.`);
return;
}
omegaaddonresults.data.allOmegaAddon.nodes.forEach(addon => {
createPage({
path: "addons/omega/" + addon.slug,
component: path.resolve(`src/templates/omega/addon.tsx`),
context: {
slug: addon.slug,
},
});
});
const omegacategoryresults = await graphql(`
query MyQuery {
allOmegaCategory {
nodes {
slug
}
}
}
`);
if (omegacategoryresults.errors) {
reporter.panicOnBuild(
`Error while running Omega Add-on Category GraphQL query.`,
);
return;
}
omegacategoryresults.data.allOmegaCategory.nodes.forEach(category => {
createPage({
path: "addons/omega/category/" + category.slug,
component: path.resolve(`src/templates/omega/category.tsx`),
context: {
slug: category.slug,
},
});
});
const omegaauthorresults = await graphql(`
query MyQuery {
allOmegaAuthor {
nodes {
slug
}
}
}
`);
if (omegaauthorresults.errors) {
reporter.panicOnBuild(`Error while running Omega Add-on Author GraphQL query.`);
return;
}
omegaauthorresults.data.allOmegaAuthor.nodes.forEach(author => {
createPage({
path: "addons/omega/author/" + author.slug,
component: path.resolve(`src/templates/omega/author.tsx`),
context: {
slug: author.slug,
},
});
});
// *** End Omega Addon Page Builds
const distresults = await graphql(`
query MyQuery {
allDistributionYaml {
nodes {
name
}
}
}
`);
if (distresults.errors) {
reporter.panicOnBuild(`Error while running Distributions GraphQL query.`);
return;
}
distresults.data.allDistributionYaml.nodes.forEach(dist => {
createPage({
path:
"download/" +
slugify(dist.name, { lower: true, remove: /[^\w\s$*_+~.()'"!\-@]+/g }),
component: path.resolve(`src/templates/distribution.tsx`),
context: {
name: dist.name,
},
});
});
};