forked from WebPurple/site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
on-create-node.js
82 lines (74 loc) · 1.85 KB
/
on-create-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
const crypto = require('crypto')
const { transliterate } = require('transliteration')
const { createFilePath } = require('gatsby-source-filesystem')
let onCreateNode = ({
node,
actions: { createNode, createNodeField },
createNodeId,
getNodes,
getNode,
}) => {
let addSlugField = () =>
createNodeField({
name: `slug`,
node,
value: transliterate(createFilePath({ node, getNode })),
})
let createTalkId = talk => createNodeId(talk.title)
switch (node.internal.type) {
case 'MarkdownRemark': // blog post
addSlugField()
break
case 'EventYaml':
addSlugField()
node.talks.forEach(talk =>
createNode({
...talk,
date: node.date,
event: node.id,
id: createTalkId(talk),
parent: node.id,
children: [],
internal: {
type: 'EventTalk',
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(talk))
.digest(`hex`),
},
}),
)
// TODO: remove this mapping
createNodeField({
node,
name: 'talks',
value: node.talks.map(createTalkId),
})
break
case 'SpeakerYaml':
addSlugField()
let userTalks = getNodes()
.filter(n => n.internal.type === 'EventYaml')
.filter(event => event.talks.some(t => t.speaker === node.id))
.reduce(
(talks, event) => [
...talks,
...event.talks
.filter(t => t.speaker === node.id)
.map(createTalkId),
],
[],
)
createNodeField({
node,
name: 'talks',
value: userTalks,
})
createNodeField({
node,
name: 'talksCount',
value: userTalks.length,
})
}
}
module.exports = onCreateNode