Skip to content

Commit 650099b

Browse files
committed
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 <noreply@anthropic.com> Fixes #14104
1 parent 0efcb24 commit 650099b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

‎resources/page/permalinks.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ 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 (_index.md), return empty string to match the behavior of pageToPermalinkFilename
340+
if l.translationBaseName(p) == "_index" {
341+
return "", nil
342+
}
339343
return l.urlize(p.PathInfo().Unnormalized().BaseNameNoIdentifier()), nil
340344
}
341345

‎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)