hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit 016dd4a69a765061bb3da8490d3cac6ec47a91eb
parent c6b599a06d66b8e3c92343d25bb8043eb4f291f1
Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date:   Mon, 23 Jul 2018 20:19:32 +0200

Add Page.FirstSection

It was added and then removed by accident some time ago. Let us add it again, as it is useful.

Diffstat:
Mhugolib/site_sections.go | 23+++++++++++++++++++++++
Mhugolib/site_sections_test.go | 2++
2 files changed, 25 insertions(+), 0 deletions(-)
diff --git a/hugolib/site_sections.go b/hugolib/site_sections.go
@@ -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.
diff --git a/hugolib/site_sections_test.go b/hugolib/site_sections_test.go
@@ -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)
@@ -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) {