From 53c707bb1d467cde1645dbd7b073493d7a547fe1 Mon Sep 17 00:00:00 2001 From: bep Date: Tue, 18 Nov 2014 22:42:49 +0100 Subject: [PATCH] Add markdownify template filter Note that this is a Markdownify filter, and is named as such; it's not a Asccidoc filter or in any way connected to a Page. Fixes #524 --- tpl/template.go | 5 +++++ tpl/template_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) 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) + } +}