diff --git a/docs/README.md b/docs/README.md index bcac04760..6f6f0ac88 100644 --- a/docs/README.md +++ b/docs/README.md @@ -27,6 +27,17 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇 举例: [https://rsshub.app/bilibili/user/coin/2267573?filter=微小微|赤九玖|暴走大事件](https://rsshub.app/bilibili/user/coin/2267573?filter=微小微|赤九玖|暴走大事件) +## RSS2.0 & ATOM + +RSSHub 同时支持 RSS2.0 和 ATOM 输出格式,在路由末尾添加 `.rss` 或 `.atom` 即可请求对应输出格式,缺省为 RSS2.0 + +举例: + +* 缺省 RSS2.0 - [https://rsshub.app/jianshu/home](https://rsshub.app/jianshu/home) +* RSS2.0 - [https://rsshub.app/jianshu/home.rss](https://rsshub.app/jianshu/home.rss) +* ATOM - [https://rsshub.app/jianshu/home.atom](https://rsshub.app/jianshu/home.atom) +* 和 filter 或其他 URL query 一起使用 [https://rsshub.app/bilibili/user/coin/2267573.atom?filter=微小微|赤九玖|暴走大事件](https://rsshub.app/bilibili/user/coin/2267573.atom?filter=微小微|赤九玖|暴走大事件) + ## RSSHub ### 支持的 RSS diff --git a/middleware/template.js b/middleware/template.js index 2d822912f..be13d7c7b 100644 --- a/middleware/template.js +++ b/middleware/template.js @@ -3,10 +3,30 @@ const path = require('path'); const config = require('../config'); module.exports = async (ctx, next) => { + const type = ctx.request.path.match(/\.([a-z]+)$/) || ['', '']; + + switch (type[1]) { + case 'atom': + ctx.request.path = ctx.request.path.slice(0, -5); + ctx.state.template = path.resolve(__dirname, '../views/atom.art'); + + break; + case 'rss': + ctx.request.path = ctx.request.path.slice(0, -4); + ctx.state.template = path.resolve(__dirname, '../views/rss.art'); + + break; + default: + ctx.state.template = path.resolve(__dirname, '../views/rss.art'); + + break; + } + await next(); if (!ctx.body) { - ctx.body = art(path.resolve(__dirname, '../views/rss.art'), { + ctx.body = art(ctx.state.template, { lastBuildDate: new Date().toUTCString(), + updated: new Date().toISOString(), ttl: config.cacheExpire, ...ctx.state.data, }); diff --git a/views/atom.art b/views/atom.art new file mode 100644 index 000000000..4157391dd --- /dev/null +++ b/views/atom.art @@ -0,0 +1,70 @@ + + + RSSHub + i@html.love + zh-cn + {{ id || link }} + <![CDATA[{{@ title || 'RSSHub' }}]]> + + {{ if subtitle }} + + {{ /if }} + + {{ updated }} + + + + + + {{if contributor }} + {{ each contributor }} + + + + {{ /each }} + {{ /if }} + + {{ if icon }} + {{ icon }} + {{ /if }} + + {{ if logo }} + {{ logo }} + {{ /if }} + + {{ each item $e }} + + {{ $e.id || $e.link }} + <![CDATA[{{@ $e.title }}]]> + + {{ $e.published || $e.updated }} + {{ $e.updated }} + + {{ if $e.author }} + + + + {{ /if }} + + + + {{ if $e.summary }} + + {{ /if }} + + {{ if $e.content }} + + {{ else if $e.description }} + + {{ /if }} + + {{ if $e.category }} + {{ each $e.category $c }} + + {{ /each }} + {{ /if }} + + + {{ /each }} + + \ No newline at end of file