-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgatsby-node.js
322 lines (295 loc) · 7.79 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
const path = require("path");
const webpack = require(`webpack`);
const SVGO = require("svgo");
const svgo = new SVGO({
plugins: [
{ removeXMLNS: true },
{
inlineStyles: {
onlyMatchedOnce: false,
},
},
{ convertStyleToAttrs: true },
{
removeAttrs: {
attrs: ["svg:id"],
},
},
],
});
// https://www.gatsbyjs.org/packages/gatsby-plugin-netlify-cms/#disable-widget-on-site
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
plugins: [
new webpack.IgnorePlugin({
resourceRegExp: /^netlify-identity-widget$/,
}),
],
});
};
exports.createSchemaCustomization = ({ actions, schema }) => {
const { createTypes } = actions;
const typeDefs = [
`
type UserLink {
text: String
link: String
}
type Navigation {
title: String
metaDescription: String
navOrder: Int
text: String
link: String
subnav: [UserLink]
}
type SideNav {
includeSidenav: Boolean
wrapSectionFirst: Int
wrapSectionLast: Int
menu: [UserLink]
includeSocial: Boolean
}
type IconElement {
icon: File @link(by: "relativePath")
title: String
cta: String
ctaLink: String
details: String
}
type ImageElement {
image: File @link(by: "relativePath")
altText: String
}
type CategoryElement {
title: String
details: String
cta: String
ctaLink: String
}
type Section {
type: String!
mdMain: MarkdownRemark @link(by: "rawMarkdownBody")
mdCallout: MarkdownRemark @link(by: "rawMarkdownBody")
heroImage: File @link(by: "relativePath")
image: File @link(by: "relativePath")
icons: [IconElement]
categories: [CategoryElement]
images: [ImageElement]
altText: String
title: String
subtitle: String
cta: String
ctaLink: String
numberTitles: Boolean
tweetLimit: Int
}
type PagesJson implements Node {
navigation: Navigation
sidenav: SideNav
sections: [Section]
}
type Frontmatter {
image: File @link(by: "relativePath")
}
type MarkdownRemark implements Node {
frontmatter: Frontmatter!
}
type ContentJson implements Node {
defaultHeroImage: File @link(by: "relativePath")
}
`,
];
createTypes(typeDefs);
};
exports.onCreateNode = async ({
node,
getNode,
actions,
createNodeId,
loadNodeContent,
createContentDigest,
}) => {
const { createNodeField, createParentChildLink, createNode } = actions;
const typesToSlug = ["MarkdownRemark", "PagesJson", "ContentJson"];
if (typesToSlug.includes(node.internal.type)) {
const fileNode = getNode(node.parent);
const fileName = fileNode.name;
createNodeField({
node,
name: "slug",
value: fileName,
});
}
if (node.internal.type === "PagesJson") {
//
// recursively create markdown nodes for any key in the json
// node that starts with 'md'
//
const createFieldsForObject = (obj, path) => {
Object.entries(obj).forEach(([key, value]) => {
const newPath = `${path}${key}`;
if (typeof value === "string" && key.startsWith("md")) {
const id = createNodeId(newPath);
const mdNode = {
id,
key,
children: [],
parent: node.id,
internal: {
type: `JsonMarkdownField`,
mediaType: "text/markdown",
content: value,
contentDigest: createContentDigest(value),
},
};
createNode(mdNode);
createParentChildLink({ parent: node, child: mdNode });
} else if (value && typeof value === "object") {
createFieldsForObject(value, `${newPath}_`);
}
});
};
createFieldsForObject(node, `${node.id}_`);
}
if (node.internal.mediaType === "image/svg+xml") {
//
// Load content of svg files so they can be rendered
// inline
//
const id = createNodeId(node.id);
const fileContent = await loadNodeContent(node);
const { data: rawSvg } = await svgo.optimize(fileContent);
const svgNode = {
id,
children: [],
rawSvg,
parent: node.id,
internal: {
type: "InlineSvg",
contentDigest: createContentDigest(rawSvg),
},
};
createNode(svgNode);
createParentChildLink({ parent: node, child: svgNode });
}
};
exports.createPages = async ({ graphql, actions: { createPage } }) => {
//
// STATIC PAGES
//
const pagesResult = await graphql(`
{
allPagesJson {
nodes {
navigation {
link
}
}
}
}
`);
const staticPageTemplate = path.resolve("src/templates/static-page.tsx");
const pages = pagesResult.data.allPagesJson.nodes;
for (let i = 0; i < pages.length; i++) {
createPage({
path: pages[i].navigation.link,
component: staticPageTemplate,
context: {
link: pages[i].navigation.link,
},
});
}
//
// ANNOUNCEMENTS AND BLOG POSTS
//
const mediaTypes = [
["announcements", "Announcements"],
["blog", "Blog"],
];
const mediaListPage = path.resolve("src/templates/media-list.tsx");
const mediaPage = path.resolve("src/templates/media-page.tsx");
const pageSize = 10;
for (let [mediaType, title] of mediaTypes) {
const { data } = await graphql(`{
allMarkdownRemark(
filter: { frontmatter: { type: { eq: "${mediaType}" }}}
sort: { fields: [frontmatter___date], order: DESC }
) {
nodes {
fields {
slug
}
}
}
}`);
const results = data.allMarkdownRemark.nodes;
const numPages = Math.ceil(results.length / pageSize);
// create list pages for this media type
for (let i = 0; i < numPages; i++) {
createPage({
path: i === 0 ? `/media/${mediaType}` : `/media/${mediaType}/${i + 1}`,
component: mediaListPage,
context: {
limit: pageSize,
skip: i * pageSize,
mediaType,
title,
numPages,
currentPage: i + 1,
basePage: `/media/${mediaType}`,
link: `/media/${mediaType}${i > 0 ? "/" + (i + 1) : ""}`,
},
});
}
// create individual pages for each of the items of this media type
for (let i = 0; i < results.length; i++) {
if (!results[i].fields.externalLink) {
createPage({
path: `/media/${mediaType}/${results[i].fields.slug}`,
component: mediaPage,
context: {
slug: results[i].fields.slug,
link: `/media/${mediaType}/${results[i].fields.slug}`,
mediaType,
},
});
}
}
}
//
// NEWS ARTICLES
//
const newsListPage = path.resolve("src/templates/news-list.tsx");
const { data: newsData } = await graphql(`
{
allMarkdownRemark(
filter: { frontmatter: { type: { eq: "news" } } }
sort: { fields: [frontmatter___date], order: DESC }
) {
nodes {
fields {
slug
}
}
}
}
`);
const results = newsData.allMarkdownRemark.nodes;
const numPages = Math.ceil(results.length / 15);
// create list pages for news articles
for (let i = 0; i < numPages; i++) {
createPage({
path: i === 0 ? `/media/news` : `/media/news/${i + 1}`,
component: newsListPage,
context: {
limit: 15,
skip: i * 15,
numPages,
currentPage: i + 1,
basePage: "/media/news",
link: `/media/news${i > 0 ? "/" + (i + 1) : ""}`,
},
});
}
};