Skip to content

Commit d8774d7

Browse files
jmooringbep
authored andcommitted
resources/page: Fix truncated summary logic
Fixes #13967 Fixes #13968
1 parent 3b8947d commit d8774d7

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

hugolib/page__content.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,9 @@ func (c *cachedContentScope) contentRendered(ctx context.Context) (contentSummar
645645
}
646646
html := cp.po.p.s.ContentSpec.TrimShortHTML(b.Bytes(), cp.po.p.m.pageConfig.Content.Markup)
647647
rs.Value.summary = page.Summary{
648-
Text: helpers.BytesToHTML(html),
649-
Type: page.SummaryTypeFrontMatter,
648+
Text: helpers.BytesToHTML(html),
649+
Type: page.SummaryTypeFrontMatter,
650+
Truncated: rs.Value.summary.Truncated,
650651
}
651652
rs.Value.contentWithoutSummary = rs.Value.content
652653
}

resources/page/page_markup.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (s HtmlSummary) trimSpace(ss string) string {
104104

105105
func (s HtmlSummary) Content() string {
106106
if s.Divider.IsZero() {
107-
return s.source
107+
return s.trimSpace(s.source)
108108
}
109109
ss := s.source[:s.Divider.Low]
110110
ss += s.source[s.Divider.High:]
@@ -139,7 +139,7 @@ func (s HtmlSummary) ContentWithoutSummary() string {
139139
}
140140

141141
func (s HtmlSummary) Truncated() bool {
142-
return s.SummaryLowHigh.High < len(s.source)
142+
return s.Summary() != s.Content()
143143
}
144144

145145
func (s *HtmlSummary) resolveParagraphTagAndSetWrapper(mt media.Type) tagReStartEnd {

resources/page/page_markup_integration_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package page_test
1515

1616
import (
17+
"strconv"
18+
"strings"
1719
"testing"
1820

1921
"github.com/gohugoio/hugo/hugolib"
@@ -347,3 +349,74 @@ Summary Truncated: {{ .Truncated }}|
347349
"Summary: <div class=\"paragraph\">\n<p>This is summary.</p>\n</div>|\nSummary Type: manual|\nSummary Truncated: true|",
348350
)
349351
}
352+
353+
func TestIssue13967(t *testing.T) {
354+
t.Parallel()
355+
356+
files := `
357+
-- hugo.toml --
358+
disableKinds = ['home','rss','section','sitemap','taxonomy','term']
359+
-- layouts/all.html --
360+
Title: {{ .Title }}|Summary: {{ .Summary }}|Truncated: {{ .Truncated }}|
361+
-- content/p1.md --
362+
---
363+
title: p1
364+
---
365+
<!--more--> one two three
366+
-- content/p2.md --
367+
---
368+
title: p2
369+
---
370+
one <!--more--> two three
371+
-- content/p3.md --
372+
---
373+
title: p3
374+
---
375+
one two <!--more--> three
376+
-- content/p4.md --
377+
---
378+
title: p4
379+
---
380+
one two three <!--more-->
381+
`
382+
b := hugolib.Test(t, files)
383+
384+
b.AssertFileContent("public/p1/index.html", `Title: p1|Summary: |Truncated: true|`)
385+
b.AssertFileContent("public/p2/index.html", `Title: p2|Summary: <p>one</p>|Truncated: true|`)
386+
b.AssertFileContent("public/p3/index.html", `Title: p3|Summary: <p>one two</p>|Truncated: true|`)
387+
b.AssertFileContent("public/p4/index.html", `Title: p4|Summary: <p>one two three</p>|Truncated: false|`)
388+
}
389+
390+
func TestIssue13968(t *testing.T) {
391+
t.Parallel()
392+
393+
files := `
394+
-- hugo.toml --
395+
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
396+
summaryLength = SUMMARY_LENGTH
397+
-- layouts/all.html --
398+
Title: {{ .Title }}|Summary: {{ .Summary }}|Truncated: {{ .Truncated }}|
399+
-- content/_index.md --
400+
---
401+
title: home
402+
---
403+
one two three
404+
`
405+
406+
tests := []struct {
407+
summaryLength int
408+
want string
409+
}{
410+
{0, "Title: home|Summary: |Truncated: true|"},
411+
{1, "Title: home|Summary: <p>one two three</p>|Truncated: false|"},
412+
{2, "Title: home|Summary: <p>one two three</p>|Truncated: false|"},
413+
{3, "Title: home|Summary: <p>one two three</p>|Truncated: false|"},
414+
{4, "Title: home|Summary: <p>one two three</p>|Truncated: false|"},
415+
}
416+
417+
for _, tt := range tests {
418+
f := strings.ReplaceAll(files, "SUMMARY_LENGTH", strconv.Itoa(tt.summaryLength))
419+
b := hugolib.Test(t, f)
420+
b.AssertFileContent("public/index.html", tt.want)
421+
}
422+
}

0 commit comments

Comments
 (0)