diff --git a/tpl/template.go b/tpl/template.go index b536c4626..9ea84dea8 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -96,6 +96,7 @@ func New() Template { "isset": IsSet, "echoParam": ReturnWhenSet, "safeHtml": SafeHtml, + "markdownify": Markdownify, "first": First, "where": Where, "highlight": Highlight, @@ -422,6 +423,10 @@ func Highlight(in interface{}, lang string) template.HTML { return template.HTML(helpers.Highlight(html.UnescapeString(str), lang)) } +func Markdownify(text string) template.HTML { + return template.HTML(helpers.RenderBytes([]byte(text), "markdown", "")) +} + func SafeHtml(text string) template.HTML { return template.HTML(text) } diff --git a/tpl/template_test.go b/tpl/template_test.go index 5eff0f067..066c75dde 100644 --- a/tpl/template_test.go +++ b/tpl/template_test.go @@ -1,6 +1,7 @@ package tpl import ( + "html/template" "reflect" "testing" ) @@ -339,3 +340,14 @@ func TestWhere(t *testing.T) { } } } + +func TestMarkdownify(t *testing.T) { + + result := Markdownify("Hello **World!**") + + expect := template.HTML("

Hello World!

\n") + + if result != expect { + t.Errorf("Markdownify: got '%s', expected '%s'", result, expect) + } +}