-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
72 lines (61 loc) · 2.09 KB
/
index.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
import "babel-polyfill";
import "./typography";
import Parser from "rss-parser";
import dateFormat from "date-fns/format";
import nextUntil from "./nextUntil";
import overrides from "./overrides";
const isSickPick = header =>
header.innerText
.toLowerCase()
.trim()
.match(/si.*ck.*pi.*ck/);
const rssParser = new Parser();
(async () => {
try {
const feedResponse = await fetch("https://feed.syntax.fm/rss");
const feedString = await feedResponse.text();
const feed = await rssParser.parseString(feedString);
let sickPicks = [];
feed.items.forEach(item => {
const sickPick = document.createElement("div");
const header = document.createElement("h2");
header.innerText = item.title;
sickPick.appendChild(header);
const pubDate = document.createElement("p");
pubDate.innerText = dateFormat(new Date(item.pubDate), "MMMM D, YYYY");
sickPick.appendChild(pubDate);
const domParser = new DOMParser();
const doc = domParser.parseFromString(item.content, "text/html");
const headers = doc.querySelectorAll("h2");
const headerTag = [...headers].find(isSickPick);
if (
headerTag ||
overrides.some(override => override.title === item.title)
) {
let picks;
if (overrides.some(override => override.title === item.title)) {
const override = domParser.parseFromString(
overrides.find(override => override.title === item.title).picks,
"text/html"
);
picks = [...override.body.children];
} else {
picks = nextUntil(headerTag, "h2");
}
picks.forEach(pick => {
if (pick instanceof Node) {
sickPick.appendChild(pick);
}
});
sickPicks.push(sickPick);
}
});
document.querySelector("#loading").style.display = "none";
sickPicks.forEach(sickPick => {
document.body.appendChild(sickPick);
});
} catch (error) {
document.querySelector("#loading").style.display = "none";
document.querySelector("#error").style.display = "block";
}
})();