-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
68 lines (61 loc) · 1.88 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
const { createRemoteFileNode } = require("gatsby-source-filesystem")
const galleryImages = require("./src/components/home/images/images.json")
exports.sourceNodes = async ({
actions,
createNodeId,
createContentDigest,
}) => {
const { createNode } = actions
const NODE_TYPE = "galleryImage"
galleryImages.forEach((image, index) => {
createNode({
id: createNodeId(`${NODE_TYPE}-${index}`),
imgData: {
id: image,
imgUrl: `https://web.nitp.ac.in/gallery/images/${image}`,
},
featuredImage: null,
parent: null,
children: [],
internal: {
type: NODE_TYPE,
content: JSON.stringify(image),
contentDigest: createContentDigest(image),
},
})
})
}
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
createTypes(`
type galleryImage implements Node {
imgData: galleryImageData
featuredImg: File @link(from: "fields.localImage")
}
type galleryImageData {
id: String
imgUrl: String
}
`)
}
exports.onCreateNode = async ({
node,
actions: { createNode, createNodeField },
createNodeId,
getCache,
}) => {
// For all MarkdownRemark nodes that have a featured image url, call createRemoteFileNode
if (node.internal.type === "galleryImage" && node.imgData.imgUrl !== null) {
const fileNode = await createRemoteFileNode({
url: node.imgData.imgUrl, // string that points to the URL of the image
parentNodeId: node.id, // id of the parent node of the fileNode you are going to create
createNode, // helper function in gatsby-node to generate the node
createNodeId, // helper function in gatsby-node to generate the node id
getCache,
})
// if the file was created, extend the node with "localFile"
if (fileNode) {
createNodeField({ node, name: "localImage", value: fileNode.id })
}
}
}