From e24906fdeb4e6bd9ab844aae02da4aff81ce8b63 Mon Sep 17 00:00:00 2001 From: Toby Tso <7851076+tpnonthealps@users.noreply.github.com> Date: Thu, 9 Sep 2021 16:37:41 +0800 Subject: [PATCH] feat(middleware): `brief` parameter (#8074) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: SettingDust Co-authored-by: ギャラ --- docs/parameter.md | 8 ++++++++ lib/middleware/parameter.js | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/docs/parameter.md b/docs/parameter.md index 2b08bd7d9..b5e302b55 100644 --- a/docs/parameter.md +++ b/docs/parameter.md @@ -93,3 +93,11 @@ RSSHub 同时支持 RSS 2.0 和 Atom 输出格式,在路由末尾添加 `.rss` - RSS 2.0 - - Atom - - 和 filter 或其他 URL query 一起使用 `https://rsshub.app/bilibili/user/coin/2267573.atom?filter=微小微|赤九玖|暴走大事件` + +## 输出简讯 + +可以使用 `brief` 参数输出特定字数 ( ≥ `100` 字 ) 的纯文本内容 + +举例: + +- 输出 100 字简讯: `?brief=100` diff --git a/lib/middleware/parameter.js b/lib/middleware/parameter.js index d78454827..aa7390f86 100644 --- a/lib/middleware/parameter.js +++ b/lib/middleware/parameter.js @@ -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 = `

${text.substring(0, ctx.query.brief)}…

`; + } else { + item.description = `

${text}

`; + } + } + }); + } else { + throw Error(`Invalid parameter brief=${ctx.query.brief}. Please check the doc https://docs.rsshub.app/parameter.html#shu-chu-jian-xun`); + } + } } } };