-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
198 lines (181 loc) · 4.71 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
const path = require('path');
const { createFilePath } = require('gatsby-source-filesystem');
const _ = require('lodash');
exports.createPages = async function createPages({
actions: { createPage },
graphql,
reporter,
}) {
const result = await graphql(`
{
posts: allMarkdownRemark(
limit: 1000
filter: { frontmatter: { templateKey: { eq: "_blog-post" } } }
) {
edges {
node {
id
fields {
slug
}
frontmatter {
tags
category
authorFull {
email
}
}
}
}
}
}
`);
if (result.errors) {
result.errors.forEach((e) => console.error(e.toString()));
return reporter.panic(result.errors);
}
const {
posts: { edges: posts },
} = result.data;
// create posts
posts.forEach(({ node }) => {
const { id } = node;
const slug = node.fields.slug;
createPage({
path: slug,
component: path.resolve('src/templates/blog-post.js'),
// additional data can be passed via context
context: {
id,
slug,
},
});
});
createTagPages(posts, createPage);
createCategoryPages(posts, createPage);
createAuthorPages(posts, createPage);
};
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (
node.internal.type === 'MarkdownRemark' &&
node.frontmatter.templateKey === '_blog-post'
) {
const filePath = createFilePath({ node, getNode });
const value = `/blog/${_.kebabCase(node.frontmatter.category)}${filePath}`;
createNodeField({
name: 'slug',
node,
value,
});
}
};
exports.createSchemaCustomization = ({ actions, schema }) => {
const { createTypes } = actions;
const typeDefs = [
`
type MarkdownRemark implements Node @infer {
frontmatter: Frontmatter!
related: [MarkdownRemark]
}
type Frontmatter @infer {
title: String!
date: Date! @dateformat
description: String!
authorFull: AuthorsJson @link(by: "email", from: "author")
featuredPost: Boolean
}
`,
schema.buildObjectType({
name: 'MarkdownRemark',
fields: {
related: {
type: '[MarkdownRemark]',
//The resolve field is called when your page query looks for related posts
//Here we can query our data for posts we deem 'related'
//Exactly how you do this is up to you
//I'm querying purely by category
//But you could pull every single post and do a text match if you really wanted
//(note that might slow down your build time a bit)
//You could even query an external API if you needed
resolve: (source, args, context) => {
const category = source.frontmatter.category;
//If this post has no categories, return an empty array
if (!category || !category.length) {
return [];
}
return context.nodeModel.runQuery({
query: {
filter: {
frontmatter: {
category: { eq: category },
templateKey: { eq: '_blog-post' },
published: { eq: true },
},
id: { ne: source.id },
},
},
type: 'MarkdownRemark',
});
},
},
},
}),
];
createTypes(typeDefs);
};
function createTagPages(posts, createPage) {
const tags = _.uniq(
_.compact(
posts.flatMap((edge) => {
return _.get(edge, 'node.frontmatter.tags');
})
)
);
// Make tag pages
tags.forEach((tag) => {
createPage({
path: `/blog/tags/${_.kebabCase(tag)}`,
component: path.resolve('src/templates/tags.js'),
context: {
tag,
},
});
});
}
function createCategoryPages(posts, createPage) {
const categories = _.uniq(
_.compact(
posts.flatMap((edge) => {
return _.get(edge, 'node.frontmatter.category');
})
)
);
categories.forEach((category) => {
createPage({
path: `/blog/${_.kebabCase(category)}`,
component: path.resolve(`src/templates/category.js`),
context: {
category,
},
});
});
}
function createAuthorPages(posts, createPage) {
const authors = _.uniq(
_.compact(
posts.flatMap((edge) => {
return _.get(edge, 'node.frontmatter.authorFull.email');
})
)
);
authors.forEach((email) => {
createPage({
path: `/blog/authors/${email}`,
component: path.resolve(`src/templates/author.js`),
context: {
author: email,
},
});
});
}