Python script to generate search results in Hugo

Hey,

I’ve just released hugo-search-json.py. It’s a small script to generate a JSON file from markdown files in websites powered by Hugo.

If you’re interested by such tool, maybe give it a try. Hopefully, it will help you.

I wanted to see how long it takes to build the feature with Python instead of using known strategies such as third-party services, Grunt scripts, or other NPM dependencies.

It’s quite experimental but relatively straightforward:

git clone https://github.com/jmau111/hugo_search_json.py
cd hugo_search_json.py
pip install -r requirements.txt
python3 hugo_search_json.py ~/my-hugo-project posts search
  • posts is the subfolder in the content/ directory that contains your posts
  • search is the filename for the JSON file to be generated

Then, you might use it like that in your js search:

fetch('/search.json')
    .then((response) => response.json())
    .then((json) => console.log(json));

Indeed, the script creates the JSON file in the static/ folder of your Hugo, making it available to fetch. Make sure you have the required fields in your markdown files, though.

As it’s a script, it can be automated. Don’t forget to add it to your build process if you decide to use it.

Cheers.

See Also