Add markdownify template filter
authorbep <bjorn.erik.pedersen@gmail.com>
Tue, 18 Nov 2014 21:42:49 +0000 (22:42 +0100)
committerspf13 <steve.francia@gmail.com>
Tue, 25 Nov 2014 04:17:25 +0000 (23:17 -0500)
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
tpl/template_test.go

index b536c46260bbe8830fe45d663c46b6bd970fc7e4..9ea84dea8eb80c0d9841d2c82ac3afe7d0bfd190 100644 (file)
@@ -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)
 }
index 5eff0f0676702668439a5017b99f4d89e9297e7f..066c75dde9756e334f1130038c82eae42de018fb 100644 (file)
@@ -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("<p>Hello <strong>World!</strong></p>\n")
+
+       if result != expect {
+               t.Errorf("Markdownify: got '%s', expected '%s'", result, expect)
+       }
+}