【发布时间】:2015-03-10 18:25:19
【问题描述】:
我正在尝试调整现有的 Jekyll 插件(取自 here),以便为 collection 中的每个文档生成 .json 版本。
但是,我无法将我的内容从 Markdown 转换为 HTML(然后我想将其转换/编码为 JSON)。在 Jekyll 中,集合“文档”与“帖子”不同,虽然 posts 可以访问满足我需要的 transform 方法,但看起来“文档”没有。
在 Jekyll 插件的上下文中,是否有其他直接的方法可以将内容提供给 Markdown 解析器?
这是迄今为止我一直在使用的插件代码。这正在生成 JSON,但 markdown 没有被转换为 HTML(像 ** 这样的 markdown 语法仍保留在文件中)。
module Jekyll
class JSONPage < Page
def initialize(site, base, dir, name, content)
@site = site
@base = base
@dir = dir
@name = name
self.data = {}
self.content = content
process(@name)
end
def read_yaml(*)
# Do nothing
end
def render_with_liquid?
false
end
end
class JSONPageGenerator < Generator
safe true
def generate(site)
site.documents.each do |document|
# Set the path of the JSON version
path = "#{document.collection.label}" + document.cleaned_relative_path + ".json"
output = document.to_liquid
# Delete unnecessary metadata
['layout', 'output'].each { |key| output.delete(key) }
site.pages << JSONPage.new(site, site.source, File.dirname(path), File.basename(path), output)
end
end
end
end
【问题讨论】:
标签: jekyll