-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
196 lines (177 loc) · 6.19 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
const {resolve} = require("path");
const {createFilePath} = require("gatsby-source-filesystem");
exports.createPages = async ({graphql, actions}) => {
const {createPage, createRedirect} = actions
const siteMetadata = (await graphql(`
{
site {
siteMetadata {
title
social {
twitter
github
}
filteredTags
pagination {
postsPerPage
}
}
}
}
`)).data.site.siteMetadata
const posts = (await graphql(`
{
allMarkdownRemark(
filter: {fields: {slug: {regex: "^/posts/"}}}
sort: { fields: [fields___sortDate], order: DESC }
) {
nodes {
fields {
slug
}
frontmatter {
title
path
tags
redirect_from
}
}
}
}
`)).data.allMarkdownRemark.nodes
const tags = posts
.map(node => node.frontmatter.tags)
.reduce((acc, cur) => ([...acc, ...(
cur
? Array.isArray(cur)
? cur
: [cur]
: []
)]), [])
.reduce((acc, cur) => {
const found = acc.find(({ name }) => (name === cur))
if (found) {
found.count++
return acc
} else {
return [
...acc,
{ name: cur, count: 1, },
]
}
}, [])
const postsPerPage = siteMetadata.pagination.postsPerPage
const createPaginatedPage = ({ basePath, component, limit, skip, maxIndex, currentIndex, context }) => {
const pageParams = {
path: `${basePath}/${currentIndex}`,
component: component,
context: {
limit: limit,
skip: skip,
maxIndex: maxIndex,
currentIndex: currentIndex,
nextPath: currentIndex > 1 ? `${basePath}/${currentIndex - 1}` : null,
prevPath: currentIndex < maxIndex ? `${basePath}/${currentIndex + 1}` : null,
...context,
},
}
createPage(pageParams)
if (currentIndex === 1) {
pageParams.path = `${basePath}/`
createPage(pageParams)
}
}
/* Create Paginated Blog posts */
const createPaginatedBlogPostsAllPage = ({ limit, skip, maxIndex, currentIndex }) => {
createPaginatedPage({
basePath: '',
component: resolve(`./src/templates/PostsAll.js`),
limit: limit,
skip: skip,
maxIndex: maxIndex,
currentIndex: currentIndex,
})
}
const postsAllMaxIndex = Math.ceil(posts.length / postsPerPage)
Array.from({ length: postsAllMaxIndex }).forEach((_, i) => {
createPaginatedBlogPostsAllPage({
limit: postsPerPage,
skip: i * postsPerPage,
maxIndex: postsAllMaxIndex,
currentIndex: i + 1,
})
})
/* Create Paginated Blog posts by Tag */
tags.forEach(({ name }, _) => {
const tag = name
const createPaginatedBlogPostsTagPage = ({ limit, skip, maxIndex, currentIndex }) => {
createPaginatedPage({
basePath: `/tag/${tag}`,
component: resolve(`./src/templates/PostsTagged.js`),
limit: limit,
skip: skip,
maxIndex: maxIndex,
currentIndex: currentIndex,
context: {
tag: tag,
},
})
}
const postsCount = posts
.filter(node => node.frontmatter.tags && node.frontmatter.tags.includes(tag)).length
const postsTagMaxIndex = Math.ceil(postsCount / postsPerPage)
Array.from({ length: postsTagMaxIndex }).forEach((_, i) => {
createPaginatedBlogPostsTagPage({
limit: postsPerPage,
skip: i * postsPerPage,
maxIndex: postsTagMaxIndex,
currentIndex: i + 1,
})
})
})
/* Create Blog post pages */
posts.forEach((post, index) => {
const prev = index === posts.length - 1 ? null : posts[index + 1]
const next = index === 0 ? null : posts[index - 1]
createPage({
path: post.frontmatter.path
? post.frontmatter.path
: post.fields.slug,
component: resolve(`./src/templates/Post.js`),
context: {
slug: post.fields.slug,
nextPath: !next
? null
: next.frontmatter.path
? next.frontmatter.path
: next.fields.slug,
nextTitle: next && next.frontmatter.title,
prevPath: !prev
? null
: prev.frontmatter.path
? prev.frontmatter.path
: prev.fields.slug,
prevTitle: prev && prev.frontmatter.title,
},
})
})
}
exports.onCreateNode = ({node, actions, getNode}) => {
const {createNodeField} = actions
if (node.internal.type === `MarkdownRemark` || node.internal.type === `ImageSharp`) {
const slug = createFilePath({node, getNode})
createNodeField({
node,
name: `slug`,
value: slug,
})
}
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({node, getNode})
createNodeField({
node,
name: `sortDate`,
value: node.frontmatter.modified ?? node.frontmatter.created ?? `1970-01-01T00:00:00.000Z`,
})
}
}