In this blog post I share my experience of building a Jekyll plugin.
I have a map page in my travel blog which shows all the places I’ve been to. This page goes through all blog posts and collects locations from YAML data. After that it builds a Google Map with a marker for each location.
I thought it might be a good idea to extract this functionality into a separate plugin and open source it, so that other people can also use it on their pages.
Introduction
Jekyll plugins allow you to change behaviour without modifying Jekyll source.
There are several ways to use plugins:
you can just dump plugin code to _plugins directory
or you can pack your code as a Ruby gem and reference it in _config.yml
Jekyll has good documentation for plugins system.
Plugins as a gem are more convenient to users and generally preferred.
Best Practices
Before diving into coding my new plugin I decided to research existing plugins implementations to find some common patterns and best practices. I’ve inspected plugins such as jekyll-assets, jekyll-feed, jekyll-sitemap, jekyll-mentions and others and found some good ideas.
Directory Structure
Most of the plugins have following directory structure:
Versioning
Plugin version is kept in a separate file and re-used everywhere when you need to display a version. It makes releasing new versions easier.
Gemspec
Gemspec describes your gem and its dependencies, I’ve came up with following for my plugin, based on several other plugins:
Testing
To test integration of your plugin in Jekyll you can use a test helper.
And use it in the tests:
Plugin Categories
There are several different ways your plugin can alter Jekyll’s behaviour:
Generators: create additional content or fill in template variables
Hooks: subscribe to different events and modify content
In jekyll-maps I used tag to create {% google_map %} tag and hooks to inject JavaScript code into the page.
Jekyll documentation provides good examples for each plugin category and you can also check my implementation on GitHub.
Releasing
To release a new version of a plugin we need to do following:
increase version number in lib/${plugin_name}/version.rb
add release notes to HISTORY.md
create a release and a tag in GitHub (optional)
build new gem
gem build ${plugin_name}.gemspec
push new version to rubygems.org
gem push ${plugin_name}-${version}.gem
When your plugin is ready to be used — don’t forget to add it to the list of plugins in Jekyll docs, so that people can find it!