【发布时间】:2015-01-07 21:30:57
【问题描述】:
我有几个网站,希望通过 RSS 显示内容,例如 Jekyll 项目中的标题。是否可以使用 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 %}
这个主题有很多可能的变化,但就是这样。
嗯,我不认为 Jekyll 本身可以做到这一点……因为 Jekyll 更像是一个 CMS。但是,Jekyll 是用 Ruby 编写的,我相信您可以轻松地使用 Jekyll 运行 ruby/rake 任务(这甚至可能是您构建 Jekyll 站点时使用的),所以我相信您应该将其作为 ruby 脚本来执行。
【讨论】: