shithub: hugo

Download patch

ref: 4a39564efe7b02a685598ae9dbae95e2326c0230
parent: b6e097cfe65ecd1d47c805969082e6805563612b
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Thu Mar 26 05:41:30 EDT 2020

Fix IsDescendant/IsAncestor for overlapping section names

Fixes #7096

--- a/hugolib/content_map_test.go
+++ b/hugolib/content_map_test.go
@@ -371,6 +371,8 @@
 	b.WithContent("blog/draftsection/sub/page.md", createPage(13))
 	b.WithContent("docs/page6.md", createPage(11))
 	b.WithContent("tags/_index.md", createPage(32))
+	b.WithContent("overlap/_index.md", createPage(33))
+	b.WithContent("overlap2/_index.md", createPage(34))
 
 	b.WithTemplatesAdded("layouts/index.html", `
 Num Regular: {{ len .Site.RegularPages }}
@@ -385,6 +387,9 @@
 {{ $page2 := .Site.GetPage "blog/page2" }}
 {{ $page4 := .Site.GetPage "blog/subsection/page4" }}
 {{ $bundle := .Site.GetPage "blog/bundle" }}
+{{ $overlap1 := .Site.GetPage "overlap" }}
+{{ $overlap2 := .Site.GetPage "overlap2" }}
+
 Home: {{ template "print-page" $home }}
 Blog Section: {{ template "print-page" $blog }}
 Blog Sub Section: {{ template "print-page" $blogSub }}
@@ -392,6 +397,10 @@
 Bundle: {{ template "print-page" $bundle }}
 IsDescendant: true: {{ $page.IsDescendant $blog }} true: {{ $blogSub.IsDescendant $blog }} true: {{ $blog.IsDescendant $home }} false: {{ $home.IsDescendant $blog }}
 IsAncestor: true: {{ $blog.IsAncestor $page }} true: {{ $home.IsAncestor $blog }} true: {{ $blog.IsAncestor $blogSub }} true: {{ $home.IsAncestor $page }} false: {{ $page.IsAncestor $blog }} false: {{ $blog.IsAncestor $home }}  false: {{ $blogSub.IsAncestor $blog }}
+IsDescendant overlap1: false: {{ $overlap1.IsDescendant $overlap2 }}
+IsDescendant overlap2: false: {{ $overlap2.IsDescendant $overlap1 }}
+IsAncestor overlap1: false: {{ $overlap1.IsAncestor $overlap2 }}
+IsAncestor overlap2: false: {{ $overlap2.IsAncestor $overlap1 }}
 FirstSection: {{ $blogSub.FirstSection.RelPermalink }} {{ $blog.FirstSection.RelPermalink }} {{ $home.FirstSection.RelPermalink }} {{ $page.FirstSection.RelPermalink }}
 InSection: true: {{ $page.InSection $blog }} false: {{ $page.InSection $blogSub }} 
 Next: {{ $page2.Next.RelPermalink }}
@@ -431,6 +440,10 @@
         Bundle: Page 12|/blog/bundle/|0001-01-01|Current Section: blog|Resources: json: /blog/bundle/data.json|page: |
         IsDescendant: true: true true: true true: true false: false
         IsAncestor: true: true true: true true: true true: true false: false false: false  false: false
+        IsDescendant overlap1: false: false
+        IsDescendant overlap2: false: false
+        IsAncestor overlap1: false: false
+        IsAncestor overlap2: false: false
         FirstSection: /blog/ /blog/ / /blog/
         InSection: true: true false: false 
         Next: /blog/page3/
@@ -437,8 +450,8 @@
         NextInSection: /blog/page3/
         Pages: /blog/page3/|/blog/subsection/|/blog/page2/|/blog/page1/|/blog/bundle/|
         Sections: /blog/|/docs/|
-        Categories: /categories/funny/; funny; 9|
-        Category Terms:  taxonomyTerm: /categories/funny/; funny; 9|
+        Categories: /categories/funny/; funny; 11|
+        Category Terms:  taxonomyTerm: /categories/funny/; funny; 11|
  		Category Funny:  taxonomy; funny: /blog/subsection/page4/;|/blog/page3/;|/blog/subsection/;|/blog/page2/;|/blog/page1/;|/blog/subsection/page5/;|/docs/page6/;|/blog/bundle/;|;|
  		Pag Num Pages: 7
         Pag Blog Num Pages: 4
--- a/hugolib/page__tree.go
+++ b/hugolib/page__tree.go
@@ -37,6 +37,10 @@
 
 	ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef()
 
+	if ref1 != nil && ref1.key == "/" {
+		return true, nil
+	}
+
 	if ref1 == nil || ref2 == nil {
 		if ref1 == nil {
 			// A 404 or other similar standalone page.
@@ -50,7 +54,12 @@
 		return false, nil
 	}
 
-	return strings.HasPrefix(ref2.key, ref1.key), nil
+	if ref2.isSection() {
+		return strings.HasPrefix(ref2.key, ref1.key+"/"), nil
+	}
+
+	return strings.HasPrefix(ref2.key, ref1.key+cmBranchSeparator), nil
+
 }
 
 func (pt pageTree) CurrentSection() page.Page {
@@ -75,6 +84,10 @@
 
 	ref1, ref2 := pt.p.getTreeRef(), tp.getTreeRef()
 
+	if ref2 != nil && ref2.key == "/" {
+		return true, nil
+	}
+
 	if ref1 == nil || ref2 == nil {
 		if ref2 == nil {
 			// A 404 or other similar standalone page.
@@ -88,7 +101,11 @@
 		return false, nil
 	}
 
-	return strings.HasPrefix(ref1.key, ref2.key), nil
+	if ref1.isSection() {
+		return strings.HasPrefix(ref1.key, ref2.key+"/"), nil
+	}
+
+	return strings.HasPrefix(ref1.key, ref2.key+cmBranchSeparator), nil
 
 }