Technotes

Technotes for future me

Algolia

Update index

create directory

mdkir temp && cd temp

Clone repo

git@github.com:adelerhof/technote.git

Download latest Hugo extended binary

Find the latest release https://github.com/gohugoio/hugo/releases/

curl -LO https://github.com/gohugoio/hugo/releases/download/v0.97.3/hugo_extended_0.97.3_Linux-64bit.tar.gz
tar zxvf hugo_extended_0.97.3_Linux-64bit.tar.gz

build new content

cd technote
../hugo -v

locate new index file

ls public/algolia.json

upload to Algolia

The next step is sending your search index to Algolia. We’ll be using a great NPM package to do this: atomic-algolia

install nodejs and npm

sudo apt update
sudo apt install nodejs
node -v

sudo apt install npm
cd temp/technote
npm install atomic-algolia --save

config package.json

vi package.json

...
{
  "dependencies": {
    "atomic-algolia": "^0.3.19"
  },
  "scripts": {
    "algolia": "atomic-algolia"
  }
}
...

update index

ALGOLIA_APP_ID=<YOUR_APP_ID> ALGOLIA_ADMIN_KEY=<YOUR_ADMIN_KEY> ALGOLIA_INDEX_NAME=<YOUR_INDEX_NAME> ALGOLIA_INDEX_FILE=public/algolia.json npm run algolia

Using a .env File

Passing in the environment variables to the NPM script each time you call it isn’t ideal. That’s why atomic-algolia supports a .env file.

Create a new file in the root of your Hugo project called .env, and add the following contents:

ALGOLIA_APP_ID=<YOUR_APP_ID>
ALGOLIA_ADMIN_KEY=<YOUR_ADMIN_KEY>
ALGOLIA_INDEX_NAME=<YOUR_INDEX_NAME>
ALGOLIA_INDEX_FILE=<PATH/TO/algolia.json>

Now you can update your index more simply by running:

npm run algolia

Onliner to build and update index when all is in place:

../hugo -v && npm run algolia

source:
https://forestry.io/blog/search-with-algolia-in-hugo/

Last updated on 5 Jul 2022
Published on 22 Apr 2022
Edit on GitHub