-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
192 lines (191 loc) · 5.36 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require('path')
const { slash } = require('gatsby-core-utils')
const { fmImagesToRelative } = require('gatsby-remark-relative-images')
exports.onCreateNode = ({ node }) => {
fmImagesToRelative(node)
}
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const result = await graphql(
`
{
allInventoryItems(sort: {order: DESC, fields: createdAt}, filter: {id: {ne: "dummy"}}) {
edges {
node {
alternative_id
categories
createdAt
id
images
moreInfoUrl
price
summary
scriptureAddress
slugId
title
}
}
}
allInventoryItemImages: allS3Object(filter: {Key: {regex: "^inventory/items/" }}) {
edges {
node {
id
Key
image: localFile {
publicURL
childImageSharp {
fluid {
base64
src
tracedSVG
}
}
}
}
}
}
allBlogPosts: allMarkdownRemark(sort: {order: DESC, fields: frontmatter___date}, filter: {frontmatter: {published: {eq: true}}}) {
edges {
node {
id
excerpt
html
frontmatter {
title
date
slug
author
coverPhoto
published
tags
}
timeToRead
}
}
}
allBlogPostImages: allS3Object(filter: {Key: {regex: "^post/images/" }}) {
edges {
node {
id
Key
image: localFile {
publicURL
childImageSharp {
fluid {
base64
src
tracedSVG
}
}
}
}
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild("Error while running GraphQL query.")
return
}
// create inventory item s3 key map
const inventoryItemTemplate = path.resolve('src/templates/inventory-item/inventory-item.js')
const authorInventoryItemTemplate = path.resolve('src/templates/author-inventory-item/author-inventory-item.js')
const { allInventoryItems, allInventoryItemImages } = result.data
const s3InventoryItemImagesMap = {}
allInventoryItemImages.edges.forEach( ({ node }) => {
s3InventoryItemImagesMap[node.Key] = node.image
})
const inventoryCategoryNames = {}
// create inventory item posts
allInventoryItems.edges.forEach( edge => {
const { categories, id, images, slugId, title } = edge.node
// construct public inventory item post
const s3ObjectList = images.map( key => s3InventoryItemImagesMap[key] )
createPage({
path: `/i/${slugId}/`,
component: slash(inventoryItemTemplate),
context: {
id,
s3ObjectList
}
})
// construct author inventory item page
createPage({
path: `/author/items/${slugId}/`,
component: slash(authorInventoryItemTemplate),
context: {
id,
allInventoryItems: allInventoryItems.edges,
s3ObjectList
}
})
// collect category names
categories.forEach( category => inventoryCategoryNames[category] = true )
})
// create category pages
const inventoryCategoryTemplate = path.resolve('src/templates/inventory-category/inventory-category.jsx')
const allInventoryCategories = Object.keys(inventoryCategoryNames)
allInventoryCategories.forEach( category => {
const inventoryItems = allInventoryItems.edges.filter( ({ node }) => node.categories.includes(category))
createPage({
path: `/i/category/${category}/`,
component: slash(inventoryCategoryTemplate),
context: {
inventoryItems,
s3ObjectMap: s3InventoryItemImagesMap,
taxonomies: allInventoryCategories
}
})
})
// create blog posts
const blogPostTemplate = path.resolve('src/templates/blog-post/blog-post.jsx')
const { allBlogPosts, allBlogPostImages } = result.data
// create blog post image s3 key map
const s3BlogPostImagesMap = {}
allBlogPostImages.edges.forEach( ({ node }) => {
s3BlogPostImagesMap[node.Key] = node.image
})
const blogTagNames = {}
allBlogPosts.edges.forEach( ({ node }, i) => {
const { excerpt, frontmatter, html, id, timeToRead } = node
// construct blog post
const next = i === allBlogPosts.edges.length - 1 ? null : allBlogPosts.edges[i + 1].node.frontmatter
const previous = i === 0 ? null : allBlogPosts.edges[i - 1].node.frontmatter
createPage({
path: `/blog/${frontmatter.slug}/`,
component: slash(blogPostTemplate),
context: {
excerpt,
html,
id,
next,
previous,
s3ObjectMap: s3BlogPostImagesMap,
timeToRead,
...frontmatter
}
})
// collect tag names
frontmatter.tags.forEach( tag => blogTagNames[tag] = true )
})
// create category pages
const blogTagTemplate = path.resolve('src/templates/blog-tag/blog-tag.jsx')
const allBlogTags = Object.keys(blogTagNames)
allBlogTags.forEach( tag => {
const allPostExcerpts = allBlogPosts.edges.filter( ({ node }) => node.frontmatter.tags.includes(tag))
createPage({
path: `/blog/tag/${tag}/`,
component: slash(blogTagTemplate),
context: {
allPostExcerpts,
s3ObjectMap: s3BlogPostImagesMap,
taxonomies: allBlogTags
}
})
})
}