-
Notifications
You must be signed in to change notification settings - Fork 23
/
gatsby-node.js
117 lines (103 loc) · 2.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
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
const { createRemoteFileNode } = require("gatsby-source-filesystem")
const galleryImages = require("./src/components/home/images/images.json")
const homeBannerImages = require("./src/components/home/nitpBackImages/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),
},
})
})
const HOME_BANNER_NODE_TYPE = "homeBannerImages"
homeBannerImages.forEach((image, index) => {
createNode({
id: createNodeId(`${HOME_BANNER_NODE_TYPE}-${index}`),
imgData: {
id: image,
imgUrl: `http://web.nitp.ac.in/home/images/${image}`,
},
featuredImage: null,
parent: null,
children: [],
internal: {
type: HOME_BANNER_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
}
type homeBannerImages implements Node {
imgData: homeBannerImageData
featuredImg: File @link(from: "fields.localImage")
}
type homeBannerImageData {
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 })
}
}
if (
node.internal.type === "homeBannerImages" &&
node.imgData.imgUrl !== null
) {
const fileNode = await createRemoteFileNode({
url: node.imgData.imgUrl,
parentNodeId: node.id,
createNode,
createNodeId,
getCache,
})
if (fileNode) {
createNodeField({ node, name: "localImage", value: fileNode.id })
}
}
}