Allow page.TableOfContents on self in shortcode

Fixes #10791
This commit is contained in:
Bjørn Erik Pedersen 2023-03-05 09:51:14 +01:00
parent f56ce01ae1
commit df5608f8a0
4 changed files with 46 additions and 18 deletions

View file

@ -17,6 +17,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"html/template"
"path" "path"
"path/filepath" "path/filepath"
"sort" "sort"
@ -64,7 +65,6 @@ var (
nopPageOutput = &pageOutput{ nopPageOutput = &pageOutput{
pagePerOutputProviders: nopPagePerOutput, pagePerOutputProviders: nopPagePerOutput,
ContentProvider: page.NopPage, ContentProvider: page.NopPage,
TableOfContentsProvider: page.NopPage,
} }
) )
@ -159,6 +159,11 @@ func (p *pageState) Fragments(ctx context.Context) *tableofcontents.Fragments {
return p.pageOutput.cp.tableOfContents return p.pageOutput.cp.tableOfContents
} }
func (p *pageState) TableOfContents(ctx context.Context) template.HTML {
p.s.initInit(ctx, p.cp.initToC, p)
return p.pageOutput.cp.tableOfContentsHTML
}
func (p *pageState) HeadingsFiltered(context.Context) tableofcontents.Headings { func (p *pageState) HeadingsFiltered(context.Context) tableofcontents.Headings {
return nil return nil
} }
@ -951,7 +956,6 @@ func (p *pageState) shiftToOutputFormat(isRenderingSite bool, idx int) error {
}) })
p.pageOutput.contentRenderer = lcp p.pageOutput.contentRenderer = lcp
p.pageOutput.ContentProvider = lcp p.pageOutput.ContentProvider = lcp
p.pageOutput.TableOfContentsProvider = lcp
p.pageOutput.PageRenderProvider = lcp p.pageOutput.PageRenderProvider = lcp
} }
} }

View file

@ -57,7 +57,6 @@ func newPageOutput(
f: f, f: f,
pagePerOutputProviders: providers, pagePerOutputProviders: providers,
ContentProvider: page.NopPage, ContentProvider: page.NopPage,
TableOfContentsProvider: page.NopPage,
PageRenderProvider: page.NopPage, PageRenderProvider: page.NopPage,
render: render, render: render,
paginator: pag, paginator: pag,
@ -84,7 +83,6 @@ type pageOutput struct {
contentRenderer page.ContentRenderer contentRenderer page.ContentRenderer
pagePerOutputProviders pagePerOutputProviders
page.ContentProvider page.ContentProvider
page.TableOfContentsProvider
page.PageRenderProvider page.PageRenderProvider
// May be nil. // May be nil.
@ -97,7 +95,6 @@ func (p *pageOutput) initContentProvider(cp *pageContentOutput) {
} }
p.contentRenderer = cp p.contentRenderer = cp
p.ContentProvider = cp p.ContentProvider = cp
p.TableOfContentsProvider = cp
p.PageRenderProvider = cp p.PageRenderProvider = cp
p.cp = cp p.cp = cp

View file

@ -128,11 +128,6 @@ func (lcp *LazyContentProvider) RenderString(ctx context.Context, args ...any) (
return lcp.cp.RenderString(ctx, args...) return lcp.cp.RenderString(ctx, args...)
} }
func (lcp *LazyContentProvider) TableOfContents(ctx context.Context) template.HTML {
lcp.init.Do(ctx)
return lcp.cp.TableOfContents(ctx)
}
func (lcp *LazyContentProvider) ParseAndRenderContent(ctx context.Context, content []byte, renderTOC bool) (converter.ResultRender, error) { func (lcp *LazyContentProvider) ParseAndRenderContent(ctx context.Context, content []byte, renderTOC bool) (converter.ResultRender, error) {
lcp.init.Do(ctx) lcp.init.Do(ctx)
return lcp.cp.ParseAndRenderContent(ctx, content, renderTOC) return lcp.cp.ParseAndRenderContent(ctx, content, renderTOC)

View file

@ -177,3 +177,35 @@ Shortcode in bundled page OK.
} }
} }
// Issue 10791.
func TestPageTableOfContentsInShortcode(t *testing.T) {
t.Parallel()
files := `
-- config.toml --
baseURL = 'http://example.com/'
disableKinds = ["taxonomy", "term"]
-- content/p1.md --
---
title: "P1"
---
{{< toc >}}
# Heading 1
-- layouts/shortcodes/toc.html --
{{ page.TableOfContents }}
-- layouts/_default/single.html --
{{ .Content }}
`
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
TxtarString: files,
},
).Build()
b.AssertFileContent("public/p1/index.html", "<nav id=\"TableOfContents\"></nav> \n<h1 id=\"heading-1\">Heading 1</h1>")
}