forked from newrelic/developer-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
112 lines (100 loc) · 2.9 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
const path = require(`path`);
const { execSync } = require('child_process');
const MAX_RESULTS = 5;
const getFileRelativePath = (absolutePath) =>
absolutePath.replace(`${process.cwd()}/`, '');
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage, createRedirect } = actions;
const result = await graphql(`
{
allMdx(
limit: 1000
filter: { fileAbsolutePath: { regex: "/src/markdown-pages/" } }
) {
edges {
node {
fileAbsolutePath
frontmatter {
path
template
redirects
resources {
url
}
}
}
}
}
}
`);
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
result.data.allMdx.edges.forEach(({ node }) => {
const { frontmatter } = node;
if (frontmatter.redirects) {
frontmatter.redirects.forEach((fromPath) => {
createRedirect({
fromPath,
toPath: frontmatter.path,
isPermanent: true,
redirectInBrowser: true,
});
});
}
createPage({
path: frontmatter.path,
component: path.resolve(`src/templates/${frontmatter.template}.js`),
context: {
fileRelativePath: getFileRelativePath(node.fileAbsolutePath),
guidesFilter:
frontmatter.template === 'OverviewTemplate'
? `${frontmatter.path}/*`
: undefined,
relatedResourceLimit: Math.max(
MAX_RESULTS - (frontmatter.resources || []).length,
0
),
},
});
});
};
exports.onCreateNode = ({ node, actions }) => {
// if we don't have a relative path, attempt to get one
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,
});
}
if (node.context && !node.context.fileRelativePath) {
const { createPage } = actions;
const { path, component } = node;
createPage({
path,
component,
context: {
fileRelativePath: getFileRelativePath(component),
},
});
}
};
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',
},
});
};