1
1
import Frisbee from 'frisbee'
2
2
import crypto from 'crypto'
3
3
4
+ const createContentDigest = obj =>
5
+ crypto
6
+ . createHash ( `md5` )
7
+ . update ( JSON . stringify ( obj ) )
8
+ . digest ( `hex` )
9
+
4
10
exports . sourceNodes = async ( {
5
11
boundActionCreators,
6
12
createNodeId
@@ -18,6 +24,8 @@ exports.sourceNodes = async ({
18
24
throw 'You need to set an teamName.'
19
25
}
20
26
27
+ const { createNode } = boundActionCreators
28
+
21
29
const api = new Frisbee ( {
22
30
baseURI : 'https://api.esa.io'
23
31
} )
@@ -34,22 +42,35 @@ exports.sourceNodes = async ({
34
42
} )
35
43
36
44
body . posts . forEach ( post => {
37
- const contentDigest = crypto
38
- . createHash ( `md5` )
39
- . update ( JSON . stringify ( post ) )
40
- . digest ( 'hex' )
45
+ const contentDigest = createContentDigest ( post )
46
+
47
+ const nodeId = createNodeId ( `EsaPost${ post . number } ` )
48
+
49
+ const bodyNode = {
50
+ id : createNodeId ( `EsaPost${ post . number } Body` ) ,
51
+ parent : nodeId ,
52
+ children : [ ] ,
53
+ internal : {
54
+ type : 'EsaPostBody' ,
55
+ mediaType : 'text/markdown' ,
56
+ content : post . body_md ,
57
+ contentDigest : createContentDigest ( post . body_md ) ,
58
+ } ,
59
+ }
41
60
42
- boundActionCreators . createNode ( {
61
+ createNode ( {
43
62
...post ,
63
+ body___NODE : bodyNode . id ,
44
64
relative_category : post . category . replace ( new RegExp ( `${ baseCategory } /?` ) , '' ) ,
45
- id : createNodeId ( `EsaPost ${ post . number } ` ) ,
65
+ id : nodeId ,
46
66
children : [ ] ,
47
- parent : `__SOURCE__` ,
67
+ parent : null ,
48
68
internal : {
49
69
type : 'EsaPost' ,
50
70
contentDigest,
51
71
}
52
72
} )
73
+ createNode ( bodyNode )
53
74
} )
54
75
55
76
next_page = body . next_page
0 commit comments