forked from quicoto/news-digest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
60 lines (50 loc) · 1.83 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
const fs = require('fs');
let Parser = require('rss-parser');
const templates = require('./templates.js');
let parser = new Parser();
const promises = [];
const sources = JSON.parse(fs.readFileSync('sources.json'));
// Create the requried folders
fs.mkdir(`./dist`, () => {});
function createFile(fileName, data) {
fs.writeFile(fileName, data, (err) => {
if (!err) {
console.log('File created: ' + fileName);
}
});
}
function itemTemplate(item) {
return `<li class="mb-1">
<a rel="noopener" target="_blank" href="${item.link}" title="${item.title}">${item.title}</a>
<time datetime="${new Date(item.pubDate).toLocaleString("fr-FR", {timeZone: "Europe/Paris"})}" class="ps-2 small">${new Date(item.pubDate).toLocaleString("fr-FR", {timeZone: "Europe/Paris"})}</time>
</li>`
}
sources.sections.forEach((section) => {
section.items.forEach((item) => {
promises.push(parser.parseURL(item.url))
});
});
Promise.all(promises).then((feeds) => {
let output = ``;
feeds.forEach((feed) => {
feed.items.sort(function(a, b) {
// Convert the date strings to Date objects
let dateA = new Date(a.pubDate).toLocaleString("fr-FR", {timeZone: "Europe/Paris"});
let dateB = new Date(b.pubDate).toLocaleString("fr-FR", {timeZone: "Europe/Paris"});
// Subtract the dates to get a value that is either negative, positive, or zero
return dateA - dateB;
});
});
feeds.forEach((feed) => {
output += `<section class="row">`;
output += `<div class="col">`;
output += `<h2 class="h3">${feed.title}</h2>`;
output += '<ul class="mb-4">';
output += feed.items.slice(0, 20).map(itemTemplate).join('');
output += '</ul>';
output += `</div>`;
output += `</section>`;
});
output = templates.document(output);
createFile('./dist/index.html', output)
});