forked from san-diego-tech-hub/sdth-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
201 lines (172 loc) · 4.59 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const { google } = require("googleapis")
const path = require("path")
const remark = require("remark")
const remarkHtml = require("remark-html")
const transformers = require("./src/data/transformers")
const traverseObject = require("./src/utils/traverseObject")
const { GC_ID } = process.env
function authenticate() {
const privateKey = process.env.GC_PRIVATE_KEY.replace(/\\n/g, "\n")
const jwtClient = new google.auth.JWT(process.env.GC_CLIENT_EMAIL, null, privateKey, [
"https://www.googleapis.com/auth/calendar",
])
return new Promise(res => {
jwtClient.authorize(err => {
if (err) {
throw new Error(err)
}
res(jwtClient)
})
})
}
async function getEvents(auth) {
const calendar = google.calendar("v3")
let events
try {
events = await calendar.events.list({
auth,
calendarId: GC_ID,
}).then(resp => resp.data.items)
for (let i = 0; i < events.length; i++) {
const event = events[i]
if (event.recurrence && event.recurrence.length > 0) {
// eslint-disable-next-line
const recurringEvents = await calendar.events.instances({
auth,
calendarId: GC_ID,
eventId: event.id
}).then(resp => resp.data.items)
events = events.concat(recurringEvents)
}
}
} catch (err) {
console.error(err)
return []
}
return events.filter(
event => event.status === "confirmed"
)
}
/**
* Converts markdown string to html string
* @param {String} value
* @returns {String}
*/
function convertMdToHtml(value = "") {
if (typeof value !== "string" || value.trim().length < 1) {
return value
}
return remark()
.use(remarkHtml)
.processSync(value)
.toString()
}
const uniqueEvents = new Set()
exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }) => {
const { createNode } = actions
const jwtClient = await authenticate()
const events = await getEvents(jwtClient)
events.forEach(event => {
const { model, eventKey } = transformers.GoogleCalendarEvent(event)
if (uniqueEvents.has(eventKey)) {
return
}
uniqueEvents.add(eventKey)
const nodeMeta = {
id: createNodeId(`my-data-${event.id}`),
parent: null,
children: [],
internal: {
type: "Event",
content: JSON.stringify(model),
contentDigest: createContentDigest(model),
},
}
const node = Object.assign({}, model, nodeMeta)
createNode(node)
})
}
let id = 0
exports.onCreateNode = ({
actions,
createContentDigest,
createNodeId,
node
}) => {
const { deleteNode, createNode, createParentChildLink } = actions
const otherEvents = ["MeetupEvent", "EventbriteEvents"]
if (otherEvents.includes(node.internal.type)) {
const { model, eventKey } = transformers[node.internal.type](node)
if (uniqueEvents.has(eventKey)) {
deleteNode({ node })
return
}
uniqueEvents.add(eventKey)
const nodeMeta = {
id: createNodeId(`${node.id}-${++id}`),
children: [],
parent: node.id,
internal: {
content: JSON.stringify(model),
contentDigest: createContentDigest(model),
type: "Event",
},
}
const newNode = Object.assign({}, model, nodeMeta)
createNode(newNode)
createParentChildLink({ parent: node, child: newNode })
}
// Convert any Markdown in frontmatter fields to HTML
if (node.internal.type === "MarkdownRemark" && node.frontmatter) {
// eslint-disable-next-line
node.frontmatter = traverseObject(node.frontmatter, convertMdToHtml)
}
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
const template = path.resolve("src/pages/event.js")
return graphql(`
query EVENTS {
allEvent {
edges {
node {
id
title
description
url
start
end
venue {
name
address
}
}
}
}
}
`).then(results => {
if (results.errors) {
throw new Error(results.errors)
}
return results.data.allEvent.edges.map(({ node }) => {
createPage({
path: `/event/${node.id}`,
component: template,
context: { event: node },
})
return null
})
})
}
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
alias: {
Components: `${__dirname}/src/components`,
Common: `${__dirname}/src/components/common`,
Images: `${__dirname}/src/images`,
Utils: `${__dirname}/src/utils`
},
},
})
}