-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathgatsby-node.js
216 lines (198 loc) · 4.82 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
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
211
212
213
214
215
216
const path = require("path");
const createPaginatedPages = require("gatsby-paginate");
const _ = require("lodash");
const slugify = require("slugify");
const createMetaPage = ({
type,
edges,
createPage,
itemPageTemplate,
listPageTemplate,
pathPrefix,
}) => {
const itemTemplate = path.resolve(itemPageTemplate);
const listTemplate = path.resolve(listPageTemplate);
const map = {};
const items = [];
edges.forEach(({ node }) => {
(node.frontmatter[type] || []).forEach((item) => {
if (!map[item]) {
map[item] = [];
items.push({
title: item,
url: `${pathPrefix}/${item}`,
});
}
map[item].push(node);
});
});
createPage({
path: `${pathPrefix}/`,
component: listTemplate,
context: { type, items },
});
Object.keys(map).forEach((item) => {
const posts = map[item];
createPage({
path: `${pathPrefix}/${item}`,
component: itemTemplate,
context: {
type,
title: item,
items: posts,
parentUrl: pathPrefix,
},
});
});
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type MarkdownRemark implements Node {
html: String
}
`;
createTypes(typeDefs);
};
exports.createPages = ({ actions, graphql }) => {
const { createPage, createRedirect } = actions;
// "Country" must start with a capital letter
createRedirect({
fromPath: "/",
force: true,
toPath: "/fr-fr/",
Country: "fr",
});
createRedirect({
fromPath: "/",
force: true,
toPath: "/zh-cn/",
Country: "cn",
});
// "Language" must start with a capital letter
createRedirect({
fromPath: "/",
force: true,
toPath: "/fr-fr/",
Language: "fr",
});
createRedirect({
fromPath: "/",
force: true,
toPath: "/zh-cn/",
Language: "zh",
});
const postTemplate = path.resolve(`src/templates/post.js`);
return graphql(`
{
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
limit: 10000
) {
edges {
node {
html
id
excerpt
frontmatter {
categories
date
externalUrl
path
title
}
}
}
}
}
`).then((result) => {
if (result.errors) {
return Promise.reject(result.errors);
}
const { edges } = result.data.allMarkdownRemark;
const allowedCategories = [
"update",
"blog post",
"technology",
"engineering",
"security",
"community",
"external"
];
edges.forEach(({ node }) => {
node.frontmatter.categories.forEach((category) => {
if (!allowedCategories.includes(category.toLowerCase())) {
return Promise.reject(
`Category - ${category} is not allowed in the posts`
);
}
});
});
createPaginatedPages({
createPage,
edges,
pageTemplate: path.resolve(`src/templates/blog.js`),
paginatePost: "/blog", // old field. not remove
pathPrefix: "/blog", // new field. not remove
pageLength: 8,
});
edges.forEach(({ node }) => {
const { path } = node.frontmatter;
if (!path) {
return;
}
createPage({
path: node.frontmatter.path,
component: postTemplate,
context: {},
});
});
const categories = _(edges)
.map((item) => _.get(item, ["node", "frontmatter", "categories"], []))
.flatten()
.map((item) => slugify(item, {lower:true}))
.uniq()
.value();
categories.forEach((category) => {
const filteredEdges = edges.filter(({ node }) =>
node.frontmatter.categories
.map((category) => slugify(category, {lower:true}))
.includes(category)
);
createPaginatedPages({
createPage,
edges: filteredEdges,
pageTemplate: path.resolve(`src/templates/blog.js`),
paginatePost: `/blog/categories/${category}`, // old field. not remove
pathPrefix: `/blog/categories/${category}`, // new field. not remove
pageLength: 8,
});
});
return Promise.resolve();
});
};
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions;
if (node.frontmatter) {
createNodeField({
node,
name: "categories",
value: node.frontmatter.categories || Array.of("*"),
});
createNodeField({
node,
name: "isFeatured",
value: node.frontmatter.isFeatured || false,
});
createNodeField({
node,
name: "ogImageUrl",
value: node.frontmatter.ogImageUrl || "",
});
createNodeField({
node,
name: "externalUrl",
value: node.frontmatter.externalUrl || "",
});
}
};