Jekyll With Multi-Collection Blog
When we host our blogs on Github, I guess most of us use Jekyll. Before I realize I need a different collection of blogs, I always start with a single collection or category as everybody does. However, I have to face a problem that to make multiple categories of blog into different URLs under the same domain. For example,
I searched the web for a little while and figure out this could be easily solved, but also a little challenging when handle some tricky cases with Github policies (we will get there later). Jekyll does a very good job for you to make your blog multi-categories. Simply put a new directory under your Jekyll site root directory would work. Inside that directory it needs to contains an index.html
file and _posts
folder. The _posts
folder is the place you can put all your post belonging to this category in the future. After that, you will have a directory structure like the following.
1 | + MySite |
This would make your site’s URL functioning well, however, it lacks of the ability to filter your blogs. To address this issue, I use the category
keyword as the collection separator inside the markdown file. So you might want to add the category information in each of your blog. For instance,
1 | --- |
To filter out the different category, you can write whatever ruby code you feel comfortable. I found Adam’s code is very clean and well designed, which I could borrow for this purpose in my site. For convenience purpose, I put the code as bellow. You save this code to your _plugins
folder.
1 | module Jekyll |
Use this Jekyll plugin is very simple, because it makes all the blog a dictionary which indexed by the category
. As an example, the code is blow.
1 | <script language="javascript"> |
After you finish this, you can test it perfectly locally. However when you publish the site to Github, the site will fail to build because the Github does not allow customized plugin in their build system. So we have to hack this a little.
An interesting compromise Github provides is that you can push your _site
directly to the master
branch where you can play any plugins as you want. So instead of maintain the code in the master
branch, I switch it to source
branch but use master
branch to push _site
. This is a little complicated because you need to manipulate the repository between remote and local back-and-forth. So I prepare a bash script that could easily do this job.
1 | cleanup() { |
so you run this script in bash shell should do the job automatically. Of course, you site will be down for a little while, but it should be back in a few seconds.