diff options
author | Bjørn Erik Pedersen <[email protected]> | 2017-08-09 09:54:21 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2017-08-10 19:52:41 +0200 |
commit | 33ae10b6ade67cd9618970121d7de5fd2ce7d781 (patch) | |
tree | 1978480a5a166ea7d559de096df4982e8f657999 | |
parent | 2d1bd876cdeaec61b92c5b4c905fd442d39f380a (diff) | |
download | hugo-33ae10b6ade67cd9618970121d7de5fd2ce7d781.tar.gz hugo-33ae10b6ade67cd9618970121d7de5fd2ce7d781.zip |
tpl/transform: Only strip p tag in markdownify if only one paragraph
Fixes #3040
-rw-r--r-- | tpl/transform/transform.go | 17 | ||||
-rw-r--r-- | tpl/transform/transform_test.go | 28 |
2 files changed, 41 insertions, 4 deletions
diff --git a/tpl/transform/transform.go b/tpl/transform/transform.go index b41b41b9c..8d404f5a7 100644 --- a/tpl/transform/transform.go +++ b/tpl/transform/transform.go @@ -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) { @@ -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 } diff --git a/tpl/transform/transform_test.go b/tpl/transform/transform_test.go index b50d7530c..5fb80c236 100644 --- a/tpl/transform/transform_test.go +++ b/tpl/transform/transform_test.go @@ -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() |