Hugo: Opensearch

Article published the
3 minute(s) to read

This article has 492 words.
RAW source of the article: MD

Description

Opensearch is a collection of simple formats for the sharing of search results on your website.

Most browsers web will offer you to add your site as a search engine, you need to manage the autodiscovery .

Hugo, by default, not manage Opensearch. We are going to modify the configuration to create a new custom output format.

Documentation

the Hugo official documentation:

the Opensearch official documentation:

Configuration

  • the main config file: config.toml

It’s necessary to modify this file config to create a new:

MediaType

The related MimeType to the Opensearch description format is: application/opensearchdescription+xml.

Hugo >= 0.20

Since Hugo 0.20, you need to add:

[mediaTypes]
[mediaTypes."application/opensearchdescription+xml"]
suffix = "xml"

Here, we added a new type of format for the mime type: application/opensearchdescription+xml, with the extension name: xml.

Hugo >= 0.44

Since Hugo 0.44, you need to add:

[mediaTypes]
[mediaTypes."application/opensearchdescription+xml"]
suffixes = ["xml"]

Tip

OuputFormat

The output format declaration to add:

[outputs]
[outputFormats.OpenSearch]
baseName = "opensearch"
isHTML = false
isPlainText = false
mediaType = "application/opensearchdescription+xml"
noUgly = true

Next, you need to add "OpenSearch" at your home variable:

[outputs]
home = ["HTML", "OpenSearch"]

Template

Simply, create the template as layouts/_default/index.opensearch.xml.

  • If your site is multilingual, the alternates links to the version of language are generated.

Info

File: layouts/_default/index.opensearch.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
{{ 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/">
	<Attribution>© {{ $.Date.Format "2006" | safeHTML }} {{ $.Site.Author.name }}; http://creativecommons.org/publicdomain/zero/1.0/legalcode.{{ .Site.Language.Lang }}</Attribution>
	<Contact>{{ .Site.Author.email | safeHTML }}</Contact>
	<Description>{{ i18n "opensearchDescription" }}</Description>
	<Developer>{{ .Site.Author.name | safeHTML }}</Developer>
	<InputEncoding>utf-8</InputEncoding>
	<Image width="64" height="64" type="image/png">{{ .Site.BaseURL }}img/Logo-64px.png</Image>
	<Image width="16" height="16" type="image/vnd.microsoft.icon">{{ .Site.BaseURL }}img/favicon.ico</Image>
	<Language>{{ .Site.LanguageCode }}</Language>
	<LongName>{{ .Site.Title }} :: {{ .Site.Language.Lang }}</LongName>
	<OutputEncoding>UTF-8</OutputEncoding>
	<ShortName>Websearch</ShortName>
	<SyndicationRight>open</SyndicationRight>
	<ie:PreviewUrl type="text/html" method="GET" template="{{ .Site.BaseURL }}{{ .Site.Language.Lang }}/tags/{searchTerms}/"/>
	<moz:SearchForm>{{ .Site.BaseURL }}{{ .Site.Language.Lang }}/tags/{searchTerms}/</moz:SearchForm>
	<Url template="{{ .Site.BaseURL }}{{ .Site.Language.Lang }}/tags/{searchTerms}/" type="text/html" />
	<Url rel="self" template="{{ .Site.BaseURL }}opensearch.xml" type="application/opensearchdescription+xml" />
</OpenSearchDescription>

autodiscovery

The autodiscovery is the method to inform the web clients about the Opensearch format description in your web site.

Warning

Atom

If you have modified your Hugo configuration to generate an Atom feed, you will need to modify it to add the following:

<link href="{{ $.Site.BaseURL }}/opensearch.xml" rel="search" type="application/opensearchdescription+xml" title="Websearch" />

HTML

Add a link element:

<link rel="search" href="/opensearch.xml" title="Websearch" type="application/opensearchdescription+xml">

RSS

If you generate your RSS feed, make sure to edit it, to add:

  • the xmlns:atom="http://www.w3.org/2005/Atom" attribute, into your rss element:
    <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  • in order to declarate atom:link item, as:
    <atom:link href="{{ $.Site.BaseURL }}/opensearch.xml" rel="search" type="application/opensearchdescription+xml" title="Websearch" />

Acknowledgments

Once again, a thruly thank you @solene who introduced me to Opensearch.