-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
51 lines (47 loc) · 1.24 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
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require(`path`);
// Log out information after a build is done
exports.onPostBuild = ({ reporter }) => {
reporter.info(`Your Gatsby site has been built!`);
};
// Create blog pages dynamically
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const result = await graphql(`
{
allMdx {
nodes {
frontmatter {
slug
tags
}
}
}
categories: allMdx {
distinct(field: frontmatter___category)
}
}
`);
result.data.allMdx.nodes.forEach(({ frontmatter: { slug, tags } }) => {
// Tag is used to search all blogs with the same tag and display their preview on the side
let tag = '';
if (tags != null && tags.length > 0) tag = tags[0];
createPage({
path: `/posts/${slug}`,
component: path.resolve(`src/templates/post-template.tsx`),
context: {
slug,
tag,
},
});
});
result.data.categories.distinct.forEach((category) => {
createPage({
path: `/${category}`,
component: path.resolve(`src/templates/category-template.tsx`),
context: {
category,
},
});
});
};