Jekyll
Creating web sites (like this one) on github-pages using Markdown
More on Jekyll
- Jekyll: On Heroku—Setting up a Jekyll site on Heroku (backup to Github Pages, use add-ons)
- Jekyll: pandoc conversion—Using pandoc to convert material in other formats to Jekyll Markdown
Jekyll
Jekyll is a software package that can be used in conjunction with github.com to publish web pages easily.
The web site you are looking at now is created via Jekyll.
- Content is authored in markdown, and kept in github.com repositories.
- Content is published automatically to a site with the url __.github.io every time you push changes to your repo.
- There is no database to configure, and authorization is handled by existing github.com authorization mechanisms.
- There is no “lock-in”; it is straightforward to migrate a Jekyll-based site to a platforms outside of github.com (for example, Heroku, or any host that supports Ruby.)
Creating pages
- Create pages as index.md files in a directory with the name that you want the page to appear under.
- For example, to make a page with the URL
site.github.io/topics/jekyll
, create the file/topics/jeykll/index.md
- For example, to make a page with the URL
.md
files must start with what Jekyll calls Front Matter.- The minimal front matter is two consecutive lines of exactly three hyphens
---
- Use this minimal front matter until you have a reason to do something more specific
- The front matter can be used to configure things such as the title of the page, and many other things.
- The format of the front matter is Yaml (a syntax for key/value associations that includes JSON as a subset).
- The minimal front matter is two consecutive lines of exactly three hyphens
Example of minimal front matter:
---
---
If you want your pages to have an html <title>
element, you need to specify at least one default layout (explained below), and the specify this
layout along with the title:
---
layout: default
title: Jekyll
---
Nested lists
- The default Markdown processor is Kramdown, which has a few restrictions beyond standard Github-Flavored Markdown.
- The one you may encounter first is that in standard Github-Flavored Markdown, nested lists may be indicated with any amount of indentation:
* First level item 1
- Second level item a
- Seond level item b
* First level item 2
In Kramdown, you must indent four spaces to get a second level of list:
* First level item 1
- Second level item a
- Second level item b
* First level item 2
Configuration
- The file
_config.yml
in the root of the repository can be used to configure various Jekyll properties- As an example,
markdown: rdiscount
specifies a different Markdown processor to use instead of the default.
- As an example,
Layouts
In Jekyll a layout specifies the common features of a category of pages on your site. For example, for a course web site, you might have different layouts for:
- general course information pages
- lab assignments
- homework assignments
- summaries of lecture notes
Strictly speaking, layouts are optional. It is possible to create a minimally functional site without providing layouts.
However, it is recommended to provide at least one default layout. If you don’t, while the pages produced will work in most browsers, pages will not be compliant HTML. For example it is the layout that provides the template for putting in the <head></head>
section that contains the page <title></title>
element. Thus, without a layout, specifying a title: my title
in the front matter will have no effect.
Layouts are created in the _layouts
subdirectory of the root of the site repository.
The jekyll documentation provides a good tutorial on creating a default.html
layout. This is a good place to start, though your default.html
layout can likely be much simpler than the one shown on that site. Here is a much simpler example. Here is some insight into what it contains:
- The
<!DOCTYPE html>
is the latest HTML 5 compliant document type - In the
head
element:- The
meta
elements provide for UTF-8 encoding, and proper scaling on small devices (e.g. mobile phone browsers). - The
title
element now referencesJekyll
which brings in the title from the front matter of your.md
file.
- The
- In the
body
element, the wordcontent
surrounded by two braces inserts the contents of your.md
file. - To actually use this, you must insert
layout: default
into the front matter of your.md
files. If you don’t want to have to do this on every single .md file (and why would you?) you can specify this as a default in your_config.yml
file (see below.)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Jekyll</title>
</head>
<body id="page-top">
<h1 id="why-should-i-set-up-continuous-integration-for-a-jekyll-site-on-travis-ci">Why should I set up continuous integration for a <a href="/topics/jekyll/">Jekyll</a> site on Travis-CI</h1>
<p>Setting up a <a href="/topics/jekyll/">Jekyll</a> site on Github Pages is super convenient.</p>
<p>But one thing Github Pages didn’t provide is <em>access to the error messages</em> that you get when your site is set up
incorrectly. Instead, you just get a “404 unavailable” message, or your site doesn’t update (your changes don’t
seem to have any effect.)</p>
<p>The best way to remedy this—in fact its Github Pages own suggestion—is to set up continuous integration for
your Jekyll site via Travis-CI.</p>
<p>Doing so has these advantages:</p>
<ul>
<li>
<p>If you are using github pages, you won’t see the error messages if/when the site build is broken. TravisCI is the
one of two ways that Github Pages suggests for making these error messages visible. (The other way is to run Jekyll locally
on your own machine (setting up the site on localhost:4000).</p>
</li>
<li>
<p>When the site is broken, TravisCI will send you emails until the site is fixed.</p>
</li>
</ul>
<p>Here’s how to do it:</p>
<ol>
<li>
<p>Add a .travis.yml file to the root of your repo, with the following contents:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>language: ruby
rvm:
- 2.1.7
script: "bundle exec jekyll build"
</code></pre></div> </div>
</li>
<li>
<p>Make sure this line is in your _config.yml:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>exclude: [vendor]
</code></pre></div> </div>
</li>
<li>
<p>Visit https://travis-ci.org/ and login with your github.com credentials—the same ones you use to maintain the github repo where the github pages site is stored.</p>
</li>
</ol>
<p>…</p>
<p>Eventually, the page you’ll visit is the same page as for your github.com repo, except with github.com replaced with travis-ci.org</p>
<p>For example:</p>
<p>For the repo:</p>
<p>https://github.com/ucsd-cse-spis-2016/ucsd-cse-spis-2016.github.io</p>
<p>Access:</p>
<p>https://travis-ci.org/ucsd-cse-spis-2016/ucsd-cse-spis-2016.github.io</p>
</body>
</html>
Specify default layout in _config.yml
Put this in your _config.yml
to specify that default
is the default layout for all .md
files that don’t specify a different layout. That way, you can just use a minmal front matter that contains only the page title.
defaults:
-
scope:
path: "" # an empty string here means all files in the project
values:
layout: "default"
Includes
You can factor out what are sometimes called “partials” in other web frameworks, i.e. something like a #include
in C/C++. To do that, you create a directory in the root of the repo called _includes
.
The Jekyll documentation contains a tutorial example of doing this, but again it is a bit complex. So, here is a simpler example of creating some common navigation for a site. This provides a link to the root of the site, and a link to the github repo where the site is hosted.
Under _includes
, create the file nav.html
. This example uses absolute links, but it would be possible to use relative links also.
<nav id="mainNav">
<table border="1" style="width:auto;">
<tr>
<td><a href="https://UCSB-CS56-pconrad.github.io">home</a></td>
<td><a href="https://github.com/UCSB-CS56-pconrad/UCSB-CS56-pconrad.github.io/">github repo</a></td>
</tr>
</table>
</nav>
Then, edit the _layouts/default.html
as shown to include this file:
<body id="page-top">
<div data-role="header">
<nav data-role="navbar" data-grid-"d">
<ul>
<li><a href="/" class="ui-btn-active">home</a></li>
<li><a href="https://github.com/pconrad-webapps">pconrad-webapps on Github</a></li>
<li><a href="https://www.cs.ucsb.edu/~pconrad">P. Conrad home page</a></li>
</ul>
</nav>
</div>
<h1 id="why-should-i-set-up-continuous-integration-for-a-jekyll-site-on-travis-ci">Why should I set up continuous integration for a <a href="/topics/jekyll/">Jekyll</a> site on Travis-CI</h1>
<p>Setting up a <a href="/topics/jekyll/">Jekyll</a> site on Github Pages is super convenient.</p>
<p>But one thing Github Pages didn’t provide is <em>access to the error messages</em> that you get when your site is set up
incorrectly. Instead, you just get a “404 unavailable” message, or your site doesn’t update (your changes don’t
seem to have any effect.)</p>
<p>The best way to remedy this—in fact its Github Pages own suggestion—is to set up continuous integration for
your Jekyll site via Travis-CI.</p>
<p>Doing so has these advantages:</p>
<ul>
<li>
<p>If you are using github pages, you won’t see the error messages if/when the site build is broken. TravisCI is the
one of two ways that Github Pages suggests for making these error messages visible. (The other way is to run Jekyll locally
on your own machine (setting up the site on localhost:4000).</p>
</li>
<li>
<p>When the site is broken, TravisCI will send you emails until the site is fixed.</p>
</li>
</ul>
<p>Here’s how to do it:</p>
<ol>
<li>
<p>Add a .travis.yml file to the root of your repo, with the following contents:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>language: ruby
rvm:
- 2.1.7
script: "bundle exec jekyll build"
</code></pre></div> </div>
</li>
<li>
<p>Make sure this line is in your _config.yml:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>exclude: [vendor]
</code></pre></div> </div>
</li>
<li>
<p>Visit https://travis-ci.org/ and login with your github.com credentials—the same ones you use to maintain the github repo where the github pages site is stored.</p>
</li>
</ol>
<p>…</p>
<p>Eventually, the page you’ll visit is the same page as for your github.com repo, except with github.com replaced with travis-ci.org</p>
<p>For example:</p>
<p>For the repo:</p>
<p>https://github.com/ucsd-cse-spis-2016/ucsd-cse-spis-2016.github.io</p>
<p>Access:</p>
<p>https://travis-ci.org/ucsd-cse-spis-2016/ucsd-cse-spis-2016.github.io</p>
Dates
Dates in Jekyll are formatted using the “Liquid filter”.
More information is here: https://help.shopify.com/themes/liquid/filters/additional-filters#date
An example is:
12-hour time (%I:%M:%S %p)
<!-- 03:20:07 PM -->
Syntax of Templates
The template used by Jekyll is called Liquid.
The following page summarizes the syntax of things such as if statements, for loops, variable assignment, etc:
https://github.com/Shopify/liquid/wiki/Liquid-for-Designers
Liquid Gotchas
A common problem I run into with using conditions in liquid templates is that you must have space around your operators.
CORRECT: {
% i
f page.textbook == "HFJ" %}
INCORRECT: {
% i
f page.textbook=="HFJ" %}
Fenced code blocks
- List of supported languages: https://github.com/jneen/rouge/wiki/List-of-supported-languages-and-lexers
Fenced code blocks with line numbers
http://stackoverflow.com/questions/25133223/jekyll-fenced-code-blocks-with-line-numbers
<figure class="highlight"><pre><code class="language-python" data-lang="python"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
</pre></td><td class="code"><pre><span class="c1"># code goes here</span>
</pre></td></tr></tbody></table></code></pre></figure>
Enabling LaTeX on a Jekyll Site on Github Pages
TBD
For loops and other control structures (using “liquid syntax”)
See: https://help.shopify.com/themes/liquid/objects
More on Jekyll
- Jekyll: On Heroku—Setting up a Jekyll site on Heroku (backup to Github Pages, use add-ons)
- Jekyll: pandoc conversion—Using pandoc to convert material in other formats to Jekyll Markdown