feat: RSS feed

This commit is contained in:
curben 2019-08-12 20:32:22 +09:30
parent 93a2494d0a
commit 801392b35d
No known key found for this signature in database
GPG Key ID: 5D9DB57A25D34EE3
5 changed files with 152 additions and 1 deletions

View File

@ -88,6 +88,9 @@ deploy:
meta_generator: false
feed:
icon: 'svg/rss.svg'
# theme config
theme_config:
# Lang
@ -104,7 +107,7 @@ theme_config:
# Your Feed Location
# https://github.com/hexojs/hexo-generator-feed
#rss: /atom.xml
rss: /atom.xml
# Profile
nickname: Curben's blog

View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>{{ config.title }}</title>
{% if icon %}<icon>{{ icon }}</icon>{% endif %}
{% if config.subtitle %}<subtitle>{{ config.subtitle }}</subtitle>{% endif %}
<link href="{{ feed_url | uriencode }}" rel="self"/>
{% if config.feed.hub %}<link href="{{ config.feed.hub | uriencode }}" rel="hub"/>{% endif %}
<link href="{{ url }}"/>
<updated>{{ posts.first().date | date }}</updated>
<id>{{ url }}</id>
{% if config.author %}
<author>
<name>{{ config.author }}</name>
{% if config.email %}<email>{{ config.email }}</email>{% endif %}
</author>
{% endif %}
<generator uri="http://hexo.io/">Hexo</generator>
{% for post in posts.toArray() %}
<entry>
<title>{{ post.title }}</title>
<link href="{{ url }}{{ post.path | uriencode }}"/>
<id>{{ url }}{{ post.path }}</id>
<published>{{ post.date | date }}</published>
<updated>{% if post.lastUpdated %}{{ post.lastUpdated | date }}{% else %}{{ post.date | date }}{% endif %}</updated>
{% if config.feed.content and post.content %}
<content type="html"><![CDATA[{{ post.more | noControlChars | safe }}]]></content>
{% endif %}
<summary type="html">
{% if post.description %}
{{ post.description }}
{% elif post.intro %}
{{ post.intro }}
{% elif post.excerpt %}
{{ post.excerpt }}
{% elif post.content %}
{% set short_content = post.content.substring(0, config.feed.content_limit) %}
{% if config.feed.content_limit_delim %}
{% set delim_pos = short_content.lastIndexOf(config.feed.content_limit_delim) %}
{% if delim_pos > -1 %}
{{ short_content.substring(0, delim_pos) }}
{% else %}
{{ short_content }}
{% endif %}
{% else %}
{{ short_content }}
{% endif %}
{% endif %}
</summary>
{% if post.image %}
<content src="{{ url }}{{ post.image | uriencode }}" type="image" />
{% endif %}
{% for category in post.categories.toArray() %}
<category term="{{ category.name }}" scheme="{{ url }}{{ category.path | uriencode }}"/>
{% endfor %}
{% for tag in post.tags.toArray() %}
<category term="{{ tag.name }}" scheme="{{ url }}{{ tag.path | uriencode }}"/>
{% endfor %}
</entry>
{% endfor %}
</feed>

View File

@ -0,0 +1,54 @@
'use strict'
const nunjucks = require('nunjucks')
const env = new nunjucks.Environment()
const pathFn = require('path')
const fs = require('fs')
const moment = require('moment')
env.addFilter('uriencode', str => {
return encodeURI(str)
})
env.addFilter('noControlChars', str => {
return str.replace(/[\x00-\x1F\x7F]/g, '') // eslint-disable-line no-control-regex
})
env.addFilter('date', str => {
return moment(str).format('YYYY-MM-DD[T00:00:00.000Z]')
})
const atomTmplSrc = pathFn.join(__dirname, './.atom.xml')
const atomTmpl = nunjucks.compile(fs.readFileSync(atomTmplSrc, 'utf8'), env)
module.exports = function (locals) {
const config = this.config
const feedConfig = config.feed
const template = atomTmpl
let posts = locals.posts.sort(feedConfig.order_by || '-date')
posts = posts.filter(post => {
return post.draft !== true
})
if (feedConfig.limit) posts = posts.limit(feedConfig.limit)
let url = config.url
if (url[url.length - 1] !== '/') url += '/'
let icon = ''
if (feedConfig.icon) icon = url + encodeURI(feedConfig.icon)
const xml = template.render({
config: config,
url: url,
icon: icon,
posts: posts,
feed_url: config.root + feedConfig.path
})
return {
path: feedConfig.path,
data: xml
}
}

View File

@ -0,0 +1,32 @@
/* global hexo */
'use strict'
/*
* Modified from the hexo version,
* https://github.com/hexojs/hexo-generator-feed
* to use post.lastUpdated and remove timezone
*/
const pathFn = require('path')
const config = hexo.config.feed = Object.assign({
type: 'atom',
limit: 20,
hub: '',
content: true,
content_limit: 140,
content_limit_delim: '',
order_by: '-date'
}, hexo.config.feed)
// Set default feed path
if (!config.path) {
config.path = config.type + '.xml'
}
// Add extension name if don't have
if (!pathFn.extname(config.path)) {
config.path += '.xml'
}
hexo.extend.generator.register('feed', require('./generator'))

View File

@ -0,0 +1,2 @@
<!-- Generated using https://realfavicongenerator.net/ -->
<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="1066.667" height="1066.667" viewBox="0 0 800 800" fill="#132873"><path d="M2 .6c0 .3 22.3 22.8 49.5 50l49.4 49.4 25-25 25-25 24 24c13.2 13.2 24.2 24 24.5 24 .3 0 .6-22.1.6-49V0h-99C46.5 0 2 .3 2 .6zM202 .6c0 .3 22.4 22.9 49.9 50.4l49.8 49.8-49.6 49.6-49.6 49.6h398.4l49.6-49.6 49.5-49.5-25-25-25-25 25-25C688.7 12.2 700 .7 700 .5c0-.3-22.5-.5-50-.5-39.3 0-50 .3-50 1.2 0 .7-22.3 23.6-49.6 50.9l-49.6 49.6L450 50.9 399.1 0h-98.5c-54.2 0-98.6.3-98.6.6zM750.6 51.9l-48.9 48.9 25 25 25 25-24.6 24.6-24.6 24.6H800v-98.5c0-54.2-.1-98.5-.3-98.5-.1 0-22.2 22-49.1 48.9zM0 150.6v48.5L50.9 250l50.8 50.8-49.6 49.6C24.8 377.7 1.9 400 1.2 400 .3 400 0 441 0 600c0 110 .2 200 .5 200 .2 0 22.7-22.3 50-49.6l49.5-49.5-25-25-25-25 25-25c13.7-13.7 25-25.2 25-25.4 0-.3-22-.5-49-.5s-49-.3-49-.6 22.3-22.8 49.5-50l49.4-49.4 49 49c26.9 26.9 49.2 49 49.5 49 .3 0 .6-89.3.6-198.5V200.9l-49.6-49.6-49.6-49.6-25 25-25 25L26 126.9C12.3 113.2.9 102 .6 102c-.3 0-.6 21.9-.6 48.6zM600 401v199H400c-159 0-200 .3-200 1.2 0 .7-22.1 23.4-49.2 50.5l-49.1 49.1 25 25 25 25-24.6 24.6-24.6 24.6h98.4l50-50 50-50 50 50 50 50h98.5c54.2 0 98.6-.3 98.6-.6s-22.1-22.6-49-49.5l-49-49 50.4-50.5 50.5-50.4 50 50 50 50 25-25 25-25 24 24c13.2 13.2 24.2 24 24.5 24 .3 0 .6-21.9.6-48.6v-48.5l-50-50-50-50 50-50 50-50V202.5l-49.6 49.6-49.6 49.6-49.8-49.8c-27.5-27.5-50.1-49.9-50.4-49.9-.3 0-.6 89.5-.6 199zM250 301v51l50.8-.2 50.7-.3.3-50.8.2-50.7H250v51z"/><path d="M450 301v51l50.8-.2 50.7-.3.3-50.8.2-50.7H450v51zM250 501v51l50.8-.2 50.7-.3.3-50.8.2-50.7H250v51zM450 501v51l50.8-.2 50.7-.3.3-50.8.2-50.7H450v51zM600 751v49h99c54.5 0 99-.2 99-.6 0-.3-21.9-22.4-48.6-49.1l-48.6-48.6-25 25-25 25-24.8-24.8c-13.7-13.7-25.1-24.9-25.4-24.9-.3 0-.6 22-.6 49z"/></svg>

After

Width:  |  Height:  |  Size: 1.8 KiB