外掛程式

Jekyll 內建支援使用外掛程式來擴充核心功能。

基本上,任何副檔名為 .rb 的檔案,只要放在網站 source 根目錄下的 _plugins 目錄中,就會在建置過程中自動載入。

此行為可依下列方式設定

  • _plugins 目錄可透過命令列或設定檔直接變更。
  • 當 Jekyll 在 safe 模式下執行時,不會載入 _plugins 目錄(或其等效目錄)中的外掛程式。
  • 此路徑無法用於擴充 Jekyll CLI。

若要使用以 gem 封裝的外掛程式,必須在設定檔中,於名為 plugins 的頂層金鑰下,列出所需 gem。此外,若在 safe 模式下建置,則 gem 必須列在名為 whitelist 的頂層金鑰下。例如

plugins:
  - jekyll-gist
  - jekyll-coffeescript
  - jekyll-seo-tag
  - some-other-jekyll-plugin

# Enable safe mode
safe: true

# Whitelist plugins under safe mode.
# Note that `some-other-jekyll-plugin` is not listed here. Therefore,
# it will not be loaded under safe mode.
whitelist:
  - jekyll-gist
  - jekyll-coffeescript
  - jekyll-seo-tag

在沒有 Gemfile 的情況下,必須在呼叫 Jekyll 之前手動確保已安裝列出的外掛程式。例如,以上清單中寶石的最新版本可透過執行以下指令安裝到系統範圍的位置

gem install jekyll-gist jekyll-coffeescript jekyll-remote-theme some-other-jekyll-plugin

使用 Gemfile

透過使用 Gemfile(通常位於網站來源的根目錄)搭配名為 bundler 的 Rubygem,可以大幅簡化各種寶石依賴項目的維護。但是,Gemfile 列出網站的所有主要依賴項,包括 Jekyll 本身,而不仅仅是網站的基於寶石的外掛程式,因為 Bundler 會將已安裝寶石的範圍縮小為僅由評估 Gemfile 解析的執行時期依賴項。例如

source "https://rubygems.org"

# Use the latest version.
gem "jekyll"

# The theme of current site, locked to a certain version.
gem "minima", "2.4.1"

# Plugins of this site loaded during a build with proper
# site configuration.
gem "jekyll-gist"
gem "jekyll-coffeescript"
gem "jekyll-seo-tag", "~> 1.5"
gem "some-other-jekyll-plugin"

# A dependency of a custom-plugin inside `_plugins` directory.
gem "nokogiri", "~> 1.11"

可以透過單純執行 bundle install 來同時安裝 Gemfile 中列出的寶石。

:jekyll_plugins Gemfile 群組

Jekyll 會對 Gemfile 中 :jekyll_plugins 群組中列出的寶石給予特殊處理。此群組中的任何寶石都會在任何 Jekyll 程序的最一開始載入,不論 --safe CLI 標記或設定檔中的項目為何。

雖然此路徑允許使用者使用其他子指令和選項來增強 Jekyll 的 CLI,或避免在設定檔中列出寶石,但缺點是必須注意群組中包含哪些寶石。例如

source "https://rubygems.org"

# Use the latest version.
gem "jekyll"

# The theme of current site, locked to a certain version.
gem "minima", "2.4.1"

# Plugins of this site loaded only if configured correctly.
gem "jekyll-gist"
gem "jekyll-coffeescript"

# Gems loaded irrespective of site configuration.
group :jekyll_plugins do
  gem "jekyll-cli-plus"
  gem "jekyll-seo-tag", "~> 1.5"
  gem "some-other-jekyll-plugin"
end
GitHub Pages 上的外掛程式

GitHub Pages 由 Jekyll 提供支援。出於安全考量,所有 GitHub Pages 網站都使用 --safe 選項來停用外掛程式(除了某些 白名單外掛程式)。很遺憾,這表示如果透過 GitHub Pages 部署,你的外掛程式將無法運作。

您仍然可以使用 GitHub Pages 來發佈您的網站,但您需要在本地建置網站,並將產生的檔案推送到您的 GitHub 儲存庫,而不是 Jekyll 原始檔。

_plugins_config.ymlGemfile 可以同時使用

如果您選擇,您可以在同一個網站中同時使用任何上述外掛程式路徑。使用其中一個不會限制使用其他外掛程式。