-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
195 lines (181 loc) · 5.26 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
// Handling multiple layouts
// If you want to use different layouts for different pages, you can pass this information in the context of the pages you create, and then conditionally render in your layout file.
// called after every page is created.
const postsPerPage = 12;
const path = require("path");
const _ = require("lodash");
exports.createResolvers = ({ createResolvers }) => {
const resolvers = {
Mdx: {
relatedPosts: {
type: ['Mdx'],
resolve: async (source, args, context, info) => {
const {entries} = await context.nodeModel.findAll({
query: {
filter: {
id: {
ne: source.id,
},
frontmatter: {
category: {
eq: source.frontmatter.category,
},
},
},
},
type: 'Mdx',
})
return entries;
},
},
},
}
createResolvers(resolvers)
};
const readingTime = require(`reading-time`);
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `Mdx`) {
createNodeField({
node,
name: `timeToRead`,
value: readingTime(node.body)
})
}
}
exports.createPages = async ({actions, graphql, reporter}) => {
const {createPage, createRedirect} = actions;
const tagTemplate = path.resolve("src/templates/tags.js");
const categoryTemplate = path.resolve("src/pages/categories.js");
const authorTemplate = path.resolve("src/templates/authors.js");
const postTemplate = path.resolve("src/templates/post.js");
const result = await graphql(`
{
tagsGroup: allMdx(limit: 2000) {
group(field: {frontmatter: {tags: SELECT}}) {
fieldValue
}
}
categoriesGroup: allMdx(limit: 2000) {
group(field: {frontmatter: {category: SELECT}}) {
fieldValue
edges {
node {
id
frontmatter {
date(formatString: "MMMM D, YYYY")
title
author
category
tags
permalink
thumbnail
}
}
}
}
}
authorsGroup: allMdx(limit: 2000) {
group(field: {frontmatter: {author: SELECT}}) {
fieldValue
}
}
posts: allMdx(sort: {frontmatter: {date: DESC}}) {
edges {
node {
id
frontmatter {
date(formatString: "MMMM D, YYYY")
title
author
category
tags
permalink
}
fields {
slug
timeToRead {
text
}
}
internal {
contentFilePath
}
body
}
}
}
}
`)
// handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
// Extract tag data from query
const tags = result.data.tagsGroup.group
// Make tag pages
tags.forEach(tag => {
createPage({
path: `/tags/${_.kebabCase(tag.fieldValue)}/`,
component: tagTemplate,
context: {
tag: tag.fieldValue,
},
})
})
// Extract category data from query
const categories = result.data.categoriesGroup.group
categories.forEach((category) => {
const numPagesCat = Math.ceil(category.edges.length / postsPerPage);
Array.from({length: numPagesCat}).forEach((_, index) => {
createPage({
path: `/categories/${category.fieldValue}${index === 0 ? `` : `/page/${index + 1}`}`,
component: categoryTemplate,
context: {
limit: postsPerPage,
numPages: numPagesCat,
currentPage: index + 1,
category: category.fieldValue,
skip: index * postsPerPage,
},
});
});
});
// Extract tag data from query
const authors = result.data.authorsGroup.group
// Make tag pages
authors.forEach(author => {
createPage({
path: `/authors/${_.kebabCase(author.fieldValue)}/`,
component: authorTemplate,
context: {
author: author.fieldValue,
},
})
})
const posts = result.data.posts.edges;
const numPages = Math.ceil(posts.length / postsPerPage);
Array.from({length: numPages}).forEach((_, i) => {
createPage({
path: i === 0 ? `/` : `/page/${i + 1}`,
component: categoryTemplate,
context: {
limit: postsPerPage,
skip: i * postsPerPage,
numPages: numPages,
currentPage: i + 1,
},
});
});
posts.forEach(post => {
const permalink = _.kebabCase(post.node.frontmatter.permalink);
createPage({
path: permalink.startsWith('/') ? `/blog${permalink}` : `/blog/${permalink}`,
component: `${postTemplate}?__contentFilePath=${post.node.internal.contentFilePath}`,
context: {
id: post.node.id,
}
})
})
}