防止 jekyll 清理生成的 JSON 文件?

Prevent jekyll from cleaning up generated JSON file?

我写了一个简单的插件,可以生成一个小 JSON 文件

module Jekyll

  require 'pathname'
  require 'json'

  class SearchFileGenerator < Generator
     safe true

     def generate(site)
       output = [{"title" => "Test"}]

       path = Pathname.new(site.dest) + "search.json"

       FileUtils.mkdir_p(File.dirname(path))
       File.open(path, 'w') do |f|
         f.write("---\nlayout: null\n---\n")
         f.write(output.to_json)
       end
       # 1/0
     end
   end
end

但是每次 Jekyll 运行完成时生成的 JSON 文件都会被删除。如果我取消注释除以零行并导致它出错,我可以看到正在生成 search.json 文件,但它随后被删除。我该如何防止这种情况?

我发现了以下问题,建议将文件添加到 keep_fileshttps://github.com/jekyll/jekyll/issues/5162 有效:

新代码似乎避免了 search.json 被删除:

module Jekyll

  require 'pathname'
  require 'json'

  class SearchFileGenerator < Generator
     safe true

     def generate(site)
       output = [{"title" => "Test"}]

       path = Pathname.new(site.dest) + "search.json"

       FileUtils.mkdir_p(File.dirname(path))
       File.open(path, 'w') do |f|
         f.write("---\nlayout: null\n---\n")
         f.write(output.to_json)
       end
       site.keep_files << "search.json"
     end
   end
end

将您的新页面添加到 site.pages :

module Jekyll
  class SearchFileGenerator < Generator
    def generate(site)
      @site  = site
      search = PageWithoutAFile.new(@site, site.source, "/", "search.json")
      search.data["layout"] = nil
      search.content = [{"title" => "Test 32"}].to_json
      @site.pages << search
    end
  end
end

灵感来自 jekyll-feed code