goveal/rendering/templates.go
2022-03-22 20:48:56 +01:00

60 lines
1.5 KiB
Go

package rendering
import (
"embed"
"hash/fnv"
"html/template"
"github.com/Masterminds/sprig/v3"
"github.com/gomarkdown/markdown"
mdhtml "github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"github.com/valyala/bytebufferpool"
"github.com/baez90/goveal/rendering/emoji"
)
var (
//go:embed templates/*.gohtml
templatesFS embed.FS
templates *template.Template
)
func init() {
templates = template.New("rendering").
Funcs(sprig.FuncMap()).
Funcs(template.FuncMap{
"renderMarkdown": func(md string) template.HTML {
rr := &RevealRenderer{
Hash: fnv.New32a(),
}
emojis := emoji.NewEmojiParser()
mdParser := parser.NewWithExtensions(parserExtensions)
mdParser.Opts.ParserHook = emojis.EmojiParser
renderer := mdhtml.NewRenderer(mdhtml.RendererOptions{
Flags: mdhtml.CommonFlags | mdhtml.HrefTargetBlank,
RenderNodeHook: rr.RenderHook,
})
renderedHTML := markdown.ToHTML([]byte(md), mdParser, renderer)
//nolint:gosec // template should be esacped
return template.HTML(renderedHTML)
},
})
var err error
if templates, err = templates.ParseFS(templatesFS, "templates/*.gohtml"); err != nil {
panic(err)
}
}
func renderTemplate(templateName string, data any) (output []byte, err error) {
buffer := bytebufferpool.Get()
defer func() {
buffer.Reset()
bytebufferpool.Put(buffer)
}()
err = templates.ExecuteTemplate(buffer, templateName, data)
return buffer.Bytes(), err
}