shithub: hugo

Download patch

ref: e442cf30a215e33b49ce588a9098147282bd883f
parent: 7e223b3baaef68d6e6f99e28f162362c81deffba
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Thu Nov 26 03:32:49 EST 2020

Fix server rebuild issue with partials referenced from render hooks

Fixes #7990

--- a/hugolib/content_render_hooks_test.go
+++ b/hugolib/content_render_hooks_test.go
@@ -20,6 +20,43 @@
 	qt "github.com/frankban/quicktest"
 )
 
+func TestRenderHookEditNestedPartial(t *testing.T) {
+	config := `
+baseURL="https://example.org"
+workingDir="/mywork"
+`
+	b := newTestSitesBuilder(t).WithWorkingDir("/mywork").WithConfigFile("toml", config).Running()
+
+	b.WithTemplates("_default/single.html", "{{ .Content }}")
+	b.WithTemplates("partials/mypartial1.html", `PARTIAL1 {{ partial "mypartial2.html" }}`)
+	b.WithTemplates("partials/mypartial2.html", `PARTIAL2`)
+	b.WithTemplates("_default/_markup/render-link.html", `Link {{ .Text | safeHTML }}|{{ partial "mypartial1.html" . }}END`)
+
+	b.WithContent("p1.md", `---
+title: P1
+---
+
+[First Link](https://www.google.com "Google's Homepage")
+
+`)
+	b.Build(BuildCfg{})
+
+	b.AssertFileContent("public/p1/index.html", `Link First Link|PARTIAL1 PARTIAL2END`)
+
+	b.EditFiles("layouts/partials/mypartial1.html", `PARTIAL1_EDITED {{ partial "mypartial2.html" }}`)
+
+	b.Build(BuildCfg{})
+
+	b.AssertFileContent("public/p1/index.html", `Link First Link|PARTIAL1_EDITED PARTIAL2END`)
+
+	b.EditFiles("layouts/partials/mypartial2.html", `PARTIAL2_EDITED`)
+
+	b.Build(BuildCfg{})
+
+	b.AssertFileContent("public/p1/index.html", `Link First Link|PARTIAL1_EDITED PARTIAL2_EDITEDEND`)
+
+}
+
 func TestRenderHooks(t *testing.T) {
 	config := `
 baseURL="https://example.org"
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -409,7 +409,7 @@
 	if templFound {
 		renderers.LinkRenderer = hookRenderer{
 			templateHandler: p.s.Tmpl(),
-			Provider:        templ.(tpl.Info),
+			SearchProvider:  templ.(identity.SearchProvider),
 			templ:           templ,
 		}
 	}
@@ -422,7 +422,7 @@
 	if templFound {
 		renderers.ImageRenderer = hookRenderer{
 			templateHandler: p.s.Tmpl(),
-			Provider:        templ.(tpl.Info),
+			SearchProvider:  templ.(identity.SearchProvider),
 			templ:           templ,
 		}
 	}
@@ -435,7 +435,7 @@
 	if templFound {
 		renderers.HeadingRenderer = hookRenderer{
 			templateHandler: p.s.Tmpl(),
-			Provider:        templ.(tpl.Info),
+			SearchProvider:  templ.(identity.SearchProvider),
 			templ:           templ,
 		}
 	}
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -1738,7 +1738,7 @@
 // where ITEM is the thing being hooked.
 type hookRenderer struct {
 	templateHandler tpl.TemplateHandler
-	identity.Provider
+	identity.SearchProvider
 	templ tpl.Template
 }
 
--- a/identity/identity.go
+++ b/identity/identity.go
@@ -64,11 +64,16 @@
 
 // Manager manages identities, and is itself a Provider of Identity.
 type Manager interface {
-	IdentitiesProvider
-	Provider
+	SearchProvider
 	Add(ids ...Provider)
-	Search(id Identity) Provider
 	Reset()
+}
+
+// SearchProvider provides access to the chained set of identities.
+type SearchProvider interface {
+	Provider
+	IdentitiesProvider
+	Search(id Identity) Provider
 }
 
 // A PathIdentity is a common identity identified by a type and a path, e.g. "layouts" and "_default/single.html".
--- a/markup/goldmark/convert.go
+++ b/markup/goldmark/convert.go
@@ -202,7 +202,7 @@
 type renderContextData interface {
 	RenderContext() converter.RenderContext
 	DocumentContext() converter.DocumentContext
-	AddIdentity(id identity.Identity)
+	AddIdentity(id identity.Provider)
 }
 
 type renderContextDataHolder struct {
@@ -219,7 +219,7 @@
 	return ctx.dctx
 }
 
-func (ctx *renderContextDataHolder) AddIdentity(id identity.Identity) {
+func (ctx *renderContextDataHolder) AddIdentity(id identity.Provider) {
 	ctx.ids.Add(id)
 }
 
--- a/markup/goldmark/render_hooks.go
+++ b/markup/goldmark/render_hooks.go
@@ -186,7 +186,7 @@
 		},
 	)
 
-	ctx.AddIdentity(h.ImageRenderer.GetIdentity())
+	ctx.AddIdentity(h.ImageRenderer)
 
 	return ast.WalkContinue, err
 
@@ -248,7 +248,10 @@
 		},
 	)
 
-	ctx.AddIdentity(h.LinkRenderer.GetIdentity())
+	// TODO(bep) I have a working branch that fixes these rather confusing identity types,
+	// but for now it's important that it's not .GetIdentity() that's added here,
+	// to make sure we search the entire chain on changes.
+	ctx.AddIdentity(h.LinkRenderer)
 
 	return ast.WalkContinue, err
 }
@@ -308,7 +311,7 @@
 		},
 	)
 
-	ctx.AddIdentity(h.HeadingRenderer.GetIdentity())
+	ctx.AddIdentity(h.HeadingRenderer)
 
 	return ast.WalkContinue, err
 }