]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
resources/page: Fix slugorcontentbasename for section pages
authorDavid Karlsson <35727626+dvdksn@users.noreply.github.com>
Mon, 10 Nov 2025 08:33:15 +0000 (09:33 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 10 Nov 2025 16:03:26 +0000 (17:03 +0100)
The slugorcontentbasename permalink token was creating an extra
subdirectory for section pages (_index.md files). This was because
pageToPermalinkContentBaseName did not have the same special handling
for _index.md files as pageToPermalinkFilename.

This commit adds the special handling to return an empty string for
section pages, ensuring backward compatibility with slugorfilename.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Fixes #14104

resources/page/permalinks.go
resources/page/permalinks_integration_test.go

index 1d6078b4ab4c3d132a691b7791f31b243427f46b..49dc815d11f939f4ec6f9ac5ff06a610510118ac 100644 (file)
@@ -336,6 +336,11 @@ func (l PermalinkExpander) pageToPermalinkSectionSlugs(p Page, attr string) (str
 
 // pageToPermalinkContentBaseName returns the URL-safe form of the content base name.
 func (l PermalinkExpander) pageToPermalinkContentBaseName(p Page, _ string) (string, error) {
+       // For section pages with _index.md files, return empty string to match the behavior of pageToPermalinkFilename.
+       // Sections without files should use their directory name.
+       if p.PathInfo().IsBranchBundle() && p.File() != nil {
+               return "", nil
+       }
        return l.urlize(p.PathInfo().Unnormalized().BaseNameNoIdentifier()), nil
 }
 
index 74df388208fb1c625838b7daa3088a36b52062e9..c1b179cd5d92d534430465ec3a03fa4fe1669952 100644 (file)
@@ -457,3 +457,39 @@ title: aBc
        b = hugolib.Test(t, files)
        b.AssertFileExists("public/aBc/index.html", true)
 }
+
+// Issue 14104
+func TestIssue14104(t *testing.T) {
+       t.Parallel()
+
+       files := `
+-- hugo.toml --
+[permalinks.page]
+foo = "/:sections[1:]/:slugorcontentbasename/"
+[permalinks.section]
+foo = "/:sections[1:]/:slugorcontentbasename/"
+-- content/foo/_index.md --
+---
+title: Foo
+---
+-- content/foo/bar/_index.md --
+---
+title: Bar
+---
+-- content/foo/bar/somepage.md --
+---
+title: Some Page
+---
+-- layouts/_default/list.html --
+List|{{ .Kind }}|{{ .RelPermalink }}|
+-- layouts/_default/single.html --
+Single|{{ .Kind }}|{{ .RelPermalink }}|
+`
+
+       b := hugolib.Test(t, files)
+
+       // Section page should be at /bar/index.html, not /bar/bar/index.html
+       b.AssertFileContent("public/bar/index.html", "List|section|/bar/|")
+       // Regular page should be at /bar/somepage/index.html
+       b.AssertFileContent("public/bar/somepage/index.html", "Single|page|/bar/somepage/|")
+}