diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-10-26 10:09:38 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-10-26 13:00:21 +0200 |
commit | 631d768be9dc2d5175eeacb8bf34ed8233e2201a (patch) | |
tree | 670dbabab18d4215f2495dd07fe012ed4e223438 /resources | |
parent | e5d2a8f6a3121701eb95712b5540989fafaa0c71 (diff) |
Revise the fix for shortcode vs output format nilpointer
We do lazy initialization and (potentially) reuse of an output format's rendered content. We do this evaluation when we
start a new rendering a new output format. There are, however, situation where these borders gets crossed (e.g.
accessing content from another output format). We have a check for this in place for most cases, but not the content
rendering of inner markdown blocks inside shortcodes. This patch applies that same logic to the newly introduced
RenderContent method (which is not available from the templates).
Fixes #10391
Diffstat (limited to 'resources')
-rw-r--r-- | resources/page/page.go | 6 | ||||
-rw-r--r-- | resources/page/page_lazy_contentprovider.go | 16 | ||||
-rw-r--r-- | resources/page/page_nop.go | 19 |
3 files changed, 39 insertions, 2 deletions
diff --git a/resources/page/page.go b/resources/page/page.go index 50459c465..f9b8fdcec 100644 --- a/resources/page/page.go +++ b/resources/page/page.go @@ -19,6 +19,7 @@ import ( "html/template" "github.com/gohugoio/hugo/identity" + "github.com/gohugoio/hugo/markup/converter" "github.com/bep/gitmap" "github.com/gohugoio/hugo/config" @@ -105,6 +106,11 @@ type ContentProvider interface { Len() int } +// ContentRenderer provides the content rendering methods for some content. +type ContentRenderer interface { + RenderContent(content []byte, renderTOC bool) (converter.Result, error) +} + // FileProvider provides the source file. type FileProvider interface { File() source.File diff --git a/resources/page/page_lazy_contentprovider.go b/resources/page/page_lazy_contentprovider.go index ba4f8f8ef..2e4ddc352 100644 --- a/resources/page/page_lazy_contentprovider.go +++ b/resources/page/page_lazy_contentprovider.go @@ -17,6 +17,7 @@ import ( "html/template" "github.com/gohugoio/hugo/lazy" + "github.com/gohugoio/hugo/markup/converter" ) // OutputFormatContentProvider represents the method set that is "outputFormat aware" and that we @@ -24,6 +25,14 @@ import ( // Note that this set is currently not complete, but should cover the most common use cases. // For the others, the implementation will be from the page.NoopPage. type OutputFormatContentProvider interface { + OutputFormatPageContentProvider + + // for internal use. + ContentRenderer +} + +// OutputFormatPageContentProvider holds the exported methods from Page that are "outputFormat aware". +type OutputFormatPageContentProvider interface { ContentProvider TableOfContentsProvider PageRenderProvider @@ -46,7 +55,7 @@ type LazyContentProvider struct { func NewLazyContentProvider(f func() (OutputFormatContentProvider, error)) *LazyContentProvider { lcp := LazyContentProvider{ init: lazy.New(), - cp: NopPage, + cp: NopCPageContentRenderer, } lcp.init.Add(func() (any, error) { cp, err := f() @@ -122,3 +131,8 @@ func (lcp *LazyContentProvider) TableOfContents() template.HTML { lcp.init.Do() return lcp.cp.TableOfContents() } + +func (lcp *LazyContentProvider) RenderContent(content []byte, renderTOC bool) (converter.Result, error) { + lcp.init.Do() + return lcp.cp.RenderContent(content, renderTOC) +} diff --git a/resources/page/page_nop.go b/resources/page/page_nop.go index cdc5fd8b1..df3227f0a 100644 --- a/resources/page/page_nop.go +++ b/resources/page/page_nop.go @@ -16,10 +16,12 @@ package page import ( + "bytes" "html/template" "time" "github.com/gohugoio/hugo/identity" + "github.com/gohugoio/hugo/markup/converter" "github.com/gohugoio/hugo/hugofs/files" "github.com/gohugoio/hugo/tpl" @@ -41,7 +43,15 @@ import ( ) var ( - NopPage Page = new(nopPage) + NopPage Page = new(nopPage) + NopContentRenderer ContentRenderer = new(nopContentRenderer) + NopCPageContentRenderer = struct { + OutputFormatPageContentProvider + ContentRenderer + }{ + NopPage, + NopContentRenderer, + } NilPage *nopPage ) @@ -513,3 +523,10 @@ func (p *nopPage) WordCount() int { func (p *nopPage) GetIdentity() identity.Identity { return identity.NewPathIdentity("content", "foo/bar.md") } + +type nopContentRenderer int + +func (r *nopContentRenderer) RenderContent(content []byte, renderTOC bool) (converter.Result, error) { + b := &bytes.Buffer{} + return b, nil +} |