--- date: 2020-04-14T16:54:07+02:00 description: "Howto set up the Opensearch description format into Hugo!" draft: false tags: ["Hugo", "Opensearch", "search"] title: "Hugo: Opensearch" translationKey: "hugo-opensearch" --- ## 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 {{}}. **Hugo**, by default, not manage Opensearch. We are going to modify the {{< anchor configuration configuration >}} to create a new custom output format. ## Documentation the Hugo official documentation: * {{< gohugo n="output-formats" s="templates" >}} the Opensearch official documentation: * https://github.com/dewitt/opensearch/blob/master/opensearch-1-1-draft-6.md ## Configuration * the main config file: `config.toml` It's necessary to modify this file config to create a new: - {{< anchor "Media Type" "mediatype" >}} - {{< anchor "Output Format" "ouputformat" >}} - {{< anchor Template "template" >}} for the Opensearch file. ### MediaType The related MimeType to the Opensearch description format is: `application/opensearchdescription+xml`. #### Hugo >= 0.20 Since Hugo 0.20, you need to add: ```toml [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: ```toml [mediaTypes] [mediaTypes."application/opensearchdescription+xml"] suffixes = ["xml"] ``` {{< note tip >}}If your old configuration was before the v0.44, you have to transform the variable `suffix` to `suffixes = ['xml']` ! {{< /note >}} ### OuputFormat The output format declaration to add: ```toml [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: ```toml [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. ---- {{}}ATTENTION: the presented template manages a multilingual website. {{}} ```xml {{ printf `` | safeHTML }} © {{ $.Date.Format "2006" | safeHTML }} {{ site.Author.name }}; http://creativecommons.org/publicdomain/zero/1.0/legalcode.{{ site.Language.Lang }} {{ site.Author.email | safeHTML }} {{ i18n "opensearchDescription" }} {{ site.Author.name | safeHTML }} utf-8 {{ site.BaseURL }}img/Logo-64px.png {{ site.BaseURL }}img/favicon.ico {{ site.LanguageCode }} {{ site.Title }} :: {{ site.Language.Lang }} UTF-8 Websearch open {{ site.BaseURL }}{{ site.Language.Lang }}/tags/{searchTerms}/ ``` ## autodiscovery The autodiscovery is the method to inform the web clients about the Opensearch format description in your web site. {{< note warning >}}Make sure that the value of the `title` attribute matches that of the `ShortName` element!{{}} ### Atom If you have modified your Hugo configuration to generate an {{< inside2 a="atom" l="web:hugo:hugo-feed" t="Atom" >}} feed, you will need to modify it to add the following: ```html ``` ### HTML Add a `link` element: ```html ``` ### RSS If you generate your {{< inside2 a="rss" l="web:hugo:hugo-feed" t="RSS" >}} feed, make sure to edit it, to add: * the `xmlns:atom="http://www.w3.org/2005/Atom"` attribute, into your `rss` element: ```html ``` * in order to declarate `atom:link` item, as: ```html