2
votes

I am trying to generate category pages for my github pages blog using jekyll. When i run locally using jekyll serve this works because it can use my plugin.

Using this example from the docs doesn't work because github will not generate custom plugins:

module Jekyll
  class CategoryPage < Page
    def initialize(site, base, dir, category)
      @site = site
      @base = base
      @dir = dir
      @name = 'index.html'

      self.process(@name)
      self.read_yaml(File.join(base, '_layouts'), 'category_index.html')
      self.data['category'] = category

      category_title_prefix = site.config['category_title_prefix'] || 'Category: '
      self.data['title'] = "#{category_title_prefix}#{category}"
    end
  end

  class CategoryPageGenerator < Generator
    safe true

    def generate(site)
      if site.layouts.key? 'category_index'
        dir = site.config['category_dir'] || 'categories'
        site.categories.each_key do |category|
          site.pages << CategoryPage.new(site, site.source, File.join(dir, category), category)
        end
      end
    end
  end
end

However, it would work if I could generate my own pages that are not in the _site directory. I would then check these in as "regular" pages even though they are generated by the system.

Is there any way to generate category pages outside of the _site directory with jekyll?

1

1 Answers

1
votes

A) You can write custom scripts (shell or Ruby or whatever) to generate files anywhere using file I/O operations. In your above example, instead of adding new pages to the site, you can write out files with front-matter into your project directory. For example (Ruby):

open('somepath/#{category}/index.html', 'w') { |f|
  f << "---\n"
  f << "key: #{variable}\n"
  f << "---\n"
}

And of course, you have to run it locally, before pushing the project to GitHub.


B) Or you can render your Jekyll site locally and push only the _site contents to GitHub. You can then use any plugin you need. You have to create a .nojekyll file in the project root to bypass Jekyll processing on GitHub, and you have to add it to Jekyll's includes as well (in _config.yml):

include: - .nojekyll

Then you can render the site (jekyll build) and push contents of _site directory to GitHub. You can automate this step with a little shell script.