-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.esm.js
200 lines (193 loc) · 4.78 KB
/
gatsby-node.esm.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
const path = require(`path`)
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
const { is_youtube, get_youtube_id } = require("./src/utils/embed")
exports.createPages = async ({
actions,
store,
cache,
createNodeId,
graphql,
reporter,
}) => {
const { createNode, createPage } = actions
// **Note:** The graphql function call returns a Promise
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise for more info
const result = await graphql(`
query {
bsp: bsp {
allSongs {
title
slug
description
contributors {
name
slug
}
renditions {
contentUrl
prio
}
tags {
id
name
slug
}
excerpts {
id
text
language {
nameEn
}
source {
author
description
excerpts {
id
text
language {
nameEn
}
source {
author
description
}
songs {
id
title
slug
description
languages {
nameEn
code
}
tags {
id
name
slug
}
contributors {
id
slug
name
}
renditions {
contentUrl
}
}
}
}
}
languages {
code
nameEn
}
}
allContributors {
id
slug
}
allLanguages {
id
code
}
allTags {
id
slug
}
}
collections: allFile(
filter: {
sourceInstanceName: { eq: "collections" }
extension: { in: ["md", "mdx"] }
}
) {
nodes {
childMdx {
frontmatter {
slug
}
}
}
}
pages: allFile(
filter: {
sourceInstanceName: { eq: "pages" }
extension: { in: ["md", "mdx"] }
}
) {
nodes {
childMdx {
frontmatter {
slug
}
}
}
}
}
`)
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
// console.log(JSON.stringify(result.data.bsp.allSongs, null, 2));
await Promise.all(
result.data.bsp.allSongs.map(async (song) => {
const youtubePerformances = song.renditions.filter((p) =>
is_youtube(p.contentUrl)
)
let fileNode = null
if (youtubePerformances.length > 0) {
let fileNode = await createRemoteFileNode({
url:
"https://img.youtube.com/vi/" +
get_youtube_id(youtubePerformances[0].contentUrl) +
"/hqdefault.jpg", // string that points to the URL of the image
parentNodeId: null, // id of the parent node of the fileNode you are going to create
createNode, // helper function in gatsby-node to generate the node
createNodeId, // helper function in gatsby-node to generate the node id
cache, // Gatsby's cache
store, // Gatsby's Redux store
ext: ".jpg",
name: song.slug,
})
// console.log(fileNode)
}
// console.log(fileNode)
createPage({
path: `/${song.slug}`,
component: path.resolve(`./src/templates/SongTemplate.js`),
context: {
// songSlug: song.slug,
song: song,
},
})
})
)
const PageTemplate = require.resolve(`./src/templates/PageTemplate.js`)
const CollectionTemplate = require.resolve(
`./src/templates/CollectionTemplate.js`
)
const pageNodes = result.data.pages.nodes
const collectionNodes = result.data.collections.nodes
pageNodes.forEach((node) => {
createPage({
path: node.childMdx.frontmatter.slug,
component: PageTemplate,
context: {
// additional data can be passed via context
slug: node.childMdx.frontmatter.slug,
},
})
})
collectionNodes.forEach((node) => {
createPage({
path: node.childMdx.frontmatter.slug,
component: CollectionTemplate,
context: {
// additional data can be passed via context
slug: node.childMdx.frontmatter.slug,
},
})
})
}