forked from jordanoverbye/gatsby-source-are.na
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
173 lines (157 loc) · 4.83 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const { createRemoteFileNode } = require('gatsby-source-filesystem');
const Arena = require('are.na');
exports.sourceNodes = async (helpers, configOptions) => {
// Check the config options
const hasValidConfigOptions = verifyConfigOptions(configOptions);
if (!hasValidConfigOptions) return false;
// Create a new instance of Arena. This is what we will use to get data from are.na
const arena = new Arena({ accessToken: configOptions.accessToken });
await Promise.all(
configOptions.channelSlugs.map(async slug => {
// Get all the data for the channel
const channel = await arena.channel(slug).get({ page: 1, per: 999 });
// Create a node id for the channel. The reason we do this here is so we can pass it as the parent for child nodes.
const channelNodeId = helpers.createNodeId(`arena-channel-${channel.id}`);
// Map over every item in the channel and either return null or the node id of the child.
// We need the child node ids to make a parent/child connection
let channelChildrenNodeIds = await Promise.all(
channel.contents.map(async item => {
if (item.base_class === 'Block') {
return await createArenaBlockNode(item, channelNodeId, helpers);
}
if (item.base_class === 'Channel') {
const innerChannel = await arena
.channel(item.slug)
.get({ page: 1, per: 999 });
const innerChannelNodeId = helpers.createNodeId(
`arena-inner-channel-${item.id}`
);
const innerContents = await Promise.all(
innerChannel.contents.map(async item => {
if (item.base_class === 'Block') {
return await createArenaBlockNode(
item,
innerChannelNodeId,
helpers
);
}
return null;
})
);
const innerChildrenIds = innerContents.filter(i => i);
await createArenaChannelNode(
innerChannelNodeId,
innerChannel,
channelNodeId,
innerChildrenIds,
true,
helpers
);
return innerChannelNodeId;
}
})
)
if (channelChildrenNodeIds !== null) {
channelChildrenNodeIds = channelChildrenNodeIds.filter(i => i);
}
await createArenaChannelNode(
channelNodeId,
channel,
null,
channelChildrenNodeIds,
false,
helpers
);
return null;
})
);
return;
};
function verifyConfigOptions(configOptions) {
if (
!configOptions.accessToken ||
typeof configOptions.accessToken !== 'string'
) {
console.error('Please pass in a valid accessToken to gatsby-source-are.na');
return false;
}
if (
!configOptions.channelSlugs ||
typeof configOptions.channelSlugs !== 'object'
) {
console.error(
'Please pass in at least 1 channelSlug to gatsby-source-are.na'
);
return false;
}
return true;
}
async function createArenaChannelNode(
id,
channel,
parent,
childrenIds,
isInner,
{ actions: { createNode }, createContentDigest }
) {
const channelData = {
channelId: channel.id,
title: channel.title,
created_at: channel.created_at,
updated_at: channel.updated_at,
added_to_at: channel.added_to_at,
published: channel.published,
slug: channel.slug,
status: channel.status,
metadata: channel.metadata,
};
const nodeData = {
id: id,
parent: parent,
children: [...childrenIds],
internal: {
type: isInner ? `ArenaInnerChannel` : `ArenaChannel`,
content: JSON.stringify(channelData),
contentDigest: createContentDigest(channelData),
},
};
await createNode(Object.assign({}, channelData, nodeData));
return null;
}
async function createArenaBlockNode(
block,
parentId,
{ actions: { createNode }, createNodeId, createContentDigest, store, cache }
) {
let fileNode;
if (block.image) {
try {
fileNode = await createRemoteFileNode({
url: block.image.original.url,
store,
cache,
createNode,
createNodeId: id => `ArenaImage-${id}`,
});
} catch (error) {
console.warn('Error creating ArenaImage node', error);
}
}
const blockWithImageNode = Object.assign({}, block, {
image: fileNode,
image___NODE: fileNode ? fileNode.id : null,
});
const nodeId = createNodeId(`arena-block-${block.id}`);
const nodeData = {
id: nodeId,
parent: parentId,
children: null,
internal: {
type: `ArenaBlock`,
content: JSON.stringify(blockWithImageNode),
contentDigest: createContentDigest(blockWithImageNode),
},
};
await createNode(Object.assign({}, blockWithImageNode, nodeData));
return nodeId;
}