Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-05-06 11:11:17 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-05-06 11:11:17 +0300
commit273a68400ff081d05508afbcb6d158711b865307 (patch)
treef86de78d822cd71133a27f50f64abfb1493c2f98 /tpl
parent7cbafa4ec58abc7e338e33bef16a84ef1d8a5ff2 (diff)
Add non-string support in markdownify
Diffstat (limited to 'tpl')
-rw-r--r--tpl/template_funcs.go3
-rw-r--r--tpl/template_funcs_test.go18
2 files changed, 14 insertions, 7 deletions
diff --git a/tpl/template_funcs.go b/tpl/template_funcs.go
index 8b99cc09d..80ecdfe0c 100644
--- a/tpl/template_funcs.go
+++ b/tpl/template_funcs.go
@@ -1203,7 +1203,8 @@ var markdownTrimPrefix = []byte("<p>")
var markdownTrimSuffix = []byte("</p>\n")
// markdownify renders a given string from Markdown to HTML.
-func markdownify(text string) template.HTML {
+func markdownify(in interface{}) template.HTML {
+ text := cast.ToString(in)
m := helpers.RenderBytes(&helpers.RenderingContext{Content: []byte(text), PageFmt: "markdown"})
m = bytes.TrimPrefix(m, markdownTrimPrefix)
m = bytes.TrimSuffix(m, markdownTrimSuffix)
diff --git a/tpl/template_funcs_test.go b/tpl/template_funcs_test.go
index 3152b13a1..32871f6f0 100644
--- a/tpl/template_funcs_test.go
+++ b/tpl/template_funcs_test.go
@@ -1699,13 +1699,19 @@ func TestReturnWhenSet(t *testing.T) {
}
func TestMarkdownify(t *testing.T) {
- result := markdownify("Hello **World!**")
-
- expect := template.HTML("Hello <strong>World!</strong>")
-
- if result != expect {
- t.Errorf("Markdownify: got '%s', expected '%s'", result, expect)
+ for i, this := range []struct {
+ in interface{}
+ expect interface{}
+ }{
+ {"Hello **World!**", template.HTML("Hello <strong>World!</strong>")},
+ {[]byte("Hello Bytes **World!**"), template.HTML("Hello Bytes <strong>World!</strong>")},
+ } {
+ result := markdownify(this.in)
+ if !reflect.DeepEqual(result, this.expect) {
+ t.Errorf("[%d] markdownify got %v (type %v) but expected %v (type %v)", i, result, reflect.TypeOf(result), this.expect, reflect.TypeOf(this.expect))
+ }
}
+
}
func TestApply(t *testing.T) {