Skip to content

Commit dec8941

Browse files
committed
Fix .Parent when there are overlapping regular pages inbetween
Fixes gohugoio#12263
1 parent d4d49e0 commit dec8941

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

hugolib/page__tree.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,16 @@ func (pt pageTree) Parent() page.Page {
124124
return pt.p.s.home
125125
}
126126

127-
_, n := pt.p.s.pageMap.treePages.LongestPrefix(dir, true, nil)
128-
if n != nil {
129-
return n.(page.Page)
127+
for {
128+
_, n := pt.p.s.pageMap.treePages.LongestPrefix(dir, true, nil)
129+
if n == nil {
130+
return pt.p.s.home
131+
}
132+
if pt.p.m.bundled || n.isContentNodeBranch() {
133+
return n.(page.Page)
134+
}
135+
dir = paths.Dir(dir)
130136
}
131-
return nil
132137
}
133138

134139
func (pt pageTree) Ancestors() page.Pages {

hugolib/site_sections_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,26 @@ Kind: {{ .Kind }}|RelPermalink: {{ .RelPermalink }}|SectionsPath: {{ .SectionsPa
398398
b.AssertFileContent("public/a/b/c/mybundle/index.html", "Kind: page|RelPermalink: /a/b/c/mybundle/|SectionsPath: /a/b/c|SectionsEntries: [a b c]|Len: 3")
399399
b.AssertFileContent("public/index.html", "Kind: home|RelPermalink: /|SectionsPath: /|SectionsEntries: []|Len: 0")
400400
}
401+
402+
func TestParentWithPageOverlap(t *testing.T) {
403+
files := `
404+
-- hugo.toml --
405+
baseURL = "https://example.com/"
406+
-- content/docs/_index.md --
407+
-- content/docs/logs/_index.md --
408+
-- content/docs/logs/sdk.md --
409+
-- content/docs/logs/sdk_exporters/stdout.md --
410+
-- layouts/_default/list.html --
411+
{{ .RelPermalink }}|{{ with .Parent}}{{ .RelPermalink }}{{ end }}|
412+
-- layouts/_default/single.html --
413+
{{ .RelPermalink }}|{{ with .Parent}}{{ .RelPermalink }}{{ end }}|
414+
415+
`
416+
b := Test(t, files)
417+
418+
b.AssertFileContent("public/index.html", "/||")
419+
b.AssertFileContent("public/docs/index.html", "/docs/|/|")
420+
b.AssertFileContent("public/docs/logs/index.html", "/docs/logs/|/docs/|")
421+
b.AssertFileContent("public/docs/logs/sdk/index.html", "/docs/logs/sdk/|/docs/logs/|")
422+
b.AssertFileContent("public/docs/logs/sdk_exporters/stdout/index.html", "/docs/logs/sdk_exporters/stdout/|/docs/logs/|")
423+
}

0 commit comments

Comments
 (0)