-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgatsby-node.js
99 lines (87 loc) · 2.39 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
// You can delete this file if you're not using it
const { createFilePath } = require('gatsby-source-filesystem')
const keyFromAudioFileName = fileName => {
const regex = /\d+/
const match = fileName.match(regex)
return match ? match[0] : fileName
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const slug = node.frontmatter.path || createFilePath({ node, getNode })
const parent = getNode(node.parent)
createNodeField({
name: `slug`,
node: node,
value: slug,
})
createNodeField({
name: `type`,
node: node,
value: parent.sourceInstanceName,
})
}
}
exports.createPages = async ({ graphql, actions: { createPage } }) => {
const {
data: { audioFilesQuery, allMarkdownQuery },
} = await graphql(
`
query {
audioFilesQuery: allFile(
filter: { sourceInstanceName: { eq: "audio" } }
) {
edges {
node {
name
publicURL
}
}
}
allMarkdownQuery: allMarkdownRemark {
edges {
node {
fields {
slug
type
}
}
}
}
}
`
)
const audioEdges = audioFilesQuery ? audioFilesQuery.edges : []
// Transform edges into an array of audio objects.
const allAudioFiles = audioEdges.map(edge => ({
key: keyFromAudioFileName(edge.node.name),
src: edge.node.publicURL,
}))
// Create a home page and add the array of audio objects to its context.
createPage({
path: `/`,
component: require.resolve('./src/templates/home.js'),
context: { allAudioFiles },
})
allAudioFiles.map(audioFile => {
createPage({
path: `${audioFile.key}`,
component: require.resolve('./src/templates/home.js'),
context: { allAudioFiles, selectedKey: audioFile.key },
})
})
// Loop through all the markdown nodes.
// Create a page for each and add the slug to its context.
allMarkdownQuery.edges.map(edge => {
createPage({
path: edge.node.fields.slug,
component: require.resolve('./src/templates/page.js'),
context: { slug: edge.node.fields.slug, allAudioFiles },
})
})
}