Jekyll集合:插件中的进程降价

时间:2015-03-10 18:25:20

标签: jekyll

我正在尝试调整现有的Jekyll插件(取自here),以生成.json中每个文档的collection版本。

但是,我无法将我的内容从markdown转换为HTML(我希望将其转换/编码为JSON)。在Jekyll中,集合“文档”与“帖子”不同,虽然posts可以访问执行我需要的transform方法,但看起来“文档”却没有。

在Jekyll插件的上下文中是否有其他直接的方式将内容提供给降价解析器?

这是我到目前为止一直在使用的插件代码。这是生成JSON,但markdown没有转换为HTML(文件中保留了像**这样的降价语法。)

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

1 个答案:

答案 0 :(得分:1)

好的,事实证明答案很简单 - 您可以直接在插件中require 'kramdown'或任何其他降价生成器。

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 JSONPostGenerator < Generator
    safe true

    def generate(site)
      require 'kramdown'

      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
        output['content'] = Kramdown::Document.new(document.content).to_html.gsub(/\n/, "")

        # Delete unnecessary metadata
        ['layout', 'output'].each { |key| output.delete(key) }


        site.pages << JSONPage.new(site, site.source, File.dirname(path), File.basename(path), output.to_json)
      end
    end
  end
end