added feeds

This commit is contained in:
Ellpeck 2019-10-04 23:20:47 +02:00
parent 12d2d2ade4
commit e5e2c126e2
5 changed files with 125 additions and 40 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
node_modules
package-lock.json

View file

@ -148,7 +148,9 @@
<h1>Blog</h1> <h1>Blog</h1>
<p> <p>
Occasionally I enjoy writing stuff. So here's some of the stuff I've written. Just click on any of the headers to expand the post. Occasionally I enjoy writing stuff. So here's some of the stuff I've written. Just click on any of the headers to expand the post.
<br>Alternatively, you can subscribe to this blog using <a href="/rss.xml">RSS</a>, <a href="/atom.xml">Atom</a> or <a href="/feed.json">JSON</a>.
</p> </p>
<div id="blog-list"> <div id="blog-list">
<em>The content that should be here is dynamically generated. Please enable JavaScript if you see this.</em> <em>The content that should be here is dynamically generated. Please enable JavaScript if you see this.</em>
</div> </div>

75
node/rss.js Normal file
View file

@ -0,0 +1,75 @@
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) {
var json = JSON.parse(data);
for (let i = json.length - 1; i >= 0; i--) {
var post = json[i];
var id = post["id"];
var date = new Date(post["date"]);
fs.readFile(__dirname + "/../blog/" + id + ".md", function (_, content) {
var 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);
}
});
}
});
}

6
node/server.js Normal file
View file

@ -0,0 +1,6 @@
const express = require("express");
var app = express();
require("./rss")(app);
require("./sitemap")(app);
app.listen(3000);

View file

@ -1,11 +1,10 @@
const express = require('express');
const { const {
createSitemap createSitemap
} = require('sitemap'); } = require('sitemap');
const fs = require("fs"); const fs = require("fs");
let app = express(); module.exports = function (app) {
app.get('/sitemap.xml', function (_req, res) { app.get('/sitemap.xml', function (_req, res) {
let sitemap = createSitemap({ let sitemap = createSitemap({
hostname: 'https://ellpeck.de', hostname: 'https://ellpeck.de',
urls: [{ urls: [{
@ -34,6 +33,7 @@ app.get('/sitemap.xml', function (_req, res) {
fs.readFile(__dirname + "/../blog/posts.json", function (_, data) { fs.readFile(__dirname + "/../blog/posts.json", function (_, data) {
var json = JSON.parse(data); var json = JSON.parse(data);
for (var post of json) { for (var post of json) {
sitemap.add({ sitemap.add({
url: "/#blog-" + post["id"], url: "/#blog-" + post["id"],
@ -44,5 +44,5 @@ app.get('/sitemap.xml', function (_req, res) {
res.header('Content-Type', 'application/xml'); res.header('Content-Type', 'application/xml');
res.send(sitemap.toXML()); res.send(sitemap.toXML());
}); });
}); });
app.listen(3000); }