Document Jekyll Filters with YAML data (#7335)

Merge pull request 7335
This commit is contained in:
Ashwin Maroli 2018-10-24 16:38:00 +05:30 committed by jekyllbot
parent fe74976942
commit 7422b741c1
2 changed files with 321 additions and 428 deletions

View File

@ -0,0 +1,298 @@
#
# ---------------------------------------------------------------------------------------
# List of Liquid Filters provided by Jekyll Core that will be utilized for their
# documentation.
#
# To document a new filter, create a new "list-item" below with the following keys:
# name: : [REQUIRED] A string label that identifies the filter
# description: : [REQUIRED] A short description of what to expect from the filter
# version_badge: : [OPTIONAL] Jekyll version that introduced the filter
# examples: : [REQUIRED] A 'nested list' comprised of inputs and outputs
# input: : [REQUIRED] The filter syntax and usage
# output: : [OPTIONAL] The output from the filter
#
# Tip: Use YAML Block notations to "fold" a long string, or to "break" a long string
# to the following line. Block notations can also be used to avoid having to use
# backslashes to escape quotes.
# ---------------------------------------------------------------------------------------
#
- name: Relative URL
description: >-
Prepend the <code>baseurl</code> value to the input. Useful if
your site is hosted at a subpath rather than the root of the domain.
examples:
- input: '{{ "/assets/style.css" | relative_url }}'
output: '/my-baseurl/assets/style.css'
#
- name: Absolute URL
description: Prepend the <code>url</code> and <code>baseurl</code> value to the input.
examples:
- input: '{{ "/assets/style.css" | absolute_url }}'
output: 'http://example.com/my-baseurl/assets/style.css'
#
- name: Date to XML Schema
description: Convert a Date into XML Schema (ISO 8601) format.
examples:
- input: '{{ site.time | date_to_xmlschema }}'
output: '2008-11-07T13:07:54-08:00'
#
- name: Date to RFC-822 Format
description: Convert a Date into the RFC-822 format used for RSS feeds.
examples:
- input: '{{ site.time | date_to_rfc822 }}'
output: 'Mon, 07 Nov 2008 13:07:54 -0800'
#
- name: Date to String
description: Convert a date to short format.
examples:
- input: '{{ site.time | date_to_string }}'
output: '07 Nov 2008'
#
- name: Date to String in ordinal US style
description: 'Format a date to ordinal, US, short format.'
version_badge: 3.8.0
examples:
- input: '{{ site.time | date_to_string: "ordinal", "US" }}'
output: 'Nov 7th, 2008'
#
- name: Date to Long String
description: Format a date to long format.
examples:
- input: '{{ site.time | date_to_long_string }}'
output: '07 November 2008'
#
- name: Date to Long String in ordinal UK style
description: 'Format a date to ordinal, UK, long format.'
version_badge: 3.8.0
examples:
- input: '{{ site.time | date_to_long_string: "ordinal" }}'
output: '7th November 2008'
#
- name: Where
description: Select all the objects in an array where the key has the given value.
examples:
- input: '{{ site.members | where:"graduation_year","2014" }}'
output:
#
- name: Where Expression
description: Select all the objects in an array where the expression is true.
version_badge: 3.2.0
examples:
- input: |-
{{ site.members | where_exp:"item",
"item.graduation_year == 2014" }}
output:
- input: |-
{{ site.members | where_exp:"item",
"item.graduation_year < 2014" }}
output:
- input: |-
{{ site.members | where_exp:"item",
"item.projects contains 'foo'" }}
output:
#
- name: Group By
description: Group an array's items by a given property.
examples:
- input: '{{ site.members | group_by:"graduation_year" }}'
output: |-
[{"name"=>"2013", "items"=>[...]},
{"name"=>"2014", "items"=>[...]}]
#
- name: Group By Expression
description: Group an array's items using a Liquid expression.
version_badge: 3.4.0
examples:
- input: |-
{{ site.members | group_by_exp: "item",
"item.graduation_year | truncate 3, ''" }}
output: |-
[{"name"=>"201...", "items"=>[...]},
{"name"=>"200...", "items"=>[...]}]
#
- name: XML Escape
description: Escape some text for use in XML.
examples:
- input: '{{ page.content | xml_escape }}'
output:
#
- name: CGI Escape
description: >-
CGI escape a string for use in a URL. Replaces any special characters
with appropriate <code>%XX</code> replacements. CGI escape normally
replaces a space with a plus <code>+</code> sign.
examples:
- input: '{{ "foo, bar; baz?" | cgi_escape }}'
output: 'foo%2C+bar%3B+baz%3F'
#
- name: URI Escape
description: >-
Percent encodes any special characters in a URI.
URI escape normally replaces a space with <code>%20</code>.
<a href="https://en.wikipedia.org/wiki/Percent-encoding#Types_of_URI_characters">Reserved characters</a>
will not be escaped.
examples:
- input: '{{ "http://foo.com/?q=foo, \bar?" | uri_escape }}'
output: 'http://foo.com/?q=foo,%20%5Cbar?'
#
- name: Number of Words
description: Count the number of words in some text.
examples:
- input: '{{ page.content | number_of_words }}'
output: 1337
#
- name: Array to Sentence
description: >-
Convert an array into a sentence. Useful for listing tags.
Optional argument for connector.
examples:
- input: '{{ page.tags | array_to_sentence_string }}'
output: 'foo, bar, and baz'
- input: '{{ page.tags | array_to_sentence_string: "or" }}'
output: 'foo, bar, or baz'
#
- name: Markdownify
description: Convert a Markdown-formatted string into HTML.
examples:
- input: '{{ page.excerpt | markdownify }}'
output:
#
- name: Smartify
description: 'Convert "quotes" into &ldquo;smart quotes.&rdquo;'
examples:
- input: '{{ page.title | smartify }}'
output:
#
- name: Converting Sass/SCSS
description: Convert a Sass- or SCSS-formatted string into CSS.
examples:
- input: '{{ some_sass | sassify }}'
output:
- input: '{{ some_scss | scssify }}'
output:
#
- name: Slugify
description: Convert a string into a lowercase URL "slug". See below for options.
examples:
- input: '{{ "The _config.yml file" | slugify }}'
output: 'the-config-yml-file'
- input: '{{ "The _config.yml file" | slugify: "pretty" }}'
output: 'the-_config.yml-file'
- input: '{{ "The _cönfig.yml file" | slugify: "ascii" }}'
output: 'the-c-nfig-yml-file'
- input: '{{ "The cönfig.yml file" | slugify: "latin" }}'
output: 'the-config-yml-file'
#
- name: Data To JSON
description: Convert Hash or Array to JSON.
examples:
- input: '{{ site.data.projects | jsonify }}'
output:
#
- name: Normalize Whitespace
description: Replace any occurrence of whitespace with a single space.
examples:
- input: '{{ "a \n b" | normalize_whitespace }}'
output:
#
- name: Sort
description: >-
Sort an array. Optional arguments for hashes
1.&nbsp;property name
2.&nbsp;nils order (<em>first</em> or <em>last</em>).
examples:
- input: '{{ page.tags | sort }}'
output:
- input: '{{ site.posts | sort: "author" }}'
output:
- input: '{{ site.pages | sort: "title", "last" }}'
output:
#
- name: Sample
description: 'Pick a random value from an array. Optionally, pick multiple values.'
examples:
- input: '{{ site.pages | sample }}'
output:
- input: '{{ site.pages | sample: 2 }}'
output:
#
- name: To Integer
description: Convert a string or boolean to integer.
examples:
- input: '{{ some_var | to_integer }}'
output:
#
- name: Array Filters
description: >-
Push, pop, shift, and unshift elements from an Array.
These are <strong>NON-DESTRUCTIVE</strong>, i.e. they do not mutate the array,
but rather make a copy and mutate that.
examples:
- input: '{{ page.tags | push: "Spokane" }}'
output: '["Seattle", "Tacoma", "Spokane"]'
- input: '{{ page.tags | pop }}'
output: '["Seattle"]'
- input: '{{ page.tags | shift }}'
output: '["Tacoma"]'
- input: '{{ page.tags | unshift: "Olympia" }}'
output: '["Olympia", "Seattle", "Tacoma"]'
#
- name: Inspect
description: Convert an object into its String representation for debugging.
examples:
- input: '{{ some_var | inspect }}'
output:

View File

@ -52,8 +52,8 @@ shopify_filters:
- url_encode
---
All of the standard Liquid
[filters](#standard-liquid-filters) are supported (see below).
All of the standard Liquid [filters](#standard-liquid-filters) are supported (see below).
To make common tasks easier, Jekyll even adds a few handy filters of its own,
all of which you can find on this page. You can also create your own filters
using [plugins](/docs/plugins/).
@ -67,432 +67,27 @@ using [plugins](/docs/plugins/).
</tr>
</thead>
<tbody>
{% for filter in site.data.jekyll_filters %}
<tr>
<td>
<p class="name"><strong>Relative URL</strong></p>
<p>Prepend the <code>baseurl</code> value to the input. Useful if your site is hosted at a subpath rather than the root of the domain.</p>
</td>
<td class="align-center">
<p class="name"><strong>{{ filter.name }}</strong></p>
<p>
<code class="filter">{% raw %}{{ "/assets/style.css" | relative_url }}{% endraw %}</code>
</p>
<p>
<code class="output">/my-baseurl/assets/style.css</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Absolute URL</strong></p>
<p>Prepend the <code>url</code> and <code>baseurl</code> value to the input.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ "/assets/style.css" | absolute_url }}{% endraw %}</code>
</p>
<p>
<code class="output">http://example.com/my-baseurl/assets/style.css</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Date to XML Schema</strong></p>
<p>Convert a Date into XML Schema (ISO 8601) format.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.time | date_to_xmlschema }}{% endraw %}</code>
</p>
<p>
<code class="output">2008-11-07T13:07:54-08:00</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Date to RFC-822 Format</strong></p>
<p>Convert a Date into the RFC-822 format used for RSS feeds.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.time | date_to_rfc822 }}{% endraw %}</code>
</p>
<p>
<code class="output">Mon, 07 Nov 2008 13:07:54 -0800</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Date to String</strong></p>
<p>Convert a date to short format.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.time | date_to_string }}{% endraw %}</code>
</p>
<p>
<code class="output">07 Nov 2008</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Date to String in ordinal US style</strong></p>
<p>Format a date to ordinal, US, short format.
{% include docs_version_badge.html version="3.8.0" %}</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.time | date_to_string: "ordinal", "US" }}{% endraw %}</code>
</p>
<p>
<code class="output">Nov 7th, 2008</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Date to Long String</strong></p>
<p>Format a date to long format.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.time | date_to_long_string }}{% endraw %}</code>
</p>
<p>
<code class="output">07 November 2008</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Date to Long String in ordinal UK style</strong></p>
<p>Format a date to ordinal, UK, long format.
{% include docs_version_badge.html version="3.8.0" %}</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.time | date_to_long_string: "ordinal" }}{% endraw %}</code>
</p>
<p>
<code class="output">7th November 2008</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Where</strong></p>
<p>Select all the objects in an array where the key has the given value.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.members | where:"graduation_year","2014" }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Where Expression</strong></p>
<p>Select all the objects in an array where the expression is true.
{% include docs_version_badge.html version="3.2.0" %}</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.members | where_exp:"item",
"item.graduation_year == 2014" }}{% endraw %}</code>
<code class="filter">{% raw %}{{ site.members | where_exp:"item",
"item.graduation_year < 2014" }}{% endraw %}</code>
<code class="filter">{% raw %}{{ site.members | where_exp:"item",
"item.projects contains 'foo'" }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Group By</strong></p>
<p>Group an array's items by a given property.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.members | group_by:"graduation_year" }}{% endraw %}</code>
</p>
<p>
<code class="output">[{"name"=>"2013", "items"=>[...]},
{"name"=>"2014", "items"=>[...]}]</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Group By Expression</strong></p>
<p>Group an array's items using a Liquid expression.
{% include docs_version_badge.html version="3.4.0" %}</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.members | group_by_exp:"item",
"item.graduation_year | truncate: 3, \"\"" }}{% endraw %}</code>
</p>
<p>
<code class="output">[{"name"=>"201...", "items"=>[...]},
{"name"=>"200...", "items"=>[...]}]</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>XML Escape</strong></p>
<p>Escape some text for use in XML.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.content | xml_escape }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>CGI Escape</strong></p>
<p>
CGI escape a string for use in a URL. Replaces any special characters
with appropriate <code>%XX</code> replacements. CGI escape normally replaces a space with a plus <code>+</code> sign.
{{- filter.description -}}
{%- if filter.version_badge %}
<span class="version-badge" title="This filter is available from version {{ filter.version_badge }}">
{{- filter.version_badge -}}
</span>
{% endif -%}
</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ "foo, bar; baz?" | cgi_escape }}{% endraw %}</code>
</p>
<p>
<code class="output">foo%2C+bar%3B+baz%3F</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>URI Escape</strong></p>
<p>
Percent encodes any special characters in a URI. URI escape normally replaces a space with <code>%20</code>. <a href="https://en.wikipedia.org/wiki/Percent-encoding#Types_of_URI_characters">Reserved characters</a> will not be escaped.
</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ "http://foo.com/?q=foo, \bar?" | uri_escape }}{% endraw %}</code>
</p>
<p>
<code class="output">http://foo.com/?q=foo,%20%5Cbar?</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Number of Words</strong></p>
<p>Count the number of words in some text.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.content | number_of_words }}{% endraw %}</code>
</p>
<p>
<code class="output">1337</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Array to Sentence</strong></p>
<p>Convert an array into a sentence. Useful for listing tags. Optional argument for connector.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.tags | array_to_sentence_string }}{% endraw %}</code>
</p>
<p>
<code class="output">foo, bar, and baz</code>
</p>
<p>
<code class="filter">{% raw %}{{ page.tags | array_to_sentence_string: 'or' }}{% endraw %}</code>
</p>
<p>
<code class="output">foo, bar, or baz</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Markdownify</strong></p>
<p>Convert a Markdown-formatted string into HTML.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.excerpt | markdownify }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Smartify</strong></p>
<p>Convert "quotes" into &ldquo;smart quotes.&rdquo;</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.title | smartify }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Converting Sass/SCSS</strong></p>
<p>Convert a Sass- or SCSS-formatted string into CSS.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ some_scss | scssify }}{% endraw %}</code>
<code class="filter">{% raw %}{{ some_sass | sassify }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Slugify</strong></p>
<p>Convert a string into a lowercase URL "slug". See below for options.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ "The _config.yml file" | slugify }}{% endraw %}</code>
</p>
<p>
<code class="output">the-config-yml-file</code>
</p>
<p>
<code class="filter">{% raw %}{{ "The _config.yml file" | slugify: 'pretty' }}{% endraw %}</code>
</p>
<p>
<code class="output">the-_config.yml-file</code>
</p>
<p>
<code class="filter">{% raw %}{{ "The _cönfig.yml file" | slugify: 'ascii' }}{% endraw %}</code>
</p>
<p>
<code class="output">the-c-nfig-yml-file</code>
</p>
<p>
<code class="filter">{% raw %}{{ "The cönfig.yml file" | slugify: 'latin' }}{% endraw %}</code>
</p>
<p>
<code class="output">the-config-yml-file</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Data To JSON</strong></p>
<p>Convert Hash or Array to JSON.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.data.projects | jsonify }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Normalize Whitespace</strong></p>
<p>Replace any occurrence of whitespace with a single space.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ "a \n b" | normalize_whitespace }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Sort</strong></p>
<p>Sort an array. Optional arguments for hashes: 1.&nbsp;property name 2.&nbsp;nils order (<em>first</em> or <em>last</em>).</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.tags | sort }}{% endraw %}</code>
</p>
<p>
<code class="filter">{% raw %}{{ site.posts | sort: 'author' }}{% endraw %}</code>
</p>
<p>
<code class="filter">{% raw %}{{ site.pages | sort: 'title', 'last' }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Sample</strong></p>
<p>Pick a random value from an array. Optional: pick multiple values.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ site.pages | sample }}{% endraw %}</code>
</p>
<p>
<code class="filter">{% raw %}{{ site.pages | sample:2 }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>To Integer</strong></p>
<p>Convert a string or boolean to integer.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ some_var | to_integer }}{% endraw %}</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Array Filters</strong></p>
<p>Push, pop, shift, and unshift elements from an Array.</p>
<p>These are <strong>NON-DESTRUCTIVE</strong>, i.e. they do not mutate the array, but rather make a copy and mutate that.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ page.tags | push: 'Spokane' }}{% endraw %}</code>
</p>
<p>
<code class="output">['Seattle', 'Tacoma', 'Spokane']</code>
</p>
<p>
<code class="filter">{% raw %}{{ page.tags | pop }}{% endraw %}</code>
</p>
<p>
<code class="output">['Seattle']</code>
</p>
<p>
<code class="filter">{% raw %}{{ page.tags | shift }}{% endraw %}</code>
</p>
<p>
<code class="output">['Tacoma']</code>
</p>
<p>
<code class="filter">{% raw %}{{ page.tags | unshift: "Olympia" }}{% endraw %}</code>
</p>
<p>
<code class="output">['Olympia', 'Seattle', 'Tacoma']</code>
</p>
</td>
</tr>
<tr>
<td>
<p class="name"><strong>Inspect</strong></p>
<p>Convert an object into its String representation for debugging.</p>
</td>
<td class="align-center">
<p>
<code class="filter">{% raw %}{{ some_var | inspect }}{% endraw %}</code>
</p>
{%- for example in filter.examples %}
<p><code class="filter">{{ example.input }}</code></p>
{% if example.output %}<p><code class="output">{{ example.output }}</code></p>{% endif %}
{% endfor -%}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>