-
Notifications
You must be signed in to change notification settings - Fork 6
/
gatsby-node.js
154 lines (132 loc) · 3.91 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
const path = require(`path`)
const fetch = require(`isomorphic-unfetch`)
const Papa = require(`papaparse`)
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
if (stage === `build-html`) {
actions.setWebpackConfig({
module: {
rules: [
{
test: /three.interaction/,
use: loaders.null(),
},
],
},
})
}
}
exports.onCreateNode = ({ node, getNode, actions: { createNodeField } }) => {
if (node.internal.type === `MarkdownRemark`) {
const fileNode = getNode(node.parent)
createNodeField({
node,
name: `code`,
value: fileNode.relativeDirectory
})
createNodeField({
node,
name: `name`,
value: fileNode.name
})
}
}
const fetchEvents = async () => {
const response = await fetch(`https://docs.google.com/spreadsheets/d/e/2PACX-1vTZO-2qE3ZYaJWf68HVCadcJT2hnyb2vdycYu2zYruYbStnPL1zNjvP8Zt7EjE8O2Fc5NXZ2oqjeeRK/pub?gid=1683764614&single=true&output=csv`)
const text = await response.text()
const data = Papa.parse(text).data
const header = data[0]
const events = data.slice(1).map(row => {
let obj = {
id: row[0]
}
header.forEach((key, j) => {
obj[key] = row[j].trim()
if (key === 'Location') {
obj[key] = obj[key].split(',')
}
})
return obj
})
events.sort((a, b) => a['Start'] - b['Start'])
return events
}
const fetchTags = async () => {
const response = await fetch(`https://docs.google.com/spreadsheets/d/e/2PACX-1vTZO-2qE3ZYaJWf68HVCadcJT2hnyb2vdycYu2zYruYbStnPL1zNjvP8Zt7EjE8O2Fc5NXZ2oqjeeRK/pub?gid=824519574&single=true&output=csv`)
const text = await response.text()
const data = Papa.parse(text).data
const headers = data[0]
const tags = {}
headers.map(col => {
tags[col] = {}
})
data.slice(1).map(row => {
headers.map((header, index) => {
if (row[index]) {
const key = row[0].trim()
const value = row[index].trim()
tags[header][key] = value
}
})
})
return tags
}
exports.createPages = async ({ graphql, actions: { createPage } }) => {
const query = await graphql(`
query allDocs {
allMarkdownRemark {
nodes{
id
html
fields {
name
code
}
}
}
}
`)
const docs = {}
query.data.allMarkdownRemark.nodes.forEach(node => {
const { html, fields: { name, code } } = node
if (!docs[name]) {
docs[name] = {}
}
docs[name][code] = html
})
const events = await fetchEvents()
const tagDict = await fetchTags()
const tags = events
.map(item => item['Tags'])
.join()
.split(',')
.filter((value, index, self) => {
value = value.trim()
return (self.indexOf(value) === index) && value.length > 0
})
const countries = events.map(item => item['Country/Region']).filter((value, index, self) => self.indexOf(value) === index)
const context = {
introduce: docs.introduce,
tags,
countries,
events,
tagDict,
}
createPage({
path: `/`,
component: require.resolve(`./src/templates/index.js`),
context: {
...context,
}
})
events.map(event => {
event['Tags'] = event['Tags'].split(',').map(tag => tag.trim())
createPage({
path: `/p/${event.id}`,
component: require.resolve(`./src/templates/index.js`),
context: {
...context,
event,
}
})
})
}