summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2020-05-24 13:03:32 +0200
committerBjørn Erik Pedersen <[email protected]>2020-05-24 13:20:57 +0200
commit9698b0dab11f52d52145e85ff71311d2f103cb4e (patch)
tree29747eb4d36a3bf34f667c6182291dde2846b6b1
parent32344fe3db862584e3f926d63bdf33b7fa7d22f7 (diff)
downloadhugo-9698b0dab11f52d52145e85ff71311d2f103cb4e.tar.gz
hugo-9698b0dab11f52d52145e85ff71311d2f103cb4e.zip
Fix RenderString vs render hooks
Fixes #7265
-rw-r--r--hugolib/content_render_hooks_test.go4
-rw-r--r--hugolib/page.go4
-rw-r--r--hugolib/page__output.go32
-rw-r--r--hugolib/page__per_output.go13
4 files changed, 35 insertions, 18 deletions
diff --git a/hugolib/content_render_hooks_test.go b/hugolib/content_render_hooks_test.go
index ef7373ea7..f161a46a2 100644
--- a/hugolib/content_render_hooks_test.go
+++ b/hugolib/content_render_hooks_test.go
@@ -364,9 +364,10 @@ func TestRenderString(t *testing.T) {
RSTART:{{ "**Bold Markdown**" | $p.RenderString }}:REND
RSTART:{{ "**Bold Block Markdown**" | $p.RenderString $optBlock }}:REND
RSTART:{{ "/italic org mode/" | $p.RenderString $optOrg }}:REND
+RSTART:{{ "## Header2" | $p.RenderString }}:REND
-`)
+`, "_default/_markup/render-heading.html", "Hook Heading: {{ .Level }}")
b.WithContent("p1.md", `---
title: "p1"
@@ -380,6 +381,7 @@ title: "p1"
RSTART:<strong>Bold Markdown</strong>:REND
RSTART:<p><strong>Bold Block Markdown</strong></p>
RSTART:<em>italic org mode</em>:REND
+RSTART:Hook Heading: 2:REND
`)
}
diff --git a/hugolib/page.go b/hugolib/page.go
index dbcc31236..083e702ed 100644
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -604,6 +604,10 @@ func (p *pageState) RenderString(args ...interface{}) (template.HTML, error) {
return "", err
}
+ if err = p.pageOutput.initRenderHooks(); err != nil {
+ return "", err
+ }
+
conv := p.getContentConverter()
if opts.Markup != "" && opts.Markup != p.m.markup {
var err error
diff --git a/hugolib/page__output.go b/hugolib/page__output.go
index 7d5b78aae..1792e8d6a 100644
--- a/hugolib/page__output.go
+++ b/hugolib/page__output.go
@@ -96,24 +96,28 @@ func (o *pageOutput) initRenderHooks() error {
return nil
}
- ps := o.cp.p
+ var initErr error
- c := ps.getContentConverter()
- if c == nil || !c.Supports(converter.FeatureRenderHooks) {
- return nil
- }
+ o.cp.renderHooks.init.Do(func() {
+ ps := o.cp.p
- h, err := ps.createRenderHooks(o.f)
- if err != nil {
- return err
- }
- if h == nil {
- return nil
- }
+ c := ps.getContentConverter()
+ if c == nil || !c.Supports(converter.FeatureRenderHooks) {
+ return
+ }
+
+ h, err := ps.createRenderHooks(o.f)
+ if err != nil {
+ initErr = err
+ }
+ if h == nil {
+ return
+ }
- o.cp.renderHooks = h
+ o.cp.renderHooks.hooks = h
+ })
- return nil
+ return initErr
}
diff --git a/hugolib/page__per_output.go b/hugolib/page__per_output.go
index 77a01801d..9a2d0b5f9 100644
--- a/hugolib/page__per_output.go
+++ b/hugolib/page__per_output.go
@@ -77,6 +77,7 @@ func newPageContentOutput(p *pageState, po *pageOutput) (*pageContentOutput, err
dependencyTracker: dependencyTracker,
p: p,
f: po.f,
+ renderHooks: &renderHooks{},
}
initContent := func() (err error) {
@@ -227,6 +228,11 @@ func newPageContentOutput(p *pageState, po *pageOutput) (*pageContentOutput, err
}
+type renderHooks struct {
+ hooks *hooks.Renderers
+ init sync.Once
+}
+
// pageContentOutput represents the Page content for a given output format.
type pageContentOutput struct {
f output.Format
@@ -244,8 +250,8 @@ type pageContentOutput struct {
placeholdersEnabled bool
placeholdersEnabledInit sync.Once
- // May be nil.
- renderHooks *hooks.Renderers
+ renderHooks *renderHooks
+
// Set if there are more than one output format variant
renderHooksHaveVariants bool // TODO(bep) reimplement this in another way, consolidate with shortcodes
@@ -285,6 +291,7 @@ func (p *pageContentOutput) Reset() {
}
p.initMain.Reset()
p.initPlain.Reset()
+ p.renderHooks = &renderHooks{}
}
func (p *pageContentOutput) Content() (interface{}, error) {
@@ -377,7 +384,7 @@ func (cp *pageContentOutput) renderContentWithConverter(c converter.Converter, c
converter.RenderContext{
Src: content,
RenderTOC: renderTOC,
- RenderHooks: cp.renderHooks,
+ RenderHooks: cp.renderHooks.hooks,
})
if err == nil {