Opensearch, Hugo and this website
Ever since I made contact with the OpenSearch spec I wanted to implement it on my website, but it was postponed, and postponed, and postponed. Not anymore, I had some free time and I implemented it (not like it was a huge change, took less than 5 minutes).
Add a new Hugo output format declaration (in config.toml):
[outputFormats]
[outputFormats.OpenSearch]
baseName = "opensearch"
isHTML = false
isPlainText = false
mediaType = "application/opensearchdescription+xml"
noUgly = true
Add OpenSearch to the list of outputs for your home page (in config.toml):
[outputs]
home = ["html", "rss", "atom", "opml", "OpenSearch"]
Add a new MediaType with OpenSearch’s MimeType (in config.toml):
[mediaTypes]
[mediaTypes."application/opensearchdescription+xml"]
suffixes = ["xml"]
Next, create layouts/_default/index.opensearch.xml (and make sure you have those two images, the 16x16 and the 64x64 ones).
{{ printf `<?xml version="1.0" encoding="utf-8" ?>` | safeHTML }}
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:ie="http://schemas.microsoft.com/Search/2008/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<ShortName>{{ .Site.Params.title }}</ShortName>
<Description>Search on the {{ .Site.Params.title }} website.</Description>
<Language>{{ site.LanguageCode }}</Language>
<Contact>{{ site.Author.email | safeHTML }}</Contact>
<Developer>{{ site.Author.name | safeHTML }}</Developer>
<Image width="16" height="16" type="image/png">{{ .Site.BaseURL }}favicon-16x16.png</Image>
<Image width="64" height="64" type="image/png">{{ .Site.BaseURL }}favicon-64x64.png</Image>
<ie:PreviewUrl type="text/html" method="GET" template="{{ .Site.BaseURL }}search/?q={searchTerms}"/>
<moz:SearchForm>{{ .Site.BaseURL }}search/?q={searchTerms}</moz:SearchForm>
<Url type="text/html" template="{{ .Site.BaseURL }}search/?q={searchTerms}"/>
<Url rel="self" template="{{ .Site.BaseURL }}opensearch.xml" type="application/opensearchdescription+xml" />
<InputEncoding>UTF-8</InputEncoding>
<OutputEncoding>UTF-8</OutputEncoding>
<SyndicationRight>open</SyndicationRight>
</OpenSearchDescription>
To enable autodiscovery, add this line to the layouts/partials/head.html (or the template that outputs the HTML head part of your website):
<link rel="search" title="{{ .Site.Params.title }}" type="application/opensearchdescription+xml" href="/opensearch.xml"/>
And, as an extra bonus, you can use URL parameters to direct the website search to the term you wish to search for. For example, search for the nokia keyword.
























