In this article, I will show you how to implement search in Hugo by using Lunr.js, a lightweight JavaScript search engine.
In this article, I will show you how to implement search in Hugo by using Lunr.js, a lightweight JavaScript search engine. Adding search functionality to your Hugo website enhances user experience and makes it easier for visitors to find relevant content. We will explore the steps involved in integrating Lunr.js, creating a search index from your Hugo content, and incorporating the search feature into your website.
To conveniently position your search form on any desired location within your website, generate the form as a partial template. Save this template as search-form.html within the layouts/partials/ directory.
Create the search folder in layouts/partials/ path, and create a page named list.html.
Include the search form in list.htm, also you can use in any other templates.
1
{{ partial "search-form.html" . }}
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- In layouts/partials/search/list.html -->{{ define "main" }}
<sectionclass="s:mt-10 mt-20">
<divclass="container sm:my-14" >
<divclass="mt-20">
{{ partial "search-form.html" . }}
</div>
<divid="results">
Enter a keyword above to search this site.
</div>
</div>
</section>
{{ end }}
Create a search page
Navigate to content folder, create a folder name search, and a file _index.md
Build your search index
In layouts/partials/ folder, create another html file search-index.html
Import lunr.js cdn and search.js, I haven’t created search.js so far.