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

@@ -93,3 +93,11 @@ RSSHub 同时支持 RSS 2.0 和 Atom 输出格式,在路由末尾添加 `.rss`
- RSS 2.0 - <https://rsshub.app/jianshu/home.rss>
- Atom - <https://rsshub.app/jianshu/home.atom>
- 和 filter 或其他 URL query 一起使用 `https://rsshub.app/bilibili/user/coin/2267573.atom?filter=微小微|赤九玖|暴走大事件`
## 输出简讯
可以使用 `brief` 参数输出特定字数 ( ≥ `100` 字 ) 的纯文本内容
举例:
- 输出 100 字简讯: `?brief=100`

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`);
}
}
}
}
};