Skip to content

Commit

Permalink
Add Page.RenderShortcodes
Browse files Browse the repository at this point in the history
A layouts/shortcodes/include.html shortcode may look like this:

```html
{{ $p := site.GetPage (.Get 0) }}
{{ $p.RenderShortcodes }}
```

Fixes gohugoio#7297
  • Loading branch information
bep committed Aug 1, 2023
1 parent 239f2e2 commit e154d6f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions hugolib/page__output.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type pageOutput struct {
page.ContentProvider
page.PageRenderProvider
page.TableOfContentsProvider
page.RenderShortcodesProvider

// May be nil.
cp *pageContentOutput
Expand All @@ -99,6 +100,7 @@ func (p *pageOutput) initContentProvider(cp *pageContentOutput) {
p.ContentProvider = cp
p.PageRenderProvider = cp
p.TableOfContentsProvider = cp
p.RenderShortcodesProvider = cp
p.cp = cp

}
Expand Down
30 changes: 30 additions & 0 deletions hugolib/page__per_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,36 @@ func (p *pageContentOutput) Fragments(ctx context.Context) *tableofcontents.Frag
return p.tableOfContents
}

func (p *pageContentOutput) RenderShortcodes(ctx context.Context) (template.HTML, error) {
source := p.p.source.parsed.Input()
renderedShortcodes := p.contentPlaceholders
c := make([]byte, 0, len(source)+(len(source)/10))
for _, it := range p.p.cmap.items {
switch v := it.(type) {
case pageparser.Item:
c = append(c, source[v.Pos():v.Pos()+len(v.Val(source))]...)
case pageContentReplacement:
// Ignore.
case *shortcode:
// Insert the rendered shortcode.
renderedShortcode, found := renderedShortcodes[v.placeholder]
if !found {
// This should never happen.
panic(fmt.Sprintf("rendered shortcode %q not found", v.placeholder))
}
b, _, err := renderedShortcode.renderShortcode(ctx)
if err != nil {
return "", fmt.Errorf("failed to render shortcode: %w", err)
}
c = append(c, []byte(b)...)
default:
panic(fmt.Sprintf("unknown item type %T", it))
}
}

return helpers.BytesToHTML(c), nil
}

func (p *pageContentOutput) TableOfContents(ctx context.Context) template.HTML {
p.p.s.initInit(ctx, p.initToC, p.p)
return p.tableOfContentsHTML
Expand Down
37 changes: 37 additions & 0 deletions hugolib/shortcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1314,3 +1314,40 @@ Hello.
b.AssertFileContent("public/p1/index.html", "<span style=\"color:#a6e22e\">Hello.</span>")

}

func TestRenderShortcodes(t *testing.T) {
t.Parallel()

files := `
-- config.toml --
-- content/p1.md --
---
title: "p1"
---
# Title
{{% include "p2" %}}
-- content/p2.md --
---
title: "p2"
---
## Second
{{< foo >}}
-- layouts/shortcodes/include.html --
{{ $p := site.GetPage (.Get 0) }}
{{ $p.RenderShortcodes }}
-- layouts/shortcodes/foo.html --
Foo.
-- layouts/_default/single.html --
{{ .Content }}
`

b := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
},
).Build()

b.AssertFileContent("public/p1/index.html", "<h1 id=\"title\">Title</h1>\n<h2 id=\"second\">Second</h2>\n<p>Foo.</p>")

}
6 changes: 6 additions & 0 deletions resources/page/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ type PageRenderProvider interface {
// PageWithoutContent is the Page without any of the content methods.
type PageWithoutContent interface {
RawContentProvider
RenderShortcodesProvider
resource.Resource
PageMetaProvider
resource.LanguageProvider
Expand Down Expand Up @@ -362,6 +363,11 @@ type RawContentProvider interface {
RawContent() string
}

type RenderShortcodesProvider interface {
// RenderShortcodes returns RawContent with any shortcodes rendered.
RenderShortcodes(context.Context) (template.HTML, error)
}

// RefProvider provides the methods needed to create reflinks to pages.
type RefProvider interface {
// Ref returns an absolute URl to a page.
Expand Down
4 changes: 4 additions & 0 deletions resources/page/page_nop.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@ func (p *nopPage) RawContent() string {
return ""
}

func (p *nopPage) RenderShortcodes(ctx context.Context) (template.HTML, error) {
return "", nil
}

func (p *nopPage) ReadingTime(context.Context) int {
return 0
}
Expand Down

0 comments on commit e154d6f

Please sign in to comment.