about summary refs log tree commit diff stats
path: root/internal/components/markdown.templ
blob: 9bbbe1bf6b06cffd365ca4c4e93985ba1d104e55 (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
package components

import (
	"context"
	"io"
	"regexp"

	"github.com/yuin/goldmark"
	"github.com/yuin/goldmark/extension"
	"gitlab.com/tozd/go/errors"

	"go.alanpearce.eu/searchix/internal/nix"
)

var (
	md = goldmark.New(
		goldmark.WithExtensions(extension.NewLinkify()),
	)
	firstSentenceRegexp = regexp.MustCompile(`^.*?\.[[:space:]]`)
)

func firstSentence[T ~string](text T) T {
	if fs := firstSentenceRegexp.FindString(string(text)); fs != "" {
		return T(fs)
	}

	return text
}

func markdown(text nix.Markdown) templ.Component {
	return templ.ComponentFunc(func(ctx context.Context, w io.Writer) errors.E {
		err := md.Convert([]byte(text), w)

		return err
	})
}