pcmt/modules/funcmap/funcmap.go
leo e8ac4e39ce
All checks were successful
continuous-integration/drone/push Build is passing
modules/funcmap: add funcs to calculate SRI hashes
* correctly handle LiveMode resp. whether or not to set/read embeds
2023-05-12 00:11:23 +02:00

50 lines
1.0 KiB
Go

package funcmap
import (
"html/template"
"io/fs"
modbluemonday "git.dotya.ml/mirre-mt/pcmt/modules/bluemonday"
)
var (
embedAssets *fs.FS
isLive = true
)
func FuncMap() template.FuncMap {
return template.FuncMap{
"ifIE": func() template.HTML { return template.HTML("<!--[if IE]>") },
"endifIE": func() template.HTML { return template.HTML("<![endif]>") },
"htmlSafe": func(html string) template.HTML {
return template.HTML( //nolint:gosec
modbluemonday.Policy.Sanitize(html),
)
},
"pageIs": func(want, got string) bool {
return want == got
},
"sha256": func(path string) string {
t := New("sha256")
r, err := t.Integrity(path, isLive)
if err != nil {
return ""
}
return *r
},
}
}
// SetEmbeds saves the pointer to embedded assets (and toggles the isLive var).
func SetEmbeds(embeds *fs.FS) {
embedAssets = embeds
isLive = false
}
// TODO: mimic https://github.com/drone/funcmap/blob/master/funcmap.go
func setFuncMap(t *template.Template) { //nolint:unused
t.Funcs(FuncMap())
}