One way to achieve this is to use the Funcs method to add a custom function to the template function map. See the Functions section of the template package docs for more info.
page.htmlwhttp.ResponseWriterpBody
Define a function:
func markDowner(args ...interface{}) template.HTML {
s := blackfriday.MarkdownCommon([]byte(fmt.Sprintf("%s", args...)))
return template.HTML(s)
}
Add it to the template function map:
tmpl := template.Must(template.New("page.html").Funcs(template.FuncMap{"markDown": markDowner}).ParseFiles("page.html"))
Execute the template:
err := tmpl.ExecuteTemplate(w, "page.html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
Then, in your template file, you can put something like:
{{.Body | markDown}}
BodymarkDowner