-
Notifications
You must be signed in to change notification settings - Fork 13
/
gatsby-node.js
262 lines (241 loc) · 6.96 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
const { createFilePath } = require('gatsby-source-filesystem');
const appInsights = require('applicationinsights');
const WebpackAssetsManifest = require('webpack-assets-manifest');
const DirectoryNamedWebpackPlugin = require('directory-named-webpack-plugin');
const path = require('path');
const axios = require('axios');
const { createContentDigest } = require('gatsby-core-utils');
if (process.env.APPLICATIONINSIGHTS_CONNECTION_STRING) {
// Log build time stats to appInsights
appInsights
.setup()
.setAutoCollectConsole(true, true) // Enable logging of console.xxx
.start();
} else {
// eslint-disable-next-line no-console
console.warn(
'Missing APPLICATIONINSIGHTS_CONNECTION_STRING, this build will not be logged to Application Insights'
);
}
let assetsManifest = {};
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === 'MarkdownRemark') {
// TODO: Workaround - The issue is being tracked on the Gatsby side - https://github.com/gatsbyjs/gatsby/issues/39136
const trimmedContent = node.internal.content
.split('\n')
.map((line) => (line.trim() === '' ? '' : line))
.join('\n');
node.internal.content = trimmedContent;
const slug = createFilePath({ node, getNode, basePath: '' });
createNodeField({
node,
name: 'slug',
value: slug,
});
}
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type MarkdownRemark implements Node @infer {
frontmatter: Frontmatter
}
type Frontmatter @infer {
archivedreason: String
authors: [Author]
related: [String]
redirects: [String]
experts: String
consulting: String
guid: String
seoDescription: String
}
type Author implements Node @dontInfer {
title: String
url: String
img: String
noimage: Boolean
}
`;
createTypes(typeDefs);
};
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
devtool: 'eval-source-map',
plugins: [
new WebpackAssetsManifest({
assets: assetsManifest, // mutates object with entries
merge: true,
}),
],
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
plugins: [
new DirectoryNamedWebpackPlugin({
exclude: /node_modules/,
}),
],
alias: {
'@': path.resolve(__dirname, 'src'),
},
},
});
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage, createRedirect } = actions;
const result = await graphql(`
query {
categories: allMarkdownRemark(
filter: { frontmatter: { type: { eq: "category" } } }
) {
nodes {
fields {
slug
}
frontmatter {
index
uri
redirects
experts
consulting
}
parent {
... on File {
name
relativeDirectory
}
}
}
}
rules: allMarkdownRemark(
filter: { frontmatter: { type: { in: ["rule"] } } }
) {
nodes {
fields {
slug
}
frontmatter {
uri
title
archivedreason
related
redirects
seoDescription
}
}
}
}
`);
const categoryTemplate = require.resolve('./src/templates/category.js');
const ruleTemplate = require.resolve('./src/templates/rule.js');
result.data.categories.nodes.forEach((node) => {
// Find any categories that can't resolve a rule
node.frontmatter.index.forEach((inCat) => {
var match = false;
result.data.rules.nodes.forEach((rulenode) => {
if (rulenode.frontmatter.uri == inCat) {
match = true;
}
if (rulenode.frontmatter.redirects) {
rulenode.frontmatter.redirects.forEach((redirect) => {
if (redirect == inCat) {
match = true;
}
});
}
});
if (match == false) {
// eslint-disable-next-line no-console
console.log(node.parent.name + ' cannot find rule ' + inCat);
}
});
// Create the page for the category
// eslint-disable-next-line no-console
console.log('Creating Category: ' + node.parent.name);
createPage({
path: node.parent.name,
component: categoryTemplate,
context: {
slug: node.fields.slug,
index: node.frontmatter.index,
redirects: node.frontmatter.redirects,
},
});
node.frontmatter.redirects?.forEach((toPath) => {
// eslint-disable-next-line no-console
console.log(`\tRedirect: ${toPath} -> ${node.frontmatter.uri}`);
createRedirect({
fromPath: toPath,
toPath: '/' + node.frontmatter.uri,
isPermanent: true,
});
});
});
result.data.rules.nodes.forEach((node) => {
// Find any rules missing a category
var match = false;
if (!node.frontmatter.archivedreason) {
result.data.categories.nodes.forEach((catNode) => {
catNode.frontmatter.index.forEach((inCat) => {
if (node.frontmatter.uri == inCat) {
match = true;
}
});
});
} else {
match = true;
}
if (match == false) {
// eslint-disable-next-line no-console
console.log(
'https://www.ssw.com.au/rules/' +
node.frontmatter.uri +
' is missing a category'
);
}
// Create the page for the rule
// eslint-disable-next-line no-console
console.log('Creating Rule: ' + node.frontmatter.title);
createPage({
path: node.frontmatter.uri,
component: ruleTemplate,
context: {
slug: node.fields.slug,
related: node.frontmatter.related ? node.frontmatter.related : [''],
uri: node.frontmatter.uri,
redirects: node.frontmatter.redirects,
file: `rules/${node.frontmatter.uri}/rule.md`,
title: node.frontmatter.title,
seoDescription: node.frontmatter.seoDescription,
},
});
node.frontmatter.redirects?.forEach((toPath) => {
// eslint-disable-next-line no-console
console.log(`\tRedirect: ${toPath} -> ${node.frontmatter.uri}`);
createRedirect({
fromPath: toPath,
toPath: '/' + node.frontmatter.uri,
isPermanent: true,
});
});
});
};
exports.sourceNodes = async ({ actions, createNodeId }) => {
const { createNode } = actions;
const res = await axios.get('https://www.ssw.com.au/api/get-megamenu');
const menuData = res.data;
menuData?.menuGroups.forEach((group) => {
const node = {
id: createNodeId(`megamenugroup-${group.name}`),
parent: null,
children: [],
internal: {
type: 'MegaMenuGroup',
contentDigest: createContentDigest(group),
},
...group,
};
createNode(node);
});
};