feat(template): add atom.art (#167)

* feat(template): add atom.art

* docs(template): add atom.art

* Update template.js
This commit is contained in:
阿卡琳 2018-05-21 14:15:21 +08:00 committed by DIYgod
parent 59c0329d1a
commit 93ff5c61c4
3 changed files with 102 additions and 1 deletions

View File

@ -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

View File

@ -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,
});

70
views/atom.art Normal file
View File

@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<generator>RSSHub</generator>
<webMaster>i@html.love</webMaster>
<language>zh-cn</language>
<id>{{ id || link }}</id>
<title><![CDATA[{{@ title || 'RSSHub' }}]]></title>
{{ if subtitle }}
<subtitle><![CDATA[{{@ subtitle }}]]></subtitle>
{{ /if }}
<updated>{{ updated }}</updated>
<link href="{{ link }}" />
<author>
<name><![CDATA[{{@ author || 'RSSHub' }}]]></name>
</author>
{{if contributor }}
{{ each contributor }}
<contributor>
<name><![CDATA[{{@ $value }}]]></name>
</contributor>
{{ /each }}
{{ /if }}
{{ if icon }}
<icon>{{ icon }}</icon>
{{ /if }}
{{ if logo }}
<logo>{{ logo }}</logo>
{{ /if }}
{{ each item $e }}
<entry>
<id>{{ $e.id || $e.link }}</id>
<title><![CDATA[{{@ $e.title }}]]></title>
<published>{{ $e.published || $e.updated }}</published>
<updated>{{ $e.updated }}</updated>
{{ if $e.author }}
<author>
<name><![CDATA[{{@ $e.author || 'RSSHub' }}]]></name>
</author>
{{ /if }}
<link href="{{ $e.link }}" />
{{ if $e.summary }}
<summary type="{{ $e.summary.type || 'html' }}"><![CDATA[{{@ $e.summary.value || $e.summary }}]]></summary>
{{ /if }}
{{ if $e.content }}
<content type="{{ $e.content.type || 'html' }}" src="{{ $e.content.src || $e.link }}"><![CDATA[{{@ $e.content.value || $e.content }}]]></content>
{{ else if $e.description }}
<content type="html" src="{{ $e.link }}"><![CDATA[{{@ $e.description }}]]></content>
{{ /if }}
{{ if $e.category }}
{{ each $e.category $c }}
<category term="{{ $c }}"></category>
{{ /each }}
{{ /if }}
</entry>
{{ /each }}
</feed>