This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
generated from SSWConsulting/SSW.GitHub.Template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
140 lines (123 loc) · 3.25 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
const path = require("path");
const fs = require("fs");
const crypto = require(`crypto`);
const { createRemoteFileNode } = require("gatsby-source-filesystem");
exports.sourceNodes = async ({ actions, createNodeId }) => {
const { createNode } = actions;
let rawdata = fs.readFileSync("ImageToUrlConfig.json");
let imageUrlData = JSON.parse(rawdata);
const turnImageObjectIntoGatsbyNode = (imageObject, imageUrlData) => {
const nodeId = createNodeId(`image-{${imageObject.id}}`);
const nodeContent = JSON.stringify(imageObject);
const nodeContentDigest = crypto
.createHash("md5")
.update(nodeContent)
.digest("hex");
const nodeData = {
...imageObject,
...imageUrlData,
id: nodeId,
parent: null,
children: [],
internal: {
type: "ImageUrls",
content: nodeContent,
contentDigest: nodeContentDigest,
},
};
return nodeData;
};
const createImageObjectFromUrl = (url) => {
const lastIndexOfSlash = url.lastIndexOf("/");
const id = url.slice(lastIndexOfSlash + 1, url.lastIndexOf("."));
return {
id,
image: id,
imageUrl: url,
};
};
imageUrlData.forEach((imageUrlData) => {
const imageObject = createImageObjectFromUrl(imageUrlData.imageUrl);
const nodeData = turnImageObjectIntoGatsbyNode(imageObject, imageUrlData);
createNode(nodeData);
});
};
exports.onCreateNode = async ({
node,
actions,
store,
getCache,
createNodeId,
}) => {
if (node.internal.type === "ImageUrls") {
const { createNode, createNodeField } = actions;
const fileNode = await createRemoteFileNode({
url: node.imageUrl,
parentNodeId: node.id,
store,
getCache,
createNode,
createNodeId,
});
if (fileNode) {
createNodeField({ node, name: "localFile", value: fileNode.id });
}
}
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
createTypes(`
type ImageUrls implements Node {
image: File @link(from: "fields.localFile")
}
`);
};
// const BASE_URL = "/ssw/";
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions;
// query for all Mdx pages
const query = graphql(`
query {
allMdx {
nodes {
fields {
source
}
slug
}
}
}
`);
const consultingTemplate = require.resolve(
"./src/templates/consulting/index.tsx"
);
const legacyTemplate = require.resolve("./src/templates/legacy/index.tsx");
return query.then((result) => {
// filter by source name "content"
const consultingNodes = result.data.allMdx.nodes.filter(
(node) => node.fields.source === "content"
);
// filter by source name "legacy"
const legacyNodes = result.data.allMdx.nodes.filter(
(node) => node.fields.source === "legacy"
);
consultingNodes.forEach((node) => {
createPage({
path: `${node.slug}`,
component: consultingTemplate,
context: {
slug: node.slug,
},
});
});
legacyNodes.forEach((node) => {
createPage({
path: `${node.slug}`,
component: legacyTemplate,
context: {
slug: node.slug,
},
});
});
});
};