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"
"context"
"fmt"
"html/template"
"path"
"path/filepath"
"sort"
@ -62,9 +63,8 @@ var (
var (
pageTypesProvider = resource.NewResourceTypesProvider(media.OctetType, pageResourceType)
nopPageOutput = &pageOutput{
pagePerOutputProviders: nopPagePerOutput,
ContentProvider: page.NopPage,
TableOfContentsProvider: page.NopPage,
pagePerOutputProviders: nopPagePerOutput,
ContentProvider: page.NopPage,
}
)
@ -159,6 +159,11 @@ func (p *pageState) Fragments(ctx context.Context) *tableofcontents.Fragments {
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 {
return nil
}
@ -951,7 +956,6 @@ func (p *pageState) shiftToOutputFormat(isRenderingSite bool, idx int) error {
})
p.pageOutput.contentRenderer = lcp
p.pageOutput.ContentProvider = lcp
p.pageOutput.TableOfContentsProvider = lcp
p.pageOutput.PageRenderProvider = lcp
}
}

View file

@ -54,13 +54,12 @@ func newPageOutput(
}
po := &pageOutput{
f: f,
pagePerOutputProviders: providers,
ContentProvider: page.NopPage,
TableOfContentsProvider: page.NopPage,
PageRenderProvider: page.NopPage,
render: render,
paginator: pag,
f: f,
pagePerOutputProviders: providers,
ContentProvider: page.NopPage,
PageRenderProvider: page.NopPage,
render: render,
paginator: pag,
}
return po
@ -84,7 +83,6 @@ type pageOutput struct {
contentRenderer page.ContentRenderer
pagePerOutputProviders
page.ContentProvider
page.TableOfContentsProvider
page.PageRenderProvider
// May be nil.
@ -97,7 +95,6 @@ func (p *pageOutput) initContentProvider(cp *pageContentOutput) {
}
p.contentRenderer = cp
p.ContentProvider = cp
p.TableOfContentsProvider = cp
p.PageRenderProvider = 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...)
}
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) {
lcp.init.Do(ctx)
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>")
}