Skip to content

Commit

Permalink
Allow page.TableOfContents on self in shortcode
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Mar 5, 2023
1 parent f56ce01 commit a588f58
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
12 changes: 8 additions & 4 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"bytes"
"context"
"fmt"
"html/template"
"path"
"path/filepath"
"sort"
Expand Down Expand Up @@ -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,
}
)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
}
Expand Down
15 changes: 6 additions & 9 deletions hugolib/page__output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -84,7 +83,6 @@ type pageOutput struct {
contentRenderer page.ContentRenderer
pagePerOutputProviders
page.ContentProvider
page.TableOfContentsProvider
page.PageRenderProvider

// May be nil.
Expand All @@ -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

Expand Down
5 changes: 0 additions & 5 deletions resources/page/page_lazy_contentprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions tpl/page/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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>")

}

0 comments on commit a588f58

Please sign in to comment.