Published 2020-10-03.
Last modified 2025-01-08.
Time to read: 4 minutes.
jekyll_plugins
collection.
Jekyll_all_collections
is a Jekyll plugin that includes a generator
triggered by a high-priority hook
and a block tag called all_collections
.
Taken together, the three new attributes of the
Jekyll site
variable
reference all the files that are likely to be processed in a Jekyll website.
These three attributes can be referenced as site.
,
site.
and
site.
.
Why Do This?
Jekyll provides inconsistent attributes for
site.
,
site.
and
site.
.
-
While the
url
attribute of items insite.posts
andsite.pages
start with a slash (/
),site.static_files
items do not have aurl
attribute. -
Static files have a
relative_path
attribute, which starts with a slash (/
), but although that attribute is also provided insite.posts
andsite.pages
, those values do not start with a slash. -
Paths ending with a slash (
/
) imply that a file calledindex.html
should be fetched. -
HTML redirect files created by the
jekyll-redirect-from
Jekyll plugin, which are included in site.static_files, should be ignored.
These inconsistencies mean that combining the standard three collections of files provided as site attributes will create a new collection that is difficult to process consistently:
# This pseudocode creates `oops`, which is problematic to process consistently oops = site.all_collections + site.pages + site.static_files
Oops
, above, is difficult to process because of inconsistencies in the provided attributes
and how the attributes are constructed.
Solving The Problem
The generator normalizes these inconsistencies by utilizing the
AllCollectionsHooks::
class
and filtering out HTML redirect files.
AllCollectionsHooks::
is used by other plugins
based on jekyll_
, in particular
jekyll_href
.
The all_collections
collection contains APage
representations of site.
.
The all_documents
collection contains APage
representations of site.
.
The everything collection contains APage
representations of:
site.collections + site.pages + site.static_files - HTML_redirect_files
The APage Class
The site.
, site
and site
attributes consist of arrays of APage
instances.
The APage
class has the following attributes:
content
(HTML or Markdown)data
(array)date
(RubyDate
)description
destination
draft
(Boolean)ext
-
href
always starts with a slash. This value is consistent witha href
values in website HTML. Paths ending with a slash (/
) haveindex.html
appended so the path specifies an actual file. label
last_modified
orlast_modified_at
(RubyDate
)layout
-
origin
indicates the original source of the item. Possible values arecollection
,individual_page
andstatic_file
. Knowing theorigin
of each item allows code to process each type of item appropriately. path
relative_path
tags
title
type
url
All_collections Block Tag
The all_collections
block tag creates a formatted listing of Jekyll files.
The ordering is configurable; by default, the listing is sorted by date
, newest to oldest.
The all_collections
tag has a data_source
parameter that specifies which new property to report on
(all_collections
, all_documents
, or everything
).
Installation
Installing In A Jekyll Website
-
Add the CSS found in
demo/assets/css/jekyll_all_collections.css
to your Jekyll layout(s). -
Add the following to your Jekyll website's
Gemfile
, within thejekyll_plugins
group:Gemfilegroup :jekyll_plugins do gem 'jekyll_all_collections', '>= 0.3.8' gem 'jekyll_draft', '>=2.1.0' end
-
Type:
Shell
$ bundle
Installing As a Gem Dependency
Add the following to your gem’s .gemspec
:
group :jekyll_plugins do spec.add_dependency 'jekyll_all_collections', '>= 0.3.8' spec.add_dependency 'jekyll_draft', '>=2.1.0' end
And then execute:
$ bundle
Demo
The demo
directory contains a demonstration website, which uses the plugin.
To run, type:
$ demo/_bin/debug -r
Now point your web browser to localhost:4444
.
Requirements
All the pages in the Jekyll website must have an implicit date
(for example, all posts are automatically assigned this property by Jekyll),
or an explicit date
set in front matter, like this:
--- date: 2022-01-01 ---
If a front matter variable called last_modified
or
last_modified_at
exists,
its value will be converted to a Ruby Date
:
--- last_modified: 2023-01-01 ---
Or:
--- last_modified_at: 2023-01-01 ---
Otherwise, if last_modified
or last_modified_at
is not present in the front matter for a page,
the date
value will be used last modified date value.
Excluding Pages
There are two ways to exclude files from the new site
attributes
-
The
exclude
entry in_config.yml
can be used as it normally would. -
Adding the following entry to a page's front matter causes that page to be
excluded from the collection created by this plugin:
Front matter in HTML or markdown page
--- exclude_from_all: true ---
Plugin Usage
Jekyll generators and tags receive an enhanced version of the Jekyll site
variable.
From a Custom Plugin
In the following example of how to use the all_collections
plugin in a custom plugin,
the do_something_with
function processes all Jekyll::
,
Jekyll:
, and static files.
@site.everything.each do |apage| do_something_with apage end
Using the Block Tag
The general form of the Jekyll tag, including all options, is:
{% all_collections date_column='date|last_modified' heading='All Posts' id='asdf' sort_by='SORT_KEYS' %}
Each of these attributes are explained below.
Date_column Attribute
One of two date columns can be displayed in the generated HTML:
either date
(when the article was originally written),
or last_modified
.
The default value for the date_column
attribute is date
.
Heading
Attribute
If no heading
attribute is specified,
a heading will automatically be generated, which contains the
sort_by
values, for example:
{% all_collections id='abcdef' sort_by="last_modified" %}
Generates a heading like:
<h2 id="abcdef">All Posts Sorted By last_modified</h2>
To suppress both a h2
heading (and the enclosed id
) from being generated,
specify an empty string for the value of heading
:
{% all_collections heading='' %}
Id Attribute
If your Jekyll layout employs
jekyll-toc
,
then id
attributes are important.
The jekyll-toc
include checks for id
attributes in
h2
... h6
HTML tags,
and if found, and if the attribute value is enclosed in double quotes
(id="my_id"
, not id='my_id'
),
then the heading is included in the table of contents.
To suppress an id
from being generated,
and thereby preventing the heading from appearing in the
automatically generated table of contents from jekyll-toc
,
specify an empty string for the value of id
, like this:
{% all_collections id='' %}
SORT_KEYS Values
SORT_KEYS
specifies how to sort the collection.
Values can include one or more of the following attributes:
date
, destination
, draft
,
label
, last_modified
, last_modified_at
, path
,
relative_path
, title
, type
, and url
.
Ascending sorts are the default; however, a descending sort can be achieved by prepending
-
before an attribute.
To specify more than one sort key, provide a comma-delimited string of values.
Included spaces are ignored.
For example, specify the primary sort key as draft
,
the secondary sort key as last_modified
,
and the tertiary key as label
:
{% all_collections date_column='last_modified' heading='All Posts' id='asdf' sort_by='draft, last_modified, label' %}
Liquid Usage Examples
Here is a short Jekyll page, including front matter, demonstrating this plugin being invoked with all default attribute values:
--- description: " Dump of all collections, sorted by date originally written, newest to oldest. The heading text will be <code>All Posts Sorted By -date</code> " layout: default title: Testing, 1, 2, 3 --- {% all_collections %}
Following are examples of how to specify the sort parameters.
Explicitly express the default sort
(sort by the date originally written, newest to oldest):
{% all_collections sort_by="-date" %}
Sort by date, from oldest to newest:
{% all_collections sort_by="date" %}
Sort by the date last modified, oldest to newest:
{% all_collections sort_by="last_modified" %}
Sort by the date last modified, newest to oldest:
{% all_collections sort_by="-last_modified" %}
Several attributes can be specified as sort criteria
by passing them as a comma-delimited string.
Included spaces are ignored:
{% all_collections sort_by="-last_modified, -date" %} {% all_collections sort_by="-last_modified, title" %} {% all_collections sort_by="-last_modified, -date, title" %}
The following two examples produce the same output:
{% all_collections sort_by="-last_modified,-date" %} {% all_collections sort_by="-last_modified, -date" %}