From e5e2c126e20e04f1737b655ed74e8b23cc826e65 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 4 Oct 2019 23:20:47 +0200 Subject: [PATCH] added feeds --- .gitignore | 2 ++ index.html | 2 ++ node/rss.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++ node/server.js | 6 ++++ node/sitemap.js | 80 ++++++++++++++++++++++++------------------------- 5 files changed, 125 insertions(+), 40 deletions(-) create mode 100644 .gitignore create mode 100644 node/rss.js create mode 100644 node/server.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..25c8fdb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +package-lock.json \ No newline at end of file diff --git a/index.html b/index.html index b514e07..b9fedc6 100644 --- a/index.html +++ b/index.html @@ -148,7 +148,9 @@

Blog

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. +
Alternatively, you can subscribe to this blog using RSS, Atom or JSON.

+
The content that should be here is dynamically generated. Please enable JavaScript if you see this.
diff --git a/node/rss.js b/node/rss.js new file mode 100644 index 0000000..133a8db --- /dev/null +++ b/node/rss.js @@ -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); + } + }); + } + }); +} \ No newline at end of file diff --git a/node/server.js b/node/server.js new file mode 100644 index 0000000..3b47ce2 --- /dev/null +++ b/node/server.js @@ -0,0 +1,6 @@ +const express = require("express"); + +var app = express(); +require("./rss")(app); +require("./sitemap")(app); +app.listen(3000); \ No newline at end of file diff --git a/node/sitemap.js b/node/sitemap.js index 9756bdc..cb729b3 100644 --- a/node/sitemap.js +++ b/node/sitemap.js @@ -1,48 +1,48 @@ -const express = require('express'); const { createSitemap } = require('sitemap'); const fs = require("fs"); -let app = express(); -app.get('/sitemap.xml', function (_req, res) { - let sitemap = createSitemap({ - hostname: 'https://ellpeck.de', - urls: [{ - url: '/', - priority: 0.8 - }, - { - url: '/#projects', - changefreq: 'monthly' - }, - { - url: '/#social', - changefreq: 'yearly' - }, - { - url: '/#about', - changefreq: 'monthly' - }, - { - url: '/#blog', - changefreq: 'weekly', - priority: 0.6 +module.exports = function (app) { + app.get('/sitemap.xml', function (_req, res) { + let sitemap = createSitemap({ + hostname: 'https://ellpeck.de', + urls: [{ + url: '/', + priority: 0.8 + }, + { + url: '/#projects', + changefreq: 'monthly' + }, + { + url: '/#social', + changefreq: 'yearly' + }, + { + url: '/#about', + changefreq: 'monthly' + }, + { + url: '/#blog', + changefreq: 'weekly', + priority: 0.6 + } + ] + }); + + fs.readFile(__dirname + "/../blog/posts.json", function (_, data) { + var json = JSON.parse(data); + + for (var post of json) { + sitemap.add({ + url: "/#blog-" + post["id"], + priority: 0.4 + }); } - ] - }); - fs.readFile(__dirname + "/../blog/posts.json", function (_, data) { - var json = JSON.parse(data); - for (var post of json) { - sitemap.add({ - url: "/#blog-" + post["id"], - priority: 0.4 - }); - } - - res.header('Content-Type', 'application/xml'); - res.send(sitemap.toXML()); + res.header('Content-Type', 'application/xml'); + res.send(sitemap.toXML()); + }); }); -}); -app.listen(3000); \ No newline at end of file +} \ No newline at end of file