-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.ts
167 lines (144 loc) · 4.48 KB
/
gatsby-node.ts
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import type { Actions, GatsbyNode } from 'gatsby';
import { createFilePath } from 'gatsby-source-filesystem';
import path from 'path';
import { AllMarkdownRemark } from './src/type';
export const onCreateWebpackConfig: GatsbyNode['onCreateWebpackConfig'] = ({ getConfig, actions }) => {
const output = getConfig().output || {};
actions.setWebpackConfig({
output,
resolve: {
alias: {
'@/src': path.resolve(__dirname, 'src/'),
'@/assets': path.resolve(__dirname, 'assets/'),
},
},
});
};
type CreatePagesFuncProps = {
createPage: Actions['createPage'];
edges: AllMarkdownRemark['edges'];
};
const createPosts = ({ createPage, edges }: CreatePagesFuncProps) => {
const posts = path.resolve(`./src/templates/posts-template/index.tsx`);
const categorySet: Set<string> = new Set();
const edgesWithMap = edges.map((edge) => {
const { categories } = edge.node.frontmatter;
const categoriesArr = categories.split(' ');
const categoriesMap = categoriesArr.reduce((acc, category) => {
acc[category] = true;
return acc;
}, {} as Record<string, boolean>);
return { ...edge, categoriesMap };
});
edgesWithMap.forEach((edge) => {
const postCategories = Object.keys(edge.categoriesMap);
postCategories.forEach((category) => {
const categoryName = category.replace('featured-', '').trim();
categorySet.add(categoryName);
});
});
const categories = ['All', ...[...categorySet].sort((a, b) => a.localeCompare(b))];
createPage({
path: `/posts`,
component: posts,
context: { currentCategory: 'All', edges, categories },
});
categories.forEach((currentCategory) => {
createPage({
path: `/posts/${currentCategory}`,
component: posts,
context: {
currentCategory,
categories,
edges: edgesWithMap.filter((edge) => edge.categoriesMap[currentCategory]),
},
});
});
};
const createPost = ({ createPage, edges }: CreatePagesFuncProps) => {
const post = path.resolve(`./src/templates/post-template/index.tsx`);
edges.forEach(({ node, next, previous }) => {
createPage({
path: node.fields.slug,
component: post,
context: {
// additional data can be passed via context
slug: node.fields.slug,
nextSlug: next?.fields.slug ?? '',
prevSlug: previous?.fields.slug ?? '',
},
});
});
};
export const createPages: GatsbyNode['createPages'] = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
// Get all markdown blog posts sorted by date
const result: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
errors?: any;
data?: {
allMarkdownRemark: AllMarkdownRemark;
};
} = await graphql(`
{
allMarkdownRemark(sort: { order: DESC, fields: [frontmatter___date] }, limit: 1000) {
edges {
node {
id
excerpt(pruneLength: 500, truncate: true)
fields {
slug
}
frontmatter {
categories
title
date(formatString: "YYYY.MM.DD")
}
}
next {
fields {
slug
}
}
previous {
fields {
slug
}
}
}
}
}
`);
if (result.errors || !result.data) {
reporter.panicOnBuild(`There was an error loading your blog posts`, result.errors);
return;
}
const filteredEdges = result.data.allMarkdownRemark.edges.map((edge) => {
const { categories } = edge.node.frontmatter;
const categoriesArr = categories.split(' ');
const categorySet: Set<string> = new Set();
categoriesArr.forEach((category) => {
const categoryName = category.replace('featured-', '').trim();
categorySet.add(categoryName);
});
return {
...edge,
node: {
...edge.node,
frontmatter: {
...edge.node.frontmatter,
categories: [...categorySet].sort((a, b) => a.localeCompare(b)).join(' '),
},
},
};
});
createPosts({ createPage, edges: filteredEdges });
createPost({ createPage, edges: filteredEdges });
};
export const onCreateNode: GatsbyNode['onCreateNode'] = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `content` });
createNodeField({ node, name: `slug`, value: slug });
}
};