-
Notifications
You must be signed in to change notification settings - Fork 5
/
gatsby-node.js
179 lines (173 loc) · 4.54 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
// exports.onCreateNode = ({ node }) => {
// console.log(node.internal.type)
// // if (node.internal.type === "JSON") {
// // console.log('JSON!')
// // } else {
// // console.log('not JSON!')
// // }
// }
const { createFilePath } = require('gatsby-source-filesystem')
const path = require('path')
exports.onCreateWebpackConfig = ({
stage,
rules,
loaders,
plugins,
actions,
}) => {
actions.setWebpackConfig({
plugins: [
],
resolve: {
fallback: { canvas: false },
}
})
if (stage === "build-html" || stage === "develop-html") {
actions.setWebpackConfig({
module: {
rules: [
{
test: /ipfs-utils|ipfs-http-client/,
use: loaders.null(),
},
],
},
})
}
}
function isSuperset(set, subset) {
for (let elem of subset) {
if (!set.has(elem)) {
return false
}
}
return true
}
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === 'Json') {
if (node.publication) {
const publication_id = String(node.publication.publication_id)
createNodeField({
node,
name: "slug",
value: `/browse/publication/${publication_id}`,
})
} else if (node.issue) {
const issue_id = String(node.issue.issue_id)
createNodeField({
node,
name: "slug",
value: `/browse/issue/${issue_id}`,
})
}
}
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const publicationTemplate = path.resolve(`src/templates/publication.js`)
const issueTemplate = path.resolve(`src/templates/issue.js`)
// **Note:** The graphql function call returns a Promise
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise for more info
const siteMetadata = await graphql(`
query {
site {
siteMetadata {
targetJournal
}
}
}
`)
const targetJournal = siteMetadata.data.site.siteMetadata.targetJournal
const publications = await graphql(`
{
allJson(filter: {publication:{ journals: {elemMatch: {journal_id: {eq: ${targetJournal}}}}}}) {
edges {
node {
fields {
slug,
}
publication {
publication_id,
}
}
}
}
}
`)
publications.data.allJson.edges.forEach(({ node }, index) => {
let prev_id = null
if (index-1 >= 0) {
prev_id = publications.data.allJson.edges[index-1].node.publication.publication_id
}
let next_id = null
if (index + 1 < publications.data.allJson.edges.length) {
next_id = publications.data.allJson.edges[index+1].node.publication.publication_id
}
createPage({
path: node.fields.slug,
component: publicationTemplate,
context: {
// Data passed to context is available in page queries as GraphQL variables.
slug: node.fields.slug,
cover: `${node.publication.publication_id}/cover.jpeg`,
pub_id: node.publication.publication_id,
prev_id,
next_id,
},
})
})
const journalPublicationIds = new Set(publications.data.allJson.edges.map(({ node }) => {
return node.publication.publication_id
}))
const issues = await graphql(`
{
allJson(
filter: {issue: {issue_id: {gt: 0}}}
sort: {fields: issue___issue_id, order: DESC}
) {
edges {
node {
fields {
slug,
}
issue {
issue_id
publications
}
}
}
}
}
`)
const issuesOfInterest = issues.data.allJson.edges.filter(({ node }) => {
if (node.issue.publications.length < 1) {
return false
}
const issuePublicationIds = new Set(node.issue.publications)
return isSuperset(journalPublicationIds, issuePublicationIds)
})
issuesOfInterest.forEach(({ node }, index) => {
let prev_id = null
if (index-1 >= 0) {
prev_id = issuesOfInterest[index-1].node.issue.issue_id
}
let next_id = null
if (index + 1 < issuesOfInterest.length) {
next_id = issuesOfInterest[index+1].node.issue.issue_id
}
const thumbnails = node.issue.publications.map((p) => `${p}/thumbnail.jpeg`)
createPage({
path: node.fields.slug,
component: issueTemplate,
context: {
// Data passed to context is available in page queries as GraphQL variables.
slug: node.fields.slug,
publications: node.issue.publications,
thumbnails,
prev_id,
next_id,
},
})
})
}