Skip to content

Commit 25c7c18

Browse files
dvdksnbep
authored andcommitted
resources/page: Fix slugorcontentbasename for section pages
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 <[email protected]> Fixes #14104
1 parent 0e8f88f commit 25c7c18

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

resources/page/permalinks.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ func (l PermalinkExpander) pageToPermalinkSectionSlugs(p Page, attr string) (str
336336

337337
// pageToPermalinkContentBaseName returns the URL-safe form of the content base name.
338338
func (l PermalinkExpander) pageToPermalinkContentBaseName(p Page, _ string) (string, error) {
339+
// For section pages with _index.md files, return empty string to match the behavior of pageToPermalinkFilename.
340+
// Sections without files should use their directory name.
341+
if p.PathInfo().IsBranchBundle() && p.File() != nil {
342+
return "", nil
343+
}
339344
return l.urlize(p.PathInfo().Unnormalized().BaseNameNoIdentifier()), nil
340345
}
341346

resources/page/permalinks_integration_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,39 @@ title: aBc
457457
b = hugolib.Test(t, files)
458458
b.AssertFileExists("public/aBc/index.html", true)
459459
}
460+
461+
// Issue 14104
462+
func TestIssue14104(t *testing.T) {
463+
t.Parallel()
464+
465+
files := `
466+
-- hugo.toml --
467+
[permalinks.page]
468+
foo = "/:sections[1:]/:slugorcontentbasename/"
469+
[permalinks.section]
470+
foo = "/:sections[1:]/:slugorcontentbasename/"
471+
-- content/foo/_index.md --
472+
---
473+
title: Foo
474+
---
475+
-- content/foo/bar/_index.md --
476+
---
477+
title: Bar
478+
---
479+
-- content/foo/bar/somepage.md --
480+
---
481+
title: Some Page
482+
---
483+
-- layouts/_default/list.html --
484+
List|{{ .Kind }}|{{ .RelPermalink }}|
485+
-- layouts/_default/single.html --
486+
Single|{{ .Kind }}|{{ .RelPermalink }}|
487+
`
488+
489+
b := hugolib.Test(t, files)
490+
491+
// Section page should be at /bar/index.html, not /bar/bar/index.html
492+
b.AssertFileContent("public/bar/index.html", "List|section|/bar/|")
493+
// Regular page should be at /bar/somepage/index.html
494+
b.AssertFileContent("public/bar/somepage/index.html", "Single|page|/bar/somepage/|")
495+
}

0 commit comments

Comments
 (0)