Open Search

This commit is contained in:
Jan-Lukas Else 2021-06-09 22:44:22 +02:00
parent 5b06d3ec59
commit 9242305be4
4 changed files with 35 additions and 1 deletions

View File

@ -227,6 +227,7 @@ func (a *goBlog) buildStaticHandlersRouters() error {
a.searchRouter.Use(a.cache.cacheMiddleware)
a.searchRouter.Get("/", a.serveSearch)
a.searchRouter.Post("/", a.serveSearch)
a.searchRouter.Get("/opensearch.xml", a.serveOpenSearch)
searchResultPath := "/" + searchPlaceholder
a.searchRouter.Get(searchResultPath, a.serveSearchResult)
a.searchRouter.Get(searchResultPath+feedPath, a.serveSearchResult)

28
opensearch.go Normal file
View File

@ -0,0 +1,28 @@
package main
import (
"fmt"
"net/http"
)
func (a *goBlog) serveOpenSearch(w http.ResponseWriter, r *http.Request) {
blog := r.Context().Value(blogContextKey).(string)
b := a.cfg.Blogs[blog]
title := b.Title
sURL := a.cfg.Server.PublicAddress + b.getRelativePath(b.Search.Path)
xml := fmt.Sprintf("<?xml version=\"1.0\"?><OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\" xmlns:moz=\"http://www.mozilla.org/2006/browser/search/\">"+
"<ShortName>%s</ShortName><Description>%s</Description>"+
"<Url type=\"text/html\" method=\"post\" template=\"%s\"><Param name=\"q\" value=\"{searchTerms}\" /></Url>"+
"<moz:SearchForm>%s</moz:SearchForm>"+
"</OpenSearchDescription>",
title, title, sURL, sURL)
w.Header().Set(contentType, "application/opensearchdescription+xml")
writeMinified(w, contentTypeXML, []byte(xml))
}
func openSearchUrl(b *configBlog) string {
if b.Search != nil && b.Search.Enabled {
return b.getRelativePath(b.Search.Path + "/opensearch.xml")
}
return ""
}

View File

@ -180,7 +180,8 @@ func (a *goBlog) initRendering() error {
}
return
},
"geotitle": a.db.geoTitle,
"geotitle": a.db.geoTitle,
"opensearch": openSearchUrl,
}
baseTemplate, err := template.New("base").Funcs(templateFunctions).ParseFiles(path.Join(templatesDir, templateBase+templatesExt))

View File

@ -20,6 +20,10 @@
<link rel="me" href="{{ . }}" />
{{ end }}
{{ end }}
{{ $os := opensearch .Blog }}
{{ if $os }}
<link rel="search" type="application/opensearchdescription+xml" href="{{ $os }}" title="{{ .Blog.Title }}" />
{{ end }}
{{ include "header" . }}
{{ block "main" . }}{{ end }}
{{ include "footer" . }}