11/30/16 - Static Site Search

Tags Jekyll

I’ve been meaning to implement a search function on the blog, mostly because I was curious how it would be done if I didn’t use a third-party service. I wanted something really simple and lightweight that I could understand. There are several options, probably the most discussed was lunr.js, but the simplest I could find was Mat Hayward’s jekyll-search. It is easy to implement and basic enough I can change the script if needed.

The method is to dump all the post text into a JSON, then use Javascript to search for the desired words. It’s straightforward, but it means duplicating your blog text. Currently it’s an 83k file. The size could be reduced by removing stopwords. lunr.js has this feature baked-in. The jekyll-search JSON is built with Liquid tags, but Liquid’s remove filter only accepts one string parameter so it’s not immediately obvious to me how to implement a stopwords filter in the template.

The non-Javascript solution is to have all the blog posts on a single page so Ctrl+F can be used (thus duplicating the text a third time) or keep tag pages and maintain good tagging practices.

The search function may be useful for me personally because sometimes I need a link and it takes a minute to click through the posts under a tag to find it. I doubt readers will need it, but if the tag/post count gets high enough it may be easier to search than navigate the tag page.