Adding search to your cobalt site - Part Two

Mark Sta Ana - Aug 19 '18 - - Dev Community

This will be a two part post, where I detail the steps it took to enable search on my Cobalt site.

As you may have gathered in part one creating manual document collections is a bit of a chore, and can be easily done using the liquid templating engine.

lunr.liquid

front matter

title: lunr index
path: /js/lunr_docs.json
---
Enter fullscreen mode Exit fullscreen mode

The item of note here, is the path which Cobalt will use to create the lunr document collection.

content

[
{% assign idx = 0 %}
{% assign post_count = posts | size %}
    }{% if idx < post_count %},{% endif %}
        "content" : "{{ post.content | strip_html | strip_newlines 
                                     | replace: "\", "\\" }}"
        "href" : "{{ post.permalink }}",
        "title" : "{{ post.title }}",
    { 
]
{% endfor %}
Enter fullscreen mode Exit fullscreen mode

The only real difference here between a blog index is that I'm tracking the last post using an index so I can omit a trailing comma.

Don't forget to remove the manually generated document collection (lunr_docs.json) from the assets directory for javascript files (js/) , as this caught me out and made me wonder why the index wasn't being updated.

Updates

2017-06-21 - Change references to the lunr index to the lunr document collection.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .