all repos — searchix @ c3fa5a442ee81a4c2252736c4d4ef461c6b3af93

Search engine for NixOS, nix-darwin, home-manager and NUR users

feat: Add basic page meta description https://codeberg.org/alinnow/searchix/pulls/15

Kaya
commit

c3fa5a442ee81a4c2252736c4d4ef461c6b3af93

parent

078020ea68fd850a3d1dcfcdfbcb2ba61312161b

M internal/components/data.gointernal/components/data.go
@@ -19,6 +19,8 @@ ExtraHeadHTML string
Code int Message string Title string + Description string + BaseURL config.URL Assets *frontend.AssetCollection SearchNav SearchNav }
M internal/components/detail.gointernal/components/detail.go
@@ -1,6 +1,9 @@
package components import ( + "fmt" + "strings" + "alin.ovh/searchix/internal/index" "alin.ovh/searchix/internal/nix"
@@ -15,6 +18,52 @@ case nix.Package:
return PackageDetail(t) default: return nil + } +} + +const MaxDescriptionLength = 160 + +func TruncateWithEllipsis(text string, maxLength int) string { + if len(text) > maxLength { + if maxLength > 1 { + return text[:maxLength-1] + "…" + } + + return text + } + + return text +} + +func MetaDescription(thing index.Indexable) string { + switch t := thing.(type) { + case nix.Package: + desc := t.Attribute + if t.Version != "" { + desc += " " + t.Version + } + if t.Description != "" { + desc += " - " + t.Description + } + + desc = TruncateWithEllipsis(desc, MaxDescriptionLength) + + return desc + case nix.Option: + desc := t.Name + if t.Type != "" { + desc = fmt.Sprintf("%s - %s option", desc, t.Type) + } + plain := strings.TrimSpace(string(t.Description)) + if plain != "" { + desc += ". " + plain + } + + desc = TruncateWithEllipsis(desc, MaxDescriptionLength) + + return desc + default: + return "" } }
M internal/components/page.gointernal/components/page.go
@@ -2,6 +2,7 @@ package components
import ( "net/url" + "strings" "alin.ovh/searchix/frontend" "alin.ovh/searchix/internal/config"
@@ -18,6 +19,23 @@ Lang("en-GB"),
Head( Meta(Charset("utf-8")), Meta(Name("viewport"), Content("width=device-width, initial-scale=1")), + g.If(tdata.Description != "", + g.Group([]g.Node{ + Meta(Name("description"), Content(tdata.Description)), + Meta(g.Attr("property", "og:description"), Content(tdata.Description)), + }), + ), + Meta(g.Attr("property", "og:type"), Content("website")), + Meta(g.Attr("property", "og:site_name"), Content("Searchix")), + Meta(g.Attr("property", "og:title"), Content(ogTitle(tdata.Title))), + g.If(tdata.BaseURL.URL != nil, + g.Group([]g.Node{ + Meta( + g.Attr("property", "og:image"), + Content(tdata.BaseURL.JoinPath("apple-touch-icon.png").String()), + ), + }), + ), TitleEl( g.Raw(tdata.Title), g.Text("Searchix"), g.If(config.DevMode, g.Text(" (Dev)")),
@@ -126,6 +144,13 @@ ),
), ), ) +} + +func ogTitle(title string) string { + t := strings.ReplaceAll(title, " ‐ ", " - ") + t += "Searchix" + + return t } func css(css *frontend.Asset) g.Node {
M internal/server/error.gointernal/server/error.go
@@ -25,6 +25,7 @@ ExtraHeadHTML: config.Web.ExtraHeadHTML,
Sources: sources, Code: code, Message: message, + BaseURL: config.Web.BaseURL, Assets: assets, } w.Header().Set("Cache-Control", "no-store")
M internal/server/global.gointernal/server/global.go
@@ -119,6 +119,11 @@ g.errorHandler(w, r, "Too many results, use pagination", http.StatusBadRequest)
} page.SetResults(results.Total) + searchDesc := fmt.Sprintf("Search results for %s", qs) + if source.Importer != config.All { + searchDesc += " in " + source.Name + } + tdata := components.ResultData{ TemplateData: components.TemplateData{ ExtraHeadHTML: g.cfg.Web.ExtraHeadHTML,
@@ -127,6 +132,8 @@ Sources: sources,
Assets: g.assets, Query: qs, Title: g.getTitle(source, qs), + Description: searchDesc, + BaseURL: g.cfg.Web.BaseURL, SearchNav: components.NewSearchNav(*r.URL).WithPagination(page), }, Query: qs,
@@ -156,6 +163,8 @@ ExtraHeadHTML: g.cfg.Web.ExtraHeadHTML,
Sources: sources, Source: source, Title: g.getTitle(source, ""), + Description: "Search Nix packages and options", + BaseURL: g.cfg.Web.BaseURL, Assets: g.assets, }, components.ResultData{},
M internal/server/source.gointernal/server/source.go
@@ -79,6 +79,8 @@ Source: h.source,
Sources: sources, Assets: h.global.assets, Title: h.getTitle(doc), + Description: components.MetaDescription(doc), + BaseURL: h.global.cfg.Web.BaseURL, } var baseErr error if r.Header.Get("Fetch") == "true" {