【问题标题】:Generate category-specific RSS feed using Jekyll using GitHub pages使用 GitHub 页面使用 Jekyll 生成特定类别的 RSS 提要
【发布时间】:2017-06-22 14:31:13
【问题描述】:

我正在尝试为 GitHub Pages Jekyll 网站生成特定于帖子类别的 RSS 提要。

我了解jekyll-feed plugin 可以为所有帖子生成 RSS 提要,但根据this GitHub Issue,尚不支持特定类别提要。

生成特定类别提要的其他方法(例如,herehere 不受 GitHub Pages 支持,因为它不支持自定义插件。

有没有办法使用 Jekyll 和 GitHub Pages 生成特定类别的 RSS 提要?

【问题讨论】:

  • 请考虑“接受”Joost 的回答,这样他才能获得荣誉。
  • 现在支持特定类别的提要。详情请见github.com/jekyll/jekyll-feed

标签: jekyll github-pages


【解决方案1】:

您可以创建自己的 XML 或 RSS 文件。对于这个答案,我使用了this example 来构建。我还使用Wikipedia 作为 RSS 提要示例。

文件名:categoryname.rss

---
layout: null
---
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
 <title>{{ site.title }}</title>
 <description>{{ site.description }}</description>
 <link>{{ site.url }}</link>
 <lastBuildDate>{{ site.time | date_to_rfc822 }}</lastBuildDate>
 <pubDate>{{ site.time | date_to_rfc822 }}</pubDate>
 <ttl>1800</ttl>

 {% for post in site.categories.categoryname %}
 <item>
  <title>{{ post.title }}</title>
  <description>{{ post.description }}</description>
  <link>{{ post.url }}</link>
  <guid isPermaLink="true">{{ site.url }}{{ post.url }}</guid>
  <pubDate>{{ post.date | date_to_rfc822 }}</pubDate>
 </item>
 {% endfor %}

</channel>
</rss>

标题应该类似于:'mysitenames categoryname archive'。描述可能是您的类别描述。链接是类别的链接。 'lastBuildDate' 值可能是您上次发帖的日期,而 'pubDate' 可能是相同的。

如果您有任何问题,请告诉我。

【讨论】:

  • 谢谢。写完指定类别的帖子后,我是否必须手动更新?
  • 不客气。不,这个脚本循环遍历所有帖子。
  • 这是纯 Jekyll,所以“是”。
  • 在上面指定的文件名上... categoryname.rss
  • 以下是一些有用的补充:{{ site.time | date_to_rfc822 }}{{ post.date | date_to_rfc822 }}{{ site.title }}{{ site.description }}
【解决方案2】:

在我为托管在 GitHub Pages 上的集合发布之后,经过修改后它也可能对类别有用。


通过 Git 的子模块功能可以在另一个项目中下载...

cd your-project
git checkout gh-pages

mkdir -vp _layouts/modules

git submodule add -b master --name feed-rss2\
 https://github.com/liquid-utilities/feed-rss2.git\
 _layouts/modules/feed-rss2

注意,https URL 是 GitHub Pages 兼容性所必需的。

...这应该会导致类似于以下的代码可用...

_layouts/modules/feed-rss2/feed-rss2.html

---
layout: null
version: 0.0.1
license: AGPL-3.0
author: S0AndS0
---
{% capture workspace__rss2 %}
  {% assign date_format = '%a, %d %b %Y %T %z' %}
  {% assign collection_name = page.collection_name | default: include.collection_name | default: nil %}
  {% assign target_collection = site[collection_name] %}

  {% assign collection_home = page.collection_home | default: collection_name %}
  {% assign collection_description = page.description | default: site.description %}
  {% assign this_time_stamp = page.date | date: '%s' %}
  {% assign latest_time_stamp = page.date | date: '%s' %}

  {% assign rss_items_has_output = false %}
  {% capture workspace__rss2__entries %}
    {% for post in target_collection %}
      {% if page.relative_path == post.relative_path or post.slug == 'feed' %}
        {% continue %}
      {% elsif post.slug == collection_name or post.slug == 'index' %}
        {% continue %}
      {% endif %}

      {% assign rss_items_has_output = true %}
      {% assign post_synopsis = post.description | default: post.excerpt %}
      {% assign post_date_updated = post.date_updated | default: post.date %}

      {% assign this_time_stamp = post_date_updated | date: '%s' %}
      {% if latest_time_stamp < this_time_stamp %}{% comment %}> Syntax highlighting work-around{% endcomment %}
        {% assign latest_time_stamp = this_time_stamp %}
      {% endif %}

      <item>
        <title>{{- post.title | xml_escape -}}</title>
        <link>{{- post.url | absolute_url -}}</link>
        <guid isPermaLink="true">{{- post.url | absolute_url -}}</guid>
        <pubDate>{{- post_date_updated | date: date_format -}}</pubDate>
        <description>{{- post_synopsis | xml_escape -}}</description>
      </item>

      {% assign post_synopsis = nil %}
    {% endfor %}
  {% endcapture %}

  {% assign page_rights = page.copyright | default: collection_home.copyright | default: site.copyright %}
  {% assign page_rights = page_rights | default: site.github.license.name | default: 'All rights reserved' %}

  <?xml version="1.0" encoding="UTF-8" ?>
  <rss version="2.0">
    <channel>
      <title>{{- page.title | default: 'RSS Title' | xml_escape -}}</title>
      <link>{{- page.url | absolute_url -}}</link>
      <description>{{ collection_description | xml_escape }}</description>
      <copyright>Copyright {{ site.time | date: '%Y' }} {{ page_author | xml_escape }}. {{ page_rights | xml_escape }}</copyright>
      <lastBuildDate>{{- site.time | date: date_format -}}</lastBuildDate>
      <pubDate>{{- latest_time_stamp | date: date_format -}}</pubDate>
      <ttl>{{- page.time_to_live | default: '1800' -}}</ttl>
      {% if rss_items_has_output %}
        {{ workspace__rss2__entries | strip }}{% assign workspace__rss2__entries = nil %}
      {% endif %}
    </channel>
  </rss>
{% endcapture %}{{ workspace__rss2 | strip }}{% assign workspace__rss2 = nil %}

...在集合目录中设置提要文件看起来像...

_example-collection/example-collection.rss

---
layout: modules/feed-rss2/feed-rss2
title: Example Collection
collection_name: example-collection
collection_home: /example-collection/
date: 2019-07-23 21:12:13 -0700
content_type: xhtml
permalink: /:collection/:name:output_ext
---

...以及对每个帖子/页面的 FrontMatter 的少量添加...

_example-collection/something-about-something.markdown

---
layout: post
title: Something About Something
description: Example collection page about something
date: 2019-07-21 11:42:11 -0300
time_to_live: 1800
---

...应该呈现类似于 GitHub Pages 托管的 live 演示的结果。

注意,请查看documentation 以获取更新和克隆提示和注意事项。


无论您是否使用上述项目的代码,我都建议您研究submodule 功能(提示git help submodule),因为这样的东西允许在多个存储库中重用代码,同时保持版本跟踪和轻松更新。此外,子模块与 GitHub Pages 兼容,这意味着子模块与第三方插件支持非常接近,无需探索持续集成解决方案即可获得。

另一个关键的收获是,尝试将布局用于这些类型的解决方案。

如果您遇到困难或上面的某些内容令人困惑,请随时发表评论。

【讨论】:

    【解决方案3】:

    扩展 JoostS 答案:

    ---
    layout: null
    ---
    <?xml version="1.0" encoding="UTF-8" ?>
    <rss version="2.0">
      <channel>
        <title>{{ site.title }}</title>
        <description>{{ site.description }}</description>
        <link>{{ site.url }}</link>
        <lastBuildDate>{{ site.time | date_to_rfc822 }}</lastBuildDate>
        <pubDate>{{ site.time | date_to_rfc822 }}</pubDate>
        <ttl>1800</ttl>
        {% for post in site.pages %}
        {% if post.title %}
        <item>
          <title>{{ post.title }}</title>
          <description>{{ post.description }}</description>
          <link>{{ site.url }}{{ post.url }}</link>
          <guid isPermaLink="true">{{ site.url }}{{ post.url }}</guid>
          <pubDate>{{ post.date | date_to_rfc822 }}</pubDate>
        </item>
        {% endif %}
        {% endfor %}
      </channel>
    </rss>
    
    

    【讨论】:

      猜你喜欢
      • 2018-12-02
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 2019-06-26
      • 2019-11-03
      • 2015-06-09
      • 2020-05-19
      • 1970-01-01
      相关资源
      最近更新 更多