-
Notifications
You must be signed in to change notification settings - Fork 3
/
gatsby-node.js
70 lines (63 loc) · 1.42 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
const path = require('path');
const { createFilePath, createRemoteFileNode } = require('gatsby-source-filesystem');
exports.sourceNodes = ({ actions: { createNode }, createNodeId, createContentDigest }) => {
const packageJson = require('./package.json');
createNode({
...packageJson,
id: createNodeId(packageJson.version),
internal: {
type: 'packageJson',
contentDigest: createContentDigest(packageJson)
}
});
};
exports.onCreateNode = async ({
node,
actions: { createNodeField, createNode },
getNode,
store,
cache,
createNodeId
}) => {
if (node.internal.type === 'Mdx') {
const path = createFilePath({ node, getNode });
await createNodeField({
node,
name: 'slug',
value: node.frontmatter.type === 'page' ? path : `/${node.frontmatter.type}s${path}`
});
}
};
exports.createPages = async ({ graphql, actions: { createPage, createRedirect } }) => {
const {
data: { allMdx }
} = await graphql(`
query {
allMdx {
nodes {
id
fields {
slug
}
frontmatter {
type
}
}
}
}
`);
allMdx.nodes.forEach((node) => {
const {
id,
fields: { slug },
frontmatter: { type }
} = node;
createPage({
path: slug,
component: path.join(__dirname, `./src/templates/${type}.js`),
context: {
id: id
}
});
});
};