-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.ts
252 lines (240 loc) · 5.59 KB
/
gatsby-node.ts
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//https://www.gatsbyjs.com/docs/how-to/custom-configuration/typescript/#gatsby-nodets
//https://dev.to/skil3e/how-to-use-gatsby-with-typescript-2d79
import type { GatsbyNode } from "gatsby"
import * as path from "path"
import { createSearchIndexFromGraphQl } from "./src/util/search"
import * as fs from 'node:fs/promises';
let miniatureObjectList: MiniatureGraphQLItem[];
export const createPages: GatsbyNode["createPages"] = async ({
graphql,
actions
}) => {
const { createPage } = actions
/* Standard pages */
const standardTemplate = path.resolve(`src/templates/standard.tsx`);
const standardPageResult = await graphql(`
query {
allFile(filter: {sourceInstanceName: {eq: "standard"}}) {
nodes {
childMarkdownRemark {
frontmatter {
slug
title
}
}
}
}
}
`)
//@ts-ignore
standardPageResult.data.allFile.nodes.forEach((node: any) => {
const { childMarkdownRemark: { frontmatter } } = node;
const page = {
path: frontmatter.slug == 'home' ? '/' : frontmatter.slug,
component: standardTemplate,
context: {
title: frontmatter.title,
slug: frontmatter.slug
},
}
createPage(page)
})
/* Blog pages */
const blogTemplate = path.resolve(`src/templates/blog.js`);
const blogPageResult = await graphql(`
query {
allFile(filter: {sourceInstanceName: {eq: "blog"}}) {
nodes {
childMarkdownRemark {
frontmatter {
slug
title
}
}
}
}
}
`)
//@ts-ignore
blogPageResult.data.allFile.nodes.forEach((node: any) => {
const { childMarkdownRemark: { frontmatter } } = node;
const page = {
path: frontmatter.slug,
component: blogTemplate,
context: {
title: frontmatter.title,
slug: frontmatter.slug
},
}
createPage(page)
})
/* Object pages */
const objectTemplate = path.resolve(`src/templates/object.tsx`)
const objectTemplateResult = await graphql(`
query {
directusgraphql {
miniatures {
id
title
collection {
id
name
}
accession_number
production_date_text
artist_text
artist_reference {
id
forename
surname
}
sitter_text
sitter_reference {
id
forename
surname
}
description_content
description_physical
monogram
dimensions_unframed_width
dimensions_unframed_height
pigments_background
pigments_costume
pigments_flesh_tones_and_lips
pigments_hair_and_beard
pigments_jewellery
materials_supports
analytical_techniques_used
exhibitions {
id
exhibitions_id {
id
name
start_date
end_date
url
}
}
references {
id
references_id {
authors {
authors_and_editors_id {
display_name
id
}
}
display_title
id
publication_year
url
}
}
image_normal_light {
id
title
filename_disk
filename_download
}
image_raking_light {
id
title
filename_disk
filename_download
}
image_infrared {
id
title
filename_disk
filename_download
}
image_uv {
id
title
filename_disk
filename_download
}
image_xray {
id
title
filename_disk
filename_download
}
image_verso {
id
title
filename_disk
filename_download
}
images_micrographs {
id
file_name
hotspot
description
micrograph {
id
}
}
slug
object_record_in_collection
Credit
images_ma_xrf_scans {
element_abbreviation
element_investigated
id
ma_xrf_scan {
id
title
filename_disk
filename_download
description
}
}
}
}
}`,
{});
//@ts-ignore
miniatureObjectList = objectTemplateResult.data.directusgraphql.miniatures;
miniatureObjectList.forEach(miniatureObject => {
createPage({
path: 'object/' + miniatureObject.slug,
component: objectTemplate,
context: {
...miniatureObject,
//@ts-ignore
collection: miniatureObject.collection.name
},
})
});
}
export const onCreatePage: GatsbyNode["onCreatePage"] = async ({
page,
actions
}) => {
const { createPage } = actions;
let context = {
...page.context
}
if (page.path == "/collections/") {
const miniaturesMap: { [id: number]: MiniatureGraphQLItem } = {};
miniatureObjectList.forEach(item => {
miniaturesMap[parseInt(item.id)] = {
...item,
//@ts-ignore
collection: item.collection.name
};
})
const searchIndex = JSON.stringify(createSearchIndexFromGraphQl(miniatureObjectList));
await fs.writeFile('static/search-index.json', searchIndex);
context = {
...page.context,
miniatures: miniaturesMap
}
}
createPage({
...page,
context
});
}