As you might have read before, our little blog is powered by Jekyll. Well I stumbled over the jsonify
filter the other day, and so I built a little “kind-of” API :) Sound interesting? Read on.
How
It all started with search, I was looking for a search feature for our blog. After some testing with Algolia I stumbled over this great post by Katy. She’s using lunr.js with Jekyll. I liked the client-side approach of lunr.js and that it has no external dependencies, so I adopted Katy’s implementation for our site and, voilà, we have search. So, why do I keep talking about search? Well lunr.js consumes data in as json objects, so that’s how I learned about the jsonify
filter.
After some playing around with curly braces and the forloop
in liquid I ended up creating the first version of our so-called API.
Basically, what I’m doing is looping through the posts and creating json objects.
I created a few different folders to expose different information, but more about that in the examples below.
Use Invoke-RestMethod to consume the json
There are various “endpoints” available, we can get meta information about the site at /meta/
or a list of all pages and posts at /pages/
and /posts/
, respectively.
In the following example, we’re getting all posts with a Category of ntSystems and then just select the tags property:
PS /Users> Invoke-RestMethod https://ntsystems.it/api/v1/posts/ |
Select-Object -ExpandProperty items |
Where-Object category -eq ntSystems |
Select-Object tags
tags
----
{migrate, jekyll}
{migrate, jekyll}
{update, jekyll}
Alternatively, we can get list of all categories, just like that:
PS /Users> Invoke-RestMethod https://ntsystems.it/api/v1/posts/ |
Select-Object -ExpandProperty items |
Select-Object Category -Unique
category
--------
Cloud
Azure
Dev
...
Awesome, right?
Well, that’s all for today. Enjoy your weekend!
Tom