parser/pageparser: Fix when only shortcode and then summary
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 24 Nov 2018 16:06:26 +0000 (17:06 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 24 Nov 2018 17:23:23 +0000 (18:23 +0100)
Fixes #5464

hugolib/page_test.go
parser/pageparser/pagelexer.go
parser/pageparser/pagelexer_test.go
parser/pageparser/pageparser_intro_test.go

index b421a1b403aa30b779983a512599a99586e15c5b..f9c26754b0627c01f7983ec272a1e0633e87057d 100644 (file)
@@ -1508,6 +1508,15 @@ title: "Hugo"
 This is a {{< sc >}}.
 <!--more--> 
 Content.
+`)
+
+       // https://github.com/gohugoio/hugo/issues/5464
+       b.WithContent("page-md-only-shortcode.md", `---
+title: "Hugo"
+---
+{{< sc >}}
+<!--more--> 
+{{< sc >}}
 `)
 
        b.WithContent("page-md-shortcode-same-line.md", `---
@@ -1570,6 +1579,11 @@ CONTENT:{{ .Content }}
                "SUMMARY:<p>Summary.</p>:END",
                "CONTENT:<p>Summary.</p>\n\n<p>Content.\t</p>\n",
        )
+
+       b.AssertFileContent("public/page-md-only-shortcode/index.html",
+               "SUMMARY:a shortcode:END",
+               "CONTENT:a shortcode\n\na shortcode\n",
+       )
 }
 
 // TODO(bep) this may be useful for other tests.
index fcea560c4ec9c163deda74e3873d7c7f2e173574..8106758a96e24afbfe2652d3a732b3bd188544c6 100644 (file)
@@ -216,7 +216,7 @@ func lexMainSection(l *pageLexer) stateFunc {
        }
 
        l2 = l.index(leftDelimSc)
-       skip := minPositiveIndex(l1, l2)
+       skip := minIndex(l1, l2)
 
        if skip > 0 {
                l.pos += skip
@@ -730,12 +730,12 @@ func (l *pageLexer) currentRightShortcodeDelim() []byte {
 
 // helper functions
 
-// returns the min index > 0
-func minPositiveIndex(indices ...int) int {
+// returns the min index >= 0
+func minIndex(indices ...int) int {
        min := -1
 
        for _, j := range indices {
-               if j <= 0 {
+               if j < 0 {
                        continue
                }
                if min == -1 {
index 5c85df0176b9477c94b3226405c04af87dd6ba5a..70def30917264da84a2f0f07017886ae7ac4fd07 100644 (file)
@@ -19,11 +19,11 @@ import (
        "github.com/stretchr/testify/require"
 )
 
-func TestMinPositiveIndex(t *testing.T) {
+func TestMinIndex(t *testing.T) {
        assert := require.New(t)
-       assert.Equal(1, minPositiveIndex(4, 1, 2, 3))
-       assert.Equal(2, minPositiveIndex(4, 0, -2, 2, 5))
-       assert.Equal(-1, minPositiveIndex())
-       assert.Equal(-1, minPositiveIndex(-2, -3))
+       assert.Equal(1, minIndex(4, 1, 2, 3))
+       assert.Equal(0, minIndex(4, 0, -2, 2, 5))
+       assert.Equal(-1, minIndex())
+       assert.Equal(-1, minIndex(-2, -3))
 
 }
index f818848bd55054c7887c1f9e8290f85cae3b6326..ba48a3ee3f35c1f848bcd1a7da4e31c7350cd57d 100644 (file)
@@ -39,6 +39,7 @@ var (
        tstSomeText            = nti(tText, "\nSome text.\n")
        tstSummaryDivider      = nti(TypeLeadSummaryDivider, "<!--more-->\n")
        tstHtmlStart           = nti(TypeHTMLStart, "<")
+       tstNewline             = nti(tText, "\n")
 
        tstORG = `
 #+TITLE: T1
@@ -70,6 +71,8 @@ var frontMatterTests = []lexerTest{
        {"Summary divider same line", "+++\nfoo = \"bar\"\n+++\n\nSome text.<!--more-->Some text.\n", []Item{tstFrontMatterTOML, nti(tText, "\nSome text."), nti(TypeLeadSummaryDivider, "<!--more-->"), nti(tText, "Some text.\n"), tstEOF}},
        // https://github.com/gohugoio/hugo/issues/5402
        {"Summary and shortcode, no space", "+++\nfoo = \"bar\"\n+++\n\nSome text.\n<!--more-->{{< sc1 >}}\nSome text.\n", []Item{tstFrontMatterTOML, tstSomeText, nti(TypeLeadSummaryDivider, "<!--more-->"), tstLeftNoMD, tstSC1, tstRightNoMD, tstSomeText, tstEOF}},
+       // https://github.com/gohugoio/hugo/issues/5464
+       {"Summary and shortcode only", "+++\nfoo = \"bar\"\n+++\n{{< sc1 >}}\n<!--more-->\n{{< sc2 >}}", []Item{tstFrontMatterTOML, tstLeftNoMD, tstSC1, tstRightNoMD, tstNewline, tstSummaryDivider, tstLeftNoMD, tstSC2, tstRightNoMD, tstEOF}},
 }
 
 func TestFrontMatter(t *testing.T) {