]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Fix .Page.Pages with similary named sections
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 28 Feb 2024 10:39:10 +0000 (11:39 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Wed, 28 Feb 2024 15:43:19 +0000 (16:43 +0100)
Fixes #12169

hugolib/content_map_page.go
hugolib/pagecollections_test.go

index 89df951e93f161c249da53ce39962363f42e4f42..1114428df0e5fe2a70a93653dfbe21c3da1dc52d 100644 (file)
@@ -348,31 +348,28 @@ func (m *pageMap) getPagesInSection(q pageMapQueryPagesInSection) page.Pages {
                w := &doctree.NodeShiftTreeWalker[contentNodeI]{
                        Tree:   m.treePages,
                        Prefix: prefix,
-                       Handle: func(key string, n contentNodeI, match doctree.DimensionFlag) (bool, error) {
-                               if q.Recursive {
-                                       if p, ok := n.(*pageState); ok && include(p) {
-                                               pas = append(pas, p)
-                                       }
-                                       return false, nil
-                               }
-
-                               // We store both leafs and branches in the same tree, so for non-recursive walks,
-                               // we need to walk until the end, but can skip
-                               // any not belonging to child branches.
-                               if otherBranch != "" && strings.HasPrefix(key, otherBranch) {
-                                       return false, nil
-                               }
+               }
 
+               w.Handle = func(key string, n contentNodeI, match doctree.DimensionFlag) (bool, error) {
+                       if q.Recursive {
                                if p, ok := n.(*pageState); ok && include(p) {
                                        pas = append(pas, p)
                                }
+                               return false, nil
+                       }
 
-                               if n.isContentNodeBranch() {
-                                       otherBranch = key + "/"
-                               }
+                       if p, ok := n.(*pageState); ok && include(p) {
+                               pas = append(pas, p)
+                       }
 
-                               return false, nil
-                       },
+                       if n.isContentNodeBranch() {
+                               currentBranch := key + "/"
+                               if otherBranch == "" || otherBranch != currentBranch {
+                                       w.SkipPrefix(currentBranch)
+                               }
+                               otherBranch = currentBranch
+                       }
+                       return false, nil
                }
 
                err := w.Walk(context.Background())
index 1cb1cbd70e9849abbaf79239086ee5652a6c5419..be81c3a66378e00ad9618b8098a10c5864d19cf3 100644 (file)
@@ -663,3 +663,33 @@ RegularPagesRecursive: {{ range .RegularPagesRecursive }}{{ .Kind }}:{{ .RelPerm
 
        b.AssertFileContent("public/index.html", `RegularPagesRecursive: page:/p1/|page:/post/p2/||End.`)
 }
+
+// Issue #12169.
+func TestPagesSimilarSectionNames(t *testing.T) {
+       files := `
+-- hugo.toml --
+-- content/draftsection/_index.md --
+---
+draft: true
+---
+-- content/draftsection/sub/_index.md --got
+-- content/draftsection/sub/d1.md --
+-- content/s1/_index.md --
+-- content/s1/p1.md --
+-- content/s1-foo/_index.md --
+-- content/s1-foo/p2.md --
+-- content/s1-foo/s2/_index.md --
+-- content/s1-foo/s2/p3.md --
+-- content/s1-foo/s2-foo/_index.md --
+-- content/s1-foo/s2-foo/p4.md --
+-- layouts/_default/list.html --
+{{ .RelPermalink }}: Pages: {{ range .Pages }}{{ .RelPermalink }}|{{ end }}$
+
+`
+       b := Test(t, files)
+
+       b.AssertFileContent("public/index.html", "/: Pages: /s1-foo/|/s1/|$")
+       b.AssertFileContent("public/s1/index.html", "/s1/: Pages: /s1/p1/|$")
+       b.AssertFileContent("public/s1-foo/index.html", "/s1-foo/: Pages: /s1-foo/p2/|/s1-foo/s2-foo/|/s1-foo/s2/|$")
+       b.AssertFileContent("public/s1-foo/s2/index.html", "/s1-foo/s2/: Pages: /s1-foo/s2/p3/|$")
+}