tpl/transform: Only strip p tag in markdownify if only one paragraph
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 9 Aug 2017 07:54:21 +0000 (09:54 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 10 Aug 2017 17:52:41 +0000 (19:52 +0200)
Fixes #3040

tpl/transform/transform.go
tpl/transform/transform_test.go

index b41b41b9cd5475d112dad96316b32626e1b2a4c0..8d404f5a7a566551271e6fae59888159ac66ec31 100644 (file)
@@ -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
 }
index b50d7530c1552bf09fd37f4d5044481bfb5b3238..5fb80c23662e3de94230d95c51816a5aa1ec015c 100644 (file)
@@ -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()