summaryrefslogtreecommitdiffstats
path: root/hugolib/page.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2023-02-11 16:20:24 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2023-02-21 17:56:41 +0100
commit90da7664bf1f3a0ca2e18144b5deacf532c6e3cf (patch)
tree78d8ac72ebb2ccee4ca4bbeeb9add3365c743e90 /hugolib/page.go
parent0afec0a9f4aace1f5f4af6822aeda6223ee3e3a9 (diff)
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/page.go')
-rw-r--r--hugolib/page.go48
1 files changed, 44 insertions, 4 deletions
diff --git a/hugolib/page.go b/hugolib/page.go
index 97f1ed351..40972d7c5 100644
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -15,6 +15,7 @@ package hugolib
import (
"bytes"
+ "context"
"fmt"
"path"
"path/filepath"
@@ -24,8 +25,10 @@ import (
"go.uber.org/atomic"
"github.com/gohugoio/hugo/identity"
+ "github.com/gohugoio/hugo/related"
"github.com/gohugoio/hugo/markup/converter"
+ "github.com/gohugoio/hugo/markup/tableofcontents"
"github.com/gohugoio/hugo/tpl"
@@ -148,6 +151,43 @@ func (p *pageState) GetIdentity() identity.Identity {
return identity.NewPathIdentity(files.ComponentFolderContent, filepath.FromSlash(p.Pathc()))
}
+func (p *pageState) Fragments(ctx context.Context) *tableofcontents.Fragments {
+ p.s.initInit(ctx, p.cp.initToC, p)
+ if p.pageOutput.cp.tableOfContents == nil {
+ return tableofcontents.Empty
+ }
+ return p.pageOutput.cp.tableOfContents
+}
+
+func (p *pageState) HeadingsFiltered(context.Context) tableofcontents.Headings {
+ return nil
+}
+
+type pageHeadingsFiltered struct {
+ *pageState
+ headings tableofcontents.Headings
+}
+
+func (p *pageHeadingsFiltered) HeadingsFiltered(context.Context) tableofcontents.Headings {
+ return p.headings
+}
+
+func (p *pageHeadingsFiltered) page() page.Page {
+ return p.pageState
+}
+
+// For internal use by the related content feature.
+func (p *pageState) ApplyFilterToHeadings(ctx context.Context, fn func(*tableofcontents.Heading) bool) related.Document {
+ if p.pageOutput.cp.tableOfContents == nil {
+ return p
+ }
+ headings := p.pageOutput.cp.tableOfContents.Headings.FilterBy(fn)
+ return &pageHeadingsFiltered{
+ pageState: p,
+ headings: headings,
+ }
+}
+
func (p *pageState) GitInfo() source.GitInfo {
return p.gitInfo
}
@@ -351,7 +391,7 @@ func (p *pageState) String() string {
// IsTranslated returns whether this content file is translated to
// other language(s).
func (p *pageState) IsTranslated() bool {
- p.s.h.init.translations.Do()
+ p.s.h.init.translations.Do(context.Background())
return len(p.translations) > 0
}
@@ -375,13 +415,13 @@ func (p *pageState) TranslationKey() string {
// AllTranslations returns all translations, including the current Page.
func (p *pageState) AllTranslations() page.Pages {
- p.s.h.init.translations.Do()
+ p.s.h.init.translations.Do(context.Background())
return p.allTranslations
}
// Translations returns the translations excluding the current Page.
func (p *pageState) Translations() page.Pages {
- p.s.h.init.translations.Do()
+ p.s.h.init.translations.Do(context.Background())
return p.translations
}
@@ -461,7 +501,7 @@ func (p *pageState) initOutputFormat(isRenderingSite bool, idx int) error {
// Must be run after the site section tree etc. is built and ready.
func (p *pageState) initPage() error {
- if _, err := p.init.Do(); err != nil {
+ if _, err := p.init.Do(context.Background()); err != nil {
return err
}
return nil