From 801392b35d1a78dbbd49d482d62a8b6ffb48f21a Mon Sep 17 00:00:00 2001
From: curben <2809763-curben@users.noreply.gitlab.com>
Date: Mon, 12 Aug 2019 20:32:22 +0930
Subject: [PATCH] feat: RSS feed
---
_config.yml | 5 ++-
themes/typing/scripts/feed/.atom.xml | 60 +++++++++++++++++++++++++
themes/typing/scripts/feed/generator.js | 54 ++++++++++++++++++++++
themes/typing/scripts/feed/index.js | 32 +++++++++++++
themes/typing/source/svg/rss.svg | 2 +
5 files changed, 152 insertions(+), 1 deletion(-)
create mode 100644 themes/typing/scripts/feed/.atom.xml
create mode 100644 themes/typing/scripts/feed/generator.js
create mode 100644 themes/typing/scripts/feed/index.js
create mode 100644 themes/typing/source/svg/rss.svg
diff --git a/_config.yml b/_config.yml
index c97b62e..27a6f5b 100644
--- a/_config.yml
+++ b/_config.yml
@@ -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
diff --git a/themes/typing/scripts/feed/.atom.xml b/themes/typing/scripts/feed/.atom.xml
new file mode 100644
index 0000000..17480f8
--- /dev/null
+++ b/themes/typing/scripts/feed/.atom.xml
@@ -0,0 +1,60 @@
+
+
+ {{ config.title }}
+ {% if icon %}{{ icon }}{% endif %}
+ {% if config.subtitle %}{{ config.subtitle }}{% endif %}
+
+ {% if config.feed.hub %}{% endif %}
+
+ {{ posts.first().date | date }}
+ {{ url }}
+ {% if config.author %}
+
+ {{ config.author }}
+ {% if config.email %}{{ config.email }}{% endif %}
+
+ {% endif %}
+ Hexo
+ {% for post in posts.toArray() %}
+
+ {{ post.title }}
+
+ {{ url }}{{ post.path }}
+ {{ post.date | date }}
+ {% if post.lastUpdated %}{{ post.lastUpdated | date }}{% else %}{{ post.date | date }}{% endif %}
+ {% if config.feed.content and post.content %}
+
+ {% endif %}
+
+ {% 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 %}
+
+ {% if post.image %}
+
+ {% endif %}
+ {% for category in post.categories.toArray() %}
+
+ {% endfor %}
+ {% for tag in post.tags.toArray() %}
+
+ {% endfor %}
+
+ {% endfor %}
+
diff --git a/themes/typing/scripts/feed/generator.js b/themes/typing/scripts/feed/generator.js
new file mode 100644
index 0000000..81af888
--- /dev/null
+++ b/themes/typing/scripts/feed/generator.js
@@ -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
+ }
+}
diff --git a/themes/typing/scripts/feed/index.js b/themes/typing/scripts/feed/index.js
new file mode 100644
index 0000000..4db03cf
--- /dev/null
+++ b/themes/typing/scripts/feed/index.js
@@ -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'))
diff --git a/themes/typing/source/svg/rss.svg b/themes/typing/source/svg/rss.svg
new file mode 100644
index 0000000..89e2ccf
--- /dev/null
+++ b/themes/typing/source/svg/rss.svg
@@ -0,0 +1,2 @@
+
+
\ No newline at end of file