在 jekyll 中生成分类页面定位 github 个页面
Generate category page in jekyll targeting github pages
我正在尝试使用 jekyll 为我的 github 页面博客生成类别页面。当我 运行 在本地使用 jekyll serve
时,这是有效的,因为它可以使用我的插件。
使用此示例 from the docs 不起作用,因为 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
目录中的自己的页面,那将是可行的。然后我会将这些作为 "regular" 页签入,即使它们是由系统生成的。
有什么方法可以用 jekyll 在 _site
目录之外生成分类页面吗?
A) 您可以编写自定义脚本(shell 或 Ruby 或其他)以 使用 [=17 在任何地方生成文件=]. 在上面的示例中,您可以将带有前端内容的文件写到项目目录中,而不是向站点添加新页面。例如 (Ruby):
open('somepath/#{category}/index.html', 'w') { |f|
f << "---\n"
f << "key: #{variable}\n"
f << "---\n"
}
当然,在将项目推送到GitHub之前,您必须在本地运行它。
B) 或者您可以在本地呈现您的 Jekyll 站点 并仅将 _site
内容推送到 GitHub.然后,您可以使用 任何您需要的插件。您必须在项目根目录中创建一个 .nojekyll
文件到 GitHub 上的 bypass Jekyll processing,并且您还必须将它添加到 Jekyll 的包含文件中(在 _config.yml
中):
include:
- .nojekyll
然后您可以呈现站点 (jekyll build
) 并将 _site
目录 的内容推送到 GitHub。您可以使用一些 shell 脚本自动执行此步骤。
我正在尝试使用 jekyll 为我的 github 页面博客生成类别页面。当我 运行 在本地使用 jekyll serve
时,这是有效的,因为它可以使用我的插件。
使用此示例 from the docs 不起作用,因为 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
目录中的自己的页面,那将是可行的。然后我会将这些作为 "regular" 页签入,即使它们是由系统生成的。
有什么方法可以用 jekyll 在 _site
目录之外生成分类页面吗?
A) 您可以编写自定义脚本(shell 或 Ruby 或其他)以 使用 [=17 在任何地方生成文件=]. 在上面的示例中,您可以将带有前端内容的文件写到项目目录中,而不是向站点添加新页面。例如 (Ruby):
open('somepath/#{category}/index.html', 'w') { |f|
f << "---\n"
f << "key: #{variable}\n"
f << "---\n"
}
当然,在将项目推送到GitHub之前,您必须在本地运行它。
B) 或者您可以在本地呈现您的 Jekyll 站点 并仅将 _site
内容推送到 GitHub.然后,您可以使用 任何您需要的插件。您必须在项目根目录中创建一个 .nojekyll
文件到 GitHub 上的 bypass Jekyll processing,并且您还必须将它添加到 Jekyll 的包含文件中(在 _config.yml
中):
include:
- .nojekyll
然后您可以呈现站点 (jekyll build
) 并将 _site
目录 的内容推送到 GitHub。您可以使用一些 shell 脚本自动执行此步骤。