stricter data handling

This commit is contained in:
DIYgod 2018-09-18 19:10:02 +08:00
parent 31a62d2a7d
commit 07e4adcca5
No known key found for this signature in database
GPG Key ID: EC0B76A252D3EF67
1 changed files with 24 additions and 18 deletions

View File

@ -30,25 +30,31 @@ module.exports = async (ctx, next) => {
template = path.resolve(__dirname, '../views/rss.art');
break;
}
// trim title length
ctx.state.data.item &&
ctx.state.data.item.forEach((item) => {
for (let length = 0, i = 0; i < item.title.length; i++) {
length += Buffer.from(item.title[i]).length !== 1 ? 2 : 1;
if (length > config.titleLengthLimit) {
item.title = `${item.title.slice(0, i)}...`;
break;
}
}
});
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.item &&
ctx.state.data.item.forEach((item) => {
item.title && (item.title = he.decode(item.title));
item.description && (item.description = he.decode(item.description));
});
if (ctx.state.data) {
// trim title length
ctx.state.data.item &&
ctx.state.data.item.forEach((item) => {
if (item.title) {
for (let length = 0, i = 0; i < item.title.length; i++) {
length += Buffer.from(item.title[i]).length !== 1 ? 2 : 1;
if (length > config.titleLengthLimit) {
item.title = `${item.title.slice(0, i)}...`;
break;
}
}
}
});
// 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.item &&
ctx.state.data.item.forEach((item) => {
item.title && (item.title = he.decode(item.title));
item.description && (item.description = he.decode(item.description));
});
}
const data = {
lastBuildDate: new Date().toUTCString(),