What we want
You can label Jekyll posts with whatever metadata you want using YAML front matter, which looks like this for the post you’re looking at:
The interesting things here are the
category value and
If we’re going to label our posts like this, it would be nice to have an index
for each category and tag; a page that lists all the other posts with the same
category or tag.
The old way
In my old post, the general idea was this:
- Create a near-empty
- Generate a list of all posts, along with their category and tags, as a JSON file;
- When a user hits the search page with a URL like
I came up with something arguably less elegant, but easier to understand.
The new way
Now, instead of generating a list of posts as a JSON file, I just generate one
list per category and tag in the
search.html page directly.
This means looping over each category, creating a header, and then an unordered
list of post titles and links.
This snippet is for the categories. The same logic applies to the loop over the tags.
search.html contains one list per category and tag, and so all we
label the user is searching for.
The logic I went for goes like this:
- The user searches for something, visiting
categoryparameter, storing the value;
- The list of
.collectioncontainers is searched, and a container is hidden if it’s
data-nameproperty doesn’t match the value of the search parameter.
As I said, I think the new implementation is more of a hack than the old. But I’m OK with that; sometimes it’s worth sacrificing a ‘pure’ implementation for something that’s easier to understand, as it encourages maintainability in the future (who wants to touch that magic one-liner?).
It should be easier for you to adapt the system I use for your own site. I’d love to hear if anyone gives it a go!