forked from newrelic/developer-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
94 lines (83 loc) · 2.17 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
const path = require(`path`);
const { execSync } = require('child_process');
const getFileRelativePath = (absolutePath) =>
absolutePath.replace(`${process.cwd()}/`, '');
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage, createRedirect } = actions;
const result = await graphql(`
{
allMdx(limit: 1000) {
edges {
node {
fileAbsolutePath
frontmatter {
path
template
redirects
}
}
}
}
}
`);
// 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,
},
});
});
};
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 }) => {
actions.setWebpackConfig({
externals: {
tessen: 'Tessen',
},
});
};