forked from newrelic/docs-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
217 lines (184 loc) · 4.86 KB
/
gatsby-node.js
File metadata and controls
217 lines (184 loc) · 4.86 KB
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
const path = require('path');
const vfileGlob = require('vfile-glob');
const { read, write } = require('to-vfile');
const { createFilePath } = require('gatsby-source-filesystem');
const TEMPLATE_DIR = 'src/templates/';
const hasOwnProperty = (obj, key) =>
Object.prototype.hasOwnProperty.call(obj, key);
exports.onPreBootstrap = async ({ reporter, store }) => {
reporter.info("generating what's new post IDs");
const { program } = store.getState();
const file = await read(
path.join(program.directory, 'src/data/whats-new-ids.json'),
'utf-8'
);
const data = JSON.parse(file.contents);
let largestID = Object.values(data).reduce(
(num, id) => Math.max(parseInt(id, 10), num),
0
);
return new Promise((resolve) => {
vfileGlob(
path.join(program.directory, 'src/content/whats-new/**/*.md')
).subscribe({
next: (file) => {
const slug = file.path
.replace(/.*?src\/content/, '')
.replace('.md', '');
if (!data[slug]) {
data[slug] = String(++largestID);
}
},
complete: async () => {
file.contents = JSON.stringify(data, null, 2);
await write(file, 'utf-8');
resolve();
},
});
});
};
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (
node.internal.type === 'Mdx' ||
(node.internal.type === 'MarkdownRemark' &&
node.fileAbsolutePath.includes('src/content'))
) {
createNodeField({
node,
name: 'slug',
value: createFilePath({ node, getNode, trailingSlash: false }),
});
}
if (node.internal.type === 'MarkdownRemark') {
createNodeField({
node,
name: 'fileRelativePath',
value: getFileRelativePath(node.fileAbsolutePath),
});
}
};
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;
const { data, errors } = await graphql(`
query {
allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/src/content/" } }
) {
edges {
node {
frontmatter {
template
}
fields {
fileRelativePath
slug
}
}
}
}
allMdx(filter: { fileAbsolutePath: { regex: "/src/content/" } }) {
edges {
node {
fields {
fileRelativePath
slug
}
frontmatter {
template
}
}
}
}
}
`);
if (errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
const { allMarkdownRemark, allMdx } = data;
allMdx.edges.concat(allMarkdownRemark.edges).forEach(({ node }) => {
const {
fields: { fileRelativePath, slug },
} = node;
const template = getTemplate(node);
if (process.env.NODE_ENV === 'development' && !template) {
createPage({
path: slug,
component: path.resolve(TEMPLATE_DIR, 'dev/missingTemplate.js'),
context: {
fileRelativePath,
layout: 'basic',
},
});
} else {
createPage({
path: slug,
component: path.resolve(path.join(TEMPLATE_DIR, `${template}.js`)),
context: {
fileRelativePath,
slug,
},
});
}
});
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type MarkdownRemarkFrontmatter {
template: String
}
type NavYaml implements Node @dontInfer {
id: ID!
title: String!
path: String
icon: String
pages: [NavYaml!]!
rootNav: Boolean!
}
`;
createTypes(typeDefs);
};
exports.createResolvers = ({ createResolvers }) => {
createResolvers({
NavYaml: {
pages: {
resolve: (source) => {
return source.pages || [];
},
},
rootNav: {
resolve: (source) =>
hasOwnProperty(source, 'rootNav') ? source.rootNav : true,
},
},
});
};
exports.onCreatePage = ({ page, actions }) => {
const { createPage } = actions;
if (page.path.match(/404/)) {
page.context.layout = 'basic';
createPage(page);
}
if (!page.context.fileRelativePath) {
page.context.fileRelativePath = getFileRelativePath(page.componentPath);
createPage(page);
}
};
const getTemplate = (node) => {
const {
fields: { fileRelativePath },
} = node;
switch (true) {
case Boolean(node.frontmatter.template):
return node.frontmatter.template;
case fileRelativePath.includes('src/content/docs/release-notes'):
return 'releaseNote';
case fileRelativePath.includes('src/content/whats-new'):
return 'whatsNew';
default:
throw new Error(`Unknown template for doc: ${fileRelativePath}`);
}
};
const getFileRelativePath = (path) => path.replace(`${process.cwd()}/`, '');