forked from newrelic/developer-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
260 lines (228 loc) · 6.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
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
const path = require(`path`);
const { execSync } = require('child_process');
const { createFilePath } = require('gatsby-source-filesystem');
const resolveQuickstartSlug = require('./src/utils/resolveQuickstartSlug.js');
const externalRedirects = require('./src/data/external-redirects.json');
const kebabCase = (string) =>
string
.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/\s+/g, '-')
.toLowerCase();
// This is a temporary customization to ensure that the path field is always
// queryable from the createPages API. We rely on the path for deprecation
// warnings to inform authors that path is no longer a supported markdown
// property. Because of Gatsby's type inference from frontmatter properties, the
// query in createPages fails without this patch if no file contains a 'path'
// frontmatter property.
//
// This patch can be safely removed when removing the deprecation warning in
// createPages.
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type MdxFrontmatter {
path: String
}
`;
createTypes(typeDefs);
};
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage, createRedirect } = actions;
const result = await graphql(`
query {
allMdx(filter: { fileAbsolutePath: { regex: "/src/markdown-pages/" } }) {
edges {
node {
fields {
fileRelativePath
slug
}
frontmatter {
path
template
redirects
resources {
url
}
}
}
}
}
allNewRelicSdkComponent {
edges {
node {
fields {
slug
}
}
}
}
allNewRelicSdkApi {
edges {
node {
fields {
slug
}
}
}
}
}
`);
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
const { allMdx, allNewRelicSdkComponent, allNewRelicSdkApi } = result.data;
if (externalRedirects.length > 0) {
externalRedirects.forEach(({ url, paths }) => {
paths.forEach((path) => {
createRedirect({
fromPath: path,
toPath: url,
isPermanent: true,
redirectInBrowser: true,
});
});
});
}
createRedirect({
fromPath: `/instant-observability/`,
toPath: `https://newrelic.com/instant-observability`,
isPermanent: true,
});
createRedirect({
fromPath: `/instant-observability/*`,
toPath: `https://newrelic.com/instant-observability/*`,
isPermanent: true,
});
allMdx.edges.forEach(({ node }) => {
const {
frontmatter,
fields: { fileRelativePath, slug },
} = node;
if (frontmatter.redirects) {
frontmatter.redirects.forEach((fromPath) => {
createRedirect({
fromPath,
toPath: slug,
isPermanent: true,
redirectInBrowser: true,
});
});
}
if (frontmatter.path) {
const recommendedPath = path.join(
'src/markdown-pages',
fileRelativePath.endsWith('index.mdx')
? path.join(frontmatter.path, 'index.mdx')
: `${frontmatter.path}.mdx`
);
const recommendation =
recommendedPath === fileRelativePath
? "Please remove the 'path' property."
: `To render this page at '${frontmatter.path}', please move this file to: '${recommendedPath}'.`;
reporter.panicOnBuild(
`
${fileRelativePath}
The 'path' property on frontmatter is deprecated and has no effect. URLs are now generated using the path of the file. ${recommendation}
`.trim()
);
}
createPage({
path: path.join(slug, '/'),
component: path.resolve(`src/templates/${frontmatter.template}.js`),
context: {
slug,
fileRelativePath,
guidesFilter: ['OverviewTemplate', 'LabOverviewTemplate'].includes(
frontmatter.template
)
? `${slug}/*`
: undefined,
},
});
});
allNewRelicSdkComponent.edges.forEach(({ node }) => {
const {
fields: { slug },
} = node;
createPage({
path: path.join(slug, '/'),
component: path.resolve('./src/templates/ComponentReferenceTemplate.js'),
context: {
slug,
},
});
});
allNewRelicSdkApi.edges.forEach(({ node }) => {
const {
fields: { slug },
} = node;
createPage({
path: path.join(slug, '/'),
component: path.resolve('./src/templates/ApiReferenceTemplate.js'),
context: {
slug,
},
});
});
};
exports.onCreatePage = async ({ page, actions }) => {
const { createPage, deletePage } = actions;
const oldPage = { ...page };
deletePage(oldPage);
createPage(page);
};
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === 'Mdx' && node.fileAbsolutePath) {
const gitAuthorTime = execSync(
`git log -1 --pretty=format:%aI ${node.fileAbsolutePath}`
).toString();
actions.createNodeField({
node,
name: 'gitAuthorTime',
value: gitAuthorTime,
});
createNodeField({
node,
name: 'slug',
value: createFilePath({ node, getNode, trailingSlash: false }),
});
}
if (node.internal.type === 'NewRelicSdkComponent') {
createNodeField({
node,
name: 'slug',
value: `/components/${kebabCase(node.name)}`,
});
}
if (node.internal.type === 'NewRelicSdkApi') {
createNodeField({
node,
name: 'slug',
value: `/apis/${kebabCase(node.name)}`,
});
}
if (node.internal.type === 'Quickstarts') {
createNodeField({
node,
name: 'slug',
value: `${resolveQuickstartSlug(node.name, node.id)}`,
});
}
};
exports.onCreateWebpackConfig = ({ actions, plugins }) => {
actions.setWebpackConfig({
// The `debug` library is causing issues when building the site by including
// invalid JS. This ensures the module resolves to the browser-capatible
// source instead of the node source. See the following issue for this
// recommendation:
// https://github.com/escaladesports/legacy-gatsby-plugin-prefetch-google-fonts/issues/18
plugins: [plugins.normalModuleReplacement(/^\.\/node\.js/, './browser.js')],
externals: {
tessen: 'Tessen',
},
});
};