diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-03-20 09:37:21 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2020-03-20 18:28:55 +0100 |
commit | 99958f90fedec11d749a1397300860aa8e8459c2 (patch) | |
tree | 902f114f6ec14694311fddb416375794d33c3a92 /hugolib/content_map_page.go | |
parent | 1d91d8e14b13bd135dc4d4a901fc936c9649b219 (diff) |
Allow headless bundles to list pages via $page.Pages and $page.RegularPages
Fixes #7075
Diffstat (limited to 'hugolib/content_map_page.go')
-rw-r--r-- | hugolib/content_map_page.go | 54 |
1 files changed, 33 insertions, 21 deletions
diff --git a/hugolib/content_map_page.go b/hugolib/content_map_page.go index 4ba97f511..bcab9ffa9 100644 --- a/hugolib/content_map_page.go +++ b/hugolib/content_map_page.go @@ -606,36 +606,47 @@ func (m *pageMap) attachPageToViews(s string, b *contentNode) { } } -func (m *pageMap) collectPages(prefix string, fn func(c *contentNode)) error { - m.pages.WalkPrefixListable(prefix, func(s string, n *contentNode) bool { +type pageMapQuery struct { + Prefix string + Filter contentTreeNodeCallback +} + +func (m *pageMap) collectPages(query pageMapQuery, fn func(c *contentNode)) error { + if query.Filter == nil { + query.Filter = contentTreeNoListFilter + } + + m.pages.WalkQuery(query, func(s string, n *contentNode) bool { fn(n) return false }) + return nil } -func (m *pageMap) collectPagesAndSections(prefix string, fn func(c *contentNode)) error { - if err := m.collectSections(prefix, fn); err != nil { +func (m *pageMap) collectPagesAndSections(query pageMapQuery, fn func(c *contentNode)) error { + if err := m.collectSections(query, fn); err != nil { return err } - if err := m.collectPages(prefix+cmBranchSeparator, fn); err != nil { + query.Prefix = query.Prefix + cmBranchSeparator + if err := m.collectPages(query, fn); err != nil { return err } return nil } -func (m *pageMap) collectSections(prefix string, fn func(c *contentNode)) error { +func (m *pageMap) collectSections(query pageMapQuery, fn func(c *contentNode)) error { var level int - isHome := prefix == "/" + isHome := query.Prefix == "/" if !isHome { - level = strings.Count(prefix, "/") + level = strings.Count(query.Prefix, "/") } - return m.collectSectionsFn(prefix, func(s string, c *contentNode) bool { - if s == prefix { + return m.collectSectionsFn(query, func(s string, c *contentNode) bool { + if s == query.Prefix { return false } @@ -649,27 +660,28 @@ func (m *pageMap) collectSections(prefix string, fn func(c *contentNode)) error }) } -func (m *pageMap) collectSectionsFn(prefix string, fn func(s string, c *contentNode) bool) error { - if !strings.HasSuffix(prefix, "/") { - prefix += "/" +func (m *pageMap) collectSectionsFn(query pageMapQuery, fn func(s string, c *contentNode) bool) error { + + if !strings.HasSuffix(query.Prefix, "/") { + query.Prefix += "/" } - m.sections.WalkPrefixListable(prefix, func(s string, n *contentNode) bool { + m.sections.WalkQuery(query, func(s string, n *contentNode) bool { return fn(s, n) }) return nil } -func (m *pageMap) collectSectionsRecursiveIncludingSelf(prefix string, fn func(c *contentNode)) error { - return m.collectSectionsFn(prefix, func(s string, c *contentNode) bool { +func (m *pageMap) collectSectionsRecursiveIncludingSelf(query pageMapQuery, fn func(c *contentNode)) error { + return m.collectSectionsFn(query, func(s string, c *contentNode) bool { fn(c) return false }) } func (m *pageMap) collectTaxonomies(prefix string, fn func(c *contentNode)) error { - m.taxonomies.WalkPrefixListable(prefix, func(s string, n *contentNode) bool { + m.taxonomies.WalkQuery(pageMapQuery{Prefix: prefix}, func(s string, n *contentNode) bool { fn(n) return false }) @@ -797,21 +809,21 @@ type pagesMapBucket struct { func (b *pagesMapBucket) getPages() page.Pages { b.pagesInit.Do(func() { - b.pages = b.owner.treeRef.collectPages() + b.pages = b.owner.treeRef.getPages() page.SortByDefault(b.pages) }) return b.pages } func (b *pagesMapBucket) getPagesRecursive() page.Pages { - pages := b.owner.treeRef.collectPagesRecursive() + pages := b.owner.treeRef.getPagesRecursive() page.SortByDefault(pages) return pages } func (b *pagesMapBucket) getPagesAndSections() page.Pages { b.pagesAndSectionsInit.Do(func() { - b.pagesAndSections = b.owner.treeRef.collectPagesAndSections() + b.pagesAndSections = b.owner.treeRef.getPagesAndSections() }) return b.pagesAndSections } @@ -821,7 +833,7 @@ func (b *pagesMapBucket) getSections() page.Pages { if b.owner.treeRef == nil { return } - b.sections = b.owner.treeRef.collectSections() + b.sections = b.owner.treeRef.getSections() }) return b.sections |