mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-17 21:01:26 +02:00
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
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user