Skip to content

Commit f0f49ed

Browse files
committed
hugolib: Add Page.Equals
1 parent 71ae9b4 commit f0f49ed

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

‎hugolib/site_sections.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ func (p *Page) IsAncestor(other interface{}) (bool, error) {
109109
return helpers.HasStringsPrefix(pp.sections, p.sections), nil
110110
}
111111

112+
// Equals returns whether the current page equals the given page.
113+
// Note that this is more accurate than doing `{{ if eq $page $otherPage }}`
114+
// since a Page can be embedded in another type.
115+
func (p *Page) Equals(other interface{}) (bool, error) {
116+
pp, err := unwrapPage(other)
117+
if err != nil {
118+
return false, err
119+
}
120+
121+
return p == pp, nil
122+
}
123+
112124
func unwrapPage(in interface{}) (*Page, error) {
113125
if po, ok := in.(*PageOutput); ok {
114126
in = po.Page

‎hugolib/site_sections_test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ PAG|{{ .Title }}|{{ $sect.InSection . }}
149149
d := p.s.getPage(KindSection, "empty2", "b", "c", "d")
150150
assert.NotNil(d)
151151
assert.Equal("T41_-1", d.Title)
152+
153+
equals, err := c.Equals(d)
154+
assert.NoError(err)
155+
assert.False(equals)
156+
equals, err = c.Equals(c)
157+
assert.NoError(err)
158+
assert.True(equals)
159+
_, err = c.Equals("asdf")
160+
assert.Error(err)
161+
152162
}},
153163
{"empty3", func(p *Page) {
154164
// b,c,d with regular page in b

0 commit comments

Comments
 (0)