Fix RenderString vs render hooks

Fixes #7265
This commit is contained in:
Bjørn Erik Pedersen 2020-05-24 13:03:32 +02:00
parent 32344fe3db
commit 9698b0dab1
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
4 changed files with 35 additions and 18 deletions

View file

@ -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
`)
}

View file

@ -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

View file

@ -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
}
o.cp.renderHooks = h
h, err := ps.createRenderHooks(o.f)
if err != nil {
initErr = err
}
if h == nil {
return
}
return nil
o.cp.renderHooks.hooks = h
})
return initErr
}

View file

@ -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 {