-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-node.js
47 lines (41 loc) · 1.27 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
const toml = require(`toml`)
const _ = require(`lodash`)
const crypto = require(`crypto`)
async function onCreateNode({ node, actions, loadNodeContent, createNodeId }) {
const { createNode, createParentChildLink } = actions
// Filter out non-toml content
// Currently TOML files are considered null in 'mime-db'
// Hence the extension test instead of mediaType test
if (node.extension !== `toml`) {
return
}
// Load TOML contents
const content = await loadNodeContent(node)
// Parse
const parsedContent = toml.parse(content)
// This version suffers from:
// 1) More TOML files -> more types
// 2) Different files with the same name creating conflicts
const parsedContentStr = JSON.stringify(parsedContent)
const contentDigest = crypto
.createHash(`md5`)
.update(parsedContentStr)
.digest(`hex`)
const newNode = {
...parsedContent,
id: parsedContent.id
? parsedContent.id
: createNodeId(`${node.id} >>> TOML`),
children: [],
parent: node.id,
internal: {
contentDigest,
// Use the relative filepath as "type"
type: _.upperFirst(_.camelCase(node.relativePath)),
},
}
createNode(newNode)
createParentChildLink({ parent: node, child: newNode })
return
}
exports.onCreateNode = onCreateNode