Generate file inside _site with Jekyll plugin

2019-07-26 22:38发布

问题:

I have written a Jekyll plugin, "Tags", which generates a file and returns string of links to that file.

Everything is fine, but if I write that file directly into the _site folder, it is removed. If I put that file outside the _site folder, it is not generated inside _site.

Where and how should I add my file so that it is available inside the _site folder?

回答1:

You should use class Page for this and call methods render and write.

This is an example to generate the archive page at my blog:

module Jekyll
  class ArchiveIndex < Page
    def initialize(site, base, dir, periods)
      @site = site
      @base = base
      @dir = dir
      @name = 'archive.html'
      self.process(@name)
      self.read_yaml(File.join(base, '_layouts'), 'archive_index.html')
      self.data['periods'] = periods
    end   
  end

  class ArchiveGenerator < Generator
    priority :low

    def generate(site)
        periods = site.posts.reverse.group_by{ |c| {"month" => Date::MONTHNAMES[c.date.month], "year" => c.date.year} }

        index = ArchiveIndex.new(site, site.source, '/', periods)
        index.render(site.layouts, site.site_payload)
        index.write(site.dest)
        site.pages << index
    end
  end
end