improve site:<domain> ux, add clear button

pull/5/head
cblgh 2021-10-30 17:30:05 +02:00 zatwierdzone przez Alexander Cobleigh
rodzic 0bf202d65b
commit 4f0b90d083
2 zmienionych plików z 35 dodań i 12 usunięć

Wyświetl plik

@ -2,6 +2,7 @@
{{ template "nav" . }}
<main id="results" class="flow2">
<h1>{{ .Data.Title }} {{ if ne .Data.Site "" }} for {{ .Data.Site }} {{ end }}</h1>
<form method="GET" class="search">
<label for="search">Search {{ .SiteName }} </label>
<span class="search__input">
@ -14,18 +15,29 @@
</button>
</span>
</form>
<nav>
<ul class="result-nav-list">
<li title="content from webring sites only"
class="{{ if .Data.IsInternal }} result__current {{ end }}">
<a href="/?q={{ .Data.Query }}">Webring</a>
</li>
<li title="content linked from webring sites, but which reside outside it"
class="{{ if .Data.IsInternal | not }} result__current {{ end }}">
<a href="/outgoing?q={{ .Data.Query }}">Outgoing</a>
</li>
</ul>
</nav>
{{ if ne .Data.Site "" }}
<!-- add a button to clear the search results if a site:<domain> param has been used -->
<form method="GET" class="search">
<span class="search__input">
<input type="hidden" minlength="1" required name="q" placeholder="Search" value="{{ .Data.Query }}" class="search-box" id="search">
<button type="submit" value="clear" name="clear">Clear</button>
</button>
</span>
</form>
{{ else }}
<nav>
<ul class="result-nav-list">
<li title="content from webring sites only"
class="{{ if .Data.IsInternal }} result__current {{ end }}">
<a href="/?q={{ .Data.Query }}">Webring</a>
</li>
<li title="content linked from webring sites, but which reside outside it"
class="{{ if .Data.IsInternal | not }} result__current {{ end }}">
<a href="/outgoing?q={{ .Data.Query }}">Outgoing</a>
</li>
</ul>
</nav>
{{ end }}
<article>
<ul role="list" class="flow2 two-columns width-126ch">
{{ range $index, $a := .Data.Pages }}

Wyświetl plik

@ -6,6 +6,7 @@ import (
"net/http"
"net/url"
"os"
"regexp"
"strings"
"html/template"
@ -57,6 +58,8 @@ var templates = template.Must(template.ParseFiles(
const useURLTitles = true
var sitePattern = regexp.MustCompile(`site:\S+`)
func (h RequestHandler) searchRoute(res http.ResponseWriter, req *http.Request) {
var query string
view := &TemplateView{}
@ -74,6 +77,14 @@ func (h RequestHandler) searchRoute(res http.ResponseWriter, req *http.Request)
domain = strings.TrimPrefix(parts[0], "https://")
domain = strings.TrimPrefix(domain, "http://")
domain = strings.TrimSuffix(domain, "/")
} else if sitePattern.MatchString(query) {
// if user searched with "site:<domain>" in text box, behave the same way as if a query param was used
domain = sitePattern.FindString(query)[5:]
}
// if clear button was used -> clear site param / search text
if parts, exists := params["clear"]; exists && parts[0] != "" {
domain = ""
query = sitePattern.ReplaceAllString(query, "")
}
}