Skip to content

Commit a2fc0f6

Browse files
authored
Open search (contao#689)
* Listen to `search` query param to trigger algolia search * Provide open search information * Move search link to custom-header.html * Fix favicon urls * Use custom output format to generate opensearch xml file * Use output format permalink to generate the search.xml link * Use site title for open search xml shortname and description
1 parent 24a1abc commit a2fc0f6

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

page/config/_default/config.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ canonifyURLs: true
55
enableGitInfo: true
66
disablePathToLower: true
77
outputs:
8-
home: ['HTML', 'JSON']
8+
home: ['HTML', 'JSON', 'OpenSearchXML']
99
markup:
1010
goldmark:
1111
parser:
@@ -14,3 +14,13 @@ markup:
1414
unsafe: true
1515
tableOfContents:
1616
endLevel: 3
17+
outputFormats:
18+
OpenSearchXML:
19+
baseName: search
20+
isPlainText: true
21+
mediaType: application/opensearchdescription+xml
22+
protocol: bep://
23+
mediaTypes:
24+
application/opensearchdescription+xml:
25+
suffixes:
26+
- xml

page/layouts/index.opensearchxml.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
2+
xmlns:moz="http://www.mozilla.org/2006/browser/search/">
3+
<ShortName>{{ .Title }}</ShortName>
4+
<Description>{{ .Title }}</Description>
5+
<InputEncoding>UTF-8</InputEncoding>
6+
<Image width="16" height="16" type="image/x-icon">https://contao.org/favicon.ico</Image>
7+
<Url type="text/html" method="get" template="{{.Permalink}}?search={searchTerms}"/>
8+
</OpenSearchDescription>
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js"></script>
2-
<script type="text/javascript"> docsearch({
3-
apiKey: '532603b4ad01695b68a0f6048dc8ea46',
4-
indexName: 'contao',
5-
inputSelector: '#algolia-search',
6-
algoliaOptions: { 'facetFilters': ["language:{{ .Page.Language | default "en" }}"] },
7-
debug: true // Set debug to true if you want to inspect the dropdown
8-
});
2+
<script type="text/javascript">(function () {
3+
var search = docsearch({
4+
apiKey: '532603b4ad01695b68a0f6048dc8ea46',
5+
indexName: 'contao',
6+
inputSelector: '#algolia-search',
7+
algoliaOptions: { 'facetFilters': ["language:{{ .Page.Language | default "en" }}"] },
8+
debug: true // Set debug to true if you want to inspect the dropdown
9+
});
10+
if (URLSearchParams !== undefined) {
11+
var params = new URLSearchParams(location.search);
12+
if (params.has('search')) {
13+
search.autocomplete.autocomplete.setVal(params.get('search'));
14+
search.autocomplete.autocomplete.open();
15+
}
16+
}
17+
})();
918
</script>

page/layouts/partials/custom-header.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@
1212
g.async=true; g.defer=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
1313
})();
1414
</script>
15+
16+
{{ $searchXml := .Site.Home.OutputFormats.Get "opensearchxml" }}
17+
{{ if ne $searchXml }}
18+
<link rel="search" type="application/opensearchdescription+xml" title="Search {{ .Site.Title }}" href="{{ $searchXml.RelPermalink }}">
19+
{{ end }}

0 commit comments

Comments
 (0)