-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
55 lines (51 loc) · 1.52 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
const fetch = require("node-fetch");
const { JSDOM } = require("jsdom");
const getNodeContent = require("./utils/getNodeContent");
exports.sourceNodes = (
{ actions: { createNode }, createNodeId, createContentDigest },
pluginOptions
) => {
if (!pluginOptions.rss) {
throw Error("Need to provide rss options");
}
return new Promise((res, rej) => {
pluginOptions.rss.forEach(async ({ selectors, name, urlToFetch }) => {
if (!selectors) {
rej(`Need to provide selectors to ${name || "one of the rss"}`);
return;
}
const response = await fetch(urlToFetch);
const body = await response.text();
const dom = new JSDOM(body, {
contentType: "text/xml",
storageQuota: 10000000,
});
const data = selectors.reduce((acc, selector) => {
let HTMLCollection;
if (selector.includes(":")) {
HTMLCollection = dom.window.document.getElementsByTagName(selector);
} else {
HTMLCollection = dom.window.document.querySelectorAll(selector);
}
return {
...acc,
[selector]: Array.from(HTMLCollection).map((node) =>
getNodeContent(node)
),
};
}, {});
createNode({
...data,
id: createNodeId(`${name}`),
parent: null,
children: [],
internal: {
type: name,
content: JSON.stringify(data),
contentDigest: createContentDigest(data),
},
});
res("Node Created");
});
});
};