Skip to content

Instantly share code, notes, and snippets.

@chrisdmacrae
Last active January 30, 2018 21:45
Show Gist options
  • Save chrisdmacrae/53880a51c287f7f84f074e11a7fc7b6d to your computer and use it in GitHub Desktop.
Save chrisdmacrae/53880a51c287f7f84f074e11a7fc7b6d to your computer and use it in GitHub Desktop.
Algolia Search - Forestry.io

Simple Algolia w/ Hugo

The following allows you to easily generate an Algolia index with Hugo consisting of whatever front matter metadata you like.

Why this compared to other plugins?

There are plugins that generate indexes using NPM packages or other tools. The issue is that they don't have any context to your site configuration or Hugo's internals, so they may miss pages or index pages that should not be indexed.

Using Hugo means your index has the full context of your site available when being generated.

Generating Index

To generate the index you create the index's layout at layouts/_default/list.algolia.json.

In this file, we loop through all of the list page's children and generate valid json for each field we want in the index.

We then use the site.outputs or page.outputs options to have a list page generate an index.

Pushing changes to Algolia

Push changes to Algolia using their Node SDK: https://www.algolia.com/doc/api-reference/api-methods/add-objects/?language=javascript#add-objects-with-automatic-objectid-assignments

var algoliasearch = require("algoliasearch")
var fs = require("fs")
var indexId = "YOUR_INDEX_ID"
var indexFile = fs.readFileSync("./public/algolia.json")
var index = JSON.parse(indexFile)
if (index) {
var APP_ID = "YOUR_APP_ID"
var ADMIN_KEY = "YOUR_ADMIN_KEY"
var index = algoliasearch(APP_ID, ADMIN_KEY).initIndex(indexId)
index.addObjects(index, function(err, content) {
if (err) throw err
else console.log("Sending index to Algolia")
})
}
## Create a custom output format for Algolia Index
## https://gohugo.io/templates/output-formats/
[outputFormats.Algolia]
baseName = "algolia"
isPlainText = true
mediaType = "application/json"
notAlternative = true
## Output the Algolia index using the homepage
## so that all pages are available
[outputs]
home = ["HTML", "RSS", "Algolia"]
{{/* Generates a valid Algolia search index */}}
[
{{- $len := len (where .Data.Pages "Params.private" "ne" "true") -}}
{{- range $i, $value := where .Data.Pages "Params.private" "ne" "true" -}}
{
"title": {{ jsonify $value.Title }},
"permalink": {{ jsonify $value.Permalink }},
"lang": {{ jsonify $value.Lang }},
"lastmod": {{ jsonify ($value.Lastmod.UTC.Unix) }},
"date": {{ jsonify ($value.Date.UTC.Unix) }},
"publishDate": {{ jsonify ($value.PublishDate.UTC.Unix) }},
"expiryDate": {{ jsonify ($value.ExpiryDate.UTC.Unix) }},
"summary": {{ jsonify $value.Summary}},
"readingTime": {{ jsonify $value.ReadingTime }},
"wordCount": {{ jsonify $value.WordCount }},
"weight": {{ jsonify $value.Weight }},
"section": {{ jsonify $value.Section }},
{{- range $key, $value := $value.Params -}}
{{- if (eq $key "tags") | or (eq $key "authors") | or (eq $key "categories") -}}
"{{ $key }}": [{{ jsonify (delimit $value ",") }}],
{{- end -}}
{{- end -}}
"objectID": {{ jsonify $value.UniqueID }}
}{{- if not (eq $len ($i | add 1)) -}},{{- end -}}
{{- end -}}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment