-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgatsby-node.js
60 lines (53 loc) · 1.45 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
const parser = require('rss-parser');
const crypto = require('crypto');
const createContentDigest = obj => crypto.createHash('md5').update(JSON.stringify(obj)).digest('hex');
function promisifiedParseURL(url) {
return new Promise((resolve, reject) => {
parser.parseURL(url, (err, data) => {
if (err) {
reject(err);
}
resolve(data.feed);
});
});
}
const createChildren = (entries, parentId, createNode) => {
const childIds = [];
entries.forEach(entry => {
childIds.push(entry.link);
const node = Object.assign({}, entry, {
id: entry.link,
title: entry.title,
link: entry.link,
description: entry.description,
parent: parentId,
children: []
});
node.internal = {
type: 'rssFeedItem',
contentDigest: createContentDigest(node)
};
createNode(node);
});
return childIds;
};
async function sourceNodes({ boundActionCreators }, { rssURL }) {
const { createNode } = boundActionCreators;
const data = await promisifiedParseURL(rssURL);
if (!data) {
return;
}
const { title, description, link, entries } = data;
const childrenIds = createChildren(entries, link, createNode);
const feedStory = {
id: link,
title,
description,
link,
parent: null,
children: childrenIds
};
feedStory.internal = { type: 'rssFeed', contentDigest: createContentDigest(feedStory) };
createNode(feedStory);
}
exports.sourceNodes = sourceNodes;