This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgatsby-node.js
168 lines (164 loc) · 4.45 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
const path = require(`path`);
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;
const platformTemplate = path.resolve(`src/templates/platform-template.js`);
const redirectTemplate = path.resolve(`src/templates/redirect-template.js`);
const articleTemplate = path.resolve(`src/templates/latest-updates/index.js`);
const tagTemplate = path.resolve(`src/templates/tag-template.js`);
const eventTemplate = path.resolve(`src/templates/events/event-template.js`);
const eventsTemplate = path.resolve(
`src/templates/events/upcoming-events-template.js`
);
const eventsArchiveTemplate = path.resolve(
`src/templates/events/past-events-template.js`
);
const result = await graphql(`
{
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___title] }
limit: 10
filter: { fileAbsolutePath: { regex: "/data/platforms/" } }
) {
edges {
node {
frontmatter {
path
}
}
}
}
allRedirectsJson {
edges {
node {
from
to
}
}
}
}
`);
const newsResults = await graphql(`
{
allMarkdownRemark(sort: { fields: frontmatter___date, order: ASC }) {
edges {
node {
fileAbsolutePath
frontmatter {
path
title
date(formatString: "MMMM DD, YYYY")
tags
}
}
}
}
}
`);
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
// Create news items pages
const articles = newsResults.data.allMarkdownRemark.edges.filter(({ node }) =>
node.fileAbsolutePath.includes("/latest-updates/")
);
articles.forEach(({ node }, index) => {
createPage({
path: node.frontmatter.path,
component: articleTemplate,
context: {
// additional data passed via context
prev: index === 0 ? null : articles[index - 1].node,
next: index === articles.length - 1 ? null : articles[index + 1].node,
},
});
});
// Create single event pages
const events = newsResults.data.allMarkdownRemark.edges.filter(({ node }) =>
node.fileAbsolutePath.includes("/events/")
);
events.forEach(({ node }, index) => {
createPage({
path: node.frontmatter.path,
component: eventTemplate,
context: {
// additional data passed via context
prev: index === 0 ? null : events[index - 1].node,
next: index === events.length - 1 ? null : events[index + 1].node,
},
});
});
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
console.log(`Creating page: ${node.frontmatter.path}`);
createPage({
path: node.frontmatter.path,
component: platformTemplate,
context: {}, // additional data can be passed via context
});
});
result.data.allRedirectsJson.edges.forEach(({ node }) => {
console.log(`Creating redirect: ${node.from} > ${node.to}`);
createPage({
path: node.from,
component: redirectTemplate,
context: node,
});
});
const todaysDate = new Date();
const dateString = `${todaysDate.getFullYear()}-${
todaysDate.getMonth() + 1 < 10 ? "0" : ""
}${todaysDate.getMonth() + 1}-${
todaysDate.getDate() < 10 ? "0" : ""
}${todaysDate.getDate()}`;
// Create upcoming event list page
createPage({
path: "/about/events",
component: eventsTemplate,
context: {
todaysDate: dateString,
},
});
// Create archived event list page
createPage({
path: "/about/events/archive",
component: eventsArchiveTemplate,
context: {
todaysDate: dateString,
},
});
// Create tag pages
const allTags = new Set();
articles.concat(events).forEach(
({
node: {
frontmatter: { tags },
},
}) => {
if (!Array.from(tags)) return;
tags.forEach((tag) => allTags.add(tag));
}
);
allTags.forEach((tag) => {
createPage({
path: `/tagged/${tag}`,
component: tagTemplate,
context: { tag },
});
});
return [...articles, ...events];
};
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
if (stage === "build-html") {
actions.setWebpackConfig({
module: {
rules: [
{
test: /@nivo/,
use: loaders.null(),
},
],
},
});
}
};