forked from newrelic/developer-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
180 lines (159 loc) · 4.11 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
const path = require(`path`);
const { execSync } = require('child_process');
const { createFilePath } = require('gatsby-source-filesystem');
const MAX_RESULTS = 5;
const kebabCase = (string) =>
string
.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/\s+/g, '-')
.toLowerCase();
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;
allMdx.edges.forEach(({ node }) => {
const {
frontmatter,
fields: { fileRelativePath, slug },
} = node;
if (frontmatter.redirects) {
frontmatter.redirects.forEach((fromPath) => {
createRedirect({
fromPath,
toPath: frontmatter.path,
isPermanent: true,
redirectInBrowser: true,
});
});
}
createPage({
path: frontmatter.path || slug,
component: path.resolve(`src/templates/${frontmatter.template}.js`),
context: {
slug,
fileRelativePath,
guidesFilter:
frontmatter.template === 'OverviewTemplate'
? `${frontmatter.path}/*`
: undefined,
relatedResourceLimit: Math.max(
MAX_RESULTS - (frontmatter.resources || []).length,
0
),
},
});
});
allNewRelicSdkComponent.edges.forEach(({ node }) => {
const {
fields: { slug },
} = node;
createPage({
path: slug,
component: path.resolve('./src/templates/ComponentReferenceTemplate.js'),
context: {
slug,
},
});
});
allNewRelicSdkApi.edges.forEach(({ node }) => {
const {
fields: { slug },
} = node;
createPage({
path: slug,
component: path.resolve('./src/templates/ApiReferenceTemplate.js'),
context: {
slug,
},
});
});
};
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)}`,
});
}
};
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',
},
});
};