about summary refs log tree commit diff stats
path: root/internal
diff options
context:
space:
mode:
authorAlan Pearce2025-03-20 13:20:16 +0100
committerAlan Pearce2025-03-20 13:20:16 +0100
commit7247322a386f065c643dc58f0ae5b57ad7ec1cc1 (patch)
tree3941fe279ffb36e88f1527aecb2ac06f9fe9aecf /internal
parent2705e97ce1cf7d6a399c5f0175c36562fdef3352 (diff)
downloadsearchix-7247322a386f065c643dc58f0ae5b57ad7ec1cc1.tar.lz
searchix-7247322a386f065c643dc58f0ae5b57ad7ec1cc1.tar.zst
searchix-7247322a386f065c643dc58f0ae5b57ad7ec1cc1.zip
feat: make list of source links dynamic
Diffstat (limited to 'internal')
-rw-r--r--internal/components/search.go29
-rw-r--r--internal/config/repository.go15
2 files changed, 39 insertions, 5 deletions
diff --git a/internal/components/search.go b/internal/components/search.go
index b8803c8..b4ef7bd 100644
--- a/internal/components/search.go
+++ b/internal/components/search.go
@@ -5,6 +5,7 @@ import (
 
 	g "go.alanpearce.eu/gomponents"
 	. "go.alanpearce.eu/gomponents/html"
+	"go.alanpearce.eu/searchix/internal/config"
 )
 
 func SearchForm(tdata TemplateData, r ResultData) g.Node {
@@ -35,11 +36,9 @@ func SearchPage(tdata TemplateData, r ResultData, children ...g.Node) g.Node {
 		tdata,
 		P(
 			g.Text("Search Nix packages and options from "),
-			A(Href("https://nixos.org"), g.Text("NixOS")),
-			g.Text(", "),
-			A(Href("https://github.com/LnL7/nix-darwin"), g.Text("nix-darwin")),
-			g.Text(" and "),
-			A(Href("https://github.com/nix-community/home-manager"), g.Text("home-manager")),
+			MapCommaList(tdata.Sources, func(source *config.Source) g.Node {
+				return A(Href(source.Repo.String()), g.Text(source.Name))
+			}),
 		),
 		g.If(Indexing.InProgress,
 			P(Class("notice"),
@@ -96,3 +95,23 @@ func SearchPage(tdata TemplateData, r ResultData, children ...g.Node) g.Node {
 		),
 	)
 }
+
+func MapCommaList[T any](items []T, fn func(T) g.Node) g.Node {
+	out := make([]g.Node, (len(items) * 2))
+	j := 0
+	last := len(items) - 2
+	for i := range items {
+		out[j] = fn(items[i])
+		j++
+		if i <= last {
+			if i == last {
+				out[j] = g.Text(" and ")
+			} else {
+				out[j] = g.Text(", ")
+			}
+		}
+		j++
+	}
+
+	return g.Group(out)
+}
diff --git a/internal/config/repository.go b/internal/config/repository.go
index 52b255e..9d6ce98 100644
--- a/internal/config/repository.go
+++ b/internal/config/repository.go
@@ -2,6 +2,7 @@ package config
 
 import (
 	"fmt"
+	"net/url"
 	"strings"
 
 	"gitlab.com/tozd/go/errors"
@@ -21,6 +22,20 @@ type Repository struct {
 	Revision string `toml:"-"`
 }
 
+func (r *Repository) String() string {
+	switch r.Type {
+	case GitHub:
+		u, err := url.JoinPath("https://github.com/", r.Owner, r.Repo)
+		if err != nil {
+			panic(err)
+		}
+
+		return u
+	default:
+		panic("need repository string implementation for type " + r.Type.String())
+	}
+}
+
 func (f RepoType) String() string {
 	switch f {
 	case GitHub: