mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-28 06:30:33 +03:00
Add Page.FirstSection
It was added and then removed by accident some time ago. Let us add it again, as it is useful.
This commit is contained in:
parent
c6b599a06d
commit
016dd4a69a
2 changed files with 25 additions and 0 deletions
|
@ -58,6 +58,29 @@ func (p *Page) CurrentSection() *Page {
|
|||
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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue