RSS feed in app
This commit is contained in:
+33
-1
@@ -2,6 +2,7 @@ const express = require('express');
|
||||
const app = express();
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const Rss = require('rss');
|
||||
|
||||
/**
|
||||
* Terminal colors and symbols to display status messages
|
||||
@@ -23,6 +24,7 @@ module.exports = (config) => {
|
||||
app.set('views', path.join(__dirname, '..'));
|
||||
|
||||
const articles = {};
|
||||
let lastRSS = '';
|
||||
|
||||
/**
|
||||
* Fetch articles from the data folder and send success as a response
|
||||
@@ -41,6 +43,9 @@ module.exports = (config) => {
|
||||
console.log(cons.ok, `loaded ${nb} article${nb > 1 ? 's' : ''}`);
|
||||
else
|
||||
console.log(cons.warn, `no articles loaded, check your configuration`);
|
||||
|
||||
lastRSS = '';
|
||||
|
||||
callback(true);
|
||||
});
|
||||
};
|
||||
@@ -87,10 +92,37 @@ module.exports = (config) => {
|
||||
if (err)
|
||||
showError(req.path, 404, res);
|
||||
else
|
||||
render(res, homePath, {articles: Object.values(articles)});
|
||||
render(res, homePath, {articles: Object.values(articles).sort((a, b) => ('' + b.path).localeCompare(a.path))});
|
||||
});
|
||||
});
|
||||
|
||||
//RSS endpoint
|
||||
app.get(config['rss']['endpoint'], (req, res) => {
|
||||
if (config['modules']['rss']) {
|
||||
if (!lastRSS) {
|
||||
const feed = new Rss({
|
||||
'title': config['rss']['title'],
|
||||
'description': config['rss']['description'],
|
||||
'feed_url': 'http://' + req.headers.host + req.url,
|
||||
'site_url': 'http://' + req.headers.host
|
||||
});
|
||||
Object.values(articles)
|
||||
.slice(0, config['rss']['length'])
|
||||
.forEach((article) => {
|
||||
feed.item({
|
||||
title: article.title,
|
||||
url: 'http://' + req.headers.host + article.url,
|
||||
date: article.date
|
||||
});
|
||||
});
|
||||
lastRSS = feed.xml();
|
||||
}
|
||||
res.type('rss').send(lastRSS);
|
||||
} else {
|
||||
showError(req.path, 404, res);
|
||||
}
|
||||
});
|
||||
|
||||
// catch all article urls and render them
|
||||
app.get('*', (req, res, next) => {
|
||||
if (/^\/\d{4}\/\d{2}\/\d{2}\/(\w*\/)?$/.test(req.path)) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"node_port": 3000,
|
||||
"data_dir": "data",
|
||||
"view_engine": "ejs",
|
||||
"loopback_url": "http://localhost:3000",
|
||||
"language": "en-us",
|
||||
"modules": {
|
||||
"plantuml": false,
|
||||
"rss": true,
|
||||
|
||||
@@ -87,6 +87,7 @@ module.exports = (config) => {
|
||||
day: parseInt(matches[3])
|
||||
};
|
||||
article.date = new Date(article.year, article.month, article.day);
|
||||
article.date.setUTCHours(0);
|
||||
remaining++;
|
||||
readIndexFile(path.join(article.realPath, config['article']['index']), config['article']['thumbnail_tag'], (err, info) => {
|
||||
if (err)
|
||||
|
||||
-40
@@ -1,40 +0,0 @@
|
||||
const convert = require('xml-js');
|
||||
|
||||
const mapArticle = (url, article) => {
|
||||
return {
|
||||
'title': {'_text': article.title},
|
||||
'link': {'_text': (url + article.url).replace(/([^:])\/\//g, '$1/')},
|
||||
'pubDate': {'_text': article.date.toString()},
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = (config) => {
|
||||
return {
|
||||
get: (dict) => {
|
||||
const items = Object.values(dict)
|
||||
.sort((a, b) => ('' + b.path).localeCompare(a.path))
|
||||
.slice(0, config['rss']['length']);
|
||||
const data = {
|
||||
'_declaration': {
|
||||
'_attributes': {
|
||||
'version': '1.0',
|
||||
'encoding': 'UTF-8'
|
||||
}
|
||||
},
|
||||
'rss': {
|
||||
'_attributes': {
|
||||
'version': '2.0'
|
||||
},
|
||||
'title': {'_text': config['rss']['title']},
|
||||
'description': {'_text': config['rss']['description']},
|
||||
'link': {'_text': config['loopback_url']},
|
||||
'lastBuildDate': {'_text': new Date().toString()},
|
||||
'lastPubDate': {'_text': new Date().toString()},
|
||||
'ttl': {'_text': '60'},
|
||||
'item': items.map((a) => mapArticle(config['loopback_url'], a))
|
||||
}
|
||||
};
|
||||
return convert.js2xml(data, {compact: true});
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user