feat(middleware): brief parameter (#8074)

Co-authored-by: SettingDust <settingdust@gmail.com>
Co-authored-by: ギャラ <me@gyara.moe>
This commit is contained in:
Toby Tso
2021-09-09 16:37:41 +08:00
committed by GitHub
parent b6771202de
commit e24906fdeb
2 changed files with 31 additions and 0 deletions

View File

@@ -239,6 +239,29 @@ module.exports = async (ctx, next) => {
item.description = simplecc(item.description, ctx.query.opencc);
});
}
// brief
if (ctx.query.brief) {
const num = /[1-9]\d{2,}/;
if (num.test(ctx.query.brief)) {
ctx.query.brief = parseInt(ctx.query.brief);
ctx.state.data.item.forEach((item) => {
let text;
if (item.description) {
text = item.description.replace(/<\/?[^>]+(>|$)/g, '');
}
if (text && text.length) {
if (text.length > ctx.query.brief) {
item.description = `<p>${text.substring(0, ctx.query.brief)}…</p>`;
} else {
item.description = `<p>${text}</p>`;
}
}
});
} else {
throw Error(`Invalid parameter <code>brief=${ctx.query.brief}</code>. Please check the doc https://docs.rsshub.app/parameter.html#shu-chu-jian-xun`);
}
}
}
}
};