Skip to content

Commit

Permalink
Add Page.FirstSection
Browse files Browse the repository at this point in the history
It was added and then removed by accident some time ago. Let us add it again, as it is useful.
  • Loading branch information
bep committed Jul 23, 2018
1 parent c6b599a commit 016dd4a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
23 changes: 23 additions & 0 deletions hugolib/site_sections.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions hugolib/site_sections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
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)
Expand Down Expand Up @@ -249,6 +250,7 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
isAncestor, err = p.IsAncestor(l1)
assert.NoError(err)
assert.False(isAncestor)
assert.Equal(l1, p.FirstSection())

}},
{"perm a,link", func(p *Page) {
Expand Down

0 comments on commit 016dd4a

Please sign in to comment.