From 255acb360f51037e2801c7a6fc05675a1f655372 Mon Sep 17 00:00:00 2001 From: partisan Date: Wed, 7 May 2025 12:54:51 +0200 Subject: [PATCH] iproved favicon caching / ID system --- common.go | 39 ++++++++++++++++++--------------------- text.go | 4 ++-- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/common.go b/common.go index 257ba6d..326c01c 100755 --- a/common.go +++ b/common.go @@ -38,8 +38,9 @@ type SearchEngine struct { } type LinkParts struct { - Domain template.HTML - Path template.HTML + Domain template.HTML + Path template.HTML + RootURL string // used by getFaviconProxyURL() } // Helper function to render templates without elapsed time measurement @@ -131,11 +132,10 @@ func FormatElapsedTime(elapsed time.Duration) string { } return fmt.Sprintf("%.2f %s", elapsed.Seconds(), Translate("seconds")) } - -func FormatURLParts(rawURL string) (domain, path string) { +func FormatURLParts(rawURL string) (domain, path, rootURL string) { parsed, err := url.Parse(rawURL) - if err != nil { - return rawURL, "" + if err != nil || parsed.Host == "" { + return "", "", "" } domain = parsed.Host @@ -143,36 +143,33 @@ func FormatURLParts(rawURL string) (domain, path string) { domain = domain[4:] } - // Clean up the path - remove empty segments and trailing slashes + rootURL = parsed.Scheme + "://" + parsed.Host + path = strings.Trim(parsed.Path, "/") pathSegments := strings.Split(path, "/") - - // Filter out empty segments var cleanSegments []string for _, seg := range pathSegments { if seg != "" { cleanSegments = append(cleanSegments, seg) } } - path = strings.Join(cleanSegments, "/") - return domain, path + return domain, path, rootURL } func FormatLinkHTML(rawURL string) LinkParts { - domain, path := FormatURLParts(rawURL) + domain, path, root := FormatURLParts(rawURL) - if path == "" { - return LinkParts{ - Domain: template.HTML(fmt.Sprintf(`%s`, template.HTMLEscapeString(domain))), - } + lp := LinkParts{ + RootURL: root, } - // Only add separators between non-empty path segments - pathDisplay := strings.ReplaceAll(path, "/", " › ") + lp.Domain = template.HTML(fmt.Sprintf(`%s`, template.HTMLEscapeString(domain))) - return LinkParts{ - Domain: template.HTML(fmt.Sprintf(`%s`, template.HTMLEscapeString(domain))), - Path: template.HTML(fmt.Sprintf(` › %s`, template.HTMLEscapeString(pathDisplay))), + if path != "" { + pathDisplay := strings.ReplaceAll(path, "/", " › ") + lp.Path = template.HTML(fmt.Sprintf(` › %s`, template.HTMLEscapeString(pathDisplay))) } + + return lp } diff --git a/text.go b/text.go index 5fddc9f..7dedb63 100755 --- a/text.go +++ b/text.go @@ -68,8 +68,8 @@ func HandleTextSearch(w http.ResponseWriter, settings UserSettings, query string // First format the link prettyLink := FormatLinkHTML(r.URL) - faviconID := faviconIDFromURL(r.URL) - faviconURL := getFaviconProxyURL("", r.URL) //string(prettyLink.Domain) + faviconID := faviconIDFromURL(prettyLink.RootURL) + faviconURL := getFaviconProxyURL("", prettyLink.RootURL) decoratedResults = append(decoratedResults, DecoratedResult{ TextSearchResult: r,