-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
40 lines (32 loc) · 1.19 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
const _ = require(`lodash`);
const crypto = require(`crypto`);
async function onCreateNode({ node, boundActionCreators, loadNodeContent }) {
const { createNode } = boundActionCreators;
// Only care source data with json
if (node.sourceInstanceName !== 'data' || node.internal.mediaType !== 'application/json') return;
const content = await loadNodeContent(node);
_.forOwn(JSON.parse(content), (value, key) => {
const JSONArray = value.map((obj, i) => {
const objStr = JSON.stringify(obj);
const contentDigest = crypto.createHash(`md5`).update(objStr).digest(`hex`);
return Object.assign({}, obj, {
id: obj.id ? obj.id : `${key} [${i}] >>> JSON`,
children: [],
parent: node.id,
internal: {
contentDigest,
mediaType: `application/json`,
// TODO make choosing the "type" a lot smarter. This assumes
// the parent node is a file.
// PascalCase
type: _.upperFirst(_.camelCase(`${key} Json`)),
content: objStr
}
});
});
//addNodeToParent({ parent: node, child: j })
_.each(JSONArray, j => createNode(j));
});
return;
}
exports.onCreateNode = onCreateNode;