Adding Pagefind search to Hugo
Pagefind makes me happy. It is a static search library that is so easy to configure and use that I can hardly believe it. Here’s a quick summary of how I implemented Pagefind search here in the Anubis2 Hugo theme1.
I first created content/search.md as content for the search page. In that document I pasted the default code from the Getting Started with Pagefind docs…
To build the search index, I run it via NodeJS like so:
That’s it! That’s all that is technically necessary. Running the indexer creates the index and puts the necessary support (css/js) files in public/pagefind/. Just deploy the site2 and visit /search.
By default, Pagefind indexes everything in the <body> element. That’s too much, so I added data-pagefind-body to the element containing only the post content in the “single.html” template.
{{ .Content }}
For titles I needed data-pagefind-meta="title" in the <H1> element:
{{ trim .Title " " | markdownify }}
I put a little search icon in the navigation.All this took me less than 30 minutes. It’s crazy.
I’m sure I’ll tweak things further, but the out-of-box experience is pretty great.