about summary refs log tree commit diff stats
path: root/internal/options/option.go
blob: b8b838a15d2d91f5eb59a1e9f4c0fdce9130e300 (plain)
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
package options

import (
	"encoding/json"
	"strings"

	"github.com/pkg/errors"
	"github.com/yuin/goldmark"
	"github.com/yuin/goldmark/extension"
)

type NixValue struct {
	Type string `json:"_type" mapstructure:"_type"`
	Text string `json:"text"`
}

type HTML struct {
	HTML string
}

var md = goldmark.New(
	goldmark.WithExtensions(extension.NewLinkify()),
)

func (html *HTML) UnmarshalText(text []byte) error {
	var out strings.Builder
	err := md.Convert(text, &out)
	if err != nil {
		return errors.WithMessage(err, "failed to convert markdown to HTML")
	}

	html.HTML = out.String()

	return nil
}

func (html *HTML) UnmarshalJSON(raw []byte) error {
	var v struct {
		HTML string
	}
	err := json.Unmarshal(raw, &v)
	if err != nil {
		return errors.WithMessage(err, "error unmarshaling json")
	}
	html.HTML = v.HTML

	return nil
}

type Link struct {
	Name string
	URL  string `json:"url"`
}

type NixOption struct {
	Option          string
	Declarations    []Link
	Default         NixValue
	Description     HTML
	Example         NixValue
	ReadOnly        bool
	Type            string
	Loc             []string
	RelatedPackages HTML
}

type NixOptions []NixOption