Web/node/rss.js
2019-10-04 23:33:45 +02:00

75 lines
2.3 KiB
JavaScript

const {
Feed
} = require("feed");
const fs = require("fs");
const showdown = require("showdown");
const converter = new showdown.Converter({
parseImgDimensions: true
});
module.exports = function (app) {
app.get('/rss.xml', function (_req, res) {
createFeed(function (feed) {
res.header('Content-Type', 'application/xml');
res.send(feed.rss2());
});
});
app.get('/feed.json', function (_req, res) {
createFeed(function (feed) {
res.header('Content-Type', 'application/json');
res.send(feed.json1());
});
});
app.get('/atom.xml', function (_req, res) {
createFeed(function (feed) {
res.header('Content-Type', 'application/xml');
res.send(feed.atom1());
});
})
}
function createFeed(callback) {
const feed = new Feed({
title: "Ellpeck's Blog",
description: "Occasionally I enjoy writing stuff. So here's some of the stuff I've written about gaming, programming and life.",
id: "https://ellpeck.de",
link: "https://ellpeck.de",
image: "https://ellpeck.de/res/logo.png",
favicon: "https://ellpeck.de/favicon.ico",
feedLinks: {
json: "https://ellpeck.de/feed.json",
atom: "https://ellpeck.de/atom.xml"
},
author: {
name: "Ellpeck",
email: "me@ellpeck.de",
link: "https://ellpeck.de"
}
});
fs.readFile(__dirname + "/../blog/posts.json", function (_, data) {
let json = JSON.parse(data);
for (let i = json.length - 1; i >= 0; i--) {
let post = json[i];
let id = post["id"];
let date = new Date(post["date"]);
fs.readFile(__dirname + "/../blog/" + id + ".md", function (_, content) {
let html = converter.makeHtml(content.toString());
feed.addItem({
title: post["name"],
id: id,
link: "https://ellpeck.de/#blog-" + id,
description: post["summary"],
content: html,
date: date,
published: date
});
if (i == 0) {
callback(feed);
}
});
}
});
}