about summary refs log tree commit diff stats
path: root/internal/components/markdown.templ
diff options
context:
space:
mode:
authorAlan Pearce2024-06-21 13:02:08 +0200
committerAlan Pearce2024-06-21 15:44:12 +0200
commita1dfc548198a1326e71f1dd70303a5d3441f7a39 (patch)
tree03e7d60dc389678ee7cadaac4d5e64596dffde91 /internal/components/markdown.templ
parentcac323d9ae70f55a43fd99b73e60cf614be11797 (diff)
downloadsearchix-a1dfc548198a1326e71f1dd70303a5d3441f7a39.tar.lz
searchix-a1dfc548198a1326e71f1dd70303a5d3441f7a39.tar.zst
searchix-a1dfc548198a1326e71f1dd70303a5d3441f7a39.zip
refactor: switch to templ for HTML templates
Diffstat (limited to 'internal/components/markdown.templ')
-rw-r--r--internal/components/markdown.templ35
1 files changed, 35 insertions, 0 deletions
diff --git a/internal/components/markdown.templ b/internal/components/markdown.templ
new file mode 100644
index 0000000..2a8787d
--- /dev/null
+++ b/internal/components/markdown.templ
@@ -0,0 +1,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
+	})
+}