Skip to content

Commit c1dc35d

Browse files
committedNov 29, 2024·
Fix server edits of resources included in shortcode/hooks
Fixes #13093
1 parent fc3d1cb commit c1dc35d

File tree

4 files changed

+86
-21
lines changed

4 files changed

+86
-21
lines changed
 

‎hugolib/page__content.go

+4
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,10 @@ type cachedContentScope struct {
863863
}
864864

865865
func (c *cachedContentScope) prepareContext(ctx context.Context) context.Context {
866+
// A regular page's shortcode etc. may be rendered by e.g. the home page,
867+
// so we need to track any changes to this content's page.
868+
ctx = tpl.Context.DependencyManagerScopedProvider.Set(ctx, c.pco.po.p)
869+
866870
// The markup scope is recursive, so if already set to a non zero value, preserve that value.
867871
if s := hugo.GetMarkupScope(ctx); s != "" || s == c.scope {
868872
return ctx

‎hugolib/rebuild_test.go

+66-11
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ const rebuildFilesSimple = `
2121
baseURL = "https://example.com"
2222
disableKinds = ["term", "taxonomy", "sitemap", "robotstxt", "404"]
2323
disableLiveReload = true
24+
[outputFormats]
25+
[outputFormats.rss]
26+
weight = 10
27+
[outputFormats.html]
28+
weight = 20
2429
[outputs]
25-
home = ["html"]
30+
home = ["rss", "html"]
2631
section = ["html"]
2732
page = ["html"]
2833
-- content/mysection/_index.md --
@@ -58,6 +63,21 @@ Home Text Content.
5863
title: "myothersectionpage"
5964
---
6065
myothersectionpage Content.
66+
-- content/mythirdsection/mythirdsectionpage.md --
67+
---
68+
title: "mythirdsectionpage"
69+
---
70+
mythirdsectionpage Content.
71+
{{< myshortcodetext >}}
72+
§§§ myothertext
73+
foo
74+
§§§
75+
-- assets/mytext.txt --
76+
Assets My Text.
77+
-- assets/myshortcodetext.txt --
78+
Assets My Shortcode Text.
79+
-- assets/myothertext.txt --
80+
Assets My Other Text.
6181
-- layouts/_default/single.html --
6282
Single: {{ .Title }}|{{ .Content }}$
6383
Resources: {{ range $i, $e := .Resources }}{{ $i }}:{{ .RelPermalink }}|{{ .Content }}|{{ end }}$
@@ -68,6 +88,13 @@ Len Resources: {{ len .Resources }}|
6888
Resources: {{ range $i, $e := .Resources }}{{ $i }}:{{ .RelPermalink }}|{{ .Content }}|{{ end }}$
6989
-- layouts/shortcodes/foo.html --
7090
Foo.
91+
-- layouts/shortcodes/myshortcodetext.html --
92+
{{ warnf "mytext %s" now}}
93+
{{ $r := resources.Get "myshortcodetext.txt" }}
94+
My Shortcode Text: {{ $r.Content }}|{{ $r.Permalink }}|
95+
-- layouts/_default/_markup/render-codeblock-myothertext.html --
96+
{{ $r := resources.Get "myothertext.txt" }}
97+
My Other Text: {{ $r.Content }}|{{ $r.Permalink }}|
7198
7299
`
73100

@@ -83,6 +110,34 @@ func TestRebuildEditTextFileInLeafBundle(t *testing.T) {
83110
b.AssertRenderCountContent(1)
84111
}
85112

113+
func TestRebuildEditTextFileInShortcode(t *testing.T) {
114+
t.Parallel()
115+
for i := 0; i < 3; i++ {
116+
b := TestRunning(t, rebuildFilesSimple)
117+
b.AssertFileContent("public/mythirdsection/mythirdsectionpage/index.html",
118+
"Text: Assets My Shortcode Text.")
119+
b.EditFileReplaceAll("assets/myshortcodetext.txt", "My Shortcode Text", "My Shortcode Text Edited").Build()
120+
fmt.Println(b.LogString())
121+
b.AssertFileContent("public/mythirdsection/mythirdsectionpage/index.html",
122+
"Text: Assets My Shortcode Text Edited.")
123+
124+
}
125+
}
126+
127+
func TestRebuildEditTextFileInHook(t *testing.T) {
128+
t.Parallel()
129+
for i := 0; i < 3; i++ {
130+
b := TestRunning(t, rebuildFilesSimple)
131+
b.AssertFileContent("public/mythirdsection/mythirdsectionpage/index.html",
132+
"Text: Assets My Other Text.")
133+
b.AssertFileContent("public/myothertext.txt", "Assets My Other Text.")
134+
b.EditFileReplaceAll("assets/myothertext.txt", "My Other Text", "My Other Text Edited").Build()
135+
b.AssertFileContent("public/mythirdsection/mythirdsectionpage/index.html",
136+
"Text: Assets My Other Text Edited.")
137+
138+
}
139+
}
140+
86141
func TestRebuiEditUnmarshaledYamlFileInLeafBundle(t *testing.T) {
87142
files := `
88143
-- hugo.toml --
@@ -140,8 +195,8 @@ func TestRebuildRenameTextFileInLeafBundle(t *testing.T) {
140195

141196
b.RenameFile("content/mysection/mysectionbundle/mysectionbundletext.txt", "content/mysection/mysectionbundle/mysectionbundletext2.txt").Build()
142197
b.AssertFileContent("public/mysection/mysectionbundle/index.html", "mysectionbundletext2", "My Section Bundle Text 2 Content.", "Len Resources: 2|")
143-
b.AssertRenderCountPage(5)
144-
b.AssertRenderCountContent(6)
198+
b.AssertRenderCountPage(8)
199+
b.AssertRenderCountContent(8)
145200
})
146201
}
147202

@@ -161,8 +216,8 @@ func TestRebuilEditContentFileThenAnother(t *testing.T) {
161216

162217
b.EditFileReplaceAll("content/myothersection/myothersectionpage.md", "myothersectionpage Content.", "myothersectionpage Content Edited.").Build()
163218
b.AssertFileContent("public/myothersection/myothersectionpage/index.html", "myothersectionpage Content Edited")
164-
b.AssertRenderCountPage(1)
165-
b.AssertRenderCountContent(1)
219+
b.AssertRenderCountPage(2)
220+
b.AssertRenderCountContent(2)
166221
}
167222

168223
func TestRebuildRenameTextFileInBranchBundle(t *testing.T) {
@@ -171,7 +226,7 @@ func TestRebuildRenameTextFileInBranchBundle(t *testing.T) {
171226

172227
b.RenameFile("content/mysection/mysectiontext.txt", "content/mysection/mysectiontext2.txt").Build()
173228
b.AssertFileContent("public/mysection/index.html", "mysectiontext2", "My Section")
174-
b.AssertRenderCountPage(2)
229+
b.AssertRenderCountPage(3)
175230
b.AssertRenderCountContent(2)
176231
}
177232

@@ -181,14 +236,14 @@ func TestRebuildRenameTextFileInHomeBundle(t *testing.T) {
181236

182237
b.RenameFile("content/hometext.txt", "content/hometext2.txt").Build()
183238
b.AssertFileContent("public/index.html", "hometext2", "Home Text Content.")
184-
b.AssertRenderCountPage(3)
239+
b.AssertRenderCountPage(5)
185240
}
186241

187242
func TestRebuildRenameDirectoryWithLeafBundle(t *testing.T) {
188243
b := TestRunning(t, rebuildFilesSimple)
189244
b.RenameDir("content/mysection/mysectionbundle", "content/mysection/mysectionbundlerenamed").Build()
190245
b.AssertFileContent("public/mysection/mysectionbundlerenamed/index.html", "My Section Bundle")
191-
b.AssertRenderCountPage(1)
246+
b.AssertRenderCountPage(2)
192247
}
193248

194249
func TestRebuildRenameDirectoryWithBranchBundle(t *testing.T) {
@@ -197,7 +252,7 @@ func TestRebuildRenameDirectoryWithBranchBundle(t *testing.T) {
197252
b.AssertFileContent("public/mysectionrenamed/index.html", "My Section")
198253
b.AssertFileContent("public/mysectionrenamed/mysectionbundle/index.html", "My Section Bundle")
199254
b.AssertFileContent("public/mysectionrenamed/mysectionbundle/mysectionbundletext.txt", "My Section Bundle Text 2 Content.")
200-
b.AssertRenderCountPage(3)
255+
b.AssertRenderCountPage(5)
201256
}
202257

203258
func TestRebuildRenameDirectoryWithRegularPageUsedInHome(t *testing.T) {
@@ -296,7 +351,7 @@ func TestRebuildRenameDirectoryWithBranchBundleFastRender(t *testing.T) {
296351
b.AssertFileContent("public/mysectionrenamed/index.html", "My Section")
297352
b.AssertFileContent("public/mysectionrenamed/mysectionbundle/index.html", "My Section Bundle")
298353
b.AssertFileContent("public/mysectionrenamed/mysectionbundle/mysectionbundletext.txt", "My Section Bundle Text 2 Content.")
299-
b.AssertRenderCountPage(3)
354+
b.AssertRenderCountPage(5)
300355
}
301356

302357
func TestRebuilErrorRecovery(t *testing.T) {
@@ -1239,7 +1294,7 @@ Single.
12391294
return strings.Replace(s, "red", "blue", 1)
12401295
}).Build()
12411296

1242-
b.AssertRenderCountPage(3)
1297+
b.AssertRenderCountPage(4)
12431298

12441299
b.AssertFileContent("public/index.html", "Home.", "<style>body {\n\tbackground: blue;\n}</style>")
12451300
}

‎identity/finder.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,21 @@ func (f *Finder) Contains(id, in Identity, maxDepth int) FinderResult {
122122

123123
defer putSearchID(sid)
124124

125-
if r := f.checkOne(sid, in, 0); r > 0 {
125+
r := FinderNotFound
126+
if i := f.checkOne(sid, in, 0); i > r {
127+
r = i
128+
}
129+
if r == FinderFound {
126130
return r
127131
}
128132

129133
m := GetDependencyManager(in)
130134
if m != nil {
131-
if r := f.checkManager(sid, m, 0); r > 0 {
132-
return r
135+
if i := f.checkManager(sid, m, 0); i > r {
136+
r = i
133137
}
134138
}
135-
return FinderNotFound
139+
return r
136140
}
137141

138142
func (f *Finder) checkMaxDepth(sid *searchID, level int) FinderResult {
@@ -279,15 +283,18 @@ func (f *Finder) search(sid *searchID, m Manager, depth int) FinderResult {
279283
var r FinderResult
280284
m.forEeachIdentity(
281285
func(v Identity) bool {
282-
if r > 0 {
283-
panic("should be terminated")
286+
i := f.checkOne(sid, v, depth)
287+
if i > r {
288+
r = i
284289
}
285-
r = f.checkOne(sid, v, depth)
286-
if r > 0 {
290+
if r == FinderFound {
287291
return true
288292
}
289293
m := GetDependencyManager(v)
290-
if r = f.checkManager(sid, m, depth+1); r > 0 {
294+
if i := f.checkManager(sid, m, depth+1); i > r {
295+
r = i
296+
}
297+
if r == FinderFound {
291298
return true
292299
}
293300
return false

‎tpl/template.go

-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ type contextKey string
172172
var Context = struct {
173173
DependencyManagerScopedProvider hcontext.ContextDispatcher[identity.DependencyManagerScopedProvider]
174174
GetDependencyManagerInCurrentScope func(context.Context) identity.Manager
175-
SetDependencyManagerInCurrentScope func(context.Context, identity.Manager) context.Context
176175
DependencyScope hcontext.ContextDispatcher[int]
177176
Page hcontext.ContextDispatcher[page]
178177
IsInGoldmark hcontext.ContextDispatcher[bool]

0 commit comments

Comments
 (0)
Please sign in to comment.