GoBlog/opensearch.go

32 lines
1.1 KiB
Go
Raw Normal View History

2021-06-09 20:44:22 +00:00
package main
import (
2021-12-30 11:40:21 +00:00
"bytes"
2021-06-09 20:44:22 +00:00
"fmt"
"net/http"
"go.goblog.app/app/pkgs/contenttype"
2021-06-09 20:44:22 +00:00
)
func (a *goBlog) serveOpenSearch(w http.ResponseWriter, r *http.Request) {
_, b := a.getBlog(r)
2021-08-05 12:53:22 +00:00
title := a.renderMdTitle(b.Title)
sURL := a.getFullAddress(b.getRelativePath(defaultIfEmpty(b.Search.Path, defaultSearchPath)))
2021-12-30 11:40:21 +00:00
var buf bytes.Buffer
_, _ = fmt.Fprintf(&buf, "<?xml version=\"1.0\"?><OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\" xmlns:moz=\"http://www.mozilla.org/2006/browser/search/\">"+
2021-06-09 20:44:22 +00:00
"<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")
2021-12-30 11:40:21 +00:00
_ = a.min.Minify(contenttype.XML, w, &buf)
2021-06-09 20:44:22 +00:00
}
func openSearchUrl(b *configBlog) string {
if b.Search != nil && b.Search.Enabled {
return b.getRelativePath(defaultIfEmpty(b.Search.Path, defaultSearchPath) + "/opensearch.xml")
2021-06-09 20:44:22 +00:00
}
return ""
}