all repos — searchix @ v0.4.10

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

internal/components/detail.go (view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package components

import (
	"fmt"
	"strings"

	"alin.ovh/searchix/internal/index"
	"alin.ovh/searchix/internal/nix"

	g "alin.ovh/gomponents"
)

func Detail(thing index.Indexable) g.Node {
	switch t := thing.(type) {
	case nix.Option:
		return OptionDetail(t)
	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 ""
	}
}

func DetailPage(tdata TemplateData, thing index.Indexable) g.Node {
	return Page(tdata, Detail(thing))
}