Debian Codegraph @

hugolib/pagesfromdata/pagesfromgotmpl_integration_test.go

raw
1 // Copyright 2025 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package pagesfromdata_test
15
16 import (
17 "bytes"
18 "fmt"
19 "strings"
20 "testing"
21
22 qt "github.com/frankban/quicktest"
23 "github.com/gohugoio/hugo/hugolib"
24 "github.com/gohugoio/hugo/markup/asciidocext"
25 "github.com/gohugoio/hugo/markup/pandoc"
26 "github.com/gohugoio/hugo/markup/rst"
27 "github.com/gohugoio/hugo/related"
28 )
29
30 const filesPagesFromDataTempleBasic = `
31 -- hugo.toml --
32 disableKinds = ["taxonomy", "term", "rss", "sitemap"]
33 baseURL = "https://example.com"
34 disableLiveReload = true
35 [security]
36 allowContent = ['.*']
37 -- assets/a/pixel.png --
38 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
39 -- assets/mydata.yaml --
40 p1: "p1"
41 draft: false
42 -- layouts/_partials/get-value.html --
43 {{ $val := "p1" }}
44 {{ return $val }}
45 -- layouts/baseof.html --
46 Baseof:
47 {{ block "main" . }}{{ end }}
48 -- layouts/single.html --
49 {{ define "main" }}
50 Single: {{ .Title }}|{{ .Content }}|Params: {{ .Params.param1 }}|Path: {{ .Path }}|
51 Dates: Date: {{ .Date.Format "2006-01-02" }}|Lastmod: {{ .Lastmod.Format "2006-01-02" }}|PublishDate: {{ .PublishDate.Format "2006-01-02" }}|ExpiryDate: {{ .ExpiryDate.Format "2006-01-02" }}|
52 Len Resources: {{ .Resources | len }}
53 Resources: {{ range .Resources }}RelPermalink: {{ .RelPermalink }}|Name: {{ .Name }}|Title: {{ .Title }}|Params: {{ .Params }}|{{ end }}$
54 {{ with .Resources.Get "featured.png" }}
55 Featured Image: {{ .RelPermalink }}|{{ .Name }}|
56 {{ with .Resize "10x10" }}
57 Resized Featured Image: {{ .RelPermalink }}|{{ .Width }}|
58 {{ end}}
59 {{ end }}
60 {{ end }}
61 -- layouts/list.html --
62 List: {{ .Title }}|{{ .Content }}|
63 RegularPagesRecursive: {{ range .RegularPagesRecursive }}{{ .Title }}:{{ .Path }}|{{ end }}$
64 Sections: {{ range .Sections }}{{ .Title }}:{{ .Path }}|{{ end }}$
65 -- content/docs/pfile.md --
66 ---
67 title: "pfile"
68 date: 2023-03-01
69 ---
70 Pfile Content
71 -- content/docs/_content.gotmpl --
72 {{ $pixel := resources.Get "a/pixel.png" }}
73 {{ $dataResource := resources.Get "mydata.yaml" }}
74 {{ $data := $dataResource | transform.Unmarshal }}
75 {{ $pd := $data.p1 }}
76 {{ $pp := partial "get-value.html" }}
77 {{ $title := printf "%s:%s" $pd $pp }}
78 {{ $date := "2023-03-01" | time.AsTime }}
79 {{ $dates := dict "date" $date }}
80 {{ $keywords := slice "foo" "Bar"}}
81 {{ $contentMarkdown := dict "value" "**Hello World**" "mediaType" "text/markdown" }}
82 {{ $contentMarkdownDefault := dict "value" "**Hello World Default**" }}
83 {{ $contentHTML := dict "value" "<b>Hello World!</b> No **markdown** here." "mediaType" "text/html" }}
84 {{ $.AddPage (dict "kind" "page" "path" "P1" "title" $title "dates" $dates "keywords" $keywords "content" $contentMarkdown "params" (dict "param1" "param1v" ) ) }}
85 {{ $.AddPage (dict "kind" "page" "path" "p2" "title" "p2title" "dates" $dates "content" $contentHTML ) }}
86 {{ $.AddPage (dict "kind" "page" "path" "p3" "title" "p3title" "dates" $dates "content" $contentMarkdownDefault "draft" false ) }}
87 {{ $.AddPage (dict "kind" "page" "path" "p4" "title" "p4title" "dates" $dates "content" $contentMarkdownDefault "draft" $data.draft ) }}
88 ADD_MORE_PLACEHOLDER
89
90
91 {{ $resourceContent := dict "value" $dataResource }}
92 {{ $.AddResource (dict "path" "p1/data1.yaml" "content" $resourceContent) }}
93 {{ $.AddResource (dict "path" "p1/mytext.txt" "content" (dict "value" "some text") "name" "textresource" "title" "My Text Resource" "params" (dict "param1" "param1v") )}}
94 {{ $.AddResource (dict "path" "p1/sub/mytex2.txt" "content" (dict "value" "some text") "title" "My Text Sub Resource" ) }}
95 {{ $.AddResource (dict "path" "P1/Sub/MyMixCaseText2.txt" "content" (dict "value" "some text") "title" "My Text Sub Mixed Case Path Resource" ) }}
96 {{ $.AddResource (dict "path" "p1/sub/data1.yaml" "content" $resourceContent "title" "Sub data") }}
97 {{ $resourceParams := dict "data2ParaM1" "data2Param1v" }}
98 {{ $.AddResource (dict "path" "p1/data2.yaml" "name" "data2.yaml" "title" "My data 2" "params" $resourceParams "content" $resourceContent) }}
99 {{ $.AddResource (dict "path" "p1/featuredimage.png" "name" "featured.png" "title" "My Featured Image" "params" $resourceParams "content" (dict "value" $pixel ))}}
100 `
101
102 func TestPagesFromGoTmplMisc(t *testing.T) {
103 t.Parallel()
104 b := hugolib.Test(t, filesPagesFromDataTempleBasic, hugolib.TestOptWarn())
105 b.AssertLogContains("! WARN")
106 b.AssertPublishDir(`
107 docs/p1/mytext.txt
108 docs/p1/sub/mytex2.tx
109 docs/p1/sub/mymixcasetext2.txt
110 `)
111
112 // Page from markdown file.
113 b.AssertFileContent("public/docs/pfile/index.html", "Dates: Date: 2023-03-01|Lastmod: 2023-03-01|PublishDate: 2023-03-01|ExpiryDate: 0001-01-01|")
114 // Pages from gotmpl.
115 b.AssertFileContent("public/docs/p1/index.html",
116 "Single: p1:p1|",
117 "Path: /docs/p1|",
118 "<strong>Hello World</strong>",
119 "Params: param1v|",
120 "Len Resources: 7",
121 "RelPermalink: /mydata.yaml|Name: data1.yaml|Title: data1.yaml|Params: map[]|",
122 "RelPermalink: /mydata.yaml|Name: data2.yaml|Title: My data 2|Params: map[data2param1:data2Param1v]|",
123 "RelPermalink: /a/pixel.png|Name: featured.png|Title: My Featured Image|Params: map[data2param1:data2Param1v]|",
124 "RelPermalink: /docs/p1/sub/mytex2.txt|Name: sub/mytex2.txt|",
125 "RelPermalink: /docs/p1/sub/mymixcasetext2.txt|Name: sub/mymixcasetext2.txt|",
126 "RelPermalink: /mydata.yaml|Name: sub/data1.yaml|Title: Sub data|Params: map[]|",
127 "Featured Image: /a/pixel.png|featured.png|",
128 "Resized Featured Image: /a/pixel_hu_a354833fd576551d.png|10|",
129 // Resource from string
130 "RelPermalink: /docs/p1/mytext.txt|Name: textresource|Title: My Text Resource|Params: map[param1:param1v]|",
131 // Dates
132 "Dates: Date: 2023-03-01|Lastmod: 2023-03-01|PublishDate: 2023-03-01|ExpiryDate: 0001-01-01|",
133 )
134 b.AssertFileContent("public/docs/p2/index.html", "Single: p2title|", "<b>Hello World!</b> No **markdown** here.")
135 b.AssertFileContent("public/docs/p3/index.html", "<strong>Hello World Default</strong>")
136 }
137
138 func TestPagesFromGoTmplAsciiDocAndSimilar(t *testing.T) {
139 supportsAsciiDoc, _ := asciidocext.Supports()
140 supportsPandoc := pandoc.Supports()
141 supportsRst := rst.Supports()
142
143 var contentGotmpl strings.Builder
144 var securityAllow []string
145 if supportsAsciiDoc {
146 contentGotmpl.WriteString("{{ $.AddPage (dict \"path\" \"asciidoc\" \"content\" (dict \"value\" \"Mark my words, #automation is essential#.\" \"mediaType\" \"text/asciidoc\" )) }}\n")
147 securityAllow = append(securityAllow, "'asciidoctor'")
148 }
149 if supportsPandoc {
150 contentGotmpl.WriteString("{{ $.AddPage (dict \"path\" \"pandoc\" \"content\" (dict \"value\" \"This ~~is deleted text.~~\" \"mediaType\" \"text/pandoc\" )) }}\n")
151 securityAllow = append(securityAllow, "'pandoc'")
152 }
153 if supportsRst {
154 contentGotmpl.WriteString("{{ $.AddPage (dict \"path\" \"rst\" \"content\" (dict \"value\" \"This is *bold*.\" \"mediaType\" \"text/rst\" )) }}\n")
155 securityAllow = append(securityAllow, "'rst2html'", "'python'")
156 }
157 contentGotmpl.WriteString("{{ $.AddPage (dict \"path\" \"org\" \"content\" (dict \"value\" \"the ability to use +strikethrough+ is a plus\" \"mediaType\" \"text/org\" )) }}\n")
158 contentGotmpl.WriteString("{{ $.AddPage (dict \"path\" \"nocontent\" \"title\" \"No Content\" ) }}\n")
159
160 files := fmt.Sprintf(`
161 -- hugo.toml --
162 disableKinds = ["taxonomy", "term", "rss", "sitemap"]
163 baseURL = "https://example.com"
164 [security]
165 [security.exec]
166 allow = [%s]
167 -- layouts/single.html --
168 |Content: {{ .Content }}|Title: {{ .Title }}|Path: {{ .Path }}|
169 -- content/docs/_content.gotmpl --
170 %s
171 `, strings.Join(securityAllow, ", "), contentGotmpl.String())
172
173 b := hugolib.Test(t, files)
174
175 if supportsAsciiDoc {
176 b.AssertFileContent("public/docs/asciidoc/index.html",
177 "Mark my words, <mark>automation is essential</mark>",
178 "Path: /docs/asciidoc|",
179 )
180 }
181 if supportsPandoc {
182 b.AssertFileContent("public/docs/pandoc/index.html",
183 "This <del>is deleted text.</del>",
184 "Path: /docs/pandoc|",
185 )
186 }
187 if supportsRst {
188 b.AssertFileContent("public/docs/rst/index.html",
189 "This is <em>bold</em>",
190 "Path: /docs/rst|",
191 )
192 }
193
194 b.AssertFileContent("public/docs/org/index.html",
195 "the ability to use <del>strikethrough</del> is a plus",
196 "Path: /docs/org|",
197 )
198
199 b.AssertFileContent("public/docs/nocontent/index.html", "|Content: |Title: No Content|Path: /docs/nocontent|")
200 }
201
202 func TestPagesFromGoTmplAddPageErrors(t *testing.T) {
203 filesTemplate := `
204 -- hugo.toml --
205 disableKinds = ["taxonomy", "term", "rss", "sitemap"]
206 baseURL = "https://example.com"
207 -- content/docs/_content.gotmpl --
208 {{ $.AddPage DICT }}
209 `
210
211 t.Run("AddPage, missing Path", func(t *testing.T) {
212 files := strings.ReplaceAll(filesTemplate, "DICT", `(dict "kind" "page" "title" "p1")`)
213 b, err := hugolib.TestE(t, files)
214 b.Assert(err, qt.IsNotNil)
215 b.Assert(err.Error(), qt.Contains, "_content.gotmpl:1:4")
216 b.Assert(err.Error(), qt.Contains, "error calling AddPage: empty path is reserved for the home page")
217 })
218
219 t.Run("Site methods not ready", func(t *testing.T) {
220 filesTemplate := `
221 -- hugo.toml --
222 disableKinds = ["taxonomy", "term", "rss", "sitemap"]
223 baseURL = "https://example.com"
224 -- content/docs/_content.gotmpl --
225 {{ .Site.METHOD }}
226 `
227
228 for _, method := range []string{"RegularPages", "Pages", "AllPages", "AllRegularPages", "Home", "Sections", "GetPage", "Menus", "MainSections", "Taxonomies"} {
229 t.Run(method, func(t *testing.T) {
230 files := strings.ReplaceAll(filesTemplate, "METHOD", method)
231 b, err := hugolib.TestE(t, files)
232 b.Assert(err, qt.IsNotNil)
233 b.Assert(err.Error(), qt.Contains, fmt.Sprintf("error calling %s: this method cannot be called before the site is fully initialized", method))
234 })
235 }
236 })
237 }
238
239 func TestPagesFromGoTmplEditGoTmpl(t *testing.T) {
240 t.Parallel()
241 b := hugolib.TestRunning(t, filesPagesFromDataTempleBasic)
242 b.EditFileReplaceAll("content/docs/_content.gotmpl", `"title" "p2title"`, `"title" "p2titleedited"`).Build()
243 b.AssertFileContent("public/docs/p2/index.html", "Single: p2titleedited|")
244 b.AssertFileContent("public/docs/index.html", "p2titleedited")
245 }
246
247 func TestPagesFromGoTmplEditDataResource(t *testing.T) {
248 t.Parallel()
249 b := hugolib.TestRunning(t, filesPagesFromDataTempleBasic)
250 b.AssertRenderCountPage(7)
251 b.EditFileReplaceAll("assets/mydata.yaml", "p1: \"p1\"", "p1: \"p1edited\"").Build()
252 b.AssertFileContent("public/docs/p1/index.html", "Single: p1edited:p1|")
253 b.AssertFileContent("public/docs/index.html", "p1edited")
254 b.AssertRenderCountPage(3)
255 }
256
257 func TestPagesFromGoTmplEditPartial(t *testing.T) {
258 t.Parallel()
259 b := hugolib.TestRunning(t, filesPagesFromDataTempleBasic)
260 b.EditFileReplaceAll("layouts/_partials/get-value.html", "p1", "p1edited").Build()
261 b.AssertFileContent("public/docs/p1/index.html", "Single: p1:p1edited|")
262 b.AssertFileContent("public/docs/index.html", "p1edited")
263 }
264
265 func TestPagesFromGoTmplRemovePage(t *testing.T) {
266 t.Parallel()
267 b := hugolib.TestRunning(t, filesPagesFromDataTempleBasic)
268 b.EditFileReplaceAll("content/docs/_content.gotmpl", `{{ $.AddPage (dict "kind" "page" "path" "p2" "title" "p2title" "dates" $dates "content" $contentHTML ) }}`, "").Build()
269 b.AssertFileContent("public/index.html", "RegularPagesRecursive: p1:p1:/docs/p1|p3title:/docs/p3|p4title:/docs/p4|pfile:/docs/pfile|$")
270 }
271
272 func TestPagesFromGoTmplAddPage(t *testing.T) {
273 t.Parallel()
274 b := hugolib.TestRunning(t, filesPagesFromDataTempleBasic)
275 b.EditFileReplaceAll("content/docs/_content.gotmpl", "ADD_MORE_PLACEHOLDER", `{{ $.AddPage (dict "kind" "page" "path" "page_added" "title" "page_added_title" "dates" $dates "content" $contentHTML ) }}`).Build()
276 b.AssertFileExists("public/docs/page_added/index.html", true)
277 b.AssertFileContent("public/index.html", "RegularPagesRecursive: p1:p1:/docs/p1|p2title:/docs/p2|p3title:/docs/p3|p4title:/docs/p4|page_added_title:/docs/page_added|pfile:/docs/pfile|$")
278 }
279
280 func TestPagesFromGoTmplDraftPage(t *testing.T) {
281 t.Parallel()
282 b := hugolib.TestRunning(t, filesPagesFromDataTempleBasic)
283 b.EditFileReplaceAll("content/docs/_content.gotmpl", `"draft" false`, `"draft" true`).Build()
284 b.AssertFileContent("public/index.html", "RegularPagesRecursive: p1:p1:/docs/p1|p2title:/docs/p2|p4title:/docs/p4|pfile:/docs/pfile|$")
285 }
286
287 func TestPagesFromGoTmplDraftFlagFromResource(t *testing.T) {
288 t.Parallel()
289 b := hugolib.TestRunning(t, filesPagesFromDataTempleBasic)
290 b.EditFileReplaceAll("assets/mydata.yaml", `draft: false`, `draft: true`).Build()
291 b.AssertFileContent("public/index.html", "RegularPagesRecursive: p1:p1:/docs/p1|p2title:/docs/p2|p3title:/docs/p3|pfile:/docs/pfile|$")
292 b.EditFileReplaceAll("assets/mydata.yaml", `draft: true`, `draft: false`).Build()
293 b.AssertFileContent("public/index.html", "RegularPagesRecursive: p1:p1:/docs/p1|p2title:/docs/p2|p3title:/docs/p3|p4title:/docs/p4|pfile:/docs/pfile|$")
294 }
295
296 func TestPagesFromGoTmplMovePage(t *testing.T) {
297 t.Parallel()
298 b := hugolib.TestRunning(t, filesPagesFromDataTempleBasic)
299 b.AssertFileContent("public/index.html", "RegularPagesRecursive: p1:p1:/docs/p1|p2title:/docs/p2|p3title:/docs/p3|p4title:/docs/p4|pfile:/docs/pfile|$")
300 b.EditFileReplaceAll("content/docs/_content.gotmpl", `"path" "p2"`, `"path" "p2moved"`).Build()
301 b.AssertFileContent("public/index.html", "RegularPagesRecursive: p1:p1:/docs/p1|p2title:/docs/p2moved|p3title:/docs/p3|p4title:/docs/p4|pfile:/docs/pfile|$")
302 }
303
304 func TestPagesFromGoTmplRemoveGoTmpl(t *testing.T) {
305 t.Parallel()
306 b := hugolib.TestRunning(t, filesPagesFromDataTempleBasic)
307 b.AssertFileContent("public/index.html",
308 "RegularPagesRecursive: p1:p1:/docs/p1|p2title:/docs/p2|p3title:/docs/p3|p4title:/docs/p4|pfile:/docs/pfile|$",
309 "Sections: Docs:/docs|",
310 )
311 b.AssertFileContent("public/docs/index.html", "RegularPagesRecursive: p1:p1:/docs/p1|p2title:/docs/p2|p3title:/docs/p3|p4title:/docs/p4|pfile:/docs/pfile|$")
312 b.RemoveFiles("content/docs/_content.gotmpl").Build()
313 // One regular page left.
314 b.AssertFileContent("public/index.html",
315 "RegularPagesRecursive: pfile:/docs/pfile|$",
316 "Sections: Docs:/docs|",
317 )
318 b.AssertFileContent("public/docs/index.html", "RegularPagesRecursive: pfile:/docs/pfile|$")
319 }
320
321 func TestPagesFromGoTmplEditOverlappingContentFile(t *testing.T) {
322 t.Parallel()
323 files := `
324 -- hugo.toml --
325 disableLiveReload = true
326 disableKinds = ["taxonomy", "term", "rss", "sitemap"]
327 -- layouts/all.html --
328 All: {{ .Content }}|{{ .Title }}|
329 -- layouts/section.html --
330 Title: {{ .Title}}|
331 RegularPages: {{ range .RegularPages }}{{ .Title }}:{{ .Path }}|{{ end }}|
332 -- content/mysection/_index.md --
333 ---
334 title: "My Section"
335 ---
336 -- content/mysection/p1.md --
337 ---
338 title: "p1 content file"
339 ---
340 Content of p1
341 -- content/_content.gotmpl --
342 {{ $.AddPage (dict "kind" "page" "path" "mysection/p2" "title" "p2 content adapter") }}
343 `
344 b := hugolib.TestRunning(t, files)
345 b.AssertFileContent("public/mysection/index.html", "Title: My Section|", "RegularPages: p1 content file:/mysection/p1|p2 content adapter:/mysection/p2|")
346
347 b.EditFileReplaceAll("content/mysection/p1.md", `"p1 content file"`, `"p1 content file edited"`).Build()
348 b.AssertFileContent("public/mysection/index.html", "RegularPages: p1 content file edited:/mysection/p1|p2 content adapter:/mysection/p2|")
349
350 b.EditFileReplaceAll("content/mysection/_index.md", "My Section", "My Section edited").Build()
351 b.AssertFileContent("public/mysection/index.html", "Title: My Section edited|", "RegularPages: p1 content file edited:/mysection/p1|p2 content adapter:/mysection/p2|")
352
353 b.RemoveFiles("content/mysection/p1.md").Build()
354 b.AssertFileContent("public/mysection/index.html", "Title: My Section edited|\nRegularPages: p2 content adapter:/mysection/p2|")
355
356 b.RemoveFiles("content/_content.gotmpl", "public/mysection/index.html").Build()
357 b.AssertFileContent("public/mysection/index.html", "Title: My Section edited|\nRegularPages: |")
358 }
359
360 // Issue #13443.
361 func TestPagesFromGoRelatedKeywords(t *testing.T) {
362 t.Parallel()
363 b := hugolib.Test(t, filesPagesFromDataTempleBasic)
364
365 p1 := b.H.Sites[0].RegularPages()[0]
366 icfg := related.IndexConfig{
367 Name: "keywords",
368 }
369 k, err := p1.RelatedKeywords(icfg)
370 b.Assert(err, qt.IsNil)
371 b.Assert(k, qt.DeepEquals, icfg.StringsToKeywords("foo", "Bar"))
372 icfg.Name = "title"
373 k, err = p1.RelatedKeywords(icfg)
374 b.Assert(err, qt.IsNil)
375 b.Assert(k, qt.DeepEquals, icfg.StringsToKeywords("p1:p1"))
376 }
377
378 func TestPagesFromGoTmplLanguagePerFile(t *testing.T) {
379 filesTemplate := `
380 -- hugo.toml --
381 defaultContentLanguage = "en"
382 defaultContentLanguageInSubdir = true
383 [languages]
384 [languages.en]
385 weight = 1
386 title = "Title"
387 [languages.fr]
388 weight = 2
389 title = "Titre"
390 disabled = DISABLE
391 -- layouts/single.html --
392 Single: {{ .Title }}|{{ .Content }}|
393 -- content/docs/_content.gotmpl --
394 {{ $.AddPage (dict "kind" "page" "path" "p1" "title" "Title" ) }}
395 -- content/docs/_content.fr.gotmpl --
396 {{ $.AddPage (dict "kind" "page" "path" "p1" "title" "Titre" ) }}
397 `
398
399 for _, disable := range []bool{false, true} {
400 t.Run(fmt.Sprintf("disable=%t", disable), func(t *testing.T) {
401 b := hugolib.Test(t, strings.ReplaceAll(filesTemplate, "DISABLE", fmt.Sprintf("%t", disable)))
402 b.AssertFileContent("public/en/docs/p1/index.html", "Single: Title||")
403 b.AssertFileExists("public/fr/docs/p1/index.html", !disable)
404 if !disable {
405 b.AssertFileContent("public/fr/docs/p1/index.html", "Single: Titre||")
406 }
407 })
408 }
409 }
410
411 func TestPagesFromGoTmplDefaultPageSort(t *testing.T) {
412 t.Parallel()
413 files := `
414 -- hugo.toml --
415 defaultContentLanguage = "en"
416 -- layouts/home.html --
417 {{ range site.RegularPages }}{{ .RelPermalink }}|{{ end}}
418 -- content/_content.gotmpl --
419 {{ $.AddPage (dict "kind" "page" "path" "docs/_p22" "title" "A" ) }}
420 {{ $.AddPage (dict "kind" "page" "path" "docs/p12" "title" "A" ) }}
421 {{ $.AddPage (dict "kind" "page" "path" "docs/_p12" "title" "A" ) }}
422 -- content/docs/_content.gotmpl --
423 {{ $.AddPage (dict "kind" "page" "path" "_p21" "title" "A" ) }}
424 {{ $.AddPage (dict "kind" "page" "path" "p11" "title" "A" ) }}
425 {{ $.AddPage (dict "kind" "page" "path" "_p11" "title" "A" ) }}
426 `
427
428 b := hugolib.Test(t, files)
429
430 b.AssertFileContent("public/index.html", "/docs/_p11/|/docs/_p12/|/docs/_p21/|/docs/_p22/|/docs/p11/|/docs/p12/|")
431 }
432
433 func TestPagesFromGoTmplEnableAllLanguages(t *testing.T) {
434 t.Parallel()
435
436 filesTemplate := `
437 -- hugo.toml --
438 defaultContentLanguage = "en"
439 defaultContentLanguageInSubdir = true
440 [languages]
441 [languages.en]
442 weight = 1
443 title = "Title"
444 [languages.fr]
445 title = "Titre"
446 weight = 2
447 disabled = DISABLE
448 -- i18n/en.yaml --
449 title: Title
450 -- i18n/fr.yaml --
451 title: Titre
452 -- content/docs/_content.gotmpl --
453 {{ .EnableAllLanguages }}
454 {{ $titleFromStore := .Store.Get "title" }}
455 {{ if not $titleFromStore }}
456 {{ $titleFromStore = "notfound"}}
457 {{ .Store.Set "title" site.Title }}
458 {{ end }}
459 {{ $title := printf "%s:%s:%s" site.Title (i18n "title") $titleFromStore }}
460 {{ $.AddPage (dict "kind" "page" "path" "p1" "title" $title ) }}
461 -- layouts/_default/single.html --
462 Single: {{ .Title }}|{{ .Content }}|
463
464 `
465
466 for _, disable := range []bool{false, true} {
467 t.Run(fmt.Sprintf("disable=%t", disable), func(t *testing.T) {
468 b := hugolib.Test(t, strings.ReplaceAll(filesTemplate, "DISABLE", fmt.Sprintf("%t", disable)))
469 b.AssertFileExists("public/fr/docs/p1/index.html", !disable)
470 if !disable {
471 b.AssertFileContent("public/en/docs/p1/index.html", "Single: Title:Title:notfound||")
472 b.AssertFileContent("public/fr/docs/p1/index.html", "Single: Titre:Titre:Title||")
473 }
474 })
475 }
476 }
477
478 func TestPagesFromGoTmplMarkdownify(t *testing.T) {
479 t.Parallel()
480
481 files := `
482 -- hugo.toml --
483 disableKinds = ["taxonomy", "term", "rss", "sitemap"]
484 baseURL = "https://example.com"
485 -- layouts/single.html --
486 |Content: {{ .Content }}|Title: {{ .Title }}|Path: {{ .Path }}|
487 -- content/docs/_content.gotmpl --
488 {{ $content := "**Hello World**" | markdownify }}
489 {{ $.AddPage (dict "path" "p1" "content" (dict "value" $content "mediaType" "text/html" )) }}
490 `
491
492 b, err := hugolib.TestE(t, files)
493
494 // This currently fails. We should fix this, but that is not a trivial task, so do it later.
495 b.Assert(err, qt.IsNotNil)
496 b.Assert(err.Error(), qt.Contains, "error calling markdownify: this method cannot be called before the site is fully initialized")
497 }
498
499 func TestPagesFromGoTmplResourceWithoutExtensionWithMediaTypeProvided(t *testing.T) {
500 t.Parallel()
501
502 files := `
503 -- hugo.toml --
504 disableKinds = ["taxonomy", "term", "rss", "sitemap"]
505 baseURL = "https://example.com"
506 -- layouts/single.html --
507 |Content: {{ .Content }}|Title: {{ .Title }}|Path: {{ .Path }}|
508 {{ range .Resources }}
509 |RelPermalink: {{ .RelPermalink }}|Name: {{ .Name }}|Title: {{ .Title }}|Params: {{ .Params }}|MediaType: {{ .MediaType }}|
510 {{ end }}
511 -- content/docs/_content.gotmpl --
512 {{ $.AddPage (dict "path" "p1" "content" (dict "value" "**Hello World**" "mediaType" "text/markdown" )) }}
513 {{ $.AddResource (dict "path" "p1/myresource" "content" (dict "value" "abcde" "mediaType" "text/plain" )) }}
514 `
515
516 b := hugolib.Test(t, files)
517
518 b.AssertFileContent("public/docs/p1/index.html", "RelPermalink: /docs/p1/myresource|Name: myresource|Title: myresource|Params: map[]|MediaType: text/plain|")
519 }
520
521 func TestPagesFromGoTmplCascade(t *testing.T) {
522 t.Parallel()
523
524 files := `
525 -- hugo.toml --
526 disableKinds = ["taxonomy", "term", "rss", "sitemap"]
527 baseURL = "https://example.com"
528 -- layouts/single.html --
529 |Content: {{ .Content }}|Title: {{ .Title }}|Path: {{ .Path }}|Params: {{ .Params }}|
530 -- content/_content.gotmpl --
531 {{ $cascade := dict "params" (dict "cascadeparam1" "cascadeparam1value" ) }}
532 {{ $.AddPage (dict "path" "docs" "kind" "section" "cascade" $cascade ) }}
533 {{ $.AddPage (dict "path" "docs/p1" "content" (dict "value" "**Hello World**" "mediaType" "text/markdown" )) }}
534
535 `
536 b := hugolib.Test(t, files)
537
538 b.AssertFileContent("public/docs/p1/index.html", "|Path: /docs/p1|Params: map[cascadeparam1:cascadeparam1value")
539 }
540
541 func TestPagesFromGoBuildOptions(t *testing.T) {
542 t.Parallel()
543
544 files := `
545 -- hugo.toml --
546 disableKinds = ["taxonomy", "term", "rss", "sitemap"]
547 baseURL = "https://example.com"
548 -- layouts/single.html --
549 |Content: {{ .Content }}|Title: {{ .Title }}|Path: {{ .Path }}|Params: {{ .Params }}|
550 -- content/_content.gotmpl --
551 {{ $.AddPage (dict "path" "docs/p1" "content" (dict "value" "**Hello World**" "mediaType" "text/markdown" )) }}
552 {{ $never := dict "list" "never" "publishResources" false "render" "never" }}
553 {{ $.AddPage (dict "path" "docs/p2" "content" (dict "value" "**Hello World**" "mediaType" "text/markdown" ) "build" $never ) }}
554
555
556 `
557 b := hugolib.Test(t, files)
558
559 b.AssertFileExists("public/docs/p1/index.html", true)
560 b.AssertFileExists("public/docs/p2/index.html", false)
561 }
562
563 func TestPagesFromGoPathsWithDotsIssue12493(t *testing.T) {
564 t.Parallel()
565
566 files := `
567 -- hugo.toml --
568 disableKinds = ['home','section','rss','sitemap','taxonomy','term']
569 -- content/_content.gotmpl --
570 {{ .AddPage (dict "path" "s-1.2.3/p-4.5.6" "title" "p-4.5.6") }}
571 -- layouts/single.html --
572 {{ .Title }}
573 `
574
575 b := hugolib.Test(t, files)
576
577 b.AssertFileExists("public/s-1.2.3/p-4.5.6/index.html", true)
578 }
579
580 func TestPagesFromGoParamsIssue12497(t *testing.T) {
581 t.Parallel()
582
583 files := `
584 -- hugo.toml --
585 disableKinds = ['home','section','rss','sitemap','taxonomy','term']
586 -- content/_content.gotmpl --
587 {{ .AddPage (dict "path" "p1" "title" "p1" "params" (dict "paraM1" "param1v" )) }}
588 {{ .AddResource (dict "path" "p1/data1.yaml" "content" (dict "value" "data1" ) "params" (dict "paraM1" "param1v" )) }}
589 -- layouts/single.html --
590 {{ .Title }}|{{ .Params.paraM1 }}
591 {{ range .Resources }}
592 {{ .Name }}|{{ .Params.paraM1 }}
593 {{ end }}
594 `
595
596 b := hugolib.Test(t, files)
597
598 b.AssertFileContent("public/p1/index.html",
599 "p1|param1v",
600 "data1.yaml|param1v",
601 )
602 }
603
604 func TestPagesFromGoTmplShortcodeNoPreceddingCharacterIssue12544(t *testing.T) {
605 t.Parallel()
606
607 files := `
608 -- hugo.toml --
609 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
610 [security]
611 allowContent = ['.*']
612 -- content/_content.gotmpl --
613 {{ $content := dict "mediaType" "text/html" "value" "x{{< sc >}}" }}
614 {{ .AddPage (dict "content" $content "path" "a") }}
615
616 {{ $content := dict "mediaType" "text/html" "value" "{{< sc >}}" }}
617 {{ .AddPage (dict "content" $content "path" "b") }}
618 -- layouts/single.html --
619 |{{ .Content }}|
620 -- layouts/_shortcodes/sc.html --
621 foo
622 {{- /**/ -}}
623 `
624
625 b := hugolib.Test(t, files)
626
627 b.AssertFileContent("public/a/index.html", "|xfoo|")
628 b.AssertFileContent("public/b/index.html", "|foo|") // fails
629 }
630
631 func TestPagesFromGoTmplMenus(t *testing.T) {
632 t.Parallel()
633
634 files := `
635 -- hugo.toml --
636 disableKinds = ['rss','section','sitemap','taxonomy','term']
637
638 [menus]
639 [[menus.main]]
640 name = "Main"
641 [[menus.footer]]
642 name = "Footer"
643 -- content/_content.gotmpl --
644 {{ .AddPage (dict "path" "p1" "title" "p1" "menus" "main" ) }}
645 {{ .AddPage (dict "path" "p2" "title" "p2" "menus" (slice "main" "footer")) }}
646 -- layouts/home.html --
647 Main: {{ range index site.Menus.main }}{{ .Name }}|{{ end }}|
648 Footer: {{ range index site.Menus.footer }}{{ .Name }}|{{ end }}|
649
650 `
651 b := hugolib.Test(t, files)
652
653 b.AssertFileContent("public/index.html",
654 "Main: Main|p1|p2||",
655 "Footer: Footer|p2||",
656 )
657 }
658
659 // Issue 13384.
660 func TestPagesFromGoTmplMenusMap(t *testing.T) {
661 t.Parallel()
662
663 files := `
664 -- hugo.toml --
665 disableKinds = ['rss','section','sitemap','taxonomy','term']
666 -- content/_content.gotmpl --
667 {{ $menu1 := dict
668 "parent" "main-page"
669 "identifier" "id1"
670 }}
671 {{ $menu2 := dict
672 "parent" "main-page"
673 "identifier" "id2"
674 }}
675 {{ $menus := dict "m1" $menu1 "m2" $menu2 }}
676 {{ .AddPage (dict "path" "p1" "title" "p1" "menus" $menus ) }}
677
678 -- layouts/home.html --
679 Menus: {{ range $k, $v := site.Menus }}{{ $k }}|{{ end }}
680
681 `
682 b := hugolib.Test(t, files)
683
684 b.AssertFileContent("public/index.html", "Menus: m1|m2|")
685 }
686
687 func TestPagesFromGoTmplMore(t *testing.T) {
688 t.Parallel()
689
690 files := `
691 -- hugo.toml --
692 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
693 [markup.goldmark.renderer]
694 unsafe = true
695 -- content/s1/_content.gotmpl --
696 {{ $page := dict
697 "content" (dict "mediaType" "text/markdown" "value" "aaa <!--more--> bbb")
698 "title" "p1"
699 "path" "p1"
700 }}
701 {{ .AddPage $page }}
702 -- layouts/single.html --
703 summary: {{ .Summary }}|content: {{ .Content}}
704 `
705
706 b := hugolib.Test(t, files)
707
708 b.AssertFileContent("public/s1/p1/index.html",
709 "<p>aaa</p>|content: <p>aaa</p>\n<p>bbb</p>",
710 )
711 }
712
713 // Issue 13063.
714 func TestPagesFromGoTmplTermIsEmpty(t *testing.T) {
715 t.Parallel()
716
717 files := `
718 -- hugo.toml --
719 baseURL = "https://example.com"
720 disableKinds = ['section', 'home', 'rss','sitemap']
721 printPathWarnings = true
722 [taxonomies]
723 tag = "tags"
724 -- content/mypost.md --
725 ---
726 title: "My Post"
727 tags: ["mytag"]
728 ---
729 -- content/tags/_content.gotmpl --
730 {{ .AddPage (dict "path" "mothertag" "title" "My title" "kind" "term") }}
731 --
732 -- layouts/taxonomy.html --
733 Terms: {{ range .Data.Terms.ByCount }}{{ .Name }}: {{ .Count }}|{{ end }}§s
734 -- layouts/single.html --
735 Single.
736 `
737
738 b := hugolib.Test(t, files, hugolib.TestOptWarn())
739
740 b.AssertFileContent("public/tags/index.html", "Terms: mytag: 1|§s")
741 }
742
743 func TestContentAdapterOutputsIssue13689(t *testing.T) {
744 t.Parallel()
745
746 files := `
747 -- hugo.toml --
748 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
749 [outputs]
750 page = ['html','json']
751 -- layouts/page.html --
752 html: {{ .Title }}
753 -- layouts/page.json --
754 json: {{ .Title }}
755 -- content/p1.md --
756 ---
757 title: p1
758 ---
759 -- content/p2.md --
760 ---
761 title: p2
762 outputs:
763 - html
764 ---
765 -- content/_content.gotmpl --
766 {{ $page := dict "path" "p3" "title" "p3" }}
767 {{ $.AddPage $page }}
768
769 {{ $page := dict "path" "p4" "title" "p4" "outputs" (slice "html") }}
770 {{ $.AddPage $page }}
771 `
772
773 b := hugolib.Test(t, files)
774
775 b.AssertFileExists("public/p1/index.html", true)
776 b.AssertFileExists("public/p1/index.json", true)
777 b.AssertFileExists("public/p2/index.html", true)
778 b.AssertFileExists("public/p2/index.json", false)
779 b.AssertFileExists("public/p3/index.html", true)
780 b.AssertFileExists("public/p3/index.json", true)
781 b.AssertFileExists("public/p4/index.html", true)
782 b.AssertFileExists("public/p4/index.json", false) // currently returns true
783 }
784
785 func TestContentAdapterOutputsIssue13692(t *testing.T) {
786 t.Parallel()
787
788 files := `
789 -- hugo.toml --
790 disableKinds = ['page','home','sitemap','taxonomy','term']
791 [[cascade]]
792 outputs = ['html','json']
793 [cascade.target]
794 path = '{/s2,/s4}'
795 -- layouts/section.html --
796 html: {{ .Title }}
797 -- layouts/section.json --
798 json: {{ .Title }}
799 -- content/s1/_index.md --
800 ---
801 title: s1
802 ---
803 -- content/s2/_index.md --
804 ---
805 title: s2
806 ---
807 -- content/_content.gotmpl --
808 {{ $page := dict "path" "s3" "title" "s3" "kind" "section" }}
809 {{ $.AddPage $page }}
810
811 {{ $page := dict "path" "s4" "title" "s4" "kind" "section" }}
812 {{ $.AddPage $page }}
813
814 {{ $page := dict "path" "s5" "title" "s5" "kind" "section" "outputs" (slice "html") }}
815 {{ $.AddPage $page }}
816 `
817
818 b := hugolib.Test(t, files)
819
820 b.AssertFileExists("public/s1/index.html", true)
821 b.AssertFileExists("public/s1/index.json", false)
822 b.AssertFileExists("public/s1/index.xml", true)
823
824 b.AssertFileExists("public/s2/index.html", true)
825 b.AssertFileExists("public/s2/index.json", true)
826 b.AssertFileExists("public/s2/index.xml", false)
827
828 b.AssertFileExists("public/s3/index.html", true)
829 b.AssertFileExists("public/s3/index.json", false)
830 b.AssertFileExists("public/s3/index.xml", true)
831
832 b.AssertFileExists("public/s4/index.html", true)
833 b.AssertFileExists("public/s4/index.json", true)
834 b.AssertFileExists("public/s4/index.xml", false)
835
836 b.AssertFileExists("public/s5/index.html", true)
837 b.AssertFileExists("public/s5/index.json", false)
838 b.AssertFileExists("public/s5/index.xml", false)
839 }
840
841 func TestContentAdapterCascadeBasic(t *testing.T) {
842 t.Parallel()
843
844 files := `
845 -- hugo.toml --
846 disableLiveReload = true
847 -- content/_index.md --
848 ---
849 cascade:
850 - title: foo
851 target:
852 path: "**"
853 ---
854 -- layouts/all.html --
855 Title: {{ .Title }}|Content: {{ .Content }}|
856 -- content/_content.gotmpl --
857 {{ $content := dict
858 "mediaType" "text/markdown"
859 "value" "The _Hunchback of Notre Dame_ was written by Victor Hugo."
860 }}
861
862 {{ $page := dict "path" "s1" "kind" "page" }}
863 {{ $.AddPage $page }}
864 {{ $page := dict "path" "s2" "kind" "page" "title" "bar" "content" $content }}
865 {{ $.AddPage $page }}
866
867 `
868
869 b := hugolib.TestRunning(t, files)
870
871 b.AssertFileContent("public/s1/index.html", "Title: foo|")
872 b.AssertFileContent("public/s2/index.html", "Title: bar|", "Content: <p>The <em>Hunchback of Notre Dame</em> was written by Victor Hugo.</p>")
873
874 b.EditFileReplaceAll("content/_index.md", "foo", "baz").Build()
875
876 b.AssertFileContent("public/s1/index.html", "Title: baz|")
877 }
878
879 func TestPagesFromGoTmplHome(t *testing.T) {
880 t.Parallel()
881
882 files := `
883 -- hugo.toml --
884 disableKinds = ["taxonomy", "term", "rss", "sitemap"]
885 baseURL = "https://example.com"
886 -- layouts/all.html --
887 {{ .Kind }}: {{ .Title }}|
888 -- content/_content.gotmpl --
889 {{ $.AddPage (dict "title" "My Home!" "kind" "home" ) }}
890
891 `
892 b := hugolib.Test(t, files)
893
894 b.AssertFileContent("public/index.html", "home: My Home!|")
895 }
896
897 func TestPagesFromGoTmplIssue14299(t *testing.T) {
898 t.Parallel()
899
900 files := `
901 -- hugo.toml --
902 disableKinds = ["taxonomy", "term", "rss", "sitemap"]
903 baseURL = "https://example.com"
904 -- layouts/all.html --
905 {{ .Kind }}: {{ .Title }}|
906 -- content/_content.gotmpl --
907 {{ $a := "foo" }}
908 -- content/_index.md --
909 -- content/bar/_content.gotmpl --
910 {{ $a := "bar" }}
911 -- content/bar/index.md --
912 `
913 // _content.gotmpl which was siblings of index.md (leaf bundles) was mistakingly classified as a content resource.
914 hugolib.Test(t, files)
915 }
916
917 // Issue 14684
918 func TestPagesFromGoTmplAddResourceFromStringContent(t *testing.T) {
919 t.Parallel()
920
921 files := `
922 -- hugo.toml --
923 disableKinds = ["taxonomy", "term", "rss", "sitemap"]
924 baseURL = "https://example.com"
925 -- assets/a/pixel.png --
926 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
927 -- layouts/single.html --
928 {{ with .Resources.Get "pixel.png" }}
929 {{ with .Resize "1x1" }}Resized: {{ .Width }}x{{ .Height }}|{{ end }}
930 {{ end }}
931 -- content/_content.gotmpl --
932 {{ $pixel := resources.Get "a/pixel.png" }}
933 {{ $content := dict "mediaType" $pixel.MediaType.Type "value" $pixel.Content }}
934 {{ $.AddPage (dict "path" "p1" "title" "p1") }}
935 {{ $.AddResource (dict "path" "p1/pixel.png" "content" $content) }}
936 `
937
938 b := hugolib.Test(t, files)
939
940 b.AssertFileContent("public/p1/index.html", "Resized: 1x1|")
941 }
942
943 // See https://github.com/gohugoio/hugo/issues/14999
944 func TestContentAdapterTemplateMetricsRelativePath(t *testing.T) {
945 t.Parallel()
946
947 files := `
948 -- hugo.toml --
949 templateMetrics = true
950 -- layouts/all.html --
951 {{ .Title }}
952 -- content/docs/_content.gotmpl --
953 {{ .AddPage (dict "path" "/docs/p1" "title" "P1") }}
954 `
955 b := hugolib.Test(t, files)
956
957 var buf bytes.Buffer
958 b.H.Metrics.WriteMetrics(&buf)
959 got := buf.String()
960
961 b.Assert(got, qt.Contains, "/docs/_content.gotmpl")
962 b.Assert(got, qt.Not(qt.Contains), "content/docs/_content.gotmpl")
963 }