forked from pennlabs/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
226 lines (202 loc) · 5.17 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
* Implement Gatsby's Node APIs in this file.
*
* NOTE these files which Gatsby builds on directly do not have TypeScript
* support (hence the use of vanilla JS)
*/
const fetch = require('node-fetch')
const path = require(`path`)
const crypto = require('crypto')
const remark = require('remark')
const html = require('remark-html')
const { paginate } = require('gatsby-awesome-pagination')
const { postsPerPage } = require('./src/constants/blog.ts')
const MemberTemplate = path.resolve(`./src/templates/Member.tsx`)
const ProductTemplate = path.resolve(`src/templates/Product.tsx`)
const TagTemplate = path.resolve(`./src/templates/Tag.tsx`)
const BlogPostTemplate = path.resolve(`./src/templates/BlogPost.tsx`)
const BlogIndexTemplate = path.resolve(`./src/templates/BlogIndex.tsx`)
const markdownProcessor = remark().use(html)
const getHash = jawn =>
crypto
.createHash(`md5`)
.update(JSON.stringify(jawn))
.digest(`hex`)
const createTagPages = (tags, createPage) => {
tags.forEach(({ node }) => {
const totalPosts = node.postCount || 0
// This part here defines, that our tag pages will use
// a `/tag/:slug/` permalink.
node.url = `/blog/tag/${node.slug}/`
// paginate
paginate({
createPage,
items: Array.from({ length: totalPosts }),
itemsPerPage: postsPerPage,
component: TagTemplate,
pathPrefix: ({ pageNumber }) => {
if (pageNumber === 0) {
return `/blog/tag/${node.slug}`
} else {
return `/blog/tag/${node.slug}/page`
}
},
context: {
slug: node.slug,
},
})
})
}
const createPostPages = (posts, createPage) => {
posts.forEach(({ frontmatter: { slug } }) => {
// This part here defines, that our posts will use
// a `/:slug/` permalink.
const url = `blog/${slug}/`
createPage({
path: url,
component: BlogPostTemplate,
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
slug: slug,
},
})
})
}
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type MembersJson implements Node {
team: String
posts: [MarkdownRemark] @link(by: "frontmatter.authors.pennkey", from: "pennkey")
}
type TeamsJson implements Node {
name: String
members: [MembersJson] @link(by: "team", from: "name")
}
type MarkdownRemark implements Node {
frontmatter: Frontmatter
}
type Frontmatter {
authors: [MembersJson] @link(by: "pennkey")
customExcerpt: String
publishedAt: Date @dateformat(formatString: "YYYY-MM-DD")
draft: Boolean
}`
createTypes(typeDefs)
}
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
/**
* Create pages for members
*/
// Retrieve ID's of all team members
const {
data: {
allMembersJson: { edges },
},
} = await graphql(`
query {
allMembersJson {
edges {
node {
id
pennkey
}
}
}
}
`)
await edges.map(({ node: { id, pennkey } }) =>
createPage({
path: `/team/${pennkey}`,
component: MemberTemplate,
context: {
// Data passed to context is available in page queries as GraphQL vars
id,
pennkey,
},
}),
)
/**
* Create pages for products
*/
const {
errors: mdErrors,
data: {
allMarkdownRemark: { edges: products },
},
} = await graphql(`
query {
allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/products/" } }
sort: { order: DESC, fields: [frontmatter___title] }
) {
edges {
node {
fileAbsolutePath
}
}
}
}
`)
if (mdErrors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
products.forEach(({ node }) => {
const { fileAbsolutePath } = node
// Example: /Users/JawnSmith/projects/pennlabs.org/src/markdown/products/penn-mobile.md
// -> 'products/penn-mobile
if (fileAbsolutePath.indexOf('/markdown') === -1) {
return
}
const productPagePath = fileAbsolutePath
.split('/markdown')[1]
.split('.md')[0]
createPage({
path: productPagePath,
component: ProductTemplate,
context: { fileAbsolutePath }, // additional data can be passed via context
})
})
const {
errors: ghostErrors,
data: {
allMarkdownRemark: { nodes: posts },
},
} = await graphql(`
query {
allMarkdownRemark(
filter: {
fileAbsolutePath: { regex: "/blog/" }
frontmatter: { draft: { ne: false } }
}
) {
nodes {
frontmatter {
slug
}
}
}
}
`)
if (ghostErrors) {
throw new Error(ghostErrors)
}
createPostPages(posts, createPage)
// Create pagination for the index page.
paginate({
createPage,
items: posts,
itemsPerPage: postsPerPage,
component: BlogIndexTemplate,
pathPrefix: ({ pageNumber }) => {
if (pageNumber === 0) {
return `/blog`
} else {
return `/blog/page`
}
},
})
}