Skip to content

Commit

Permalink
tpl/transform: Only strip p tag in markdownify if only one paragraph
Browse files Browse the repository at this point in the history
Fixes #3040
  • Loading branch information
bep committed Aug 10, 2017
1 parent 2d1bd87 commit 33ae10b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
17 changes: 13 additions & 4 deletions tpl/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ func (ns *Namespace) HTMLUnescape(s interface{}) (string, error) {
return html.UnescapeString(ss), nil
}

var markdownTrimPrefix = []byte("<p>")
var markdownTrimSuffix = []byte("</p>\n")
var (
markdownTrimPrefix = []byte("<p>")
markdownTrimSuffix = []byte("</p>\n")
markdownParagraphIndicator = []byte("<p")
)

// Markdownify renders a given input from Markdown to HTML.
func (ns *Namespace) Markdownify(s interface{}) (template.HTML, error) {
Expand All @@ -97,8 +100,14 @@ func (ns *Namespace) Markdownify(s interface{}) (template.HTML, error) {
Config: ns.deps.ContentSpec.NewBlackfriday(),
},
)
m = bytes.TrimPrefix(m, markdownTrimPrefix)
m = bytes.TrimSuffix(m, markdownTrimSuffix)

// Strip if this is a short inline type of text.
first := bytes.Index(m, markdownParagraphIndicator)
last := bytes.LastIndex(m, markdownParagraphIndicator)
if first == last {
m = bytes.TrimPrefix(m, markdownTrimPrefix)
m = bytes.TrimSuffix(m, markdownTrimSuffix)
}

return template.HTML(m), nil
}
Expand Down
28 changes: 28 additions & 0 deletions tpl/transform/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,34 @@ func TestMarkdownify(t *testing.T) {
}
}

// Issue #3040
func TestMarkdownifyBlocksOfText(t *testing.T) {
t.Parallel()

assert := require.New(t)

ns := New(newDeps(viper.New()))

text := `
#First
This is some *bold* text.
## Second
This is some more text.
And then some.
`

result, err := ns.Markdownify(text)
assert.NoError(err)
assert.Equal(template.HTML(
"<p>#First</p>\n\n<p>This is some <em>bold</em> text.</p>\n\n<h2 id=\"second\">Second</h2>\n\n<p>This is some more text.</p>\n\n<p>And then some.</p>\n"),
result)

}

func TestPlainify(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 33ae10b

Please sign in to comment.