Fix output format handling for render hooks

Fixes #8176
This commit is contained in:
Bjørn Erik Pedersen
2021-03-09 10:26:44 +01:00
parent 35bfb66222
commit 18074d0c23
7 changed files with 102 additions and 24 deletions

View File

@@ -315,8 +315,10 @@ func TestRenderHooksRSS(t *testing.T) {
b.WithTemplates("index.html", `
{{ $p := site.GetPage "p1.md" }}
{{ $p2 := site.GetPage "p2.md" }}
P1: {{ $p.Content }}
P2: {{ $p2.Content }}
`, "index.xml", `
@@ -330,6 +332,8 @@ P3: {{ $p3.Content }}
`,
"_default/_markup/render-link.html", `html-link: {{ .Destination | safeURL }}|`,
"_default/_markup/render-link.rss.xml", `xml-link: {{ .Destination | safeURL }}|`,
"_default/_markup/render-heading.html", `html-heading: {{ .Text }}|`,
"_default/_markup/render-heading.rss.xml", `xml-heading: {{ .Text }}|`,
)
b.WithContent("p1.md", `---
@@ -337,12 +341,14 @@ title: "p1"
---
P1. [I'm an inline-style link](https://www.gohugo.io)
# Heading in p1
`, "p2.md", `---
title: "p2"
---
P1. [I'm an inline-style link](https://www.bep.is)
# Heading in p2
`,
"p3.md", `---
@@ -356,10 +362,15 @@ P3. [I'm an inline-style link](https://www.example.org)
b.Build(BuildCfg{})
b.AssertFileContent("public/index.html", "P1: <p>P1. html-link: https://www.gohugo.io|</p>")
b.AssertFileContent("public/index.html", `
P1: <p>P1. html-link: https://www.gohugo.io|</p>
html-heading: Heading in p1|
html-heading: Heading in p2|
`)
b.AssertFileContent("public/index.xml", `
P2: <p>P1. xml-link: https://www.bep.is|</p>
P3: <p>P3. xml-link: https://www.example.org|</p>
xml-heading: Heading in p2|
`)
}

View File

@@ -390,7 +390,7 @@ func (ps *pageState) initCommonProviders(pp pagePaths) error {
return nil
}
func (p *pageState) createRenderHooks(f output.Format) (*hooks.Renderers, error) {
func (p *pageState) createRenderHooks(f output.Format) (hooks.Renderers, error) {
layoutDescriptor := p.getLayoutDescriptor()
layoutDescriptor.RenderingHook = true
layoutDescriptor.LayoutOverride = false
@@ -401,7 +401,7 @@ func (p *pageState) createRenderHooks(f output.Format) (*hooks.Renderers, error)
layoutDescriptor.Kind = "render-link"
templ, templFound, err := p.s.Tmpl().LookupLayout(layoutDescriptor, f)
if err != nil {
return nil, err
return renderers, err
}
if templFound {
renderers.LinkRenderer = hookRenderer{
@@ -414,7 +414,7 @@ func (p *pageState) createRenderHooks(f output.Format) (*hooks.Renderers, error)
layoutDescriptor.Kind = "render-image"
templ, templFound, err = p.s.Tmpl().LookupLayout(layoutDescriptor, f)
if err != nil {
return nil, err
return renderers, err
}
if templFound {
renderers.ImageRenderer = hookRenderer{
@@ -427,7 +427,7 @@ func (p *pageState) createRenderHooks(f output.Format) (*hooks.Renderers, error)
layoutDescriptor.Kind = "render-heading"
templ, templFound, err = p.s.Tmpl().LookupLayout(layoutDescriptor, f)
if err != nil {
return nil, err
return renderers, err
}
if templFound {
renderers.HeadingRenderer = hookRenderer{
@@ -437,7 +437,7 @@ func (p *pageState) createRenderHooks(f output.Format) (*hooks.Renderers, error)
}
}
return &renderers, nil
return renderers, nil
}
func (p *pageState) getLayoutDescriptor() output.LayoutDescriptor {

View File

@@ -107,12 +107,39 @@ func (o *pageOutput) initRenderHooks() error {
h, err := ps.createRenderHooks(o.f)
if err != nil {
initErr = err
}
if h == nil {
return
}
o.cp.renderHooks.hooks = h
if !o.cp.renderHooksHaveVariants || h.IsZero() {
// Check if there is a different render hooks template
// for any of the other page output formats.
// If not, we can reuse this.
for _, po := range ps.pageOutputs {
if po.f.Name != o.f.Name {
h2, err := ps.createRenderHooks(po.f)
if err != nil {
initErr = err
return
}
if h2.IsZero() {
continue
}
if o.cp.renderHooks.hooks.IsZero() {
o.cp.renderHooks.hooks = h2
}
o.cp.renderHooksHaveVariants = !h2.Eq(o.cp.renderHooks.hooks)
if o.cp.renderHooksHaveVariants {
break
}
}
}
}
})
return initErr

View File

@@ -226,7 +226,7 @@ func newPageContentOutput(p *pageState, po *pageOutput) (*pageContentOutput, err
}
type renderHooks struct {
hooks *hooks.Renderers
hooks hooks.Renderers
init sync.Once
}