在jekyll中针对github页面生成类别页面 [英] Generate category page in jekyll targeting github pages

查看:81
本文介绍了在jekyll中针对github页面生成类别页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jekyll为我的github页面博客生成类别页面.当我使用jekyll serve在本地运行时,此方法有效,因为它可以使用我的插件.

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.

使用此示例来自文档的方法不起作用,因为github不会生成自定义插件:

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

但是,如果我可以生成不在_site目录中的我自己的页面,它将起作用.然后,即使它们是系统生成的,我也将它们作为常规"页面检入.

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.

是否可以使用jekyll在_site目录之外生成类别页面?

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

推荐答案

A)您可以编写自定义脚本(shell或Ruby或其他)来使用

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"
}

当然,您必须在本地运行它,然后再将项目推送到GitHub.

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

B),或者您可以在本地渲染Jekyll网站,然后仅将_site内容推送到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

include: - .nojekyll

然后,您可以呈现站点(jekyll build)并将_site目录的内容推送到GitHub.您可以使用一些Shell脚本来自动化此步骤.

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.

这篇关于在jekyll中针对github页面生成类别页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆