mirror of
https://github.com/gohugoio/hugo.git
synced 2024-07-05 08:42:12 +00:00
Add non-string support in markdownify
This commit is contained in:
parent
7cbafa4ec5
commit
273a68400f
|
@ -1203,7 +1203,8 @@ var markdownTrimPrefix = []byte("<p>")
|
||||||
var markdownTrimSuffix = []byte("</p>\n")
|
var markdownTrimSuffix = []byte("</p>\n")
|
||||||
|
|
||||||
// markdownify renders a given string from Markdown to HTML.
|
// 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 := helpers.RenderBytes(&helpers.RenderingContext{Content: []byte(text), PageFmt: "markdown"})
|
||||||
m = bytes.TrimPrefix(m, markdownTrimPrefix)
|
m = bytes.TrimPrefix(m, markdownTrimPrefix)
|
||||||
m = bytes.TrimSuffix(m, markdownTrimSuffix)
|
m = bytes.TrimSuffix(m, markdownTrimSuffix)
|
||||||
|
|
|
@ -1699,13 +1699,19 @@ func TestReturnWhenSet(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMarkdownify(t *testing.T) {
|
func TestMarkdownify(t *testing.T) {
|
||||||
result := markdownify("Hello **World!**")
|
for i, this := range []struct {
|
||||||
|
in interface{}
|
||||||
expect := template.HTML("Hello <strong>World!</strong>")
|
expect interface{}
|
||||||
|
}{
|
||||||
if result != expect {
|
{"Hello **World!**", template.HTML("Hello <strong>World!</strong>")},
|
||||||
t.Errorf("Markdownify: got '%s', expected '%s'", result, expect)
|
{[]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) {
|
func TestApply(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue