This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
205 lines (184 loc) · 6.04 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
const _ = require('lodash')
const moment = require('moment')
const Promise = require('bluebird')
const path = require('path')
const { createFilePath } = require('gatsby-source-filesystem')
const { createPaginationPages } = require('gatsby-pagination')
// Calculate post defaults.
const calculateDefaults = (node, getNode) => {
const defaultSlug = createFilePath({ node, getNode, basePath: `pages` })
const isPostShaped = defaultSlug.match(
/^\/([\d]{4}-[\d]{2}-[\d]{2})-{1}(.+)\/$/
)
if (isPostShaped) {
const [, defaultDate, defaultTitle] = isPostShaped
return [defaultSlug, defaultTitle, defaultDate]
}
const [, defaultTitle] = defaultSlug.match(/^\/(.*)\/$/)
const defaultDate = moment().format('YYYY-MM-DD')
return [defaultSlug, defaultTitle, defaultDate]
}
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
try {
if (node.fields.sourceName !== `comments`) {
const [defaultSlug, defaultTitle, defaultDate] = calculateDefaults(
node,
getNode
)
const date = node.frontmatter.date || defaultDate
const title = defaultTitle
const { categories } = node.frontmatter
const categoriesPath = categories
.join('/')
.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/\s+/g, '-')
.toLowerCase()
const slug = `/${categoriesPath}/${title}/`
createNodeField({ node, name: `slug`, value: slug })
createNodeField({ node, name: `date`, value: date })
}
} catch (ex) {
console.log('Error onCreateNode():', node.fileAbsolutePath, '\n', ex)
throw ex
}
}
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return new Promise((resolve, reject) => {
const indexPage = path.resolve('./src/templates/index.js')
const blogPostTemplate = path.resolve('./src/templates/blog-post.js')
const categoryPageTemplate = path.resolve('./src/templates/categories.js')
const tagPageTemplate = path.resolve('./src/templates/tags.js')
resolve(
graphql(
`
{
allMarkdownRemark(
sort: { fields: [fields___date], order: DESC }
filter: { fields: { sourceName: { ne: "comments" } } }
) {
edges {
node {
id
excerpt(pruneLength: 280)
timeToRead
fields {
slug
date(formatString: "MMMM DD, YYYY")
}
frontmatter {
title
excerpt
categories
tags
image {
path {
childImageSharp {
fluid(maxWidth: 750, quality: 90) {
base64
aspectRatio
src
srcSet
sizes
}
}
}
cover
}
}
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
const posts = result.data.allMarkdownRemark.edges
// Create post pages
_.each(posts, (post, index) => {
const previous =
index === posts.length - 1 ? null : posts[index + 1].node
const next = index === 0 ? null : posts[index - 1].node
createPage({
path: post.node.fields.slug,
component: blogPostTemplate,
context: {
slug: post.node.fields.slug,
previous,
next,
},
})
})
// Create paginated index page
createPaginationPages({
createPage,
edges: posts,
component: indexPage,
limit: 5,
})
const tagSet = new Set()
const tagMap = new Map()
const categorySet = new Set()
const categoryMap = new Map()
posts.forEach(edge => {
if (edge.node.frontmatter.tags) {
edge.node.frontmatter.tags.forEach(tag => {
tagSet.add(tag)
const array = tagMap.has(tag) ? tagMap.get(tag) : []
array.push(edge)
tagMap.set(tag, array)
})
}
if (edge.node.frontmatter.categories) {
edge.node.frontmatter.categories.forEach(category => {
categorySet.add(category)
const array = categoryMap.has(category)
? categoryMap.get(category)
: []
array.push(edge)
categoryMap.set(category, array)
})
}
})
const tagList = Array.from(tagSet)
const categoryList = Array.from(categorySet)
tagList.forEach(tag => {
// Create paginated tag pages
const tagFormatter = tag => route =>
`/tag/${_.kebabCase(tag)}/${route !== 1 ? route : ''}`
createPaginationPages({
createPage,
edges: tagMap.get(tag),
component: tagPageTemplate,
pathFormatter: tagFormatter(tag),
limit: 5,
context: {
tag,
},
})
})
categoryList.forEach(category => {
// Create paginated category pages
const categoryFormatter = category => route =>
`/${_.kebabCase(category)}/${route !== 1 ? route : ''}`
createPaginationPages({
createPage,
edges: categoryMap.get(category),
component: categoryPageTemplate,
pathFormatter: categoryFormatter(category),
limit: 5,
context: {
category,
},
})
})
})
)
})
}