-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
211 lines (193 loc) · 5.47 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
206
207
208
209
210
211
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
function urlTransform(text) {
return text.replace(/ /g, '-').replace(/\./g, '').toLowerCase()
}
function createTILPages(graphql, createPage) {
const tilTemplate = path.resolve(`./src/templates/til-per-month.js`)
const tilCategoryTemplate = path.resolve(`./src/templates/til-category.js`)
const tilPage = path.resolve(`./src/templates/til.js`)
return graphql(
`
{
allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
filter: { fields: { type: { eq: "til" } } }
) {
edges {
node {
frontmatter {
category
title
date(formatString: "YYYY-MM-DD")
}
}
}
}
}
`
).then(result => {
if (result.errors) {
throw result.errors
}
const things = result.data.allMarkdownRemark.edges
const dates = [...new Set(things.map(thing => {
const date = thing.node.frontmatter.date
const [year, month] = date.split('-')
return `${year}-${month}`
}))]
dates.forEach((date, index) => {
const [ year, month ] = date.split('-')
const previous = index === dates.length - 1 ? null : dates[index + 1]
const next = index === 0 ? null : dates[index - 1]
createPage({
path: `/til/${year}/${month}`,
component: tilTemplate,
context: {
startInMonth: `${year}-${month}-01`,
endInMonth: `${year}-${month}-31`,
time: date,
previous,
next,
},
})
})
things.forEach((til, index) => {
const { date, title } = til.node.frontmatter
const previous = index === things.length - 1 ? null : {
time: things[index + 1].node.frontmatter.date,
title: things[index + 1].node.frontmatter.title
}
const next = index === 0 ? null : {
time: things[index - 1].node.frontmatter.date,
title: things[index - 1].node.frontmatter.title
}
createPage({
path: `/til/${date}_${title.toLowerCase().replace(/ /g, '-')}`,
component: tilPage,
context: {
time: date,
title,
previous,
next
}
})
})
const categories = [...new Set(things.map(thing => thing.node.frontmatter.category))]
categories.forEach((category) => {
createPage({
path: `/til/category/${category.toLowerCase()}`,
component: tilCategoryTemplate,
context: {
category
}
})
})
})
}
function createPostPages(graphql, createPage) {
const blogPost = path.resolve(`./src/templates/blog-post.js`)
const categoryComponent = path.resolve(`./src/templates/category.js`)
const tagComponent = path.resolve(`./src/templates/tag.js`)
return graphql(
`
{
allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
limit: 1000
filter: { fields: { type: { ne: "til" } } }
) {
edges {
node {
fields {
slug
type
}
frontmatter {
title
category
tags
}
}
}
}
}
`
).then(result => {
if (result.errors) {
throw result.errors
}
// Create blog posts pages.
const posts = result.data.allMarkdownRemark.edges
posts.forEach((post, index) => {
const previous = index === posts.length - 1 ? null : posts[index + 1].node
const next = index === 0 ? null : posts[index - 1].node
createPage({
path: `/blog/post${post.node.fields.slug}`,
component: blogPost,
context: {
slug: post.node.fields.slug,
previous,
next,
},
})
})
// create category pages
const categories = posts.reduce((categorySet, post) => {
categorySet.add(post.node.frontmatter.category)
return categorySet
}, new Set())
Array.from(categories).forEach((cat) => {
createPage({
path: `/blog/category/${urlTransform(cat)}/`,
component: categoryComponent,
context: {
category: cat,
},
})
})
// create tag pages
const tags = posts.reduce((tagSet, post) => {
post.node.frontmatter.tags.split(',').forEach((t) => {
tagSet.add(t.trim())
})
return tagSet
}, new Set())
Array.from(tags).forEach((tag) => {
createPage({
path: `/blog/tag/${urlTransform(tag)}/`,
component: tagComponent,
context: {
tag,
tagPattern: `/(?:^|, )${tag.replace(/\./g, '\\.')}(?:$|,)/i`,
},
})
})
})
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return Promise.all([
createPostPages(graphql, createPage),
createTILPages(graphql, createPage),
])
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const content = path.resolve(__dirname, 'content')
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const relative = path.relative(content, node.fileAbsolutePath)
const folder = relative.split('/')[0]
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
createNodeField({
name: `type`,
node,
value: folder,
})
}
}