-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathcreateBlogPages.js
More file actions
96 lines (88 loc) · 2.1 KB
/
createBlogPages.js
File metadata and controls
96 lines (88 loc) · 2.1 KB
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
const slugify = require("../utils/slugify");
const createBlogPages = async ({
graphql,
createPage,
reporter,
isCollectionEnabled,
blogPostTemplate,
blogCategoryListTemplate,
blogTagListTemplate,
}) => {
const result = await graphql(`
{
blogPosts: allMdx(
filter: {
fields: { collection: { eq: "blog" } }
frontmatter: { published: { eq: true } }
}
) {
nodes {
fields {
slug
}
internal {
contentFilePath
}
}
}
blogTags: allMdx(
filter: {
fields: { collection: { eq: "blog" } }
frontmatter: { published: { eq: true } }
}
) {
group(field: { frontmatter: { tags: SELECT } }) {
fieldValue
}
}
blogCategory: allMdx(
filter: {
fields: { collection: { eq: "blog" } }
frontmatter: { published: { eq: true } }
}
) {
group(field: { frontmatter: { category: SELECT } }) {
fieldValue
}
}
}
`);
if (result.errors) {
reporter.panicOnBuild("Error while running GraphQL query for blog pages.");
return;
}
if (!isCollectionEnabled("blog")) {
return;
}
const blogs = result.data.blogPosts.nodes;
const blogCategory = result.data.blogCategory.group;
const blogTags = result.data.blogTags.group;
blogs.forEach((blog) => {
createPage({
path: blog.fields.slug,
component: `${blogPostTemplate}?__contentFilePath=${blog.internal.contentFilePath}`,
context: {
slug: blog.fields.slug,
},
});
});
blogCategory.forEach((category) => {
createPage({
path: `/blog/category/${slugify(category.fieldValue)}`,
component: blogCategoryListTemplate,
context: {
category: category.fieldValue,
},
});
});
blogTags.forEach((tag) => {
createPage({
path: `/blog/tag/${slugify(tag.fieldValue)}`,
component: blogTagListTemplate,
context: {
tag: tag.fieldValue,
},
});
});
};
module.exports = createBlogPages;