I remember using and integrating Google Search results in various web projects of mine, including this blog but never did occur to me that using Google Search for Jekyll was pretty painful. It wasn’t the feature Google was providing their free users; it wasn’t the idea behind the implementation but rather the various factors required to implement it correctly. A small mistake in proper configurations could make our end goal turn south.

I did not notice this problem until a week after I wrote a blog post - Importing and exporting files using ReactJS. and noticed that while searching for import or export in my search page, the blog post was not included in the search results. Like I mentioned before, if not configured right, problems and problems everywhere and so I decided to go the plain and simple way.

The simple way I chose to follow was a little too simple and lacks features for now, which I will eventually add as the time goes by but for starters, definitely gets the job done.

First, let me list1 some of Jekyll’s liquid template syntaxes that I used for this task.

Jekyll Syntaxes
  1. site.posts - We will use it initialize and loop through all our posts.
  2. post.title - To access our post’s title.
  3. - Date, the one we specify in the top of our post’s markdown file.
  4. post.categories - One of the post properties that we set.
  5. post.url - This one is generated by Jekyll during build but can be accessed and has a pattern unless specified.
  6. post.tags - Our blog post’s tags, in case we use it, if not, not necessary.

I used these syntaxes to create and populate a JSON file named posts.json with our blog posts data.

layout: null
    "posts": [
        {% for post in site.posts %}
                "title": "{{ post.title }}",
                "date": "{{ }}",
                "category": "{{ post.categories }}",
                "link": "{{ post.url | replace:'index.html','' | absolute_url }}",
                "tags": {{ post.tags  | append: "" | join: ', ' }}
            }{% unless forloop.last %}, {% endunless %}
        {% endfor %}

Our posts.json file does not necessarily need category and tags JSON keys but I thought it would ease the search process if we can search the categories and tags! Also, we could extend this search feature by initializing and accessing various post properties2.

Our next process includes writing a search trigger which I already made a long time ago when Google Search was still being used for this blog, therefore, all I had to do was modify the search function. There are two conditions we need to focus on, to be able to search our title, tags and categories. I achieved this by using the .filter() method.

If we consider data to be the contents from our posts.json, our search/filter should something similar to the code below as I wanted the readers to be able to search my blog posts by their title, tags and categories`.

const filteredData = data.posts.filter(post =>
    post.title.toLowerCase().includes(searchKey.toLowerCase()) ||
    post.tags.join().toLowerCase().includes(searchKey.toLowerCase()) ||

The remaining task is to format the filteredData in an organized manner, depending on layout and style of your preference. Mine is a bit messy but works well with the framework and libraries I’ve used!

/* Search trigger - using manual button click.
–––––––––––––––––––––––––––––––––––––––––––––––––– */
$(".gcse-trigger").click(function (e) {
    var searchKey = $('input#toSearch').val();
    $.getJSON("../posts.json", {}, function (data) {
        const filteredData = data.posts.filter(post =>
            post.title.toLowerCase().includes(searchKey.toLowerCase()) ||
            post.tags.join().toLowerCase().includes(searchKey.toLowerCase()) ||
        .append("<h5 class='totalSearchResults'>Found " + 
        filteredData.length + " results for " + 
        searchKey + ".</h5>");

        $.each(filteredData, (key, value) => {
            var initialFormatting = '<div class="row result">' +
                '<div class="u-full-width">' +
                '<h5><a target="_blank" href="{0}">{1}</a></h5>' +
                '<p>{2}</p>' +
                '<p>{3}<br/>{4}</p>' +
                    .replace("{1}", value.title)
                    .replace("{2}", value.tags.join(', ').toUpperCase())
                    .replace("{3}", value.category.toUpperCase())

The outcome I decided to settle with, I am pretty satisfied with it.

Search Demo


Enjoy & Happy Coding!

Photo by João Silas on Unsplash.

  • Edited a little, didn’t want the full-sized photo.

  1. The reason, I enlisted these syntaxes is because we need it to generate a new file with those contents and form a valid JSON content. 

  2. Post properties refer to the contents between --- on top of our page, You can add more properties, such as featuredImages etc.