Fix crashes for 404 in IsAncestor etc.

Fixes #6931
This commit is contained in:
Bjørn Erik Pedersen 2020-02-22 10:57:43 +01:00
parent c1eb625124
commit a524124beb
4 changed files with 64 additions and 6 deletions

View File

@ -5,13 +5,13 @@
: The page's first section below root, e.g. `/docs`, `/blog` etc.
.InSection $anotherPage
: 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.
: Whether the given page is in the current section.
.IsAncestor $anotherPage
: Whether the current page is an ancestor of the given page. Note that this method is not relevant for taxonomy lists and taxonomy terms pages.
: Whether the current page is an ancestor of the given page.
.IsDescendant $anotherPage
: Whether the current page is a descendant of the given page. Note that this method is not relevant for taxonomy lists and taxonomy terms pages.
: Whether the current page is a descendant of the given page.
.Parent
: A section's parent section or a page's section.

View File

@ -21,15 +21,41 @@ func Test404(t *testing.T) {
t.Parallel()
b := newTestSitesBuilder(t)
b.WithSimpleConfigFile().WithTemplatesAdded("404.html", "<html><body>Not Found! Parent: {{ .Parent.Kind }}</body></html>")
b.WithSimpleConfigFile().WithTemplatesAdded(
"404.html",
`
{{ $home := site.Home }}
404:
Parent: {{ .Parent.Kind }}
IsAncestor: {{ .IsAncestor $home }}/{{ $home.IsAncestor . }}
IsDescendant: {{ .IsDescendant $home }}/{{ $home.IsDescendant . }}
CurrentSection: {{ .CurrentSection.Kind }}|
FirstSection: {{ .FirstSection.Kind }}|
InSection: {{ .InSection $home.Section }}|{{ $home.InSection . }}
Sections: {{ len .Sections }}|
Page: {{ .Page.RelPermalink }}|
Data: {{ len .Data }}|
`,
)
b.Build(BuildCfg{})
// Note: We currently have only 1 404 page. One might think that we should have
// multiple, to follow the Custom Output scheme, but I don't see how that would work
// right now.
b.AssertFileContent("public/404.html", `
Not Found
404:
Parent: home
IsAncestor: false/true
IsDescendant: true/false
CurrentSection: home|
FirstSection: home|
InSection: false|true
Sections: 0|
Page: /404.html|
Data: 1|
`)
}

View File

@ -806,6 +806,9 @@ func (b *pagesMapBucket) getPagesAndSections() page.Pages {
func (b *pagesMapBucket) getSections() page.Pages {
b.sectionsInit.Do(func() {
if b.owner.treeRef == nil {
return
}
b.sections = b.owner.treeRef.collectSections()
})

View File

@ -37,6 +37,15 @@ func (pt pageTree) IsAncestor(other interface{}) (bool, error) {
ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef()
if ref1 == nil || ref2 == nil {
if ref1 == nil {
// A 404 or other similar standalone page.
return false, nil
}
return ref1.n.p.IsHome(), nil
}
if !ref1.isSection() {
return false, nil
}
@ -66,6 +75,15 @@ func (pt pageTree) IsDescendant(other interface{}) (bool, error) {
ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef()
if ref1 == nil || ref2 == nil {
if ref2 == nil {
// A 404 or other similar standalone page.
return false, nil
}
return ref2.n.p.IsHome(), nil
}
if !ref2.isSection() {
return false, nil
}
@ -76,6 +94,9 @@ func (pt pageTree) IsDescendant(other interface{}) (bool, error) {
func (pt pageTree) FirstSection() page.Page {
ref := pt.p.getTreeRef()
if ref == nil {
return pt.p.s.home
}
key := ref.key
if !ref.isSection() {
key = path.Dir(key)
@ -99,6 +120,14 @@ func (pt pageTree) InSection(other interface{}) (bool, error) {
ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef()
if ref1 == nil || ref2 == nil {
if ref1 == nil {
// A 404 or other similar standalone page.
return false, nil
}
return ref1.n.p.IsHome(), nil
}
s1, _ := ref1.getCurrentSection()
s2, _ := ref2.getCurrentSection()
@ -123,7 +152,7 @@ func (pt pageTree) Parent() page.Page {
tree := p.getTreeRef()
if pt.p.Kind() == page.KindTaxonomyTerm || tree == nil {
if tree == nil || pt.p.Kind() == page.KindTaxonomyTerm {
return pt.p.s.home
}