Add Lunr Search to Hugo site
What’s covered in this post?
Hugo ships with out search, yet I love it. However, I’ve managed to integrate lunr search that is supported by the theme Loveit
I’m using on this website. With that being said, this post will cover how I integrate lurn
on my website
- for demo, go ahead and search for something on this website :)
Install or update!
Make sure you update or install before we start.
Today, there are handfull of search tools available for Hugo websites. One popular option is Hugo-lunr, which utilizes Lunr search. It’s easily installed via npm. For more information, visit its npm page: hugo-lunr .
1. Edit package.json
Lets start!
Go to package.json
in project root directory and add the following
|
|
here is my package.json for quick reference
2. Edit config.toml
Make sure you enable search in your config.toml - if don’t exist, go ahead and add at the end of the page.
|
|
and then add out-puts if not added already (must go into config.toml thats in root directory of the your project)
|
|
config.toml
file that exist at the root of your project.3. Build the index
After step 1 and 2 you shall be able to build the site index. Go ahead and open the terminal and run the following command from your project root directory.
|
|
once you run this command, you will see the file lunr.json
generated in static
folder at the root of your project directory.
Now build your website.
|
|
This will copy the lunr.json
from static
dir to the public
dir of your project.
4. Edit baseof.html
Open themes/<theme-name>/layouts/_default/baseof.html
(this path might be diff in your case, based on your theme) and add the following line before closing the head
|
|
and at the end of the page, add the following script. this will fetch the data from lurn.json
file that’s generated in step 3
|
|
5. Test the ‘search’!
By following the above steps, you have successfully setup lunr
on your webpage. You shall be able to search your website.
- run the following to build the index
npm run index
- run
hugo
to build the website - start your server with
hugo server -D
Now start your hugo server using the cmd hugo server -D
and you shall be able to search your through your content.
Conclusion
I don’t claim expertise in Hugo or Lunr. I integrated Lunr by researching various resources and credit those who explained it before me. I only take credit for detailing how I implemented Lunr search on my website. If you have any questions or suggestions, please post them here .