ref: 016dd4a69a765061bb3da8490d3cac6ec47a91eb
parent: c6b599a06d66b8e3c92343d25bb8043eb4f291f1
author: Bjørn Erik Pedersen <[email protected]>
date: Mon Jul 23 16:19:32 EDT 2018
Add Page.FirstSection It was added and then removed by accident some time ago. Let us add it again, as it is useful.
--- a/hugolib/site_sections.go
+++ b/hugolib/site_sections.go
@@ -58,6 +58,29 @@
return v.parent
}
+// FirstSection returns the section on level 1 below home, e.g. "/docs".
+// For the home page, this will return itself.
+func (p *Page) FirstSection() *Page {
+ v := p
+ if v.origOnCopy != nil {
+ v = v.origOnCopy
+ }
+
+ if v.parent == nil || v.parent.IsHome() {
+ return v
+ }
+
+ parent := v.parent
+ for {
+ current := parent
+ parent = parent.parent
+ if parent == nil || parent.IsHome() {
+ return current
+ }
+ }
+
+}
+
// InSection returns whether the given page is in the current section.
// Note that this will always return false for pages that are
// not either regular, home or section pages.
--- a/hugolib/site_sections_test.go
+++ b/hugolib/site_sections_test.go
@@ -176,6 +176,7 @@
active, err := home.InSection(home)
assert.NoError(err)
assert.True(active)
+ assert.Equal(p, p.FirstSection())
}},
{"l1", func(p *Page) {
assert.Equal("L1s", p.title)
@@ -249,6 +250,7 @@
isAncestor, err = p.IsAncestor(l1)
assert.NoError(err)
assert.False(isAncestor)
+ assert.Equal(l1, p.FirstSection())
}},
{"perm a,link", func(p *Page) {