-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.js
65 lines (57 loc) · 2 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
const DocumentManager = require('./DocumentManager')
exports.sourceNodes = async (
{ actions, cache, createContentDigest },
configOptions
) => {
const { createNode, touchNode } = actions
delete configOptions.plugins
const format = configOptions.format ? configOptions.format : 'markdown'
const documentManager = new DocumentManager(
configOptions.access_token,
format
)
console.log('\nPulling data from Dropbox Paper...\n')
for (let id of await documentManager.getAll()) {
let meta = await documentManager.getMeta(id)
const postCacheKey = `dropbox-post-for-${id}`
const cachedPaperNode = await cache.get(postCacheKey)
//if the prior node(s) created by this plugin haven't had an update in Dropbox Paper
//touchNode prevents the gatsby cache from garbage collecting the nodes; we don't need to
//fetch the content because nothing is stale.
if (
cachedPaperNode &&
cachedPaperNode.last_updated_date === meta.last_updated_date
) {
touchNode({ nodeId: cachedPaperNode.nodeId })
} else {
let content = await documentManager.getContent(id)
const nodeData = Object.assign(
{
content,
...meta,
},
{
id: id,
parent: null,
children: [],
internal: {
type: `DropboxPaperDocument`,
mediaType: 'text/markdown',
content: content,
contentDigest: createContentDigest(content),
},
}
)
createNode(nodeData)
//use the id from dropbox to set a deterministically referenceable position in the cache
await cache.set(postCacheKey, {
nodeId: id,
last_updated_date: meta.last_updated_date,
contentUpdated: true, //provide flag for any other cached consumers of this data
})
}
}
console.log(
'\nThanks for using the gatsby-source-dropbox-paper plugin. Help make it better by contributing here: https://github.com/alexmacarthur/gatsby-source-dropbox-paper\n'
)
}