Generate category page in jekyll targeting github pages

Doug

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?

juzraai

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Github pages w/ Jekyll 404 on specific page

Github Pages - trouble rendering a Page with Jekyll

Jekyll, Liquid - Get all pages from category from page

I use jquery in github pages which use jekyll to generate website

404 page not working with permalink: pretty using GitHub Pages + Jekyll

Get a blank page after pushing a site using Github pages with Jekyll

How do I customize Jekyll theme for a single page in GitHub pages?

Accessible index page configured wrong (Jekyll + Github Pages)

Using Github pages with Jekyll in a subdirectory

Github pages jekyll images not loading

Installing Jekyll 3.8.5 on GitHub Pages

Jekyll on Github Pages not rendering CSS

Setting Jekyll environment for GitHub Pages

Animation on jekyll run github pages

Problem with GitHub Pages Jekyll Template

Github Pages: How to access github project page when root site is jekyll

Why isn't this static project page showing up on a Jekyll-generated user page on GitHub Pages?

GitHub Pages Jekyll theme working for pages but not for posts

How to separate posts by pages on Jekyll GitHub Pages?

Rendering a GitHub wiki on a GitHub Pages Jekyll site

List pages in a category on a parent page

Generate standalone Jekyll page with variables

Github Jekyll page behaves not properly

Jekyll: Generate an include once and include it to all pages

Github Pages and Jekyll permalinks going to different paths

Jekyll (for GitHub Pages): How to create a carousel of Divs?

Jekyll includes not working in GitHub Pages without a template

How to prevent jekyll build on github pages

GitHub Pages Jekyll won't build