DEV Community

Diego Carrasco Gubernatis
Diego Carrasco Gubernatis

Posted on • Originally published at diegocarrasco.com on

Introducing the FlexSearch Plugin for Nikola

TLDR

I wanted to have search functionality in this site, which is powered by Nikola SSG. After some research I decided to create a new plugin using Flexsearch and the plugin is not available in Nikola Plugin repository.

Context

I use this site a lot as personal repository of stuff I forget. This means I refer to it when I have to do something and I no longer remember what command it was. I also refer people here when they ask something I´ve already answered here. My usual workflow was to go through my articles and use the search feature from the browser, but that was not that efficient because sometimes I only remember that one phrase of that one post.

Well, this is no longer necessary because now this site has full-text search functionality, courtesy of yours truly. This is a new Nikola Plugin, under MIT License (so that you can just use it as you want) and the FlexSearch Library.

How does this work?

Great that you ask. This is a static site. That means that whenever I post a new article I have to rebuild the site to generate HTML from my source (markdown, rst, asciidoc and others) from my git repository. In other words, this site does not use any database. The search functionality has 3 steps:

  1. When the site is build, a search:index.json is build. This index contains all the titles, slugs and body of all my articles.
  2. There is a search box now in th main menu. This does nothing else than calling a javascript snippet, which in turn uses the FlexSearch Library, to use that search_index as input and return a list of post that matches your search query.
  3. The small javascript snippet renders the search results in a div for you to browse.

That´s all. It is completely offline, no Google, Databases, third party services or whatever. Pretty cool, isn´t it?

Of course, I´m not the first one to think about this. There are many examples out there. These are some of them

If you are using Nikola for your site, you are welcome to give it a try.

To install the plugin just run

nikola plugin -i flexsearch_plugin

Enter fullscreen mode Exit fullscreen mode

and follow the instructions.

References

Top comments (0)