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