shithub: hugo

Download patch

ref: 33ae10b6ade67cd9618970121d7de5fd2ce7d781
parent: 2d1bd876cdeaec61b92c5b4c905fd442d39f380a
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Wed Aug 9 05:54:21 EDT 2017

tpl/transform: Only strip p tag in markdownify if only one paragraph

Fixes #3040

--- a/tpl/transform/transform.go
+++ b/tpl/transform/transform.go
@@ -79,8 +79,11 @@
 	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 @@
 			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
 }
--- a/tpl/transform/transform_test.go
+++ b/tpl/transform/transform_test.go
@@ -168,6 +168,34 @@
 	}
 }
 
+// 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()