有没有办法使用 Jekyll 解析外部 RSS 提要



我有几个网站,想通过RSS显示内容,就像Jekyll项目中的标题一样。是否可以使用 jekyll 解析外部 rss 提要并使用它们?

是的。您要么想要创建一个插件来在jekyll build期间获取和解析外部提要,要么计划 B,您可以随时使用 AJAX 在客户端获取和解析提要。既然你问了一个 Jekyll 的答案,这里是前一种方法的粗略近似值:

# Runs during jekyll build
class RssFeedCollector < Generator
   safe true
   priority :high
   def generate(site)
      # TODO: Insert code here to fetch RSS feeds
      rss_item_coll = null;
      # Create a new on-the-fly Jekyll collection called "external_feed"
      jekyll_coll = Jekyll::Collection.new(site, 'external_feed')
      site.collections['external_feed'] = jekyll_coll
      # Add fake virtual documents to the collection
      rss_item_coll.each do |item|
         title = item[:title]
         content = item[:content]
         guid = item[:guid]
         path = "_rss/" + guid + ".md"
         path = site.in_source_dir(path)
         doc = Jekyll::Document.new(path, { :site => site, :collection => jekyll_coll })
         doc.data['title'] = title;
         doc.data['feed_content'] = content;
         jekyll_coll.docs << doc
      end
   end
end

然后,您可以像这样访问模板中的集合:

{% for item in site.collections['external_feed'].docs %}
<h2>{{ item.title }}</h2>
<p>{{ item.feed_content }}</p>
{% endfor %}

主题有很多可能的变化,但这就是想法。

好吧

,我不认为杰基尔本身可以做到这一点......因为杰基尔更像是一个CMS。然而,Jekyll 是用 Ruby 编写的,我相信你可以轻松地使用 Jekyll 运行 ruby/rake 任务(这甚至可能是你构建 Jekyll 网站时使用的),所以我相信你应该把它作为一个 ruby 脚本。

最新更新