Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-08-09 10:54:21 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2017-08-10 20:52:41 +0300
commit33ae10b6ade67cd9618970121d7de5fd2ce7d781 (patch)
tree1978480a5a166ea7d559de096df4982e8f657999 /tpl/transform
parent2d1bd876cdeaec61b92c5b4c905fd442d39f380a (diff)
tpl/transform: Only strip p tag in markdownify if only one paragraph
Fixes #3040
Diffstat (limited to 'tpl/transform')
-rw-r--r--tpl/transform/transform.go17
-rw-r--r--tpl/transform/transform_test.go28
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()