diff --git a/docs/parameter.md b/docs/parameter.md index 2b08bd7d9b..b5e302b554 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 d784548272..aa7390f863 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`); + } + } } } };