Browse Source

Improve feeds

pull/1/head
Yarmo Mackenbach 4 months ago
parent
commit
11b34ddf3e
6 changed files with 97 additions and 27 deletions
  1. 3
      index.js
  2. 45
      routes/feed.js
  3. 19
      routes/rss.js
  4. 26
      server/util.js
  5. 27
      views/feeds.pug
  6. 4
      views/templates/main.pug

3
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')}`)

45
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

19
routes/rss.js

@ -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

26
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 = {

27
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
| )

4
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

Loading…
Cancel
Save