aboutsummaryrefslogtreecommitdiffhomepage
path: root/hugolib/site.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2023-02-11 16:20:24 +0100
committerBjørn Erik Pedersen <[email protected]>2023-02-21 17:56:41 +0100
commit90da7664bf1f3a0ca2e18144b5deacf532c6e3cf (patch)
tree78d8ac72ebb2ccee4ca4bbeeb9add3365c743e90 /hugolib/site.go
parent0afec0a9f4aace1f5f4af6822aeda6223ee3e3a9 (diff)
downloadhugo-90da7664bf1f3a0ca2e18144b5deacf532c6e3cf.tar.gz
hugo-90da7664bf1f3a0ca2e18144b5deacf532c6e3cf.zip
Add page fragments support to Related
The main topic of this commit is that you can now index fragments (content heading identifiers) when calling `.Related`. You can do this by: * Configure one or more indices with type `fragments` * The name of those index configurations maps to an (optional) front matter slice with fragment references. This allows you to link page<->fragment and page<->page. * This also will index all the fragments (heading identifiers) of the pages. It's also possible to use type `fragments` indices in shortcode, e.g.: ``` {{ $related := site.RegularPages.Related .Page }} ``` But, and this is important, you need to include the shortcode using the `{{<` delimiter. Not doing so will create infinite loops and timeouts. This commit also: * Adds two new methods to Page: Fragments (can also be used to build ToC) and HeadingsFiltered (this is only used in Related Content with index type `fragments` and `enableFilter` set to true. * Consolidates all `.Related*` methods into one, which takes either a `Page` or an options map as its only argument. * Add `context.Context` to all of the content related Page API. Turns out it wasn't strictly needed for this particular feature, but it will soon become usefil, e.g. in #9339. Closes #10711 Updates #9339 Updates #10725
Diffstat (limited to 'hugolib/site.go')
-rw-r--r--hugolib/site.go22
1 files changed, 13 insertions, 9 deletions
diff --git a/hugolib/site.go b/hugolib/site.go
index 0ca7a81b4..e90fa41ff 100644
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -14,6 +14,7 @@
package hugolib
import (
+ "context"
"fmt"
"html/template"
"io"
@@ -173,7 +174,7 @@ type Site struct {
}
func (s *Site) Taxonomies() page.TaxonomyList {
- s.init.taxonomies.Do()
+ s.init.taxonomies.Do(context.Background())
return s.taxonomies
}
@@ -214,8 +215,9 @@ func (init *siteInit) Reset() {
init.taxonomies.Reset()
}
-func (s *Site) initInit(init *lazy.Init, pctx pageContext) bool {
- _, err := init.Do()
+func (s *Site) initInit(ctx context.Context, init *lazy.Init, pctx pageContext) bool {
+ _, err := init.Do(ctx)
+
if err != nil {
s.h.FatalError(pctx.wrapError(err))
}
@@ -227,7 +229,7 @@ func (s *Site) prepareInits() {
var init lazy.Init
- s.init.prevNext = init.Branch(func() (any, error) {
+ s.init.prevNext = init.Branch(func(context.Context) (any, error) {
regularPages := s.RegularPages()
for i, p := range regularPages {
np, ok := p.(nextPrevProvider)
@@ -254,7 +256,7 @@ func (s *Site) prepareInits() {
return nil, nil
})
- s.init.prevNextInSection = init.Branch(func() (any, error) {
+ s.init.prevNextInSection = init.Branch(func(context.Context) (any, error) {
var sections page.Pages
s.home.treeRef.m.collectSectionsRecursiveIncludingSelf(pageMapQuery{Prefix: s.home.treeRef.key}, func(n *contentNode) {
sections = append(sections, n.p)
@@ -311,12 +313,12 @@ func (s *Site) prepareInits() {
return nil, nil
})
- s.init.menus = init.Branch(func() (any, error) {
+ s.init.menus = init.Branch(func(context.Context) (any, error) {
s.assembleMenus()
return nil, nil
})
- s.init.taxonomies = init.Branch(func() (any, error) {
+ s.init.taxonomies = init.Branch(func(context.Context) (any, error) {
err := s.pageMap.assembleTaxonomies()
return nil, err
})
@@ -327,7 +329,7 @@ type siteRenderingContext struct {
}
func (s *Site) Menus() navigation.Menus {
- s.init.menus.Do()
+ s.init.menus.Do(context.Background())
return s.menus
}
@@ -1821,7 +1823,9 @@ func (s *Site) renderForTemplate(name, outputFormat string, d any, w io.Writer,
return nil
}
- if err = s.Tmpl().Execute(templ, w, d); err != nil {
+ ctx := context.Background()
+
+ if err = s.Tmpl().ExecuteWithContext(ctx, templ, w, d); err != nil {
return fmt.Errorf("render of %q failed: %w", name, err)
}
return