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

import (
	"regexp"

	"searchix/internal/nix"

	"github.com/yuin/goldmark"
	"github.com/yuin/goldmark/extension"
	"context"
	"io"
)

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) error {
		err := md.Convert([]byte(text), w)

		return err
	})
}