-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
160 lines (143 loc) · 3.95 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require('path')
const String = require('./src/utils/string')
exports.onCreateNode = ({ node }) => {
if (node.internal.type === `TalkJson`) {
node.presenter = {
name: node.presenter.name || null,
path: node.presenter.name
? `/speaker/${String.slugify(node.presenter.name)}`
: null,
}
}
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return new Promise(resolve => {
graphql(`
{
allTalkJson {
edges {
node {
description
slug
tag
presenter {
name
}
}
}
}
}
`).then(result => {
// Create speaker pages
Array.from(
result.data.allTalkJson.edges.reduce((acc, edge) => {
if (!edge.node.presenter.name) return acc
const name = edge.node.presenter.name.normalize()
const slug = String.slugify(name)
if (acc.has(slug)) {
const speaker = acc.get(slug)
acc.set(slug, {
count: speaker.count + 1,
name: speaker.name,
slug: speaker.slug,
})
} else {
acc.set(slug, {
count: 1,
name: name,
slug: slug,
})
}
return acc
}, new Map())
)
.reduce((acc, tag) => {
acc.push(tag[1])
return acc
}, [])
// Sort by count DESC
.sort((a, b) => {
if (a.count > b.count) return -1
if (a.count < b.count) return 1
return 0
})
.forEach(speaker => {
const speakerPath = `/speaker/${speaker.slug}`
createPage({
path: speakerPath,
component: path.resolve(`./src/templates/speaker/index.js`),
// Data passed to context is available in page queries as GraphQL variables.
context: {
speaker: speaker.name,
},
})
})
// Create tag pages
Array.from(
result.data.allTalkJson.edges.reduce((acc, edge) => {
edge.node.tag.forEach(tag => {
const name = tag.normalize()
const slug = String.slugify(tag.normalize())
if (acc.has(slug)) {
const tag = acc.get(slug)
acc.set(slug, {
count: tag.count + 1,
name: tag.name,
slug: tag.slug,
})
} else {
acc.set(slug, {
count: 1,
name: name,
slug: slug,
})
}
})
return acc
}, new Map())
)
.reduce((acc, tag) => {
acc.push(tag[1])
return acc
}, [])
// Sort by count DESC
.sort((a, b) => {
if (a.count > b.count) return -1
if (a.count < b.count) return 1
return 0
})
.forEach(tag => {
const tagPath = `/tag/${tag.slug}`
createPage({
path: tagPath,
component: path.resolve(`./src/templates/tag/index.js`),
// Data passed to context is available in page queries as GraphQL variables.
context: {
slug: tag.slug,
tag: tag.name,
},
})
})
// Create talk pages
result.data.allTalkJson.edges.map(({ node }) => {
const talkPath = `/talk/${node.slug}`
createPage({
path: talkPath,
component: path.resolve(`./src/templates/talk/index.js`),
// Data passed to context is available in page queries as GraphQL variables.
context: {
slug: node.slug,
tags: node.tag,
},
})
})
resolve()
})
})
}