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