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

	"go.alanpearce.eu/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
	})
}