-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgatsby-node.js
75 lines (66 loc) · 1.58 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
const startCase = require('lodash/startCase');
exports.createPages = async ({ actions, graphql, reporter }) => {
const result = await graphql(
`
{
allMdx {
edges {
node {
fields {
id
slug
}
tableOfContents
}
}
}
}
`
);
if (result.errors) {
reporter.panic('error loading content', result.errors);
return;
}
result.data.allMdx.edges.forEach(({ node }) => {
actions.createPage({
path: node.fields.slug ? node.fields.slug : '/',
component: require.resolve('./src/templates/docs'),
context: {
id: node.fields.id
}
});
});
};
exports.onCreateNode = ({ node, getNode, actions, reporter }) => {
const { createNodeField } = actions;
if (node.internal.type === `Mdx`) {
const parent = getNode(node.parent);
const title = node.frontmatter.title || startCase(parent.name)
let value = node.frontmatter.slug;
if(!value && parent.relativePath){
value = parent.relativePath.replace(parent.ext, '');
}
if (!value) {
reporter.panic(`Can not create node with title: ${title} there is no relative path or frontmatter to set the "slug" field`);
return;
}
if (value === 'index') {
value = '';
}
createNodeField({
name: `slug`,
node,
value: `/${value}`
});
createNodeField({
name: 'id',
node,
value: node.id
});
createNodeField({
name: 'title',
node,
value: title
});
}
};