From c0fbf11f843af84e8891a708c4d217dd6c523473 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Mon, 6 May 2024 10:14:17 +0200 Subject: feat: render markdown examples --- internal/server/server.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'internal/server/server.go') 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, -- cgit 1.4.1