OSDN Git Service

Regular updates
[twpd/master.git] / jekyll.md
1 ---
2 title: Jekyll
3 jekyll_escape: true
4 layout: 2017/sheet
5 prism_languages: [bash, yaml, ruby]
6 category: Jekyll
7 updated: 2018-08-25
8 ---
9
10 {% raw %}
11
12 ### Installation
13
14 ```bash
15 # Install the gems
16 gem install jekyll bundler
17 ```
18
19 ```bash
20 # Create a new site at `./myblog`
21 jekyll new myblog
22 cd myblog
23 ```
24
25 ```bash
26 # Optional: if you're targeting github-pages,
27 # use this Gemfile instead.
28 cat > Gemfile <<-END
29 source 'https://rubygems.org'
30 gem 'github-pages', group: :jekyll_plugins
31 END
32 ```
33
34 ```bash
35 bundle exec jekyll serve
36 ```
37
38 See: [Jekyll quickstart](http://jekyllrb.com/docs/quickstart/)<br>
39 See: [github/pages-gem](https://github.com/github/pages-gem)
40
41 ### Directories
42
43 ```
44 ./
45 ├── _config.yml
46
47 ├── _data/
48 │   └── ...
49
50 ├── _drafts/
51 │   └── ...
52
53 ├── _posts/
54 │   └── 2014-01-01-hello.md
55
56 ├── _layouts/
57 │   ├── default.html
58 │   └── post.html
59
60 ├── _includes/             - partials
61 │   ├── header.html
62 │   └── footer.html
63
64 └── _site/
65     └── ...
66 ```
67 {: .-box-chars}
68
69 ## Front-matter
70 {: .-three-column}
71
72 ### Basic frontmatter
73
74 ```
75 ---
76 layout: post
77 title: Hello
78 ---
79 Hello! this is my post.
80 ```
81 {: data-line="1,2,3,4"}
82
83 Attach metadata to a page by adding them on top of the page, delimited by `---`.
84 See: [Front-matter](http://jekyllrb.com/docs/frontmatter/)
85
86 ### Other frontmatter stuff
87
88 ```yaml
89 permalink: '/hello'
90 published: false
91 category: apple
92 categories: ['html', 'css']
93 tags: ['html', 'css']
94 ```
95
96 ### Configuration
97
98 In `_config.yml`:
99 {: .-setup}
100
101 ```yaml
102 source: .
103 destination: _site
104 exclude:
105 - Gemfile
106 - Gemfile.lock
107 include: ['.htaccess']
108 ```
109
110 All config keys are optional.
111 See: [Configuration](http://jekyllrb.com/docs/configuration/)
112
113 Markup
114 ------
115 {: .-three-column}
116
117 ### Page variables
118
119 ```html
120 <title>
121   {{ page.title }}
122 </title>
123 ```
124 {: data-line="2"}
125
126
127 ### Filters
128
129 ```html
130 <p>
131   {{ page.description | truncate_words: 20 }}
132 </p>
133 ```
134 {: data-line="2"}
135
136 ### Loops
137
138 ```html
139 {% for post in site.posts %}
140   <a href="{{ post.url }}">
141     <h2>{{ post.title }}</h2>
142     <p>{{ post.date | date_to_string }}</p>
143   </a>
144 {% endfor %}
145 ```
146 {: data-line="1,6"}
147
148 ### Dates
149
150 ```html
151 {{ page.date | date: "%b %d, %Y" }}
152 ```
153
154 ### Conditionals
155
156 ```html
157 {% if page.image.feature %}
158   ...
159 {% elsif xyz %}
160   ...
161 {% else %}
162   ...
163 {% endif %}
164 ```
165 {: data-line="1,3,5,7 }
166
167 ```html
168 {% if page.category == 'React' %}
169 {% if page.category == 'React' or page.featured %}
170 {% if page.tags contains 'Featured' %}
171 ```
172
173 ### Case
174
175 ```html
176 {% case shipping.title %}
177   {% when 'international' %}
178      Arriving in 2-3 weeks
179   {% when 'Domestic' %}
180      Arriving in 2-3 days
181   {% else %}
182      Thank you for your order!
183 {% endcase %}
184 ```
185 {: data-line="1,2,4,6,8"}
186
187 ### Includes (partials)
188
189 ```
190 {% include header.html %}
191 ```
192 {: data-line="1"}
193
194 ```html
195 <!-- Including local vars -->
196 {% include header.html page=page %}
197 ```
198 {: data-line="2"}
199
200 ### Comments
201
202 ```html
203 {% comment %}
204   This is a comment!
205 {% endcomment %}
206 ```
207 {: data-line="1,3"}
208
209 ## Variables
210
211 ### Top-level variables
212
213
214 | `{{ site }}` | Data from `config.yml` |
215 | `{{ page }}` | From frontmatter, and page-specific info |
216 | `{{ content }}` | HTML content (use in layouts) |
217 | `{{ paginator }}` | Paginator |
218
219 See: [Variables](http://jekyllrb.com/docs/variables/)
220
221 ### Site
222
223 ```html
224 {{ site.time }}
225 ```
226 {: .-setup}
227
228 | `site.time` | Current time |
229 | `site.pages` | List of pages |
230 | `site.posts` | List of blog posts |
231 | `site.related_posts` | List of posts related to current |
232 | `site.categories.CATEGORY` | List |
233 | `site.tags.TAG` | List |
234 | `site.static_files` | List |
235
236 ### Page
237
238 ```html
239 {{ page.content }}  - un-rendered content
240 {{ page.title }}
241 {{ page.excerpt }}  - un-rendered excerpt
242 {{ page.url }}
243 {{ page.date }}
244 {{ page.id }}       - unique id for RSS feeds
245 {{ page.categories }}
246 {{ page.tags }}
247 {{ page.path }}
248 {{ page.dir }}
249 {{ page.excerpt | remove: '<p>' | remove: '</p>' }}
250 {{ page.excerpt | strip_html }}
251 ```
252
253 ```html
254 <!-- blog pagination: -->
255 {{ page.next }}
256 {{ page.previous }}
257 ```
258
259 Filters
260 -------
261 {: .-three-column}
262
263 ### Dates
264
265 ```ruby
266 {{ site.time | date: "%Y %m %d" }}
267 ```
268 {: .-setup}
269
270 | `date_to_xmlschema` | → `2008-11-07T13:07:54-08:00` |
271 | `date_to_rfc822` | → `Mon, 07 Nov 2008 13:07:54 -0800` |
272 | `date_to_string` | → `07 Nov 2008` |
273 | `date_to_long_string` | → `07 November 2008` |
274 | `date:` _'%Y %m %d'_ | → `2017 Nov  7` |
275
276 ### Preprocessors
277
278
279 ```ruby
280 {{ page.description | markdownify }}
281 ```
282 {: .-setup}
283
284 | Filter | Description |
285 | --- | --- |
286 | `textilize` | Textile |
287 | `markdownify` | Markdown |
288 | `jsonify` | JSON |
289 | `sassify` | Sass |
290 | `scssify` | SCSS |
291 | `smartify` | Smartypants |
292
293 ### Array filters
294
295 ```ruby
296 {{ site.pages | where: "year", "2014" }}
297 ```
298 {: .-setup}
299
300 | Filter | Description |
301 | --- | --- |
302 | `where:` _"year", "2014"_ | |
303 | `where_exp:` _"item", "item.year >= 2014"_ | |
304 | --- | --- |
305 | `group_by:` _"genre"_   | → `{name, items}` |
306 | `group_by_exp:` _"item", "item.genre"_   | → `{name, items}` |
307 | --- | --- |
308 | `sort` | |
309 | `sort:` _'author'_ | |
310 | --- | --- |
311 | `uniq` | |
312 | --- | --- |
313 | `first` | |
314 | `last` | |
315 | `join:` _','_ | |
316 | `array_to_sentence_string` | → `"X, Y and Z"` |
317 | --- | --- |
318 | `map:` _'post'_ | Works like 'pluck' |
319 | --- | --- |
320 | `size` | |
321 | `push:` _'xxx'_ | Adds an item |
322
323 ### String filters
324
325 ```ruby
326 {{ page.title | default: "xxx" }}
327 ```
328 {: .-setup}
329
330 | Filter                             | Description |
331 | ---                                | ---         |
332 | `default:` _'xxx'_                 |             |
333 | ---                                | ---         |
334 | `upcase`                           |             |
335 | `downcase`                         |             |
336 | ---                                | ---         |
337 | `remove:` _'p'_                    |             |
338 | `replace:` _'super', 'mega'_       |             |
339 | `remove_first:` _'p'_              |             |
340 | `replace_first:` _'super', 'mega'_ |             |
341 | ---                                | ---         |
342 | `truncate:` _5_                    |             |
343 | `truncatewords:` _20_              |             |
344 | ---                                | ---         |
345 | `prepend:` _'Mr. '_                |             |
346 | `append:` _'Jr.'_                  |             |
347 | ---                                | ---         |
348 | `camelize`                         |             |
349 | `capitalize`                       |             |
350 | `strip_html`                       |             |
351 | `strip_newlines`                   |             |
352 | `newlines_to_br`                   |             |
353 | ---                                | ---         |
354 | `split:` _','_                     |             |
355 | ---                                | ---         |
356 | `escape`                           |             |
357 | `escape_once`                      |             |
358 | ---                                | ---         |
359 | `slice:` _-3, 3_                   |             |
360
361 See: [String filters](http://docs.shopify.com/themes/liquid-documentation/filters)
362
363 ### String filters (Jekyll-only)
364
365 ```ruby
366 {{ page.excerpt | number_of_words }}
367 ```
368 {: .-setup}
369
370 | Filter | Description |
371 | --- | --- |
372 | `number_of_words` | |
373 | `slugify` | |
374 | --- | --- |
375 | `xml_escape` | → `CDATA` |
376 | `cgi_escape` | → `foo%2Cbar` |
377 | `uri_escape` | → `foo,%20bar` |
378
379 ### Numbers
380
381 ```
382 {{ site.posts.size | minus: 2 }}
383 ```
384 {: .-setup}
385
386 | Filter | Description |
387 | --- | --- |
388 | `minus:` _2_ | |
389 | `plus:` _2_ | |
390 | `times:` _2_ | |
391 | `divided_by:` _2_ | |
392 | `modulo:` _2_ | |
393 | --- | --- |
394 | `ceil` | |
395 | `floor` | |
396 | `round` | |
397
398 ## Paginator
399
400 ### Paginator setup
401
402 Add this to `_config.yml`:
403 {: .-setup}
404
405 ```yml
406 paginate: 5
407 paginate_path: "blog/:num"
408 ```
409
410 See: [Paginator](http://jekyllrb.com/docs/pagination/)
411
412 ### Numbers
413
414 ```
415 {{ paginator.page }}         - page number
416 {{ paginator.total_posts }}
417 {{ paginator.total_pages }}
418 {{ paginator.per_page }}
419 ```
420
421 ### Iterating through posts
422
423 ```
424 {% for post in paginator.posts %} ... {% endfor %}
425 ```
426
427 ### Previous button
428
429 ```
430 {% if paginator.total_pages > 1 %}
431   {% if paginator.previous_page %}
432     <a href="{{ paginator.previous_page_path }}">Previous</a>
433   {% else %}
434   {% endif %}
435 {% endif %}
436 ```
437
438 ```
439 {{ paginator.next_page }}     - page number
440 {{ paginator.next_page_path }}
441 ```
442
443 ## Blogging
444
445 ### Paths
446
447     _posts/YEAR-MONTH-DAY-title.md
448
449 See: [Blogging](http://jekyllrb.com/docs/posts/)
450
451 ### Image paths
452
453     ![My helpful screenshot]({{ site.url }}/assets/screenshot.jpg)
454
455 See: [Image paths](http://jekyllrb.com/docs/posts/#including-images-and-resources)
456
457 ### Drafts
458
459     vi _drafts/a-draft-post.md
460     jekyll build --drafts
461
462 Posts in `_drafts` only show up in development, but not production.
463 See: [Drafts](http://jekyllrb.com/docs/drafts/)
464
465 ### Defining excerpts
466
467 ```
468 ---
469 title: My blog post
470 excerpt: This post is about cats
471 ---
472
473 Hello, let's talk about cats. (···)
474 ```
475
476 Put a key `excerpt` in the frontmatter.
477 See: [Excerpts](http://jekyllrb.com/docs/posts/#post-excerpts)
478
479 ### Displaying excerpts
480
481 ```html
482 {{ post.excerpt }}
483 ```
484
485 ```html
486 {{ post.excerpt | remove: '<p>' | remove: '</p>' }}
487 {{ post.excerpt | strip_html }}
488 ```
489
490 ### Excerpt separator
491
492 ```html
493 ---
494 excerpt_separator: <!--more-->
495 ---
496
497 Excerpt here
498 <!--more-->
499 More post body here
500 ```
501
502 Alternatively, you can put excerpts inline in your post by defining `excerpt_separator`.
503
504 ### Permalinks
505
506     # _config.yml
507     permalink: date   # /:categories/:year/:month/:day/:title.html
508     permalink: pretty # /:categories/:year/:month/:day/:title/
509     permalink: none   # /:categories/:title.html
510     permalink: "/:title"
511
512 See: [Permalinks](http://jekyllrb.com/docs/permalinks/)
513
514 ## More features
515
516 ### Data
517
518 ```
519 _data/members.yml
520 ```
521 {: .-setup}
522
523 ```
524 {% for member in site.data.members %}
525   ...
526 {% endfor %}
527 ```
528
529 See: [Data](http://jekyllrb.com/docs/datafiles/)
530
531 ### Collections
532
533 ```yml
534 # _config.yml
535 collections:
536   - authors
537 ```
538 {: .-setup}
539
540 ```yml
541 # _/authors/a-n-roquelaire.md
542 ---
543 name: A. N. Roquelaire
544 real_name: Anne Rice
545 ---
546 ```
547
548 ```
549 {% for author in site.authors %}
550 ```
551
552 See: [Collections](http://jekyllrb.com/docs/collections/)
553
554 ### Code highlighter
555
556 ```html
557 {% highlight ruby linenos %}
558 def show
559   ...
560 end
561 {% endhighlight %}
562 ```
563
564 Integration
565 -----------
566
567 ### Bundler
568
569 In `_plugins/bundler.rb`:
570 {: .-setup}
571
572 ```ruby
573 require "bunder/setup"
574 Bundler.require :default
575 ```
576
577 ### Compass
578
579   * [Compass](https://gist.github.com/parkr/2874934)
580   * [Asset pipeline](https://github.com/matthodan/jekyll-asset-pipeline)
581
582 Also see
583 --------
584 {: .-one-column}
585
586 * [Jekyll docs](http://jekyllrb.com/docs/home/) _jekyllrb.com_
587 * [CloudCannon Jekyll cheatsheet](https://learn.cloudcannon.com/jekyll-cheat-sheet/) _cloudcannon.com_
588 * [Jekyll: templates](http://jekyllrb.com/docs/templates/) _jekyllrb.com_
589 * [Liquid: output](http://docs.shopify.com/themes/liquid-basics/output) _shopify.com_
590 * [Liquid: logic](http://docs.shopify.com/themes/liquid-basics/logic) _shopify.com_
591 * [Liquid: filters](http://docs.shopify.com/themes/liquid-documentation/filters) _shopify.com_
592 * [Liquid for designers](https://github.com/Shopify/liquid/wiki/Liquid-for-Designers) _github.com/Shopify_
593 {: .-also-see}
594
595 {% endraw %}