-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
185 lines (167 loc) · 4.39 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
const path = require('path')
const createTagPages = (createPage, edges) => {
const tagResultsTemplate = path.resolve(`src/templates/tag-results/index.jsx`)
const tagResults = {}
edges.forEach(({ node }) => {
if (node.frontmatter.tags) {
node.frontmatter.tags.forEach(tag => {
if (!tagResults[tag]) {
tagResults[tag] = []
}
tagResults[tag].push(node)
})
}
})
Object.keys(tagResults).forEach(tagName => {
const tagResult = tagResults[tagName]
createPage({
path: `/tags/${tagName}/`,
component: tagResultsTemplate,
context: {
tagResults,
tagResult,
tag: tagName,
},
})
})
}
const createCategoryPages = (createPage, edges) => {
const categoryResultsTemplate = path.resolve(
`src/templates/category-results/index.jsx`
)
const categoryResults = {}
edges.forEach(({ node }) => {
if (node.frontmatter.category) {
const category = node.frontmatter.category
if (!categoryResults[category]) {
categoryResults[category] = []
}
categoryResults[category].push(node)
}
})
Object.keys(categoryResults).forEach(category => {
const categoryResult = categoryResults[category]
createPage({
path: `/categories/${category}/`,
component: categoryResultsTemplate,
context: {
categoryResults,
categoryResult,
category,
},
})
})
}
const createAuthorPages = (createPage, edges) => {
const authorResultsTemplate = path.resolve(`src/templates/author-results/index.jsx`)
const authorResults = {}
edges.forEach(({ node }) => {
if (node.frontmatter.author) {
const author = node.frontmatter.author
if (!authorResults[author]) {
authorResults[author] = []
}
authorResults[author].push(node)
}
})
Object.keys(authorResults).forEach(author => {
const authorResult = authorResults[author]
createPage({
path: `/authors/${author}/`,
component: authorResultsTemplate,
context: {
authorResults,
authorResult,
author,
},
})
})
}
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage, createRedirect } = actions
const blogPostTemplate = path.resolve(`src/templates/blog-post/index.jsx`)
const result = await graphql(`
{
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
) {
edges {
node {
excerpt(pruneLength: 250)
html
id
timeToRead
frontmatter {
date
path
author
category
tags
title
meta_title
meta_description
the_excerpt
}
}
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
const posts = result.data.allMarkdownRemark.edges
createTagPages(createPage, posts)
createCategoryPages(createPage, posts)
createAuthorPages(createPage, posts)
// Create pages for each markdown file.
posts.forEach(({ node }, index) => {
const prev = index === 0 ? null : posts[index - 1].node
const next = index === posts.length - 1 ? null : posts[index + 1].node
createPage({
path: node.frontmatter.path,
component: blogPostTemplate,
context: {
prev,
next,
},
})
})
// Create Blog pagination
const limit = 4
const totalPosts = posts.length
const numPages = Math.ceil(totalPosts / limit)
Array.from({ length: numPages }).forEach((_, i) => {
const currentPage = i + 1
const previousPageNumber = i === 0 ? null : currentPage - 1
const nextPageNumber = i === numPages - 1 ? null : currentPage + 1
createPage({
path: i === 0 ? `/blog/` : `/blog/${i + 1}/`,
component: path.resolve('./src/templates/blog-list/index.jsx'),
context: {
previousPageNumber,
nextPageNumber,
limit,
skip: i * limit,
totalPosts,
numPages,
currentPage,
},
})
})
// Redirects
createRedirect({
fromPath: '/blog/1/',
toPath: '/blog/',
isPermanent: true,
redirectInBrowser: true,
})
createRedirect({
fromPath: '/blog/1',
toPath: '/blog/',
isPermanent: true,
redirectInBrowser: true,
})
}