-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
163 lines (142 loc) · 3.52 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
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
exports.createPages = async ({ actions, graphql }) => {
const tagSet = new Set();
const yearSet = new Set();
const blogposts = await graphql(`
{
posts: allMdx(filter: { frontmatter: { published: { ne: false } } }) {
nodes {
frontmatter {
slug
tags
}
fields {
year
}
id
parent {
... on File {
relativeDirectory
sourceInstanceName
}
}
}
}
}
`);
const pages = blogposts.data.posts.nodes;
pages.forEach((page) => {
page.frontmatter.tags.forEach((tag) => tagSet.add(tag));
yearSet.add(page.fields.year);
actions.createPage({
path: `/blog/${page.frontmatter.slug.replace(/ /g, '-')}`,
component: require.resolve('./src/templates/blog-page-template.jsx'),
context: {
id: page.id,
relativeDirectory: page.parent.relativeDirectory,
},
});
});
tagSet.forEach((tag) => {
actions.createPage({
path: `/blog/tags/${tag.toLowerCase().replace(/ /g, '-')}`,
component: require.resolve('./src/templates/blog-tag-page-template.jsx'),
context: {
tag,
},
});
});
yearSet.forEach((year) => {
actions.createPage({
path: `/blog/tags/${year}`,
component: require.resolve('./src/templates/blog-year-page-template.jsx'),
context: {
year,
},
});
});
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
createTypes(`
type BlogTag implements Node {
name: String!
totalCount: Int!
}
`);
createTypes(`
type BlogYear implements Node {
name: String!
totalCount: Int!
}
`);
};
exports.sourceNodes = ({ actions, getNodesByType }) => {
const tagsMap = new Map();
const yearMap = new Map();
const { createNode } = actions;
// Get all Mdx nodes
const allMdxNodes = getNodesByType('Mdx');
// Compute the tag counts
allMdxNodes.forEach((node) => {
const { tags, published, date } = node.frontmatter;
if (published === false) return;
if (tags && tags.length > 0) {
tags.forEach((tag) => {
if (!tagsMap.has(tag)) {
tagsMap.set(tag, 1);
} else {
tagsMap.set(tag, tagsMap.get(tag) + 1);
}
});
}
const year = new Date(date).getFullYear();
if (!yearMap.has(year)) {
yearMap.set(year, 1);
} else {
yearMap.set(year, yearMap.get(year) + 1);
}
});
const tagArr = Array.from(tagsMap, ([name, totalCount]) => ({
name,
totalCount,
}));
const yearArr = Array.from(yearMap, ([name, totalCount]) => ({
name,
totalCount,
}));
// Create BlogTag nodes
tagArr.forEach(({ name, totalCount }) => {
createNode({
id: `BlogTag-${name}`,
name,
totalCount,
internal: {
type: 'BlogTag',
contentDigest: JSON.stringify({ name, totalCount }),
},
});
});
// Create BlogYear nodes
yearArr.forEach(({ name, totalCount }) => {
createNode({
id: `BlogYear-${name}`,
name,
totalCount,
internal: {
type: 'BlogYear',
contentDigest: JSON.stringify({ name, totalCount }),
},
});
});
};
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === 'Mdx') {
const year = new Date(node.frontmatter.date).getFullYear();
createNodeField({
node,
name: 'year',
value: year,
});
}
};