cheatsheets/jekyll.md

595 lines
11 KiB
Markdown
Raw Permalink Normal View History

2013-10-14 01:55:21 +00:00
---
title: Jekyll
2014-06-15 16:03:39 +00:00
jekyll_escape: true
2017-09-01 19:01:59 +00:00
prism_languages: [bash, yaml, ruby]
2017-08-28 17:25:55 +00:00
category: Jekyll
2020-07-04 13:33:09 +00:00
updated: 2018-08-25
2013-10-14 01:55:21 +00:00
---
2014-07-14 11:13:42 +00:00
{% raw %}
2017-08-27 06:27:00 +00:00
2013-10-14 01:55:21 +00:00
### Installation
2017-08-27 06:27:00 +00:00
```bash
# Install the gems
gem install jekyll bundler
```
```bash
# Create a new site at `./myblog`
jekyll new myblog
cd myblog
```
```bash
# Optional: if you're targeting github-pages,
# use this Gemfile instead.
2017-08-30 10:24:20 +00:00
cat > Gemfile <<-END
source 'https://rubygems.org'
gem 'github-pages', group: :jekyll_plugins
2017-08-30 10:24:20 +00:00
END
2017-08-27 06:27:00 +00:00
```
```bash
bundle exec jekyll serve
```
See: [Jekyll quickstart](https://jekyllrb.com/docs/quickstart/)<br>
2017-08-27 06:27:00 +00:00
See: [github/pages-gem](https://github.com/github/pages-gem)
2013-10-14 01:55:21 +00:00
### Directories
```
./
├── _config.yml
├── _data/
│ └── ...
├── _drafts/
│ └── ...
├── _posts/
│ └── 2014-01-01-hello.md
├── _layouts/
│ ├── default.html
│ └── post.html
├── _includes/ - partials
│ ├── header.html
│ └── footer.html
└── _site/
└── ...
```
{: .-box-chars}
2013-10-14 01:55:21 +00:00
2017-08-27 06:27:00 +00:00
## Front-matter
{: .-three-column}
### Basic frontmatter
2013-10-14 01:55:21 +00:00
2017-08-30 11:29:42 +00:00
```
---
layout: post
title: Hello
---
Hello! this is my post.
```
{: data-line="1,2,3,4"}
2013-10-14 01:55:21 +00:00
2017-08-30 11:29:42 +00:00
Attach metadata to a page by adding them on top of the page, delimited by `---`.
See: [Front-matter](https://jekyllrb.com/docs/frontmatter/)
2017-08-27 06:27:00 +00:00
2013-10-14 01:55:21 +00:00
### Other frontmatter stuff
2017-08-30 11:29:42 +00:00
```yaml
permalink: '/hello'
published: false
category: apple
categories: ['html', 'css']
tags: ['html', 'css']
```
2013-10-14 01:55:21 +00:00
2017-08-27 06:27:00 +00:00
### Configuration
2013-10-14 01:55:21 +00:00
2017-08-30 11:29:42 +00:00
In `_config.yml`:
{: .-setup}
```yaml
source: .
destination: _site
exclude:
- Gemfile
- Gemfile.lock
include: ['.htaccess']
```
2013-10-14 01:55:21 +00:00
All config keys are optional.
See: [Configuration](https://jekyllrb.com/docs/configuration/)
2017-08-27 06:27:00 +00:00
Markup
------
2017-09-07 18:58:49 +00:00
{: .-three-column}
2017-08-27 06:27:00 +00:00
### Page variables
```html
2017-09-07 18:58:49 +00:00
<title>
{{ page.title }}
</title>
2017-08-27 06:27:00 +00:00
```
2017-09-07 18:58:49 +00:00
{: data-line="2"}
### Filters
2017-08-27 06:27:00 +00:00
```html
2017-09-07 18:58:49 +00:00
<p>
{{ page.description | truncate_words: 20 }}
</p>
2017-08-27 06:27:00 +00:00
```
2017-09-07 18:58:49 +00:00
{: data-line="2"}
2017-08-27 06:27:00 +00:00
### Loops
2017-09-07 18:58:49 +00:00
```html
{% for post in site.posts %}
<a href="{{ post.url }}">
<h2>{{ post.title }}</h2>
<p>{{ post.date | date_to_string }}</p>
2017-09-07 18:58:49 +00:00
</a>
{% endfor %}
```
{: data-line="1,6"}
2017-08-27 06:27:00 +00:00
### Dates
2017-09-07 18:58:49 +00:00
```html
{{ page.date | date: "%b %d, %Y" }}
```
2017-08-27 06:27:00 +00:00
2017-08-28 16:37:00 +00:00
### Conditionals
2017-08-27 06:27:00 +00:00
2017-08-28 16:37:00 +00:00
```html
{% if page.image.feature %}
2017-09-07 18:58:49 +00:00
...
{% elsif xyz %}
...
2017-08-28 16:37:00 +00:00
{% else %}
2017-09-07 18:58:49 +00:00
...
2017-08-28 16:37:00 +00:00
{% endif %}
```
2017-09-07 18:58:49 +00:00
{: data-line="1,3,5,7 }
2017-08-28 16:37:00 +00:00
```html
{% if page.category == 'React' %}
{% if page.category == 'React' or page.featured %}
{% if page.tags contains 'Featured' %}
```
### Case
```html
{% case shipping.title %}
{% when 'international' %}
Arriving in 2-3 weeks
{% when 'Domestic' %}
Arriving in 2-3 days
{% else %}
Thank you for your order!
{% endcase %}
```
2017-09-07 18:58:49 +00:00
{: data-line="1,2,4,6,8"}
2017-08-27 06:27:00 +00:00
### Includes (partials)
```
{% include header.html %}
```
2017-09-07 18:58:49 +00:00
{: data-line="1"}
2017-08-27 06:27:00 +00:00
```html
<!-- Including local vars -->
{% include header.html page=page %}
```
2017-09-07 18:58:49 +00:00
{: data-line="2"}
2017-08-27 06:27:00 +00:00
### Comments
```html
{% comment %}
2017-09-07 18:58:49 +00:00
This is a comment!
2017-08-27 06:27:00 +00:00
{% endcomment %}
```
2017-09-07 18:58:49 +00:00
{: data-line="1,3"}
2017-08-27 06:27:00 +00:00
2017-08-30 14:19:40 +00:00
## Variables
2017-08-27 06:27:00 +00:00
### Top-level variables
2013-10-14 01:55:21 +00:00
2017-08-30 14:19:40 +00:00
| `{{ site }}` | Data from `config.yml` |
| `{{ page }}` | From frontmatter, and page-specific info |
| `{{ content }}` | HTML content (use in layouts) |
| `{{ paginator }}` | Paginator |
2013-10-14 01:55:21 +00:00
See: [Variables](https://jekyllrb.com/docs/variables/)
2017-08-27 06:27:00 +00:00
2013-10-14 01:55:21 +00:00
### Site
2017-08-30 14:19:40 +00:00
```html
{{ site.time }}
```
{: .-setup}
2013-10-14 01:55:21 +00:00
2017-08-30 14:19:40 +00:00
| `site.time` | Current time |
| `site.pages` | List of pages |
| `site.posts` | List of blog posts |
| `site.related_posts` | List of posts related to current |
| `site.categories.CATEGORY` | List |
| `site.tags.TAG` | List |
| `site.static_files` | List |
2014-06-15 16:03:39 +00:00
2013-10-14 01:55:21 +00:00
### Page
2017-08-27 06:27:00 +00:00
```html
{{ page.content }} - un-rendered content
{{ page.title }}
{{ page.excerpt }} - un-rendered excerpt
{{ page.url }}
{{ page.date }}
{{ page.id }} - unique id for RSS feeds
{{ page.categories }}
{{ page.tags }}
{{ page.path }}
2017-08-30 14:19:40 +00:00
{{ page.dir }}
{{ page.excerpt | remove: '<p>' | remove: '</p>' }}
{{ page.excerpt | strip_html }}
2017-08-27 06:27:00 +00:00
```
2014-06-15 16:03:39 +00:00
2017-08-27 06:27:00 +00:00
```html
<!-- blog pagination: -->
{{ page.next }}
{{ page.previous }}
```
2014-06-15 16:03:39 +00:00
2017-08-30 14:19:40 +00:00
Filters
-------
{: .-three-column}
### Dates
```ruby
{{ site.time | date: "%Y %m %d" }}
```
{: .-setup}
| `date_to_xmlschema` | → `2008-11-07T13:07:54-08:00` |
| `date_to_rfc822` | → `Mon, 07 Nov 2008 13:07:54 -0800` |
| `date_to_string` | → `07 Nov 2008` |
| `date_to_long_string` | → `07 November 2008` |
| `date:` _'%Y %m %d'_ | → `2017 Nov 7` |
### Preprocessors
```ruby
{{ page.description | markdownify }}
```
{: .-setup}
| Filter | Description |
| --- | --- |
| `textilize` | Textile |
| `markdownify` | Markdown |
| `jsonify` | JSON |
| `sassify` | Sass |
| `scssify` | SCSS |
| `smartify` | Smartypants |
### Array filters
```ruby
{{ site.pages | where: "year", "2014" }}
```
{: .-setup}
| Filter | Description |
| --- | --- |
| `where:` _"year", "2014"_ | |
| `where_exp:` _"item", "item.year >= 2014"_ | |
| --- | --- |
| `group_by:` _"genre"_ | → `{name, items}` |
| `group_by_exp:` _"item", "item.genre"_ | → `{name, items}` |
| --- | --- |
| `sort` | |
| `sort:` _'author'_ | |
| --- | --- |
| `uniq` | |
| --- | --- |
| `first` | |
| `last` | |
| `join:` _','_ | |
2017-08-31 18:16:34 +00:00
| `array_to_sentence_string` | → `"X, Y and Z"` |
2017-08-30 14:19:40 +00:00
| --- | --- |
| `map:` _'post'_ | Works like 'pluck' |
| --- | --- |
| `size` | |
| `push:` _'xxx'_ | Adds an item |
### String filters
```ruby
{{ page.title | default: "xxx" }}
```
{: .-setup}
| Filter | Description |
| --- | --- |
| `default:` _'xxx'_ | |
| --- | --- |
| `upcase` | |
| `downcase` | |
| --- | --- |
| `remove:` _'p'_ | |
| `replace:` _'super', 'mega'_ | |
| `remove_first:` _'p'_ | |
| `replace_first:` _'super', 'mega'_ | |
| --- | --- |
| `truncate:` _5_ | |
| `truncatewords:` _20_ | |
| --- | --- |
| `prepend:` _'Mr. '_ | |
| `append:` _'Jr.'_ | |
| --- | --- |
| `camelize` | |
| `capitalize` | |
| `strip_html` | |
| `strip_newlines` | |
| `newlines_to_br` | |
| --- | --- |
| `split:` _','_ | |
| --- | --- |
| `escape` | |
| `escape_once` | |
| --- | --- |
| `slice:` _-3, 3_ | |
See: [String filters](https://docs.shopify.com/themes/liquid-documentation/filters)
2017-08-30 14:19:40 +00:00
### String filters (Jekyll-only)
```ruby
{{ page.excerpt | number_of_words }}
```
{: .-setup}
| Filter | Description |
| --- | --- |
| `number_of_words` | |
| `slugify` | |
| --- | --- |
| `xml_escape` | → `CDATA` |
| `cgi_escape` | → `foo%2Cbar` |
| `uri_escape` | → `foo,%20bar` |
### Numbers
```
{{ site.posts.size | minus: 2 }}
```
{: .-setup}
| Filter | Description |
| --- | --- |
| `minus:` _2_ | |
| `plus:` _2_ | |
| `times:` _2_ | |
| `divided_by:` _2_ | |
| `modulo:` _2_ | |
| --- | --- |
| `ceil` | |
| `floor` | |
| `round` | |
2017-08-27 06:27:00 +00:00
## Paginator
2013-10-14 01:55:21 +00:00
2017-08-27 06:27:00 +00:00
### Paginator setup
2014-06-15 16:03:39 +00:00
Add this to `_config.yml`:
2017-08-27 06:27:00 +00:00
{: .-setup}
2014-06-15 16:03:39 +00:00
2017-08-27 06:27:00 +00:00
```yml
paginate: 5
paginate_path: "blog/:num"
```
2014-06-15 16:03:39 +00:00
See: [Paginator](https://jekyllrb.com/docs/pagination/)
2014-06-15 16:03:39 +00:00
2017-08-27 06:27:00 +00:00
### Numbers
2013-10-14 01:55:21 +00:00
2017-08-27 06:27:00 +00:00
```
{{ paginator.page }} - page number
{{ paginator.total_posts }}
{{ paginator.total_pages }}
{{ paginator.per_page }}
```
2013-10-14 01:55:21 +00:00
2017-08-27 06:27:00 +00:00
### Iterating through posts
2013-10-14 01:55:21 +00:00
2017-08-27 06:27:00 +00:00
```
{% for post in paginator.posts %} ... {% endfor %}
```
2013-10-14 01:55:21 +00:00
2017-08-27 06:27:00 +00:00
### Previous button
2013-10-14 01:55:21 +00:00
2017-08-27 06:27:00 +00:00
```
{% if paginator.total_pages > 1 %}
{% if paginator.previous_page %}
<a href="{{ paginator.previous_page_path }}">Previous</a>
{% else %}
{% endif %}
{% endif %}
```
2013-10-14 01:55:21 +00:00
2017-08-27 06:27:00 +00:00
```
{{ paginator.next_page }} - page number
{{ paginator.next_page_path }}
```
2013-10-14 01:55:21 +00:00
2017-08-27 06:27:00 +00:00
## Blogging
2013-10-14 01:55:21 +00:00
2017-08-27 06:27:00 +00:00
### Paths
2014-06-15 16:03:39 +00:00
_posts/YEAR-MONTH-DAY-title.md
See: [Blogging](https://jekyllrb.com/docs/posts/)
2017-08-27 06:27:00 +00:00
### Image paths
2014-07-14 11:13:42 +00:00
![My helpful screenshot]({{ site.url }}/assets/screenshot.jpg)
2014-06-15 16:03:39 +00:00
See: [Image paths](https://jekyllrb.com/docs/posts/#including-images-and-resources)
2017-08-27 06:27:00 +00:00
### Drafts
2014-06-15 16:03:39 +00:00
vi _drafts/a-draft-post.md
jekyll build --drafts
2017-08-27 06:38:58 +00:00
Posts in `_drafts` only show up in development, but not production.
See: [Drafts](https://jekyllrb.com/docs/drafts/)
2017-08-27 06:27:00 +00:00
2017-08-27 06:38:58 +00:00
### Defining excerpts
2015-03-11 08:15:23 +00:00
2017-08-27 06:38:58 +00:00
```
---
title: My blog post
excerpt: This post is about cats
---
Hello, let's talk about cats. (···)
```
Put a key `excerpt` in the frontmatter.
See: [Excerpts](https://jekyllrb.com/docs/posts/#post-excerpts)
2017-08-27 06:38:58 +00:00
### Displaying excerpts
```html
{{ post.excerpt }}
```
```html
{{ post.excerpt | remove: '<p>' | remove: '</p>' }}
{{ post.excerpt | strip_html }}
```
2015-03-11 08:15:23 +00:00
### Excerpt separator
2017-08-27 06:38:58 +00:00
```html
---
excerpt_separator: <!--more-->
---
2015-03-11 08:15:23 +00:00
2017-08-27 06:38:58 +00:00
Excerpt here
<!--more-->
More post body here
```
2015-03-11 08:15:23 +00:00
2017-08-27 06:38:58 +00:00
Alternatively, you can put excerpts inline in your post by defining `excerpt_separator`.
### Permalinks
2014-06-15 16:03:39 +00:00
# _config.yml
permalink: date # /:categories/:year/:month/:day/:title.html
permalink: pretty # /:categories/:year/:month/:day/:title/
permalink: none # /:categories/:title.html
permalink: "/:title"
See: [Permalinks](https://jekyllrb.com/docs/permalinks/)
2017-08-27 06:38:58 +00:00
2017-08-27 06:27:00 +00:00
## More features
### Data
```
_data/members.yml
```
{: .-setup}
2014-06-15 16:03:39 +00:00
2017-08-27 06:27:00 +00:00
```
{% for member in site.data.members %}
...
{% endfor %}
```
2014-06-15 16:03:39 +00:00
See: [Data](https://jekyllrb.com/docs/datafiles/)
2014-06-15 16:03:39 +00:00
2017-08-27 06:27:00 +00:00
### Collections
2015-03-11 08:15:23 +00:00
2017-08-27 06:27:00 +00:00
```yml
2015-03-11 08:15:23 +00:00
# _config.yml
collections:
- authors
```
2017-08-27 06:27:00 +00:00
{: .-setup}
2015-03-11 08:15:23 +00:00
2017-08-27 06:27:00 +00:00
```yml
2015-03-11 08:15:23 +00:00
# _/authors/a-n-roquelaire.md
---
name: A. N. Roquelaire
real_name: Anne Rice
---
```
```
{% for author in site.authors %}
```
See: [Collections](https://jekyllrb.com/docs/collections/)
2017-08-27 06:27:00 +00:00
2017-08-30 11:29:42 +00:00
### Code highlighter
```html
{% highlight ruby linenos %}
def show
...
end
{% endhighlight %}
```
2013-10-14 01:55:21 +00:00
Integration
-----------
### Bundler
2017-08-27 06:27:00 +00:00
In `_plugins/bundler.rb`:
2017-08-30 11:29:42 +00:00
{: .-setup}
2017-08-27 06:27:00 +00:00
2017-08-30 11:29:42 +00:00
```ruby
2017-08-27 06:27:00 +00:00
require "bunder/setup"
Bundler.require :default
```
2013-10-14 01:55:21 +00:00
### Compass
2013-10-14 02:36:58 +00:00
* [Compass](https://gist.github.com/parkr/2874934)
* [Asset pipeline](https://github.com/matthodan/jekyll-asset-pipeline)
2014-06-15 16:03:39 +00:00
2017-08-27 06:27:00 +00:00
Also see
--------
{: .-one-column}
* [Jekyll docs](https://jekyllrb.com/docs/home/) _jekyllrb.com_
2017-08-29 15:35:17 +00:00
* [CloudCannon Jekyll cheatsheet](https://learn.cloudcannon.com/jekyll-cheat-sheet/) _cloudcannon.com_
* [Jekyll: templates](https://jekyllrb.com/docs/templates/) _jekyllrb.com_
* [Liquid: output](https://docs.shopify.com/themes/liquid-basics/output) _shopify.com_
* [Liquid: logic](https://docs.shopify.com/themes/liquid-basics/logic) _shopify.com_
* [Liquid: filters](https://docs.shopify.com/themes/liquid-documentation/filters) _shopify.com_
2017-08-27 06:27:00 +00:00
* [Liquid for designers](https://github.com/Shopify/liquid/wiki/Liquid-for-Designers) _github.com/Shopify_
{: .-also-see}
2014-06-15 16:03:39 +00:00
2014-07-14 11:13:42 +00:00
{% endraw %}