-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
126 lines (111 loc) · 3.06 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
const crypto = require('crypto')
const path = require('path')
const { getNBAHighlights } = require('./src/data/index.js')
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
/**
* Create a content digest to be used in a new node creation
*/
const createContentDigest = obj =>
crypto
.createHash(`md5`)
.update(JSON.stringify(obj))
.digest(`hex`)
/**
* Helper function for creating a new node
*/
const makeNewNode = async ({ createNode, node, type, description }) => {
const newNode = Object.assign({}, node, {
parent: null,
children: [],
internal: {
type,
...(description && { description }),
},
})
newNode.internal.contentDigest = createContentDigest(newNode)
// Wait for the node to be created before returning
await createNode(newNode)
return newNode
}
/**
* Set the data for Gatsby
* https://www.gatsbyjs.org/docs/node-apis/#sourceNodes
*/
exports.sourceNodes = async ({ actions }) => {
const { createNode, createParentChildLink } = actions
const nbaHighlights = await getNBAHighlights()
// Create nodes for each item in nba highlights
// TODO: Refactor this section to be less about nba highlights
// and more generic for an array of sport types.
nbaHighlights.forEach(async node => {
const gameNode = await makeNewNode({
createNode,
node: {
...node,
highlights: null,
sport: 'nba',
},
type: 'Game',
})
node.highlights.items.forEach(async highlight => {
const highlightNode = await makeNewNode({
createNode,
node: {
...highlight,
id: highlight.id.videoId,
},
type: 'highlight',
description: 'video highlight of sports game',
})
createParentChildLink({
parent: gameNode,
child: highlightNode,
})
})
})
}
/**
* Check for a video/highlight node and create remote file node for
* the video thumbnail
*/
exports.onCreateNode = async ({ node, actions, store, cache }) => {
const { createNode, createParentChildLink } = actions
if (node.internal.type === 'highlight') {
const localImageNode = await createRemoteFileNode({
url: node.snippet.thumbnails.medium.url,
store,
cache,
createNode,
createNodeId: () => `highlight-${node.id}`,
})
createParentChildLink({
parent: node,
child: localImageNode,
internal: {
type: `highlightThumbnail`,
},
})
}
}
/**
* Create a page for highlights by sport
*/
exports.createPages = ({ actions }) => {
const template = path.resolve(`src/templates/highlights-by-sport.js`)
const sports = ['nba']
// For now we only have one sport with highlights, so having index plus
// a named page is overkill. But in the future, the index page will be
// more of an overview and we will want to generate a seperate page per
// sport.
if (sports.length > 1) {
sports.forEach(sport => {
actions.createPage({
path: `/${sport}`,
component: template,
context: {
sport,
},
})
})
}
}