-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (32 loc) · 1.17 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
import fs from "fs";
import fetch from "node-fetch";
import parser from "xml2json";
const FEED_URL = "https://dharma.hypotheses.org/feed";
const TAG_OPEN = `<!-- FEED-START -->`;
const TAG_CLOSE = `<!-- FEED-END -->`;
const fetchArticles = async () => {
const articles = await fetch(FEED_URL);
const articlesText = await articles.text();
const articlesJSON = parser.toJson(articlesText);
const newC = JSON.parse(articlesJSON).rss.channel.item.slice(0, 5);
return newC.map(({ title, link }) => `<a href="${link}" class="list-group-item list-group-item-action">${title}</a>`).join("\n");
};
async function main() {
const readme = fs.readFileSync("./index.html", "utf8");
const indexBefore = readme.indexOf(TAG_OPEN) + TAG_OPEN.length;
const indexAfter = readme.indexOf(TAG_CLOSE);
const readmeContentChunkBreakBefore = readme.substring(0, indexBefore);
const readmeContentChunkBreakAfter = readme.substring(indexAfter);
const posts = await fetchArticles();
const readmeNew = `
${readmeContentChunkBreakBefore}
${posts}
${readmeContentChunkBreakAfter}
`;
fs.writeFileSync("./index.html", readmeNew.trim());
}
try {
main();
} catch (error) {
console.error(error);
}