-
Notifications
You must be signed in to change notification settings - Fork 10
/
gatsby-node.mjs
210 lines (190 loc) · 5.88 KB
/
gatsby-node.mjs
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const _ = require("lodash");
const path = require("path");
const { createFilePath } = require("gatsby-source-filesystem");
export const createSchemaCustomization = ({ actions: { createTypes } }) => {
createTypes(`
type Mdx implements Node {
frontmatter: MdxFrontmatter
}
type MdxFrontmatter {
tags: [String]
hidden: String
author: String
excerpt: String!
links: [String]
}
`);
};
export const createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return new Promise((resolve, reject) => {
const pageSize = 10;
const blogPost = path.resolve("./src/templates/blog-post.js");
const tagTemplate = path.resolve("./src/templates/tag.js");
const paginatedPosts = path.resolve("./src/templates/paginated-posts.js");
resolve(
graphql(`
{
allMdx(sort: { frontmatter: { date: DESC } }) {
edges {
node {
frontmatter {
title
tags
date(formatString: "MMM DD, YYYY")
path
links
series
order
excerpt
featuredImage {
childImageSharp {
gatsbyImageData(
layout: CONSTRAINED
formats: [AUTO, WEBP, AVIF]
)
original {
src
}
}
}
}
internal {
contentFilePath
}
}
}
}
}
`).then((result) => {
if (result.errors) {
console.log(result.errors);
reject(result.errors);
}
// Create blog posts pages.
const posts = result.data.allMdx.edges;
// Tag pages:
let tags = [];
// Iterate through each post, putting all found tags into `tags`
posts.forEach((edge) => {
if (_.get(edge, "node.frontmatter.tags")) {
tags = tags.concat(edge.node.frontmatter.tags);
}
});
// Eliminate duplicate tags
tags = _.uniq(tags);
// Make tag pages
tags.forEach((tag) => {
createPage({
path: `/tags/${_.kebabCase(tag)}/`,
component: tagTemplate,
context: {
tag
}
});
});
posts.forEach((post) => {
let related = posts.filter((p) => {
if (p.node.frontmatter.path === post.node.frontmatter.path) {
return false;
}
// Exclude posts from the same series
if (p.node.frontmatter.series && (p.node.frontmatter.series === post.node.frontmatter.series)) {
return false;
}
let filteredTags = post.node.frontmatter.tags.filter((tag) => {
if (p.node.frontmatter.tags.indexOf(tag) !== -1) {
return true;
}
return false;
});
if (filteredTags && filteredTags.length > 0) {
return true;
}
return false;
});
related = _.shuffle(related).slice(0, 6);
let links;
if (!post.node.frontmatter.links) {
links = {};
} else {
links = {};
post.node.frontmatter.links.forEach((link) => {
links[link] = posts.find(
(post) => post.node.frontmatter.path === link
).node;
});
}
let seriesInfo = {};
if (post.node.frontmatter.series) {
seriesInfo.series = post.node.frontmatter.series;
let seriesPosts = posts.filter(p => p.node.frontmatter.series === seriesInfo.series);
seriesPosts.sort((a, b) => {
let first = a.node.frontmatter.order;
let second = b.node.frontmatter.order;
if (!first) {
first = 999;
}
if (!second) {
second = 999;
}
return first - second;
});
seriesInfo.posts = seriesPosts.map(p => {
let isCurrent = false;
if (post.node.frontmatter.path === p.node.frontmatter.path) {
isCurrent = true;
}
return {
path: p.node.frontmatter.path,
title: p.node.frontmatter.title,
order: p.node.frontmatter.order,
isCurrent: isCurrent };
});
}
createPage({
path: post.node.frontmatter.path,
component: `${blogPost}?__contentFilePath=${post.node.internal.contentFilePath}`,
context: {
slug: post.node.frontmatter.path,
related,
links,
seriesInfo
}
});
});
// pagination
const chunkedPosts = _.chunk(posts, pageSize);
chunkedPosts.forEach((chunk, index) => {
let path = "/";
if (index > 0) {
path = `/pages/${index + 1}/`;
}
createPage({
path: path,
component: paginatedPosts,
context: {
pageSize: pageSize,
pageSkip: index * pageSize,
pagesTotal: Math.ceil(posts.length / pageSize),
currentPage: index + 1
}
});
});
})
);
});
};
export const onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === `Mdx`) {
const value = node.frontmatter.path
createNodeField({
name: `slug`,
node,
value
});
}
};