summaryrefslogtreecommitdiffstats
path: root/resources/page/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 /resources/page/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 'resources/page/page.go')
-rw-r--r--resources/page/page.go41
1 files changed, 28 insertions, 13 deletions
diff --git a/resources/page/page.go b/resources/page/page.go
index eeb2cdb28..00e716e83 100644
--- a/resources/page/page.go
+++ b/resources/page/page.go
@@ -16,10 +16,12 @@
package page
import (
+ "context"
"html/template"
"github.com/gohugoio/hugo/identity"
"github.com/gohugoio/hugo/markup/converter"
+ "github.com/gohugoio/hugo/markup/tableofcontents"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/tpl"
@@ -76,40 +78,40 @@ type ChildCareProvider interface {
// ContentProvider provides the content related values for a Page.
type ContentProvider interface {
- Content() (any, error)
+ Content(context.Context) (any, error)
// Plain returns the Page Content stripped of HTML markup.
- Plain() string
+ Plain(context.Context) string
// PlainWords returns a string slice from splitting Plain using https://pkg.go.dev/strings#Fields.
- PlainWords() []string
+ PlainWords(context.Context) []string
// Summary returns a generated summary of the content.
// The breakpoint can be set manually by inserting a summary separator in the source file.
- Summary() template.HTML
+ Summary(context.Context) template.HTML
// Truncated returns whether the Summary is truncated or not.
- Truncated() bool
+ Truncated(context.Context) bool
// FuzzyWordCount returns the approximate number of words in the content.
- FuzzyWordCount() int
+ FuzzyWordCount(context.Context) int
// WordCount returns the number of words in the content.
- WordCount() int
+ WordCount(context.Context) int
// ReadingTime returns the reading time based on the length of plain text.
- ReadingTime() int
+ ReadingTime(context.Context) int
// Len returns the length of the content.
// This is for internal use only.
- Len() int
+ Len(context.Context) int
}
// ContentRenderer provides the content rendering methods for some content.
type ContentRenderer interface {
// RenderContent renders the given content.
// For internal use only.
- RenderContent(content []byte, renderTOC bool) (converter.Result, error)
+ RenderContent(ctx context.Context, content []byte, renderTOC bool) (converter.Result, error)
}
// FileProvider provides the source file.
@@ -167,6 +169,11 @@ type Page interface {
PageWithoutContent
}
+type PageFragment interface {
+ resource.ResourceLinksProvider
+ resource.ResourceMetaProvider
+}
+
// PageMetaProvider provides page metadata, typically provided via front matter.
type PageMetaProvider interface {
// The 4 page dates
@@ -252,7 +259,7 @@ type PageMetaProvider interface {
// PageRenderProvider provides a way for a Page to render content.
type PageRenderProvider interface {
// Render renders the given layout with this Page as context.
- Render(layout ...string) (template.HTML, error)
+ Render(ctx context.Context, layout ...string) (template.HTML, error)
// RenderString renders the first value in args with tPaginatorhe content renderer defined
// for this Page.
// It takes an optional map as a second argument:
@@ -260,7 +267,7 @@ type PageRenderProvider interface {
// display (“inline”):
// - inline or block. If inline (default), surrounding <p></p> on short snippets will be trimmed.
// markup (defaults to the Page’s markup)
- RenderString(args ...any) (template.HTML, error)
+ RenderString(ctx context.Context, args ...any) (template.HTML, error)
}
// PageWithoutContent is the Page without any of the content methods.
@@ -323,6 +330,14 @@ type PageWithoutContent interface {
// Used in change/dependency tracking.
identity.Provider
+ // Fragments returns the fragments for this page.
+ Fragments(context.Context) *tableofcontents.Fragments
+
+ // Headings returns the headings for this page when a filter is set.
+ // This is currently only triggered with the Related content feature
+ // and the "fragments" type of index.
+ HeadingsFiltered(context.Context) tableofcontents.Headings
+
DeprecatedWarningPageMethods
}
@@ -387,7 +402,7 @@ type SitesProvider interface {
// TableOfContentsProvider provides the table of contents for a Page.
type TableOfContentsProvider interface {
// TableOfContents returns the table of contents for the page rendered as HTML.
- TableOfContents() template.HTML
+ TableOfContents(context.Context) template.HTML
}
// TranslationsProvider provides access to any translations.