Problems using Algolia with Hugo: javascript and go


I am trying to use Algolia to build search on my site. I have created a hitTemplate to use with instantsearch.js which I’ve placed in /js/search.js, which looks like this:

var hitTemplate =
  '<div class="hit>' +
    '<div>' +
      '<a class="{{if eq .Params.highlight "true"}} red{{end}}" href="{{website}}">' +
        '<h1>{{title}}</h1>' +
        '<h2>{{description}}</h2>' +
        '<ul>' +
          '<li>{{locations}}</li>' +
        '</ul>' +
      '</a>' +
    '</div>' +

The problem I am having is mixing Hugo code with Algolia’s javascript. Any Hugo code in search.js will not generate. If I place the template in index.html, Algolia’s javascript won’t work. How do I combine the two?


There are more than a few discussions about implementing Algolia in Hugo. You might consider reading through those and looking at their repos to see how they implemented it.

Depending on where your “hitTemplate” lives, you either need to have Hugo generate the JavaScript as a custom output, or generate the script in an HTML doc and make sure to pass everything through safe filters. In either case, please refer to the docs and the post about requesting help. :slight_smile:


I don’t understand this response. Is it defensive? I gave you three distinct options, with links. Is there something in the way I said it that was dismissive or patronizing?