shithub: hugo

Download patch

ref: 9698b0dab11f52d52145e85ff71311d2f103cb4e
parent: 32344fe3db862584e3f926d63bdf33b7fa7d22f7
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Sun May 24 09:03:32 EDT 2020

Fix RenderString vs render hooks

Fixes #7265

--- a/hugolib/content_render_hooks_test.go
+++ b/hugolib/content_render_hooks_test.go
@@ -364,9 +364,10 @@
 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 @@
 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
 `)
 
 }
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -604,6 +604,10 @@
 		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
--- a/hugolib/page__output.go
+++ b/hugolib/page__output.go
@@ -96,24 +96,28 @@
 		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
 
 }
 
--- a/hugolib/page__per_output.go
+++ b/hugolib/page__per_output.go
@@ -77,6 +77,7 @@
 		dependencyTracker: dependencyTracker,
 		p:                 p,
 		f:                 po.f,
+		renderHooks:       &renderHooks{},
 	}
 
 	initContent := func() (err error) {
@@ -227,6 +228,11 @@
 
 }
 
+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 @@
 	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 @@
 	}
 	p.initMain.Reset()
 	p.initPlain.Reset()
+	p.renderHooks = &renderHooks{}
 }
 
 func (p *pageContentOutput) Content() (interface{}, error) {
@@ -377,7 +384,7 @@
 		converter.RenderContext{
 			Src:         content,
 			RenderTOC:   renderTOC,
-			RenderHooks: cp.renderHooks,
+			RenderHooks: cp.renderHooks.hooks,
 		})
 
 	if err == nil {