-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.js
43 lines (36 loc) · 1.46 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
const dotCMSApi = require('./dotcms-api');
exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
const { createNode } = actions
// Gatsby adds a configOption that's not needed for this plugin, delete it
delete configOptions.plugins
// Helper function that processes a contentlet to match Gatsby's node structure
const processContentlet = contentlet => {
const nodeId = createNodeId(`dotcms-${contentlet.contentType}-${contentlet.inode}`)
const nodeContent = JSON.stringify(contentlet)
const nodeData = {
...contentlet,
id: nodeId,
parent: null,
children: [],
internal: {
type: `DotCMS${contentlet.contentType}`,
content: nodeContent,
contentDigest: JSON.stringify(contentlet),
},
}
return nodeData
}
// Gatsby expects sourceNodes to return a promise
return (
dotCMSApi.getContentlets(configOptions)
.then(contentlets => {
// Process the response data into a node
contentlets.forEach((contentlet) => {
// Process each contentlet data to match the structure of a Gatsby node
const nodeData = processContentlet(contentlet)
// Use Gatsby's createNode helper to create a node from the node data
createNode(nodeData)
})
})
)
}