Skip to content

Commit

Permalink
plugin/rss: multilingual support & remove content cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ntzyz committed Mar 11, 2019
1 parent 457e9bb commit 1580742
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 23 deletions.
2 changes: 1 addition & 1 deletion plugins/rss-feed/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rss-feed",
"version": "1.0.0",
"version": "1.1.0",
"author": {
"name": "ntzyz",
"email": "ntzyz@live.cn"
Expand Down
27 changes: 5 additions & 22 deletions plugins/rss-feed/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ const express = require('express');
const RSS_CACHE_STATUS_HEADER = 'X-RSS-From-Cache';

function installer ({ site, utils, config }) {
let rssCacheContent = null;

function renderXML (posts) {
function renderXML (posts, acceptLanguage) {
let resolve, promise;
promise = new Promise(r => resolve = r);

Expand All @@ -19,8 +17,8 @@ function installer ({ site, utils, config }) {
feedUrl: config.url + '/feeds',
language: config.language,
description: '',
posts: utils.render(posts, { preview: false }),
renderedPosts: utils.render(posts, { preview: true }),
posts: utils.render(posts, { preview: false, acceptLanguage }),
renderedPosts: utils.render(posts, { preview: true, acceptLanguage }),
cdata (text, options) {
return '<![CDATA[' + text.replace(/\]\]>/g, ']]]]><![CDATA[>') + ']]>';
}
Expand All @@ -32,17 +30,6 @@ function installer ({ site, utils, config }) {
return promise;
}

function rssCacheController (req, res, next) {
switch (req.method) {
case 'PUT':
case 'POST':
case 'DELETE':
rssCacheContent = null;
default:
next();
}
}

const router = express.Router();

router.use((req, res, next) => {
Expand All @@ -57,10 +44,7 @@ function installer ({ site, utils, config }) {
});

router.get('/', async (req, res) => {
if (rssCacheContent) {
res.header(RSS_CACHE_STATUS_HEADER, 'true');
return res.send(rssCacheContent);
}
const acceptLanguage = req.query.acceptLanguage || req.headers['accept-language'] || 'zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7,ja;q=0.6';

try {
let cursor = utils.db.conn.collection('posts').find({
Expand All @@ -76,11 +60,10 @@ function installer ({ site, utils, config }) {
message: utils.messages.ERR_MONGO_FAIL
});
}
rssCacheContent = await renderXML(posts);
rssCacheContent = await renderXML(posts, acceptLanguage);
res.send(rssCacheContent);
});

site.use(rssCacheController);
site.use('/feeds', router);
}

Expand Down

0 comments on commit 1580742

Please sign in to comment.