-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
288 lines (262 loc) · 7.39 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const _ = require("lodash");
const { createFilePath } = require(`gatsby-source-filesystem`);
const util = require("util");
const Twig = require("twig");
var twigDrupal = require('twig-drupal-filters')
const twigBEM = require('bem-twig-extension');
const twigAddAttributes = require('add-attributes-twig-extension');
const yaml = require("js-yaml");
const fs = require("fs");
const readFile = util.promisify(fs.readFile);
const renderTwig = util.promisify(Twig.renderFile);
const IN_PRODUCTION = process.env.NODE_ENV === "production";
twigDrupal(Twig);
twigBEM(Twig);
twigAddAttributes(Twig);
Twig.cache(IN_PRODUCTION);
function relativeDirEq(value) {
return file => file.relativeDirectory === value;
}
/**
* Uses the presence of published md files as a way to decide what assets should show in the styleuide and groups them together.
*/
function createAssetMap(mdFiles, twigFiles, dataFiles, cssFiles, jsFiles) {
const dirs = {};
return mdFiles.reduce((acc, current) => {
const mdParentDir = current.fields.parentDir;
if (!dirs[mdParentDir]) {
dirs[mdParentDir] = true;
return [
...acc,
{
mdFile: current,
// Organize assets that are in the same directory as the published md file
cssFile: cssFiles.find(relativeDirEq(mdParentDir)),
jsFile: jsFiles.find(relativeDirEq(mdParentDir)),
twigFile: twigFiles.find(relativeDirEq(mdParentDir)),
dataFile: dataFiles.find(relativeDirEq(mdParentDir))
}
];
}
return acc;
}, []);
}
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions;
const ComponentPost = require.resolve(`./src/components/Templates/layout.js`);
const IsolatedTwigComponent = require.resolve(
`./src/components/Templates/IsolatedTwigComponent.js`
);
return graphql(`
{
allMdx(
limit: 1000
filter: { frontmatter: { publishToStyleGuide: { eq: true } } }
) {
nodes {
fields {
parentDir
slug
}
frontmatter {
title
description
publishToStyleGuide
}
}
}
twigFiles: allFile(filter: { extension: { eq: "twig" } }) {
nodes {
extension
relativePath
relativeDirectory
absolutePath
# DO NOT REMOVE: ctime is needed to bust gatsby cache for live reloading.
ctime
name
base
}
}
jsFiles: allFile(filter: { extension: { eq: "js" } }) {
nodes {
extension
relativePath
relativeDirectory
# DO NOT REMOVE: ctime is needed to bust gatsby cache for live reloading.
ctime
absolutePath
name
base
}
}
cssFiles: allFile(filter: { extension: { eq: "css" } }) {
nodes {
extension
relativePath
relativeDirectory
# DO NOT REMOVE: ctime is needed to bust gatsby cache for live reloading.
ctime
absolutePath
name
base
}
}
dataFiles: allFile(filter: { extension: { eq: "yml" } }) {
nodes {
extension
relativePath
relativeDirectory
# DO NOT REMOVE: ctime is needed to bust gatsby cache for live reloading.
ctime
absolutePath
name
base
}
}
}
`).then(result => {
if (result.errors) {
throw result.errors;
}
// Create component pages.
const mdFiles = result.data.allMdx.nodes;
const twigComponents = result.data.twigFiles.nodes;
const cssFiles = result.data.cssFiles.nodes;
const dataFiles = result.data.dataFiles.nodes;
const jsFiles = result.data.jsFiles.nodes;
const assetMap = createAssetMap(
mdFiles,
twigComponents,
dataFiles,
cssFiles,
jsFiles
);
mdFiles.forEach(mdFile => {
const asset = assetMap.find(
asset => mdFile.fields.parentDir === asset.mdFile.fields.parentDir
);
const name = asset.twigFile
? asset.twigFile.name.replace(/\s+/g, "-").toLowerCase()
: null;
const iframePath = `${name}-isolated`;
const fileRead = asset.twigFile
? readFile(asset.twigFile.absolutePath)
: Promise.resolve("No Code found");
return fileRead.then(twigCode => {
createPage({
path: mdFile.fields.slug,
component: ComponentPost,
context: {
iframePath,
twigCode: String(twigCode),
slug: mdFile.fields.slug,
collection: mdFile.fields.collection,
parentDir: mdFile.fields.parentDir
}
});
});
});
return Promise.all(
assetMap.map(assets => {
const { twigFile, dataFile, jsFile, cssFile } = assets;
if (twigFile) {
return readFile(dataFile.absolutePath, "utf8").then(yml => {
const data = yaml.safeLoad(yml);
const name = twigFile.name.replace(/\s+/g, "-").toLowerCase();
return createPage({
path: `${name}-isolated`,
component: IsolatedTwigComponent,
context: {
data,
...twigFile,
jsFile,
cssFile,
assetMap
}
});
});
}
return Promise.resolve();
})
);
});
};
exports.onCreateNode = async ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === `Mdx`) {
let value = createFilePath({
node,
getNode
}).toLowerCase();
value = value.replace(/\s+/g, "-").toLowerCase();
createNodeField({
name: `slug`,
node,
value
});
// Get the parent node
const parent = getNode(_.get(node, "parent"));
createNodeField({
node,
name: "collection",
value: _.get(parent, "sourceInstanceName")
});
createNodeField({
node,
name: "parentDir",
value: _.get(parent, "relativeDirectory")
});
}
if (
node.internal.type === "SitePage" &&
node.context &&
node.context.extension === "twig"
) {
const fileReads = [];
if (node.context.absolutePath) {
fileReads.push(renderTwig(node.context.absolutePath, node.context.data));
}
const jsFileReads = [];
node.context.assetMap.forEach(asset => {
if (asset.jsFile) {
jsFileReads.push(readFile(asset.jsFile.absolutePath, "utf8"));
}
});
const jsFiles = await Promise.all(jsFileReads);
if (jsFiles.length) {
fileReads.push(jsFiles.join("\n"));
} else {
fileReads.push("");
}
const cssFileReads = [];
node.context.assetMap.forEach(asset => {
if (asset.cssFile) {
cssFileReads.push(readFile(asset.cssFile.absolutePath, "utf8"));
}
});
const cssFiles = await Promise.all(cssFileReads);
// lol
if (cssFiles.length) {
fileReads.push(cssFiles.join("\n"));
} else {
fileReads.push("");
}
return Promise.all(fileReads).then(([componentHtml, js, css]) => {
createNodeField({
node,
name: "componentHtml",
value: componentHtml
});
createNodeField({
node,
name: "jsCode",
value: js
});
createNodeField({
node,
name: "cssCode",
value: css
});
});
}
};