forked from mParticle/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
450 lines (405 loc) · 15.1 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// In your gatsby-node.js
const path = require(`path`);
const crypto = require(`crypto`);
const redirects = require(`./config/manualRedirects.json`);
const fs = require('fs');
const fs_e = require('fs-extra');
const remark = require('remark')
const strip = require('strip-markdown')
const { v4: uuidv4 } = require('uuid');
const { execSync } = require('child_process');
// Documentation for these APIs and other functionality is here:
// https://www.gatsbyjs.org/docs/node-apis/
exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
const { createNodeField } = boundActionCreators
let slug
// set 'frontmatter' field on Javascript files that have a 'frontmatter' export so that they can
// have the same structure as pages generated from markdown
if (node.internal.mediaType === `application/javascript` && node.children && node.children[0]) {
const childNodeName = node.children[0];
const childNode = getNode(childNodeName);
if (childNode.frontmatter) {
createNodeField({ node, name: `frontmatter`, value: childNode.frontmatter });
}
}
// For both Javascript and Markdown pages -> generate a 'slug' or path for the file
if (node.internal.type === `MarkdownRemark` || node.internal.type === `JavascriptFrontmatter`) {
const fileNode = getNode(node.parent)
const parsedFilePath = path.parse(fileNode.relativePath)
/* Potential clash for 'index' and '_template' files - but we shouldn't be using both */
if (parsedFilePath.name !== `index` && parsedFilePath.name !== `_template` && parsedFilePath.dir !== ``) {
slug = `/${parsedFilePath.dir}/${parsedFilePath.name}/`
} else if (parsedFilePath.dir === ``) {
slug = `/${parsedFilePath.name}/`
} else {
slug = `/${parsedFilePath.dir}/`
}
// Add slug as a field on the node.
createNodeField({ node, name: `slug`, value: slug })
// Add Timestamps to Markdown Nodes
const modifiedDatetime = execSync(
`git log -1 --pretty=format:%aI ${node.fileAbsolutePath}`,
).toString().replace('\n', '');
createNodeField({ node, name: 'modifiedDatetime', value: modifiedDatetime })
const createdDatetime = execSync(
`git log --reverse --pretty=format:%aI ${node.fileAbsolutePath} | head -1`,
).toString().replace('\n', '');
createNodeField({ node, name: 'createdDatetime', value: createdDatetime })
}
}
exports.onCreateWebpackConfig = ({
stage,
rules,
loaders,
plugins,
actions,
}) => {
actions.setWebpackConfig({
module: {
rules: [
/* Don't need this less webpack config because 'gatsby-plugin-less' is used instead
{
test: /\.less$/,
use: [
// We don't need to add the matching ExtractText plugin
// because gatsby already includes it and makes sure its only
// run at the appropriate stages, e.g. not in development
loaders.miniCssExtract(),
loaders.css({ importLoaders: 1 }),
// the postcss loader comes with some nice defaults
// including autoprefixer for our configured browsers
loaders.postcss(),
`less-loader`,
],
},*/
/* This custom loader override was used in v0 but not in v2
{
test: /\.md$/,
loaders: ['markdown-loader'],
}, */
],
},
plugins: [
plugins.define({
__DEVELOPMENT__: stage === `develop` || stage === `develop-html`,
}),
],
})
}
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type PageMetadata implements Node @infer {
path: String
metadataParent: PageMetadata @link
metadataChildren: [PageMetadata] @link
metadataSiblings: [PageMetadata] @link
title: String
order: Float
redirect: String
partnerId: Int
partnerImageOverride: String
draft: Boolean
sourceCode: String
showWhenLast: Boolean
noBreadcrumb: Boolean
headersTocOnly: Boolean
}
`
createTypes(typeDefs)
}
exports.createPages = async function({ graphql, boundActionCreators }) {
const { createPage, createNode, createRedirect } = boundActionCreators
const metadataFields = [
'title',
'order',
'redirect',
'partnerId',
'partnerImageOverride',
'draft',
'sourceCode',
'showWhenLast',
'noBreadcrumb',
'headingsTocOnly'
];
const pages = []
const markdownTemplate = path.resolve('src/templates/MarkdownTemplate.jsx');
const integrationsTemplate = path.resolve('src/templates/IntegrationsTemplate.jsx');
// Query for all markdown "nodes" and for the slug we previously created.
await graphql(
`
{
allMarkdownRemark {
edges {
node {
frontmatter {
title
order
redirect
}
fields {
slug
}
}
}
}
allJavascriptFrontmatter {
edges {
node {
fileAbsolutePath
frontmatter {
${metadataFields.join('\n')}
}
fields {
slug
}
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
/*
BEGIN HELPER CODE SECTION FOR GENERATING PageMetadata nodes in GraphQL framework
*/
// This map is used to build a real tree/graph of the pages in the site along with their
// respective metadata to use for NavPane or other data that may be needed in a component
const metadataMap = {};
// This function adds parent nodes starting at {path} and ending at '/' if they haven't been added yet
// The nodes are type PageMetadata
let createParentTreeFromPath = (path) => {
const pathComponents = path.split('/').filter(x => x);
let nodeId = pathComponents.join('/');
let parentPath = pathComponents.length > 1 ? pathComponents.slice(0, pathComponents.length - 1).join('/') : null;
let parentId = parentPath ? `${parentPath} PageMetadata` : null;
nodeId = `${nodeId} PageMetadata`;
let firstNodeVisit = false;
if (!metadataMap[nodeId]) {
metadataMap[nodeId] = { id: nodeId, internal: { type: 'PageMetadata'} };
firstNodeVisit = true;
}
metadataMap[nodeId].path = path;
if (parentId) {
metadataMap[nodeId].metadataParent = parentId;
if (!metadataMap[parentId]) {
createParentTreeFromPath(`/${parentPath}/`);
}
if (!metadataMap[parentId].metadataChildren) {
metadataMap[parentId].metadataChildren = [];
}
if (firstNodeVisit) {
metadataMap[parentId].metadataChildren.push(nodeId);
}
}
return metadataMap[nodeId];
};
// Copy metadata/frontmatter fields from target node into a PageMetadata node
let extendMetadata = (node) => {
const currNode = createParentTreeFromPath(node.fields.slug);
if (node.frontmatter) {
metadataFields.forEach(f => {
if (node.frontmatter[f])
currNode[f] = node.frontmatter[f];
});
}
return currNode;
}
/*
END HELPER CODE SECTION
*/
// Iterate over all JS files that have exported frontmatter
// These files should already have pages created for them automatically but we still
// need to generate the PageMetadata nodes for them
// Visit these first so that parent data will be available for the child
// markdown pages in the next loop
result.data.allJavascriptFrontmatter.edges.forEach(edge => {
extendMetadata(edge.node);
if (edge.node.frontmatter.redirect) {
createRedirect({
fromPath: edge.node.fields.slug,
isPermanent: false,
toPath: edge.node.frontmatter.redirect,
redirectInBrowser: true
});
}
});
// Create blog posts pages from markdown files
result.data.allMarkdownRemark.edges.forEach(edge => {
const path = edge.node.fields.slug;
// Populate the PageMetadata nodes for the markdown file
const metadataNode = extendMetadata(edge.node);
/*if (metadataNode.metadataParent && metadataMap[metadataNode.metadataParent].sourceCode) {
metadataNode.sourceCode = metadataMap[metadataNode.metadataParent].sourceCode;
}*/
// Add a 'redirect' instead of a page if 'redirect' is specified in frontmatter
if (edge.node.frontmatter.redirect) {
createRedirect({
fromPath: edge.node.fields.slug,
isPermanent: true,
toPath: edge.node.frontmatter.redirect,
redirectInBrowser: true
});
}
else {
// All values passed in the 'context' object can be exposed in a GraphQL page query
// https://www.gatsbyjs.org/docs/page-query/
const targetComponent = path.indexOf('/integrations') === 0 ? integrationsTemplate : markdownTemplate;
createPage({
path: path, // required
component: targetComponent,
context: {
slug: path
},
})
}
})
// Reorder all 'children' in PageMetadata nodes by 'order' field
Object.keys(metadataMap).forEach(k => {
const currNode = metadataMap[k];
if (currNode.metadataChildren) {
currNode.metadataChildren = currNode.metadataChildren
.sort((a, b) => {
let titleA = metadataMap[a].title;
let titleB = metadataMap[b].title;
let orderA = metadataMap[a].order;
let orderB = metadataMap[b].order;
if (orderA) {
if (orderB) {
return orderA - orderB;
} else {
return -1;
}
} else if (orderB) {
return 1;
} else {
let titleA = (metadataMap[a].title || '').toLowerCase();
let titleB = (metadataMap[b].title || '').toLowerCase();
titleA = titleA && titleA.toLowerCase();
titleB = titleB && titleB.toLowerCase();
return titleA.localeCompare(titleB);
}
});
}
});
// Create a PageMetadata node for each object in the metadataMap
Object.keys(metadataMap).forEach(k => {
const currNode = metadataMap[k];
const objStr = JSON.stringify(currNode);
// This contentDigest is a required field that has something to do with internal diffing
// that Gatsby uses to decide what needs to be rebuilt if a file changes
const contentDigest = crypto.createHash(`md5`).update(objStr).digest(`hex`);
currNode.internal.contentDigest = contentDigest;
createNode(currNode);
});
// Create a redirect for all pairs in 'manualRedirects.json'
redirects.forEach((r) => {
createRedirect({
fromPath: r[0],
isPermanent: true,
toPath: r[1],
redirectInBrowser: true
});
});
return
})
}
exports.onPostBuild = (args) => {
return Promise.resolve(args.graphql(`
{
allPageMetadata {
edges {
node {
metadataParent {
title
}
title
path
}
}
}
allMarkdownRemark {
edges {
node {
id
fileAbsolutePath
rawMarkdownBody
frontmatter {
title
redirect
noindex
}
fields {
slug
}
}
}
}
allJavascriptFrontmatter (filter: {frontmatter: {redirect: {ne: null}}}) {
edges {
node {
fileAbsolutePath
frontmatter {
redirect
}
}
}
}
}
`)).then((result) => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
const copyForRedirect = (page, redirect) => {
let writePieces = path.parse(page);
const isDefaultName = writePieces.name === 'index' || writePieces.name === '_template';
const writePath = path.join(writePieces.dir, isDefaultName ? '' : writePieces.name, '/index.html')
.replace('/src/pages/', '/public/').replace('\\src\\pages\\', '\\public\\');
//const writePath = path.join('./public', page, '/index.html');
const readPath = path.join(__dirname, '/public/', redirect, '/index.html');
fs_e.copySync(readPath, writePath);
}
var slugTitles = {}
const generateSearchItem = function(node) {
if (!node.rawMarkdownBody || !node.rawMarkdownBody.trim().length) {
return null
}
var searchItem = { id: uuidv4(), type: 'add'}
searchItem.fields = { title: slugTitles[node.fields.slug] || node.frontmatter.title}
searchItem.fields.url = node.fields.slug.substring(1)
searchItem.fields.content = remark()
.use(strip)
.processSync(node.rawMarkdownBody)
.toString()
return searchItem
}
result.data.allPageMetadata.edges.forEach((e) => {
if (e.node.metadataParent) {
slugTitles[e.node.path] = e.node.metadataParent.title + ' | ' + e.node.title
} else {
slugTitles[e.node.path] = e.node.title
}
});
var searchItems = []
result.data.allMarkdownRemark.edges.forEach((e) => {
if (e.node.frontmatter.redirect) {
copyForRedirect(e.node.fileAbsolutePath, e.node.frontmatter.redirect)
}
var newItem = generateSearchItem(e.node)
if (newItem) {
searchItems.push(newItem)
}
});
fs.writeFileSync('latest_search_items.json', JSON.stringify(searchItems, null, 4))
result.data.allJavascriptFrontmatter.edges.forEach((e) => {
copyForRedirect(e.node.fileAbsolutePath, e.node.frontmatter.redirect);
});
// TODO: Need to do manualRedirects file? Probably not since these are legacy paths
// and we might not want to index them
return;
});
};