One of the features that I needed to add to Jekyll was the ability to easily group a collection of posts as a series. There are times when I need to link posts together in sequential order – like a travelog, for instance. I want a table of contents in the sidebar to let the user know that they're in the middle of a series and I want a link at the bottom to encourage the user to go to the next post in the series.
By default, Jekyll creates next and previous properties that link a post to the next post in the site. That doesn't work for me because posts don't really have anything to do with each other most of the time. But I like the properties, so I decided to override how they work.
To create a table of contents, I added a new property to posts called siblings. This property is a hash of all the other posts in the series.
The name attribute is an arbitrary string to group posts into a series. The index attribute describes where the current post lives in relation to its siblings.
The first thing we want to do is override the initialize method so that we can assign any series data to the object itself. We still want to call the original initialize method, so we alias it from the main Post class, and then redefine it. This method calls the main initialize method, then assigns series data to the object if it finds it in the YAML front matter.
Next, we want to define a new method for getting the sibling data. Create a method that loops through the posts and selects only the ones that have a matching series name. Be sure to sort them by their index so that they are in the correct order.
Then, to make sure that the next and previous attributes work as expected, we override the functionality of those functions to pull the next and previous posts in the series.
And finally, we override the to_liquid method to add the series data if it exists.
To see the whole code, go to the project on Github.
Trying to show liquid code in a liquid template on Jekyll? This is so easy and obvious that no one bothered to post a solution. I tracked down the answer by looking at someone's source code. I hope this makes someone else's life easier.