diff --git a/index.js b/index.js index 038255c..5fdf6d3 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,8 @@ app.set('port', process.env.PORT || 3000) app.use('/', require('./routes/main')) app.use('/', require('./routes/static')) -app.use('/rss', require('./routes/rss')) +app.use('/feed', require('./routes/feed')) +app.use('/rss', require('./routes/feed')) app.listen(app.get('port'), () => { console.log(`Node server listening at http://localhost:${app.get('port')}`) diff --git a/routes/feed.js b/routes/feed.js new file mode 100644 index 0000000..edc98a0 --- /dev/null +++ b/routes/feed.js @@ -0,0 +1,45 @@ +const router = require('express').Router() +const fs = require('fs') +const _ = require('lodash') +const util = require('../server/util') + +router.get('/all', async (req, res) => { + res.setHeader('Content-Type', 'application/rss+xml') + res.end(await util.getRSS({ channel: 'all', format: 'rss' })) +}) +router.get('/all.atom', async (req, res) => { + res.setHeader('Content-Type', 'application/atom+xml') + res.end(await util.getRSS({ channel: 'all', format: 'atom' })) +}) +router.get('/all.json', async (req, res) => { + res.setHeader('Content-Type', 'application/feed+json') + res.end(await util.getRSS({ channel: 'all', format: 'json' })) +}) + +router.get('/blog', async (req, res) => { + res.setHeader('Content-Type', 'application/rss+xml') + res.end(await util.getRSS({ include: ['blog'], channel: 'blog', format: 'rss' })) +}) +router.get('/blog.atom', async (req, res) => { + res.setHeader('Content-Type', 'application/atom+xml') + res.end(await util.getRSS({ include: ['blog'], channel: 'blog', format: 'atom' })) +}) +router.get('/blog.json', async (req, res) => { + res.setHeader('Content-Type', 'application/feed+json') + res.end(await util.getRSS({ include: ['blog'], channel: 'blog', format: 'json' })) +}) + +router.get('/notes', async (req, res) => { + res.setHeader('Content-Type', 'application/rss+xml') + res.end(await util.getRSS({ include: ['notes'], channel: 'notes', format: 'rss' })) +}) +router.get('/notes.atom', async (req, res) => { + res.setHeader('Content-Type', 'application/atom+xml') + res.end(await util.getRSS({ include: ['notes'], channel: 'notes', format: 'atom' })) +}) +router.get('/notes.json', async (req, res) => { + res.setHeader('Content-Type', 'application/feed+json') + res.end(await util.getRSS({ include: ['notes'], channel: 'notes', format: 'json' })) +}) + +module.exports = router diff --git a/routes/rss.js b/routes/rss.js deleted file mode 100644 index ffc9ee5..0000000 --- a/routes/rss.js +++ /dev/null @@ -1,19 +0,0 @@ -const router = require('express').Router() -const fs = require('fs') -const _ = require('lodash') -const util = require('../server/util') - -router.get('/all', async (req, res) => { - res.setHeader('Content-Type', 'application/xml') - res.end(await util.getRSS()) -}) -router.get('/blog', async (req, res) => { - res.setHeader('Content-Type', 'application/xml') - res.end(await util.getRSS({ include: ['blog'] })) -}) -router.get('/notes', async (req, res) => { - res.setHeader('Content-Type', 'application/xml') - res.end(await util.getRSS({ include: ['notes'] })) -}) - -module.exports = router diff --git a/server/util.js b/server/util.js index 9c622fa..94ba534 100644 --- a/server/util.js +++ b/server/util.js @@ -110,8 +110,8 @@ const getWebmentions = async (url) => { const getRSS = async (opts) => { if (!opts) { opts = {} } opts.include = 'include' in opts ? opts.include : ['blog', 'notes'] - // opts.excludeBlogPosts = 'excludeBlogPosts' in opts ? opts.excludeBlogPosts : false - // opts.excludeNotes = 'excludeNotes' in opts ? opts.excludeNotes : false + opts.channel = 'channel' in opts ? opts.channel : 'all' + opts.format = 'format' in opts ? opts.format : 'rss' const feed = new Feed({ title: "Yarmo's blog and notes", @@ -122,6 +122,11 @@ const getRSS = async (opts) => { favicon: "https://yarmo.eu/favicon.png", copyright: "All rights reserved 2020 Yarmo Mackenbach", updated: new Date(Date.now()), + feedLinks: { + rss: `https://yarmo.eu/feed/${opts.channel}`, + json: `https://yarmo.eu/feed/${opts.channel}.json`, + atom: `https://yarmo.eu/feed/${opts.channel}.atom` + }, author: { name: "Yarmo Mackenbach", email: "yarmo@yarmo.eu", @@ -165,7 +170,22 @@ const getRSS = async (opts) => { }) }) - return feed.rss2() + let response + switch (opts.format) { + case 'rss': + case 'xml': + default: + response = feed.rss2() + break; + case 'atom': + response = feed.atom1() + break; + case 'json': + response = feed.json1() + break; + } + + return response } module.exports = { diff --git a/views/feeds.pug b/views/feeds.pug index 369358d..0a19763 100644 --- a/views/feeds.pug +++ b/views/feeds.pug @@ -13,8 +13,29 @@ block content main ul li - a(href="/rss/all") RSS blog and notes + p + | Feed for blog posts and notes ( + a(href="/feed/all") RSS + | - + a(href="/feed/all.atom") ATOM + | - + a(href="/feed/all.") JSON + | ) li - a(href="/rss/blog") RSS blog + p + | Feed for blog posts ( + a(href="/feed/blog") RSS + | - + a(href="/feed/blog.atom") ATOM + | - + a(href="/feed/blog.json") JSON + | ) li - a(href="/rss/notes") RSS notes + p + | Feed for notes ( + a(href="/feed/notes") RSS + | - + a(href="/feed/notes.atom") ATOM + | - + a(href="/feed/notes.json") JSON + | ) diff --git a/views/templates/main.pug b/views/templates/main.pug index 2e92d56..301b14e 100644 --- a/views/templates/main.pug +++ b/views/templates/main.pug @@ -9,7 +9,9 @@ html link(rel="stylesheet", href="/static/norm.css") link(rel="stylesheet", href="/static/style.css") link(rel="shortcut icon", href="/favicon.png") - link(rel="alternate", href="/rss/all", title="RSS blog feed for yarmo.eu", type="application/rss+xml") + link(rel="alternate", href="/feed/all", title="RSS blog feed for yarmo.eu", type="application/rss+xml") + link(rel="alternate", href="/feed/all.atom", title="RSS blog feed for yarmo.eu", type="application/atom+xml") + link(rel="alternate", href="/feed/all.json", title="RSS blog feed for yarmo.eu", type="application/feed+json") link(rel="webmention", href="https://webm.yarmo.eu/receive") script(asyc, defer, data-domain="yarmo.eu", src="https://plausible.io/js/plausible.js") body