我正在尝试使用 jekyll 为我的 github 页面博客生成类别页面。当我在本地运行时使用jekyll serve
这有效,因为它可以使用我的插件。
使用这个例子从文档 https://jekyllrb.com/docs/plugins/#generators不起作用,因为 github 不会生成自定义插件:
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
目录。然后,我会将这些作为“常规”页面签入,即使它们是由系统生成的。
有没有办法在外部生成类别页面_site
与 jekyll 的目录?
A)您可以编写自定义脚本(shell 或 Ruby 或其他)使用以下命令在任何地方生成文件文件 I/O 操作 https://stackoverflow.com/questions/2777802/how-to-write-to-file-in-ruby.在上面的示例中,您可以将带有 front-matter 的文件写入项目目录,而不是向站点添加新页面。例如(红宝石):
open('somepath/#{category}/index.html', 'w') { |f|
f << "---\n"
f << "key: #{variable}\n"
f << "---\n"
}
当然,你必须在推送之前在本地运行它项目到 GitHub。
B)或者你可以在本地渲染您的 Jekyll 站点并只推_site
内容到 GitHub。然后您可以使用任何插件你需要。你必须创建一个.nojekyll
项目根目录中的文件绕过 Jekyll 处理 https://blog.github.com/2009-12-29-bypassing-jekyll-on-github-pages/在 GitHub 上,您还必须将其添加到 Jekyll 的包含中(在_config.yml
):
include:
- .nojekyll
然后你可以渲染网站(jekyll build
) and 推送内容_site
目录到 GitHub。您可以使用一些 shell 脚本自动执行此步骤。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)