summaryrefslogtreecommitdiffstats
path: root/hugolib
diff options
context:
space:
mode:
authorSepts <github@septs.pw>2022-11-30 12:02:57 +0800
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-12-23 10:14:53 +0100
commit3a216186b2cfa479c250dabb64eff022a388fb40 (patch)
tree77ebcc25658f473b6c9b077593ab572974fae946 /hugolib
parent7874b96815abf1e10b2947d9b3804767108ec939 (diff)
resource/page: Add Page.Ancestors
Fixes #10567
Diffstat (limited to 'hugolib')
-rw-r--r--hugolib/page__tree.go9
-rw-r--r--hugolib/site_sections_test.go6
2 files changed, 15 insertions, 0 deletions
diff --git a/hugolib/page__tree.go b/hugolib/page__tree.go
index 828500e62..6acd649fd 100644
--- a/hugolib/page__tree.go
+++ b/hugolib/page__tree.go
@@ -178,6 +178,15 @@ func (pt pageTree) Parent() page.Page {
return b.p
}
+func (pt pageTree) Ancestors() (parents page.Pages) {
+ parent := pt.Parent()
+ for parent != nil {
+ parents = append(parents, parent)
+ parent = parent.Parent()
+ }
+ return
+}
+
func (pt pageTree) Sections() page.Pages {
if pt.p.bucket == nil {
return nil
diff --git a/hugolib/site_sections_test.go b/hugolib/site_sections_test.go
index 2a4c39533..ccc8c51cb 100644
--- a/hugolib/site_sections_test.go
+++ b/hugolib/site_sections_test.go
@@ -181,12 +181,14 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
c.Assert(err, qt.IsNil)
c.Assert(active, qt.Equals, true)
c.Assert(p.FirstSection(), qt.Equals, p)
+ c.Assert(len(p.Ancestors()), qt.Equals, 1)
}},
{"l1", func(c *qt.C, p page.Page) {
c.Assert(p.Title(), qt.Equals, "L1s")
c.Assert(len(p.Pages()), qt.Equals, 4) // 2 pages + 2 sections
c.Assert(p.Parent().IsHome(), qt.Equals, true)
c.Assert(len(p.Sections()), qt.Equals, 2)
+ c.Assert(len(p.Ancestors()), qt.Equals, 1)
}},
{"l1,l2", func(c *qt.C, p page.Page) {
c.Assert(p.Title(), qt.Equals, "T2_-1")
@@ -195,6 +197,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
c.Assert(p.Parent().Title(), qt.Equals, "L1s")
c.Assert(p.RelPermalink(), qt.Equals, "/l1/l2/")
c.Assert(len(p.Sections()), qt.Equals, 1)
+ c.Assert(len(p.Ancestors()), qt.Equals, 2)
for _, child := range p.Pages() {
if child.IsSection() {
@@ -237,6 +240,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
c.Assert(p.Pages()[0].File().Path(), qt.Equals, filepath.FromSlash("l1/l2_2/page_2_2_1.md"))
c.Assert(p.Parent().Title(), qt.Equals, "L1s")
c.Assert(len(p.Sections()), qt.Equals, 0)
+ c.Assert(len(p.Ancestors()), qt.Equals, 2)
}},
{"l1,l2,l3", func(c *qt.C, p page.Page) {
nilp, _ := p.GetPage("this/does/not/exist")
@@ -245,6 +249,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
c.Assert(len(p.Pages()), qt.Equals, 2)
c.Assert(p.Parent().Title(), qt.Equals, "T2_-1")
c.Assert(len(p.Sections()), qt.Equals, 0)
+ c.Assert(len(p.Ancestors()), qt.Equals, 3)
l1 := getPage(p, "/l1")
isDescendant, err := l1.IsDescendant(p)
@@ -307,6 +312,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
}
c.Assert(home, qt.Not(qt.IsNil))
+ c.Assert(len(home.Ancestors()), qt.Equals, 0)
c.Assert(len(home.Sections()), qt.Equals, 9)
c.Assert(s.Info.Sections(), deepEqualsPages, home.Sections())