resources/page: Fix truncated summary logic

Fixes #13967
Fixes #13968
This commit is contained in:
Joe Mooring 2025-09-05 12:21:17 -07:00 committed by Bjørn Erik Pedersen
parent 3b8947d821
commit d8774d7fc3
3 changed files with 78 additions and 4 deletions

View File

@ -645,8 +645,9 @@ func (c *cachedContentScope) contentRendered(ctx context.Context) (contentSummar
}
html := cp.po.p.s.ContentSpec.TrimShortHTML(b.Bytes(), cp.po.p.m.pageConfig.Content.Markup)
rs.Value.summary = page.Summary{
Text: helpers.BytesToHTML(html),
Type: page.SummaryTypeFrontMatter,
Text: helpers.BytesToHTML(html),
Type: page.SummaryTypeFrontMatter,
Truncated: rs.Value.summary.Truncated,
}
rs.Value.contentWithoutSummary = rs.Value.content
}

View File

@ -104,7 +104,7 @@ func (s HtmlSummary) trimSpace(ss string) string {
func (s HtmlSummary) Content() string {
if s.Divider.IsZero() {
return s.source
return s.trimSpace(s.source)
}
ss := s.source[:s.Divider.Low]
ss += s.source[s.Divider.High:]
@ -139,7 +139,7 @@ func (s HtmlSummary) ContentWithoutSummary() string {
}
func (s HtmlSummary) Truncated() bool {
return s.SummaryLowHigh.High < len(s.source)
return s.Summary() != s.Content()
}
func (s *HtmlSummary) resolveParagraphTagAndSetWrapper(mt media.Type) tagReStartEnd {

View File

@ -14,6 +14,8 @@
package page_test
import (
"strconv"
"strings"
"testing"
"github.com/gohugoio/hugo/hugolib"
@ -347,3 +349,74 @@ Summary Truncated: {{ .Truncated }}|
"Summary: <div class=\"paragraph\">\n<p>This is summary.</p>\n</div>|\nSummary Type: manual|\nSummary Truncated: true|",
)
}
func TestIssue13967(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ['home','rss','section','sitemap','taxonomy','term']
-- layouts/all.html --
Title: {{ .Title }}|Summary: {{ .Summary }}|Truncated: {{ .Truncated }}|
-- content/p1.md --
---
title: p1
---
<!--more--> one two three
-- content/p2.md --
---
title: p2
---
one <!--more--> two three
-- content/p3.md --
---
title: p3
---
one two <!--more--> three
-- content/p4.md --
---
title: p4
---
one two three <!--more-->
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/p1/index.html", `Title: p1|Summary: |Truncated: true|`)
b.AssertFileContent("public/p2/index.html", `Title: p2|Summary: <p>one</p>|Truncated: true|`)
b.AssertFileContent("public/p3/index.html", `Title: p3|Summary: <p>one two</p>|Truncated: true|`)
b.AssertFileContent("public/p4/index.html", `Title: p4|Summary: <p>one two three</p>|Truncated: false|`)
}
func TestIssue13968(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
summaryLength = SUMMARY_LENGTH
-- layouts/all.html --
Title: {{ .Title }}|Summary: {{ .Summary }}|Truncated: {{ .Truncated }}|
-- content/_index.md --
---
title: home
---
one two three
`
tests := []struct {
summaryLength int
want string
}{
{0, "Title: home|Summary: |Truncated: true|"},
{1, "Title: home|Summary: <p>one two three</p>|Truncated: false|"},
{2, "Title: home|Summary: <p>one two three</p>|Truncated: false|"},
{3, "Title: home|Summary: <p>one two three</p>|Truncated: false|"},
{4, "Title: home|Summary: <p>one two three</p>|Truncated: false|"},
}
for _, tt := range tests {
f := strings.ReplaceAll(files, "SUMMARY_LENGTH", strconv.Itoa(tt.summaryLength))
b := hugolib.Test(t, f)
b.AssertFileContent("public/index.html", tt.want)
}
}