shithub: hugo

Download patch

ref: 42e150fbfac736bd49bc7e50cb8cdf9f81386f59
parent: 028b356787426dbc190ce9868fbc9a6400c2996e
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Fri Jul 3 14:02:32 EDT 2020

Fix server reload when non-HTML shortcode changes

Fixes #7448

--- a/hugolib/hugo_sites.go
+++ b/hugolib/hugo_sites.go
@@ -981,14 +981,17 @@
 		}
 
 		for _, s := range p.shortcodeState.shortcodes {
-			for id := range idset {
-				if idm, ok := s.info.(identity.Manager); ok && idm.Search(id) != nil {
-					for _, po := range p.pageOutputs {
-						if po.cp != nil {
-							po.cp.Reset()
+			for _, templ := range s.templs {
+				sid := templ.(identity.Manager)
+				for id := range idset {
+					if sid.Search(id) != nil {
+						for _, po := range p.pageOutputs {
+							if po.cp != nil {
+								po.cp.Reset()
+							}
 						}
+						return false
 					}
-					return false
 				}
 			}
 		}
--- a/hugolib/hugo_sites_rebuild_test.go
+++ b/hugolib/hugo_sites_rebuild_test.go
@@ -26,6 +26,7 @@
 title = "Rebuild this"
 contentDir = "content"
 enableInlineShortcodes = true
+timeout = "5s"
 
 
 `
@@ -213,6 +214,46 @@
 
 		b.AssertFileContent("public/index.html", `
 Render /prender/: Baseof:Single Main: Page 1|Mypartial1: Mypartial1|Mypartial3: Mypartial3 Edited:END
+`)
+
+	})
+
+	t.Run("Edit RSS shortcode", func(t *testing.T) {
+		b := createSiteBuilder(t)
+
+		b.WithContent("output.md", `---
+title: Output
+outputs: ["HTML", "AMP"]
+layout: output
+---
+
+Content for Output.
+
+{{< output >}}
+
+`)
+
+		b.WithTemplates(
+			"layouts/_default/output.html", `Output HTML: {{ .RelPermalink }}|{{ .Content }}`,
+			"layouts/_default/output.amp.html", `Output AMP: {{ .RelPermalink }}|{{ .Content }}`,
+			"layouts/shortcodes/output.html", `Output Shortcode HTML`,
+			"layouts/shortcodes/output.amp.html", `Output Shortcode AMP`)
+
+		b.Build(BuildCfg{})
+
+		b.AssertFileContent("public/output/index.html", `
+Output Shortcode HTML
+`)
+		b.AssertFileContent("public/amp/output/index.html", `
+Output Shortcode AMP
+`)
+
+		b.EditFiles("layouts/shortcodes/output.amp.html", `Output Shortcode AMP Edited`)
+
+		b.Build(BuildCfg{})
+
+		b.AssertFileContent("public/amp/output/index.html", `
+Output Shortcode AMP Edited
 `)
 
 	})
--- a/hugolib/shortcode.go
+++ b/hugolib/shortcode.go
@@ -176,7 +176,8 @@
 	ordinal   int
 	err       error
 
-	info tpl.Info
+	info   tpl.Info       // One of the output formats (arbitrary)
+	templs []tpl.Template // All output formats
 
 	// If set, the rendered shortcode is sent as part of the surrounding content
 	// to Blackfriday and similar.
@@ -541,15 +542,14 @@
 
 			sc.name = currItem.ValStr()
 
-			// Check if the template expects inner content.
-			// We pick the first template for an arbitrary output format
-			// if more than one. It is "all inner or no inner".
-			tmpl, found, _ := s.s.Tmpl().LookupVariant(sc.name, tpl.TemplateVariants{})
-			if !found {
+			// Used to check if the template expects inner content.
+			templs := s.s.Tmpl().LookupVariants(sc.name)
+			if templs == nil {
 				return nil, _errors.Errorf("template for shortcode %q not found", sc.name)
 			}
 
-			sc.info = tmpl.(tpl.Info)
+			sc.info = templs[0].(tpl.Info)
+			sc.templs = templs
 		case currItem.IsInlineShortcodeName():
 			sc.name = currItem.ValStr()
 			sc.isInline = true
--- a/tpl/collections/apply_test.go
+++ b/tpl/collections/apply_test.go
@@ -40,6 +40,10 @@
 	return nil, false, false
 }
 
+func (templateFinder) LookupVariants(name string) []tpl.Template {
+	return nil
+}
+
 func (templateFinder) LookupLayout(d output.LayoutDescriptor, f output.Format) (tpl.Template, bool, error) {
 	return nil, false, nil
 }
--- a/tpl/template.go
+++ b/tpl/template.go
@@ -68,6 +68,7 @@
 	// We are currently only interested in output formats, so we should improve
 	// this for speed.
 	LookupVariant(name string, variants TemplateVariants) (Template, bool, bool)
+	LookupVariants(name string) []Template
 }
 
 // Template is the common interface between text/template and html/template.
--- a/tpl/tplimpl/template.go
+++ b/tpl/tplimpl/template.go
@@ -354,6 +354,23 @@
 
 }
 
+// LookupVariants returns all variants of name, nil if none found.
+func (t *templateHandler) LookupVariants(name string) []tpl.Template {
+	name = templateBaseName(templateShortcode, name)
+	s, found := t.shortcodes[name]
+	if !found {
+		return nil
+	}
+
+	variants := make([]tpl.Template, len(s.variants))
+	for i := 0; i < len(variants); i++ {
+		variants[i] = s.variants[i].ts
+	}
+
+	return variants
+
+}
+
 func (t *templateHandler) HasTemplate(name string) bool {
 
 	if _, found := t.baseof[name]; found {
@@ -963,6 +980,10 @@
 }
 
 func (t *textTemplateWrapperWithLock) LookupVariant(name string, variants tpl.TemplateVariants) (tpl.Template, bool, bool) {
+	panic("not supported")
+}
+
+func (t *textTemplateWrapperWithLock) LookupVariants(name string) []tpl.Template {
 	panic("not supported")
 }