title: 安装RSS插件实现订阅
tags:
  - Hexo
  - Butterfly
categories:
  - Hexo
top_img: false
cover: '/upload/cdn0files/20200721121836.jpg'
abbrlink: c2535796
date: 2019-12-09 21:50:07
updated: 2019-12-09 21:50:07

安装插件

$ cnpm install -- save hexo-generator-feed

修改配置文件

$ vim _config.yml
#RSS订阅
plugin: hexo-generator-feed
#Feed Atom
feed:
  type: atom
  path: atom.xml
  limit: 20
  hub:
  content: true
  content_limit: 140
  content_limit_delim: ' '
  order_by: -date
  icon: icon.png
  autodiscovery: true
  template: ./source/rss/atom.xml

因为我是Butterfly主题,所以已经集成了rss功能,所以设置到这一步就可以了。如果你的主题里面没有rss相关设置,也可以添加如下代码:

  rss: /atom.xml

然后按照模板的路径./source/rss/atom.xml新建一个atom.xml文件,文件中写入以下模板:

<?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 | uriencode }}"/>
  <updated>{{ posts.first().updated.toISOString() }}</updated>
  <id>{{ url | uriencode }}</id>
  {% if config.author %}
  <author>
    <name>{{ config.author }}</name>
    {% if config.email %}<email>{{ config.email }}</email>{% endif %}
  </author>
  {% endif %}
  <generator uri="https://hexo.io/">Hexo</generator>
  {% for post in posts.toArray() %}
  <entry>
    <title>{{ post.title }}</title>
    <link href="{{ post.permalink | uriencode }}"/>
    <id>{{ post.permalink | uriencode }}</id>
    <published>{{ post.date.toISOString() }}</published>
    <updated>{{ post.updated.toISOString() }}</updated>
    {% if config.feed.content and post.content %}
    <content type="html"><![CDATA[{{ post.content | 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>

Yes!

官方文档请参考:https://github.com/hexojs/hexo-generator-feed