I have a collection called sections
that contains red.html
, green.html
, and blue.html
, which I want displayed in a list in that order rather than in alphabetical order. I could add a field called order
to the front matter of each, with the values 0, 1, and 2, but then if I decide I want to add a page orange.html
in between red.html
and green.html
, I would have to edit green.html
and blue.html
to change their numbers. Since I’m actually going to have lots of pages and be moving them around fairly often, I don’t want to do this. Instead, I would like to define a list in _config.yml
that specifies the order of the collection elements:
ordering:
- /red/
- /green/
- /blue/
(where for convenience I’m listing the pages’ permalinks - I could use anything else that’s unique per page). If I was writing in Ruby or some other language, I would then use the “decorate-and-sort” pattern to sort the collection pages:
- Create a hash from permalink to sequence
{'/red/': 0, '/green/': 1, '/blue/': 2}
. - Loop over the collection pages and add an
ordering
field to each by using the page’s permalink to look up its sequence number. - Sort the collection using the
ordering
key.
I want a solution that will run with GitHub Pages without any plugins, so is there a way to do this in pure Jekyll? Thanks.