forked from newrelic/developer-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
270 lines (237 loc) · 6.39 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
const path = require(`path`);
const { execSync } = require('child_process');
const { createFilePath } = require('gatsby-source-filesystem');
const resolveQuickstartSlug = require('./src/utils/resolveQuickstartSlug.js');
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
}
}
}
}
allQuickstarts {
edges {
node {
fields {
slug
}
id
}
}
}
}
`);
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
const {
allMdx,
allNewRelicSdkComponent,
allNewRelicSdkApi,
allQuickstarts,
} = result.data;
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,
},
});
});
allQuickstarts.edges.forEach(({ node }) => {
const {
fields: { slug },
id,
} = node;
createPage({
path: path.join(slug, '/'),
component: path.resolve('./src/templates/QuickstartDetails.js'),
context: {
id,
layout: 'QuickStartLayout',
},
});
});
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 };
if (page.path === '/instant-observability/') {
page.context.layout = 'QuickStartLayout';
}
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',
},
});
};