Hexo添加RSS

Hexo的Next主题集成RSS可以说是非常简单的一个操作,但也是一个极其容易搞错的操作,这里把失败和成功的过程都分享出来。

[TOC]

失败的操作

安装

执行npm install hexo-generator-feed,安装成功。

1
2
3
4
5
6
$ cd taoblog
$ npm install hexo-generator-feed
npm WARN babel-eslint@10.0.2 requires a peer of eslint@>= 4.12.1 but none is installed. You must install peer dependencies yourself.

+ hexo-generator-feed@1.2.2
added 2 packages from 2 contributors in 2.985s

编辑配置文件

  • Hexo站点配置文件添加如下配置
1
2
3
4
5
6
7
8
9
10
# Extensions
## Plugins: https://hexo.io/plugins/
# RSS
plugins:
- hexo-generator-feed
#Feed Atom
feed:
type: atom
path: atom.xml
limit: 20
  • NexT主题配置文件添加如下配置
1
2
3
4
# Set rss to false to disable feed link.
# Leave rss as empty to use site's feed link.
# Set rss to specific value if you have burned your feed already.
rss: /atom.xml

hexo生成报错

执行hexo g后报错,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ hexo clean
INFO Deleted database.
INFO Deleted public folder.
$ hexo g
INFO Start processing
FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Template render error: (unknown path) [Line 8, Column 25]
Error: Unable to call `the return value of (posts["first"])["updated"]["toISOString"]`, which is undefined or falsey
at Object._prettifyError (/Users/user/taoblog/node_modules/nunjucks/src/lib.js:36:11)
at /Users/user/taoblog/node_modules/nunjucks/src/environment.js:567:19
at Template.root [as rootRenderFunc] (eval at _compile (/Users/user/taoblog/node_modules/nunjucks/src/environment.js:637:18), <anonymous>:214:3)
at Template.render (/Users/user/taoblog/node_modules/nunjucks/src/environment.js:556:10)
at Hexo.module.exports (/Users/user/taoblog/node_modules/hexo-generator-feed/lib/generator.js:40:22)
at Hexo.tryCatcher (/Users/user/taoblog/node_modules/bluebird/js/release/util.js:16:23)
at Hexo.<anonymous> (/Users/user/taoblog/node_modules/bluebird/js/release/method.js:15:34)
at /Users/user/taoblog/node_modules/hexo/lib/hexo/index.js:318:20
at tryCatcher (/Users/user/taoblog/node_modules/bluebird/js/release/util.js:16:23)
at MappingPromiseArray._promiseFulfilled (/Users/user/taoblog/node_modules/bluebird/js/release/map.js:61:38)
at MappingPromiseArray.PromiseArray._iterate (/Users/user/taoblog/node_modules/bluebird/js/release/promise_array.js:114:31)
at MappingPromiseArray.init (/Users/user/taoblog/node_modules/bluebird/js/release/promise_array.js:78:10)
at MappingPromiseArray._asyncInit (/Users/user/taoblog/node_modules/bluebird/js/release/map.js:30:10)
at _drainQueueStep (/Users/user/taoblog/node_modules/bluebird/js/release/async.js:142:12)
at _drainQueue (/Users/user/taoblog/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/Users/user/taoblog/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/Users/user/taoblog/node_modules/bluebird/js/release/async.js:17:14)
at processImmediate (internal/timers.js:439:21)

卸载并恢复配置信息

卸载hexo-generator-feed,删除站点配置文件、主题配置文件中的相关配置后,hexo生成即发布恢复正常。

卸载时的提示如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ npm uninstall hexo-generator-feed
npm WARN babel-eslint@10.0.2 requires a peer of eslint@>= 4.12.1 but none is installed. You must install peer dependencies yourself.

removed 2 packages in 2.575s


╭────────────────────────────────────────────────────────────────╮
│ │
│ New minor version of npm available! 6.10.3 → 6.11.2 │
│ Changelog: https://github.com/npm/cli/releases/tag/v6.11.2 │
│ Run npm install -g npm to update! │
│ │
╰────────────────────────────────────────────────────────────────╯

成功的操作

重点:参考hexo官方Plugins以及hexo-generator-feed插件官方页面的安装与配置指导,顺利完成集成,具体如下。

Generate Atom 1.0 or RSS 2.0 feed.

Install

1
$ npm install hexo-generator-feed --save
  • Hexo 3: 1.x
  • Hexo 2: 0.x

执行结果如下:

1
2
3
4
5
6
$ cd taoblog
$ npm install hexo-generator-feed --save
npm WARN babel-eslint@10.0.2 requires a peer of eslint@>= 4.12.1 but none is installed. You must install peer dependencies yourself.

+ hexo-generator-feed@1.2.2
added 2 packages from 2 contributors in 2.789s

Use

In the front-matter of your post, you can optionally add a description, intro or excerpt setting to write a summary for the post. Otherwise the summary will default to the excerpt or the first 140 characters of the post.

Options

在hexo站点配置文件 _config.yml# Extensions字段#Plugins: https://hexo.io/plugins/项下添加如下配置信息,即可完成。

1
2
3
4
5
6
7
8
9
10
feed:
type: atom
path: atom.xml
limit: 20
hub:
content:
content_limit: 140
content_limit_delim: ' '
order_by: -date
icon: icon.png
  • type - Feed type. (atom/rss2)
  • path - Feed path. (Default: atom.xml/rss2.xml)
  • limit - Maximum number of posts in the feed (Use 0 or false to show all posts)
  • hub - URL of the PubSubHubbub hubs (Leave it empty if you don’t use it)
  • content - (optional) set to ‘true’ to include the contents of the entire post in the feed.
  • content_limit - (optional) Default length of post content used in summary. Only used, if content setting is false and no custom post description present.
  • content_limit_delim - (optional) If content_limit is used to shorten post contents, only cut at the last occurrence of this delimiter before reaching the character limit. Not used by default.
  • order_by - Feed order-by. (Default: -date)
  • icon - (optional) Custom feed icon. Defaults to a gravatar of email specified in the main config.

Generate & deploy

1
2
3
$ hexo clean
$ hexo g
$ hexo d

原因分析

  • 成功操作与失败操作安装的都是同一个版本的Feed插件即hexo-generator-feed@1.2.2版本,成功的操作只需配置站点配置文件,而且配置信息与失败操作中参考的配置信息不同,而且并不需要配置主题配置文件。
  • 教训:首先要参考官方相关文档,会躲过很多坑。