chore: optimize performance (replace he with entities) (#6497)

This commit is contained in:
oppilate
2020-12-22 18:38:10 +00:00
committed by GitHub
parent e735aef8a7
commit dfdb9630b7
5 changed files with 19 additions and 16 deletions

View File

@@ -1,4 +1,4 @@
const he = require('he');
const entities = require('entities');
const mercury_parser = require('@postlight/mercury-parser');
const cheerio = require('cheerio');
const OpenCC = require('opencc');
@@ -24,14 +24,14 @@ module.exports = async (ctx, next) => {
ctx.state.data.item = ctx.state.data.item || [];
// decode HTML entities
ctx.state.data.title && (ctx.state.data.title = he.decode(ctx.state.data.title + ''));
ctx.state.data.description && (ctx.state.data.description = he.decode(ctx.state.data.description + ''));
ctx.state.data.title && (ctx.state.data.title = entities.decodeXML(ctx.state.data.title + ''));
ctx.state.data.description && (ctx.state.data.description = entities.decodeXML(ctx.state.data.description + ''));
// sort items
ctx.state.data.item = ctx.state.data.item.sort((a, b) => +new Date(b.pubDate || 0) - +new Date(a.pubDate || 0));
ctx.state.data.item.forEach((item) => {
item.title && (item.title = he.decode(item.title + ''));
const handleItem = async (item) => {
item.title && (item.title = entities.decodeXML(item.title + ''));
// handle pubDate
if (item.pubDate) {
@@ -111,9 +111,12 @@ module.exports = async (ctx, next) => {
$ele.removeAttr(e);
});
});
item.description = he.decode($('body').html() + '') + (config.suffix || '');
item.description = entities.decodeXML($('body').html() + '') + (config.suffix || '');
}
});
return item;
};
ctx.state.data.item = await Promise.all(ctx.state.data.item.map(handleItem));
if (ctx.query) {
// limit