This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.js
157 lines (138 loc) · 3.45 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require(`path`)
const _ = require("lodash")
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions
const blogPostTemplate = path.resolve(`src/templates/post.js`)
const listTemplate = path.resolve("src/templates/list.js")
const result = await graphql(`
{
postsRemark: allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
) {
edges {
node {
fields {
slug
}
frontmatter {
path
}
}
}
}
tagsGroup: allMarkdownRemark(limit: 2000) {
group(field: frontmatter___tags) {
fieldValue
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
const posts = result.data.postsRemark.edges
// Create post detail pages
posts.forEach(({ node }) => {
createPage({
path: node.fields.slug,
component: blogPostTemplate,
})
})
// Extract tag data from query
const tags = result.data.tagsGroup.group
// Make tag pages
tags.forEach(tag => {
createPage({
path: `/${_.kebabCase(tag.fieldValue)}/`,
component: listTemplate,
context: {
tag: tag.fieldValue,
},
})
})
}
// RSS Feed generation
const { createFilePath } = require(`gatsby-source-filesystem`)
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}
}
// ICS generation
const { writeFileSync } = require(`fs`)
exports.onPostBuild = async ({ graphql }) => {
console.log('Generating ICS……');
const ics = require(`ics`)
const moment = require(`moment`)
let events = []
const result = await graphql(`
{
event: allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
) {
edges {
node {
frontmatter {
title
date
endDate
locationName
}
fields {
slug
}
excerpt
}
}
}
siteMeta: site {
siteMetadata {
title
siteUrl
}
}
}
`)
if (result.errors) {
console.log(result.errors)
throw new Error("GraphQL fail, see console output above")
}
result.data.event.edges.forEach(({ node }) => {
let event = {
start: moment(node.frontmatter.date).format('YYYY-M-D-H-m').split("-"),
title: node.frontmatter.title,
description: node.excerpt,
location: node.frontmatter.locationName,
url: result.data.siteMeta.siteMetadata.siteUrl + node.fields.slug,
status: 'CONFIRMED',
}
if(node.frontmatter.endDate) {
event.end = moment(node.frontmatter.endDate).format('YYYY-M-D-H-m').split("-")
} else {
event.duration = { hours: 1, minutes: 0 }
}
events.push(event)
})
ics.createEvents(events, (error, value) => {
if (error) {
console.log(error)
throw new Error("ICS generation fail, see console output above")
}
//console.log(value)
writeFileSync(`${__dirname}/public/events.ics`, value)
})
}