]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
resources/page: Fix truncated summary logic
authorJoe Mooring <joe.mooring@veriphor.com>
Fri, 5 Sep 2025 19:21:17 +0000 (12:21 -0700)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 7 Sep 2025 09:07:41 +0000 (11:07 +0200)
Fixes #13967
Fixes #13968

hugolib/page__content.go
resources/page/page_markup.go
resources/page/page_markup_integration_test.go

index 4b78a1ed003b12cf0eb792fa1cb13561170d95f0..d2a68c24bab31a28ea1510a0d5119880439e8450 100644 (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
                }
index 44980e8b063778db48a870eb5f8771a4bba17457..8f58fc2abbae836c89954d8fc5a0c44861626673 100644 (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 {
index 42509921586c824ab3f3ef58ce1163aaf7ec0e97..b8e010e21bf36193179035caa8219badbe7b5abe 100644 (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)
+       }
+}