-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
349 lines (318 loc) · 8.29 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// These are duplicated from helper.js because we can't do ES6 imports in gatsby-node.js but we want to do them everywhere else
const toMachineName = (string, space_character) => {
space_character = space_character || '_'
return string
.toLowerCase()
.replace(/[!,.':#?]/g, '')
.replace(/\s/g, space_character)
.replace(/[$]/g, 'z')
}
const path = require('path')
exports.createPages = async ({ graphql, actions }) => {
const result = await graphql(`
{
pages: allMdx(sort: { frontmatter: { date: DESC } }) {
group(field: { fields: { type: SELECT } }) {
fieldValue
nodes {
fields {
slug
type
parentDir
}
internal {
contentFilePath
}
frontmatter {
title
template
tags
gallery
related_gigs
}
}
}
}
allGigs: allGigYaml(sort: { date: DESC }) {
nodes {
title
venue
artists {
name
}
fields {
slug
fileName
type
}
}
}
allVenues: allVenueYaml {
nodes {
title
fields {
slug
fileName
type
}
}
}
allArtists: allArtistYaml {
nodes {
title
fields {
slug
fileName
type
}
}
}
allVaultsessions: allVaultsessionYaml {
nodes {
title
fields {
slug
fileName
type
}
}
}
blogsByTag: allMdx(sort: { frontmatter: { date: DESC } }, filter: { fields: { type: { eq: "blog" } } }) {
group(field: { frontmatter: { tags: SELECT } }) {
fieldValue
totalCount
}
}
}
`)
if (result.errors) {
console.log(result.errors)
}
const { createPage } = actions
const layouts = {
gig: path.resolve('./src/templates/gig/gig.js'),
blog: path.resolve('./src/templates/blog/blog.js'),
artist: path.resolve('./src/templates/artist/artist.js'),
venue: path.resolve('./src/templates/venue/venue.js'),
vaultsession: path.resolve('./src/templates/vaultsession/vaultsession.js'),
page: path.resolve('./src/templates/page/page.js'),
tags: path.resolve('./src/templates/tags/tags.js'),
}
// Markdown pages
const createMdx = (group) => {
group.nodes.forEach((node, index) => {
const previous = index === group.nodes.length - 1 ? null : group.nodes[index + 1]
const next = index === 0 ? null : group.nodes[index - 1]
const context = {
slug: node.fields.slug,
previous,
next,
parentDir: node.fields.parentDir,
title: node.frontmatter.title,
tags: node.frontmatter.tags || [],
related_gigs: node.frontmatter.related_gigs || [],
isGallery: node.frontmatter.gallery ? true : false,
}
let component = layouts[node.fields.type]
if (node.frontmatter.template) component = path.resolve(`./src/templates/${node.frontmatter.template}`)
createPage({
path: node.fields.slug,
component: `${component}?__contentFilePath=${node.internal.contentFilePath}`,
context: context,
})
})
}
// Yaml entities
const createYml = (node, index, nodes) => {
const previous = index === nodes.length - 1 ? null : nodes[index + 1]
const next = index === 0 ? null : nodes[index - 1]
const context = {
previous,
next,
slug: node.fields.slug,
fileName: node.fields.fileName,
type: node.fields.type,
title: node.title,
}
if (node.venue) context.venue = node.venue
if (node.artists) context.artists = node.artists.map((artist) => artist.name)
const component = layouts[node.fields.type]
createPage({
path: node.fields.slug,
component: `${component}`,
context: context,
})
}
// Yaml pages
result.data.allGigs.nodes.forEach(createYml)
result.data.allVenues.nodes.forEach(createYml)
result.data.allArtists.nodes.forEach(createYml)
result.data.allVaultsessions.nodes.forEach(createYml)
// Markdown pages
result.data.pages.group.forEach(createMdx)
result.data.blogsByTag.group.forEach(({ fieldValue }) => {
createPage({
path: `/blog/tags/${fieldValue}`,
component: layouts.tags,
context: {
tag: fieldValue,
},
})
})
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.sourceInstanceName === 'media') {
const pathComponents = node.relativeDirectory.split('/')
const mediaDir = pathComponents[0]
const parentDir = pathComponents.at(-1)
createNodeField({
name: `mediaDir`,
node,
value: mediaDir,
})
createNodeField({
name: `parentDir`,
node,
value: parentDir,
})
// Gig media needs to know the parents parent too
if (mediaDir === 'gig') {
createNodeField({
name: `gigDir`,
node,
value: pathComponents[1],
})
}
} else if (node.internal.type === `Mdx`) {
const parent = getNode(node.parent)
const pathComponents = parent.relativeDirectory.split('/')
const nodeType = pathComponents[0]
const nodeTitle = toMachineName(node.frontmatter.title, '_')
const nodeSlug = '/' + nodeType + '/' + nodeTitle
const parentDir = pathComponents[1]
createNodeField({
name: `slug`,
node,
value: nodeSlug,
})
createNodeField({
name: `type`,
node,
value: nodeType,
})
createNodeField({
name: `parentDir`,
node,
value: parentDir,
})
} else if (node.internal.type.includes(`Yaml`)) {
const parentNode = getNode(node.parent)
const fileName = parentNode.name
const nodeType = node.internal.type.split('Yaml')[0].toLowerCase()
// To preserve URLs from old site
const nodeTitle = nodeType === 'gig' ? toMachineName(node.title, '-') : toMachineName(node.title, '_')
const slugType = nodeType + 's'
const nodeSlug = '/' + slugType + '/' + nodeTitle
createNodeField({
name: `slug`,
node,
value: nodeSlug,
})
createNodeField({
name: `type`,
node,
value: nodeType,
})
createNodeField({
name: `fileName`,
node,
value: fileName,
})
if (nodeType === 'gig') {
const date = new Date(node.date)
const year = date.getFullYear()
const month = date.getMonth()
// So we can group and sort by these
createNodeField({
name: `year`,
node,
value: year,
})
createNodeField({
name: `month`,
node,
value: month,
})
createNodeField({
name: `yearAndMonth`,
node,
value: '' + year + (month < 10 ? '0' + month : month),
})
}
}
}
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type Audioculture {
link: String!
snippet: String!
image: String
}
type Track {
title: String!
time: String!
link: String
}
type GigVid {
link: String!
title: String
}
type GigArtist {
name: String!
vid: [GigVid]
tracklist: [Track]
}
type GigYaml implements Node {
title: String!
venue: String!
artists: [GigArtist!]
description: String
intro: String
feature_vid: String
audioOnly: Boolean
}
type ArtistYaml implements Node {
title: String!
description: String
facebook: String
bandcamp: String
website: String
soundcloud: String
instagram: String
spotify: String
origin: String
soundcloud: String
audioculture: Audioculture
died: Date
}
type VenueYaml implements Node {
title: String!
lat: Float!
lng: Float!
description: String
facebook: String
website: String
died: Date
}
type VaultsessionYaml implements Node {
title: String!
artist: String!
full_video: String!
description: String!
tracklist: [Track!]
}
`
createTypes(typeDefs)
}