about summary refs log tree commit diff stats
path: root/internal/server/server.go
diff options
context:
space:
mode:
authorAlan Pearce2024-05-06 10:14:17 +0200
committerAlan Pearce2024-05-06 10:14:17 +0200
commitc0fbf11f843af84e8891a708c4d217dd6c523473 (patch)
tree1fca048be6f784dd895d2c2d9ff09200d5d610fd /internal/server/server.go
parent7d08b696e9ab6de61c53a5dc9153595a5a8a6d98 (diff)
downloadsearchix-c0fbf11f843af84e8891a708c4d217dd6c523473.tar.lz
searchix-c0fbf11f843af84e8891a708c4d217dd6c523473.tar.zst
searchix-c0fbf11f843af84e8891a708c4d217dd6c523473.zip
feat: render markdown examples
Diffstat (limited to 'internal/server/server.go')
-rw-r--r--internal/server/server.go19
1 files changed, 17 insertions, 2 deletions
diff --git a/internal/server/server.go b/internal/server/server.go
index ad75ac1..76fbecb 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -4,6 +4,7 @@ import (
 	"context"
 	"encoding/json"
 	"fmt"
+	"html"
 	"html/template"
 	"io"
 	"log"
@@ -25,6 +26,8 @@ import (
 	"github.com/osdevisnot/sorvor/pkg/livereload"
 	"github.com/pkg/errors"
 	"github.com/shengyanli1982/law"
+	"github.com/yuin/goldmark"
+	"github.com/yuin/goldmark/extension"
 )
 
 var config *cfg.Config
@@ -74,9 +77,20 @@ func applyDevModeOverrides(config *cfg.Config) {
 	config.CSP.ConnectSrc = slices.Insert(config.CSP.ConnectSrc, 0, "'self'")
 }
 
+var md = goldmark.New(
+	goldmark.WithExtensions(extension.NewLinkify()),
+)
 var templateFuncs = template.FuncMap{
-	"HTML": func(input string) template.HTML {
-		return template.HTML(input) // #nosec G203
+	"markdown": func(input options.Markdown) template.HTML {
+		var out strings.Builder
+		err := md.Convert([]byte(input), &out)
+		if err != nil {
+			slog.Warn(fmt.Sprintf("markdown conversion failed: %v", err))
+
+			return template.HTML(html.EscapeString(err.Error())) // #nosec G203 -- duh?
+		}
+
+		return template.HTML(out.String()) // #nosec G203
 	},
 }
 
@@ -177,6 +191,7 @@ func New(runtimeConfig *Config) (*Server, error) {
 	if err != nil {
 		slog.Error(fmt.Sprintf("error parsing json file: %v", err))
 	}
+
 	mux.HandleFunc("/options/results", func(w http.ResponseWriter, r *http.Request) {
 		tdata := OptionResultData{
 			TemplateData: indexData,