shithub: hugo

Download patch

ref: 19e12caf8c90516e3b803ae8a40b907bd89dc96c
parent: 20f2211fce55e1811629245f9e5e4a2ac754d788
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Tue Feb 18 09:00:58 EST 2020

Fix RenderString for pages without content

Fixes #6882

--- a/hugolib/content_render_hooks_test.go
+++ b/hugolib/content_render_hooks_test.go
@@ -372,3 +372,30 @@
 `)
 
 }
+
+// https://github.com/gohugoio/hugo/issues/6882
+func TestRenderStringOnListPage(t *testing.T) {
+	renderStringTempl := `
+{{ .RenderString "**Hello**" }}
+`
+	b := newTestSitesBuilder(t)
+	b.WithContent("mysection/p1.md", `FOO`)
+	b.WithTemplates(
+		"index.html", renderStringTempl,
+		"_default/list.html", renderStringTempl,
+		"_default/single.html", renderStringTempl,
+	)
+
+	b.Build(BuildCfg{})
+
+	for _, filename := range []string{
+		"index.html",
+		"mysection/index.html",
+		"categories/index.html",
+		"tags/index.html",
+		"mysection/p1/index.html",
+	} {
+		b.AssertFileContent("public/"+filename, `<strong>Hello</strong>`)
+	}
+
+}
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -631,6 +631,20 @@
 }
 
 func (p *pageState) getContentConverter() converter.Converter {
+	var err error
+	p.m.contentConverterInit.Do(func() {
+		markup := p.m.markup
+		if markup == "html" {
+			// Only used for shortcode inner content.
+			markup = "markdown"
+		}
+		p.m.contentConverter, err = p.m.newContentConverter(p, markup, p.m.renderingConfigOverrides)
+
+	})
+
+	if err != nil {
+		p.s.Log.ERROR.Println("Failed to create content converter:", err)
+	}
 	return p.m.contentConverter
 }
 
--- a/hugolib/page__meta.go
+++ b/hugolib/page__meta.go
@@ -19,6 +19,7 @@
 	"path/filepath"
 	"regexp"
 	"strings"
+	"sync"
 	"time"
 
 	"github.com/gohugoio/hugo/markup/converter"
@@ -118,7 +119,9 @@
 
 	s *Site
 
-	contentConverter converter.Converter
+	renderingConfigOverrides map[string]interface{}
+	contentConverterInit     sync.Once
+	contentConverter         converter.Converter
 }
 
 func (p *pageMeta) Aliases() []string {
@@ -686,17 +689,8 @@
 			renderingConfigOverrides = maps.ToStringMap(bfParam)
 		}
 
-		markup := p.markup
-		if markup == "html" {
-			// Only used for shortcode inner content.
-			markup = "markdown"
-		}
+		p.renderingConfigOverrides = renderingConfigOverrides
 
-		cp, err := p.newContentConverter(n.p, markup, renderingConfigOverrides)
-		if err != nil {
-			return err
-		}
-		p.contentConverter = cp
 	}
 
 	return nil
@@ -709,7 +703,7 @@
 	}
 	cp := p.s.ContentSpec.Converters.Get(markup)
 	if cp == nil {
-		return nil, errors.Errorf("no content renderer found for markup %q", p.markup)
+		return converter.NopConverter, errors.Errorf("no content renderer found for markup %q", p.markup)
 	}
 
 	cpp, err := cp.New(
@@ -722,7 +716,7 @@
 	)
 
 	if err != nil {
-		return nil, err
+		return converter.NopConverter, err
 	}
 
 	return cpp, nil
--- a/markup/converter/converter.go
+++ b/markup/converter/converter.go
@@ -14,6 +14,8 @@
 package converter
 
 import (
+	"bytes"
+
 	"github.com/gohugoio/hugo/common/loggers"
 	"github.com/gohugoio/hugo/config"
 	"github.com/gohugoio/hugo/identity"
@@ -63,6 +65,18 @@
 
 func (n newConverter) Name() string {
 	return n.name
+}
+
+var NopConverter = new(nopConverter)
+
+type nopConverter int
+
+func (nopConverter) Convert(ctx RenderContext) (Result, error) {
+	return &bytes.Buffer{}, nil
+}
+
+func (nopConverter) Supports(feature identity.Identity) bool {
+	return false
 }
 
 // Converter wraps the Convert method that converts some markup into