Jekyll:生成一次包含并将其包含到所有页面

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

TL;DR:我可以说以某种方式为 {% include %} 生成一次内容,然后在多个位置将其标记出来,而不必在每个位置重新生成它吗?

我正在使用 Jekyll 构建一个相当大的文档站点,该站点现在有 50 多篇文章。它有一个侧边栏,其中列出了所有文章。侧边栏构建在一个单独的 sidebar.html 中,然后它包含在网站的每个页面中 {% include sidebar.html %} in default.html.

我遇到的问题是每篇文章 运行 都是 sidebar.html 的生成,所以我有超过 50 代通过了那段代码。我添加的每篇文章都为此添加了另一遍,并使所有的遍都慢了一点,因为生成侧边栏必须解析项目中的每一篇文章。

构建时间已经从基本为零增加到超过 100 秒,如果我删除 {% include sidebar.html %} 然后它会下降到 5 秒。当我收到所有文章时,我估计大约有 100-200 篇。然后我应该在未来对所有文章进行版本控制,这意味着在长 运行 中很容易有 1000 多篇文章。那时,如果更改一个文件中的一个字母需要大约一个小时才能重新生成 jekyll servejekyll build.

中的文件,我不会感到惊讶。

我想做的是在构建过程开始时构建 sidebar.html 一次,然后在生成所述页面时将其标记到每个页面。这可能吗?

最快的方法。

_includes/sidebar.html移动到sidebar-template.html

添加这个前面的内容:

---
layout: null
permalink: sidebar-template.html
---

创建 Rakefile

TPL = "_site/sidebar-template.html"
TST = "_includes/sidebar.html"

task :default => :nav

desc "Generates sidebar then copy it to be used as an include"
task :nav do

  if !File.exist?(TST)
    puts "Creating dummy #{TST} file"
    open(TST, 'w') do |f|
      f.puts warning
    end
  end

  puts "Building Jekyll 1st run"
  system "jekyll build --trace"

  # delete target file (TST) if exist
  if File.exist?(TST)
      puts "#{TST} exists deleting it"
      rm TST
  end

  # copy generated file as an include
  cp(TPL, TST)

  puts "Building Jekyll AGAIN"
  system "jekyll build --trace"

  puts "task END"
end

只需 运行 rake 即可生成侧边栏包含。

感谢 Ben Balter,现在有了更好的方法。

而不是:{% include yourtemplate.html %} 使用:{% include_cached yourtemplate.html %}

当用于需要构建一次的较大项目(例如站点层次结构)时,将缓存该项目。对于其他因页面而异的项目,您仍然希望像往常一样使用包含。

这里解释的很好:https://github.com/benbalter/jekyll-include-cache

绝对减少网站启动时间!