Fix RenderString for pages without content
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 18 Feb 2020 13:00:58 +0000 (14:00 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 18 Feb 2020 13:08:59 +0000 (14:08 +0100)
Fixes #6882

hugolib/content_render_hooks_test.go
hugolib/page.go
hugolib/page__meta.go
markup/converter/converter.go

index 1db232d05be6442d4e04c744f389c3be7c79c042..fb9b6b83c7b60d7c8115cf79a4ef2f2b5d8b6ce5 100644 (file)
@@ -372,3 +372,30 @@ RSTART:<em>italic org mode</em>:REND
 `)
 
 }
+
+// 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>`)
+       }
+
+}
index 4ffcb626427c88c6bfdba5ee8cc8c61e39b0aa48..fa6c84d87ddf4975a9c4ec2caaf97d7c9197a0aa 100644 (file)
@@ -631,6 +631,20 @@ func (p *pageState) wrapError(err error) error {
 }
 
 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
 }
 
index 8ecffbda37c464a335adb62dadfc87941aa0f968..7eb7cbfe156e14f003efdeea5accaa70f859083b 100644 (file)
@@ -19,6 +19,7 @@ import (
        "path/filepath"
        "regexp"
        "strings"
+       "sync"
        "time"
 
        "github.com/gohugoio/hugo/markup/converter"
@@ -118,7 +119,9 @@ type pageMeta struct {
 
        s *Site
 
-       contentConverter converter.Converter
+       renderingConfigOverrides map[string]interface{}
+       contentConverterInit     sync.Once
+       contentConverter         converter.Converter
 }
 
 func (p *pageMeta) Aliases() []string {
@@ -686,17 +689,8 @@ func (p *pageMeta) applyDefaultValues(n *contentNode) error {
                        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 @@ func (p *pageMeta) newContentConverter(ps *pageState, markup string, renderingCo
        }
        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 @@ func (p *pageMeta) newContentConverter(ps *pageState, markup string, renderingCo
        )
 
        if err != nil {
-               return nil, err
+               return converter.NopConverter, err
        }
 
        return cpp, nil
index b8a5c92c13bba69bcb45df995362b1e8f5497f3b..3537758269f7dfcffb4dba9d5cbb8e004784370a 100644 (file)
@@ -14,6 +14,8 @@
 package converter
 
 import (
+       "bytes"
+
        "github.com/gohugoio/hugo/common/loggers"
        "github.com/gohugoio/hugo/config"
        "github.com/gohugoio/hugo/identity"
@@ -65,6 +67,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
 // another format, e.g. Markdown to HTML.
 type Converter interface {