Debian Codegraph @

hugolib/page_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 hugolib
15
16 import (
17 "context"
18 "fmt"
19 "html/template"
20 "path/filepath"
21 "strings"
22 "testing"
23 "time"
24
25 "github.com/bep/clocks"
26 "github.com/gohugoio/hugo/htesting"
27 "github.com/gohugoio/hugo/markup/asciidocext"
28 "github.com/gohugoio/hugo/markup/rst"
29 "github.com/gohugoio/hugo/tpl"
30 "github.com/spf13/cast"
31
32 "github.com/gohugoio/hugo/config"
33
34 "github.com/gohugoio/hugo/common/hashing"
35 "github.com/gohugoio/hugo/common/htime"
36
37 "github.com/gohugoio/hugo/resources/page"
38 "github.com/gohugoio/hugo/resources/resource"
39
40 qt "github.com/frankban/quicktest"
41 )
42
43 const (
44 homePage = "---\ntitle: Home\n---\nHome Page Content\n"
45 simplePage = "---\ntitle: Simple\n---\nSimple Page\n"
46
47 simplePageRFC3339Date = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content"
48
49 simplePageWithoutSummaryDelimiter = `---
50 title: SimpleWithoutSummaryDelimiter
51 ---
52 [Lorem ipsum](https://lipsum.com/) dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
53
54 Additional text.
55
56 Further text.
57 `
58
59 simplePageWithSummaryDelimiter = `---
60 title: Simple
61 ---
62 Summary Next Line
63
64 <!--more-->
65 Some more text
66 `
67
68 simplePageWithSummaryParameter = `---
69 title: SimpleWithSummaryParameter
70 summary: "Page with summary parameter and [a link](http://www.example.com/)"
71 ---
72
73 Some text.
74
75 Some more text.
76 `
77
78 simplePageWithSummaryDelimiterAndMarkdownThatCrossesBorder = `---
79 title: Simple
80 ---
81 The [best static site generator][hugo].[^1]
82 <!--more-->
83 [hugo]: http://gohugo.io/
84 [^1]: Many people say so.
85 `
86
87 simplePageWithSummaryDelimiterSameLine = `---
88 title: Simple
89 ---
90 Summary Same Line<!--more-->
91
92 Some more text
93 `
94
95 simplePageWithAllCJKRunes = `---
96 title: Simple
97 ---
98
99
100 € € € € €
101 你好
102 도형이
103 カテゴリー
104
105
106 `
107
108 simplePageWithMainEnglishWithCJKRunes = `---
109 title: Simple
110 ---
111
112
113 In Chinese, 好 means good. In Chinese, 好 means good.
114 In Chinese, 好 means good. In Chinese, 好 means good.
115 In Chinese, 好 means good. In Chinese, 好 means good.
116 In Chinese, 好 means good. In Chinese, 好 means good.
117 In Chinese, 好 means good. In Chinese, 好 means good.
118 In Chinese, 好 means good. In Chinese, 好 means good.
119 In Chinese, 好 means good. In Chinese, 好 means good.
120 More then 70 words.
121
122
123 `
124
125 simplePageWithIsCJKLanguageFalse = `---
126 title: Simple
127 isCJKLanguage: false
128 ---
129
130 In Chinese, 好的啊 means good. In Chinese, 好的呀 means good.
131 In Chinese, 好的啊 means good. In Chinese, 好的呀 means good.
132 In Chinese, 好的啊 means good. In Chinese, 好的呀 means good.
133 In Chinese, 好的啊 means good. In Chinese, 好的呀 means good.
134 In Chinese, 好的啊 means good. In Chinese, 好的呀 means good.
135 In Chinese, 好的啊 means good. In Chinese, 好的呀 means good.
136 In Chinese, 好的啊 means good. In Chinese, 好的呀呀 means good enough.
137 More then 70 words.
138
139
140 `
141
142 simplePageWithLongContent = `---
143 title: Simple
144 ---
145
146 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
147 incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
148 nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
149 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
150 fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
151 culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit
152 amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
153 et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
154 ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
155 in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
156 pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
157 officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet,
158 consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
159 dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
160 laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
161 reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
162 Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
163 deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur
164 adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
165 aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
166 ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
167 voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
168 occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim
169 id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
170 do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
171 veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
172 consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
173 cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
174 proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem
175 ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
176 incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
177 nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
178 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
179 fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
180 culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit
181 amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore
182 et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
183 ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
184 in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
185 pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
186 officia deserunt mollit anim id est laborum.`
187
188 pageWithToC = `---
189 title: TOC
190 ---
191 For some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.
192
193 ## AA
194
195 I have no idea, of course, how long it took me to reach the limit of the plain,
196 but at last I entered the foothills, following a pretty little canyon upward
197 toward the mountains. Beside me frolicked a laughing brooklet, hurrying upon
198 its noisy way down to the silent sea. In its quieter pools I discovered many
199 small fish, of four-or five-pound weight I should imagine. In appearance,
200 except as to size and color, they were not unlike the whale of our own seas. As
201 I watched them playing about I discovered, not only that they suckled their
202 young, but that at intervals they rose to the surface to breathe as well as to
203 feed upon certain grasses and a strange, scarlet lichen which grew upon the
204 rocks just above the water line.
205
206 ### AAA
207
208 I remember I felt an extraordinary persuasion that I was being played with,
209 that presently, when I was upon the very verge of safety, this mysterious
210 death--as swift as the passage of light--would leap after me from the pit about
211 the cylinder and strike me down. ## BB
212
213 ### BBB
214
215 "You're a great Granser," he cried delightedly, "always making believe them little marks mean something."
216 `
217
218 simplePageWithURL = `---
219 title: Simple
220 url: simple/url/
221 ---
222 Simple Page With URL`
223
224 simplePageWithSlug = `---
225 title: Simple
226 slug: simple-slug
227 ---
228 Simple Page With Slug`
229
230 simplePageWithDate = `---
231 title: Simple
232 date: '2013-10-15T06:16:13'
233 ---
234 Simple Page With Date`
235
236 UTF8Page = `---
237 title: ラーメン
238 ---
239 UTF8 Page`
240
241 UTF8PageWithURL = `---
242 title: ラーメン
243 url: ラーメン/url/
244 ---
245 UTF8 Page With URL`
246
247 UTF8PageWithSlug = `---
248 title: ラーメン
249 slug: ラーメン-slug
250 ---
251 UTF8 Page With Slug`
252
253 UTF8PageWithDate = `---
254 title: ラーメン
255 date: '2013-10-15T06:16:13'
256 ---
257 UTF8 Page With Date`
258 )
259
260 func checkPageTitle(t *testing.T, page page.Page, title string) {
261 if page.Title() != title {
262 t.Fatalf("Page title is: %s. Expected %s", page.Title(), title)
263 }
264 }
265
266 func checkPageContent(t *testing.T, page page.Page, expected string, msg ...any) {
267 t.Helper()
268 a := normalizeContent(expected)
269 b := normalizeContent(content(page))
270 if a != b {
271 t.Fatalf("Page content is:\n%q\nExpected:\n%q (%q)", b, a, msg)
272 }
273 }
274
275 func normalizeContent(c string) string {
276 norm := c
277 norm = strings.Replace(norm, "\n", " ", -1)
278 norm = strings.Replace(norm, " ", " ", -1)
279 norm = strings.Replace(norm, " ", " ", -1)
280 norm = strings.Replace(norm, " ", " ", -1)
281 norm = strings.Replace(norm, "p> ", "p>", -1)
282 norm = strings.Replace(norm, "> <", "> <", -1)
283 return strings.TrimSpace(norm)
284 }
285
286 func checkPageTOC(t *testing.T, page page.Page, toc string) {
287 t.Helper()
288 if page.TableOfContents(context.Background()) != template.HTML(toc) {
289 t.Fatalf("Page TableOfContents is:\n%q.\nExpected %q", page.TableOfContents(context.Background()), toc)
290 }
291 }
292
293 func checkPageSummary(t *testing.T, page page.Page, summary string, msg ...any) {
294 s := string(page.Summary(context.Background()))
295 a := normalizeContent(s)
296 b := normalizeContent(summary)
297 if a != b {
298 t.Fatalf("Page summary is:\n%q.\nExpected\n%q (%q)", a, b, msg)
299 }
300 }
301
302 func checkPageType(t *testing.T, page page.Page, pageType string) {
303 if page.Type() != pageType {
304 t.Fatalf("Page type is: %s. Expected: %s", page.Type(), pageType)
305 }
306 }
307
308 func checkPageDate(t *testing.T, page page.Page, time time.Time) {
309 if page.Date() != time {
310 t.Fatalf("Page date is: %s. Expected: %s", page.Date(), time)
311 }
312 }
313
314 func normalizeExpected(ext, str string) string {
315 str = normalizeContent(str)
316 switch ext {
317 default:
318 return str
319 case "html":
320 return strings.Trim(tpl.StripHTML(str), " ")
321 case "ad":
322 paragraphs := strings.Split(str, "</p>")
323 var expected strings.Builder
324 for _, para := range paragraphs {
325 if para == "" {
326 continue
327 }
328 expected.WriteString(fmt.Sprintf("<div class=\"paragraph\">\n%s</p></div>\n", para))
329 }
330
331 return expected.String()
332 case "rst":
333 if str == "" {
334 return "<div class=\"document\"></div>"
335 }
336 return fmt.Sprintf("<div class=\"document\">\n\n\n%s</div>", str)
337 }
338 }
339
340 func testAllMarkdownEnginesForPages(t *testing.T,
341 assertFunc func(t *testing.T, ext string, pages page.Pages), settings map[string]any, pageSources ...string,
342 ) {
343 engines := []struct {
344 ext string
345 shouldExecute func() bool
346 }{
347 {"md", func() bool { return true }},
348 {"ad", func() bool { ok, _ := asciidocext.Supports(); return ok }},
349 {"rst", func() bool { return !htesting.IsRealCI() && rst.Supports() }},
350 }
351
352 for _, e := range engines {
353 if !e.shouldExecute() {
354 continue
355 }
356
357 t.Run(e.ext, func(t *testing.T) {
358 cfg := config.New()
359 for k, v := range settings {
360 cfg.Set(k, v)
361 }
362
363 if s := cfg.GetString("contentDir"); s != "" && s != "content" {
364 panic("contentDir must be set to 'content' for this test")
365 }
366
367 var files strings.Builder
368 files.WriteString(`
369 -- hugo.toml --
370 [security]
371 [security.exec]
372 allow = ['^python$', '^rst2html.*', '^asciidoctor$']
373 `)
374
375 for i, source := range pageSources {
376 files.WriteString(fmt.Sprintf("-- content/p%d.%s --\n%s\n", i, e.ext, source))
377 }
378 homePath := fmt.Sprintf("_index.%s", e.ext)
379 files.WriteString(fmt.Sprintf("-- content/%s --\n%s\n", homePath, homePage))
380
381 b := Test(t, files.String(), TestOptOsFs(), TestOptWithConfig(func(c *IntegrationTestConfig) { c.BaseCfg = cfg }))
382
383 s := b.H.Sites[0]
384
385 b.Assert(len(s.RegularPages()), qt.Equals, len(pageSources))
386
387 assertFunc(t, e.ext, s.RegularPages())
388
389 home := s.Home()
390 b.Assert(home, qt.Not(qt.IsNil))
391 b.Assert(home.File().Path(), qt.Equals, homePath)
392 b.Assert(content(home), qt.Contains, "Home Page Content")
393 })
394
395 }
396 }
397
398 // Issue #1076
399 func TestPageWithDelimiterForMarkdownThatCrossesBorder(t *testing.T) {
400 t.Parallel()
401
402 files := `
403 -- hugo.toml --
404 baseURL = "http://example.com/"
405 -- content/simple.md --
406 ` + simplePageWithSummaryDelimiterAndMarkdownThatCrossesBorder + `
407 `
408 b := Test(t, files, TestOptSkipRender())
409
410 b.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 1)
411
412 p := b.H.Sites[0].RegularPages()[0]
413
414 b.Assert(p.Summary(context.Background()), qt.Equals, template.HTML(
415 "<p>The <a href=\"http://gohugo.io/\">best static site generator</a>.<sup id=\"fnref:1\"><a href=\"#fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup></p>",
416 ))
417
418 cnt := content(p)
419 b.Assert(cnt, qt.Equals, "<p>The <a href=\"http://gohugo.io/\">best static site generator</a>.<sup id=\"fnref:1\"><a href=\"#fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup></p>\n<div class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"fn:1\">\n<p>Many people say so.&#160;<a href=\"#fnref:1\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;</a></p>\n</li>\n</ol>\n</div>")
420 }
421
422 func TestPageDatesTerms(t *testing.T) {
423 t.Parallel()
424
425 files := `
426 -- hugo.toml --
427 baseURL = "http://example.com/"
428 -- content/p1.md --
429 ---
430 title: p1
431 date: 2022-01-15
432 lastMod: 2022-01-16
433 tags: ["a", "b"]
434 categories: ["c", "d"]
435 ---
436 p1
437 -- content/p2.md --
438 ---
439 title: p2
440 date: 2017-01-16
441 lastMod: 2017-01-17
442 tags: ["a", "c"]
443 categories: ["c", "e"]
444 ---
445 p2
446 -- layouts/list.html --
447 {{ .Title }}|Date: {{ .Date.Format "2006-01-02" }}|Lastmod: {{ .Lastmod.Format "2006-01-02" }}|
448
449 `
450 b := Test(t, files)
451
452 b.AssertFileContent("public/categories/index.html", "Categories|Date: 2022-01-15|Lastmod: 2022-01-16|")
453 b.AssertFileContent("public/categories/c/index.html", "C|Date: 2022-01-15|Lastmod: 2022-01-16|")
454 b.AssertFileContent("public/categories/e/index.html", "E|Date: 2017-01-16|Lastmod: 2017-01-17|")
455 b.AssertFileContent("public/tags/index.html", "Tags|Date: 2022-01-15|Lastmod: 2022-01-16|")
456 b.AssertFileContent("public/tags/a/index.html", "A|Date: 2022-01-15|Lastmod: 2022-01-16|")
457 b.AssertFileContent("public/tags/c/index.html", "C|Date: 2017-01-16|Lastmod: 2017-01-17|")
458 }
459
460 func TestPageDatesAllKinds(t *testing.T) {
461 t.Parallel()
462
463 pageContent := `
464 ---
465 title: Page
466 date: 2017-01-15
467 tags: ["hugo"]
468 categories: ["cool stuff"]
469 ---
470 `
471
472 files := `
473 -- hugo.toml --
474 baseURL = "http://example.com/"
475 -- content/page.md --
476 ` + pageContent + `
477 -- content/blog/page.md --
478 ` + pageContent + `
479 `
480
481 b := Test(t, files)
482
483 b.Assert(len(b.H.Sites), qt.Equals, 1)
484 s := b.H.Sites[0]
485
486 checkDate := func(t time.Time, msg string) {
487 b.Helper()
488 b.Assert(t.Year(), qt.Equals, 2017, qt.Commentf(msg))
489 }
490
491 checkDated := func(d resource.Dated, msg string) {
492 b.Helper()
493 checkDate(d.Date(), "date: "+msg)
494 checkDate(d.Lastmod(), "lastmod: "+msg)
495 }
496 for _, p := range s.Pages() {
497 checkDated(p, p.Kind())
498 }
499 checkDate(s.Lastmod(), "site")
500 }
501
502 func TestPageDatesSections(t *testing.T) {
503 t.Parallel()
504
505 var files strings.Builder
506 files.WriteString(`
507 -- hugo.toml --
508 baseURL = "http://example.com/"
509 -- content/no-index/page.md --
510 ---
511 title: Page
512 date: 2017-01-15
513 ---
514 -- content/with-index-no-date/_index.md --
515 ---
516 title: No Date
517 ---
518 -- content/with-index-date/_index.md --
519 ---
520 title: Date
521 date: 2018-01-15
522 ---
523 -- content/with-index-date/p1.md --
524 ---
525 title: Date
526 date: 2018-01-15
527 ---
528 -- content/with-index-date/p2.md --
529 ---
530 title: Date
531 date: 2018-01-15
532 ---
533 `)
534 for i := 1; i <= 20; i++ {
535 files.WriteString(fmt.Sprintf(`
536 -- content/main-section/p%d.md --
537 ---
538 title: Date
539 date: 2012-01-12
540 ---
541 `, i))
542 }
543
544 b := Test(t, files.String())
545
546 b.Assert(len(b.H.Sites), qt.Equals, 1)
547 s := b.H.Sites[0]
548
549 checkDate := func(p page.Page, year int) {
550 b.Assert(p.Date().Year(), qt.Equals, year)
551 b.Assert(p.Lastmod().Year(), qt.Equals, year)
552 }
553
554 checkDate(s.getPageOldVersion("/"), 2018)
555 checkDate(s.getPageOldVersion("/no-index"), 2017)
556 b.Assert(s.getPageOldVersion("/with-index-no-date").Date().IsZero(), qt.Equals, true)
557 checkDate(s.getPageOldVersion("/with-index-date"), 2018)
558
559 b.Assert(s.Site().Lastmod().Year(), qt.Equals, 2018)
560 }
561
562 func TestPageSummary(t *testing.T) {
563 t.Parallel()
564 htesting.SkipSlowTestUnlessCI(t)
565 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
566 p := pages[0]
567 checkPageTitle(t, p, "SimpleWithoutSummaryDelimiter")
568 // Source is not AsciiDoc- or RST-compatible so don't test them
569 if ext != "ad" && ext != "rst" {
570 checkPageContent(t, p, normalizeExpected(ext, "<p><a href=\"https://lipsum.com/\">Lorem ipsum</a> dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>\n\n<p>Additional text.</p>\n\n<p>Further text.</p>\n"), ext)
571 checkPageSummary(t, p, normalizeExpected(ext, "<p><a href=\"https://lipsum.com/\">Lorem ipsum</a> dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p><p>Additional text.</p>"), ext)
572 }
573 checkPageType(t, p, "page")
574 }
575
576 testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithoutSummaryDelimiter)
577 }
578
579 func TestPageWithDelimiter(t *testing.T) {
580 htesting.SkipSlowTestUnlessCI(t)
581 t.Parallel()
582 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
583 p := pages[0]
584 checkPageTitle(t, p, "Simple")
585 checkPageContent(t, p, normalizeExpected(ext, "<p>Summary Next Line</p>\n\n<p>Some more text</p>\n"), ext)
586 checkPageSummary(t, p, normalizeExpected(ext, "<p>Summary Next Line</p>"), ext)
587 checkPageType(t, p, "page")
588 }
589
590 testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithSummaryDelimiter)
591 }
592
593 func TestPageWithSummaryParameter(t *testing.T) {
594 t.Parallel()
595 htesting.SkipSlowTestUnlessCI(t)
596 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
597 p := pages[0]
598 checkPageTitle(t, p, "SimpleWithSummaryParameter")
599 checkPageContent(t, p, normalizeExpected(ext, "<p>Some text.</p>\n\n<p>Some more text.</p>\n"), ext)
600 // Summary is not AsciiDoc- or RST-compatible so don't test them
601 if ext != "ad" && ext != "rst" {
602 checkPageSummary(t, p, normalizeExpected(ext, "Page with summary parameter and <a href=\"http://www.example.com/\">a link</a>"), ext)
603 }
604 checkPageType(t, p, "page")
605 }
606
607 testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithSummaryParameter)
608 }
609
610 // Issue #3854
611 // Also see https://github.com/gohugoio/hugo/issues/3977
612 func TestPageWithDateFields(t *testing.T) {
613 t.Parallel()
614 htesting.SkipSlowTestUnlessCI(t)
615
616 c := qt.New(t)
617 pageWithDate := `---
618 title: P%d
619 weight: %d
620 %s: 2017-10-13
621 ---
622 Simple Page With Some Date`
623
624 hasDate := func(p page.Page) bool {
625 return p.Date().Year() == 2017
626 }
627
628 datePage := func(field string, weight int) string {
629 return fmt.Sprintf(pageWithDate, weight, weight, field)
630 }
631
632 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
633 c.Assert(len(pages) > 0, qt.Equals, true)
634 for _, p := range pages {
635 c.Assert(hasDate(p), qt.Equals, true)
636 }
637 }
638
639 fields := []string{"date", "publishdate", "pubdate", "published"}
640 pageContents := make([]string, len(fields))
641 for i, field := range fields {
642 pageContents[i] = datePage(field, i+1)
643 }
644
645 testAllMarkdownEnginesForPages(t, assertFunc, nil, pageContents...)
646 }
647
648 func TestPageRawContent(t *testing.T) {
649 files := `
650 -- hugo.toml --
651 -- content/basic.md --
652 ---
653 title: "basic"
654 ---
655 **basic**
656 -- content/empty.md --
657 ---
658 title: "empty"
659 ---
660 -- layouts/single.html --
661 |{{ .RawContent }}|
662 `
663
664 b := Test(t, files)
665
666 b.AssertFileContent("public/basic/index.html", "|**basic**|")
667 b.AssertFileContent("public/empty/index.html", "! title")
668 }
669
670 func TestTableOfContents(t *testing.T) {
671 t.Parallel()
672
673 files := `
674 -- hugo.toml --
675 baseURL = "http://example.com/"
676 -- content/tocpage.md --
677 ` + pageWithToC + `
678 `
679 b := Test(t, files, TestOptSkipRender())
680
681 b.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 1)
682
683 p := b.H.Sites[0].RegularPages()[0]
684
685 checkPageContent(t, p, "<p>For some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.</p><h2 id=\"aa\">AA</h2> <p>I have no idea, of course, how long it took me to reach the limit of the plain, but at last I entered the foothills, following a pretty little canyon upward toward the mountains. Beside me frolicked a laughing brooklet, hurrying upon its noisy way down to the silent sea. In its quieter pools I discovered many small fish, of four-or five-pound weight I should imagine. In appearance, except as to size and color, they were not unlike the whale of our own seas. As I watched them playing about I discovered, not only that they suckled their young, but that at intervals they rose to the surface to breathe as well as to feed upon certain grasses and a strange, scarlet lichen which grew upon the rocks just above the water line.</p><h3 id=\"aaa\">AAA</h3> <p>I remember I felt an extraordinary persuasion that I was being played with, that presently, when I was upon the very verge of safety, this mysterious death&ndash;as swift as the passage of light&ndash;would leap after me from the pit about the cylinder and strike me down. ## BB</p><h3 id=\"bbb\">BBB</h3> <p>&ldquo;You&rsquo;re a great Granser,&rdquo; he cried delightedly, &ldquo;always making believe them little marks mean something.&rdquo;</p>")
686 checkPageTOC(t, p, "<nav id=\"TableOfContents\">\n <ul>\n <li><a href=\"#aa\">AA</a>\n <ul>\n <li><a href=\"#aaa\">AAA</a></li>\n <li><a href=\"#bbb\">BBB</a></li>\n </ul>\n </li>\n </ul>\n</nav>")
687 }
688
689 func TestPageWithMoreTag(t *testing.T) {
690 htesting.SkipSlowTestUnlessCI(t)
691 t.Parallel()
692 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
693 p := pages[0]
694 checkPageTitle(t, p, "Simple")
695 checkPageContent(t, p, normalizeExpected(ext, "<p>Summary Same Line</p>\n\n<p>Some more text</p>\n"))
696 checkPageSummary(t, p, normalizeExpected(ext, "<p>Summary Same Line</p>"))
697 checkPageType(t, p, "page")
698 }
699
700 testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithSummaryDelimiterSameLine)
701 }
702
703 func TestSummaryInFrontMatter(t *testing.T) {
704 t.Parallel()
705 Test(t, `
706 -- hugo.toml --
707 -- content/simple.md --
708 ---
709 title: Simple
710 summary: "Front **matter** summary"
711 ---
712 Simple Page
713 -- layouts/single.html --
714 Summary: {{ .Summary }}|Truncated: {{ .Truncated }}|
715
716 `).AssertFileContent("public/simple/index.html", "Summary: Front <strong>matter</strong> summary|", "Truncated: false")
717 }
718
719 func TestSummaryManualSplit(t *testing.T) {
720 t.Parallel()
721 Test(t, `
722 -- hugo.toml --
723 -- content/simple.md --
724 ---
725 title: Simple
726 ---
727 This is **summary**.
728 <!--more-->
729 This is **content**.
730 -- layouts/single.html --
731 Summary: {{ .Summary }}|Truncated: {{ .Truncated }}|
732 Content: {{ .Content }}|
733
734 `).AssertFileContent(
735 "public/simple/index.html",
736 "Summary: <p>This is <strong>summary</strong>.</p>|",
737 "Truncated: true|",
738 "Content: <p>This is <strong>summary</strong>.</p>\n<p>This is <strong>content</strong>.</p>|",
739 )
740 }
741
742 func TestSummaryManualSplitHTML(t *testing.T) {
743 t.Parallel()
744 Test(t, `
745 -- hugo.toml --
746 [security]
747 allowContent = ['.*']
748 -- content/simple.html --
749 ---
750 title: Simple
751 ---
752 <div>
753 This is <b>summary</b>.
754 </div>
755 <!--more-->
756 <div>
757 This is <b>content</b>.
758 </div>
759 -- layouts/single.html --
760 Summary: {{ .Summary }}|Truncated: {{ .Truncated }}|
761 Content: {{ .Content }}|
762
763 `).AssertFileContent("public/simple/index.html", "Summary: <div>\nThis is <b>summary</b>.\n</div>\n|Truncated: true|\nContent: \n\n<div>\nThis is <b>content</b>.\n</div>|")
764 }
765
766 func TestSummaryAuto(t *testing.T) {
767 t.Parallel()
768 Test(t, `
769 -- hugo.toml --
770 summaryLength = 10
771 -- content/simple.md --
772 ---
773 title: Simple
774 ---
775 This is **summary**.
776 This is **more summary**.
777 This is *even more summary**.
778 This is **more summary**.
779
780 This is **content**.
781 -- layouts/single.html --
782 Summary: {{ .Summary }}|Truncated: {{ .Truncated }}|
783 Content: {{ .Content }}|
784
785 `).AssertFileContent("public/simple/index.html",
786 "Summary: <p>This is <strong>summary</strong>.\nThis is <strong>more summary</strong>.\nThis is <em>even more summary</em>*.\nThis is <strong>more summary</strong>.</p>|",
787 "Truncated: true|",
788 "Content: <p>This is <strong>summary</strong>.")
789 }
790
791 // See issue 14044.
792 func TestSummaryAutoBalancesContainerTags(t *testing.T) {
793 t.Parallel()
794
795 files := `
796 -- hugo.toml --
797 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
798 summaryLength = 1
799 -- content/p1.md --
800 ---
801 title: p1
802 ---
803 > foo
804 -- content/p2.md --
805 ---
806 title: p2
807 ---
808 - item 1 line 1
809
810 item 1 line 2
811 -- layouts/page.html --
812 |{{ .Summary }}|
813 `
814
815 b := Test(t, files)
816
817 b.AssertFileContent("public/p1/index.html", "|<blockquote>\n<p>foo</p>\n</blockquote>|")
818 b.AssertFileContent("public/p2/index.html", "|<ul>\n<li>\n<p>item 1 line 1</p>\n<p>item 1 line 2</p>\n</li>\n</ul>|")
819 }
820
821 // #2973
822 func TestSummaryWithHTMLTagsOnNextLine(t *testing.T) {
823 htesting.SkipSlowTestUnlessCI(t)
824 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
825 c := qt.New(t)
826 p := pages[0]
827 s := string(p.Summary(context.Background()))
828 c.Assert(s, qt.Contains, "Happy new year everyone!")
829 c.Assert(s, qt.Not(qt.Contains), "User interface")
830 }
831
832 testAllMarkdownEnginesForPages(t, assertFunc, nil, `---
833 title: Simple
834 ---
835 Happy new year everyone!
836
837 Here is the last report for commits in the year 2016. It covers hrev50718-hrev50829.
838
839 <!--more-->
840
841 <h3>User interface</h3>
842
843 `)
844 }
845
846 // Issue 9383
847 func TestRenderStringForRegularPageTranslations(t *testing.T) {
848 t.Parallel()
849
850 files := `
851 -- hugo.toml --
852 baseurl = "https://example.org/"
853 title = "My Site"
854
855 defaultContentLanguage = "ru"
856 defaultContentLanguageInSubdir = true
857
858 [languages.ru]
859 contentDir = 'content/ru'
860 weight = 1
861
862 [languages.en]
863 weight = 2
864 contentDir = 'content/en'
865
866 [outputs]
867 home = ["HTML", "JSON"]
868 -- layouts/home.html --
869 {{- range .Site.Home.Translations -}}
870 <p>{{- .RenderString "foo" -}}</p>
871 {{- end -}}
872 {{- range .Site.Home.AllTranslations -}}
873 <p>{{- .RenderString "bar" -}}</p>
874 {{- end -}}
875 -- layouts/single.html --
876 {{ .Content }}
877 -- layouts/index.json --
878 {"Title": "My Site"}
879 -- content/ru/a.md --
880 -- content/en/a.md --
881 `
882 b := Test(t, files)
883
884 b.AssertFileContent("public/ru/index.html", `
885 <p>foo</p>
886 <p>foo</p>
887 <p>bar</p>
888 <p>bar</p>
889 `)
890
891 b.AssertFileContent("public/en/index.html", `
892 <p>foo</p>
893 <p>foo</p>
894 <p>bar</p>
895 <p>bar</p>
896 `)
897 }
898
899 // Issue 8919
900 func TestContentProviderWithCustomOutputFormat(t *testing.T) {
901 t.Parallel()
902 htesting.SkipSlowTestUnlessCI(t)
903
904 files := `
905 -- hugo.toml --
906 baseURL = 'http://example.org/'
907 title = 'My New Hugo Project'
908
909 timeout = 600000 # ten minutes in case we want to pause and debug
910
911 defaultContentLanguage = "en"
912
913 [languages]
914 [languages.en]
915 title = "Repro"
916 label = "English"
917 contentDir = "content/en"
918
919 [languages.zh_CN]
920 title = "Repro"
921 label = "简体中文"
922 contentDir = "content/zh_CN"
923
924 [outputFormats]
925 [outputFormats.metadata]
926 baseName = "metadata"
927 mediaType = "text/html"
928 isPlainText = true
929 notAlternative = true
930
931 [outputs]
932 home = ["HTML", "metadata"]
933 -- layouts/home.metadata.html --
934 <h2>Translations metadata</h2>
935 <ul>
936 {{ $p := .Page }}
937 {{ range $p.Translations}}
938 <li>Title: {{ .Title }}, {{ .Summary }}</li>
939 <li>Content: {{ .Content }}</li>
940 <li>Plain: {{ .Plain }}</li>
941 <li>PlainWords: {{ .PlainWords }}</li>
942 <li>Summary: {{ .Summary }}</li>
943 <li>Truncated: {{ .Truncated }}</li>
944 <li>FuzzyWordCount: {{ .FuzzyWordCount }}</li>
945 <li>ReadingTime: {{ .ReadingTime }}</li>
946 <li>Len: {{ .Len }}</li>
947 {{ end }}
948 </ul>
949 -- layouts/baseof.html --
950 <html>
951
952 <body>
953 {{ block "main" . }}{{ end }}
954 </body>
955
956 </html>
957 -- layouts/home.html --
958 {{ define "main" }}
959 <h2>Translations</h2>
960 <ul>
961 {{ $p := .Page }}
962 {{ range $p.Translations}}
963 <li>Title: {{ .Title }}, {{ .Summary }}</li>
964 <li>Content: {{ .Content }}</li>
965 <li>Plain: {{ .Plain }}</li>
966 <li>PlainWords: {{ .PlainWords }}</li>
967 <li>Summary: {{ .Summary }}</li>
968 <li>Truncated: {{ .Truncated }}</li>
969 <li>FuzzyWordCount: {{ .FuzzyWordCount }}</li>
970 <li>ReadingTime: {{ .ReadingTime }}</li>
971 <li>Len: {{ .Len }}</li>
972 {{ end }}
973 </ul>
974 {{ end }}
975 -- content/en/_index.md --
976 ---
977 title: Title (en)
978 summary: Summary (en)
979 ---
980
981 Here is some content.
982 -- content/zh_CN/_index.md --
983 ---
984 title: Title (zh)
985 summary: Summary (zh)
986 ---
987
988 这是一些内容
989 `
990 b := Test(t, files)
991
992 b.AssertFileContent("public/index.html", `<html>
993
994 <body>
995
996 <h2>Translations</h2>
997 <ul>
998
999
1000 <li>Title: Title (zh), Summary (zh)</li>
1001 <li>Content: <p>这是一些内容</p>
1002 </li>
1003 <li>Plain: 这是一些内容
1004 </li>
1005 <li>PlainWords: [这是一些内容]</li>
1006 <li>Summary: Summary (zh)</li>
1007 <li>Truncated: false</li>
1008 <li>FuzzyWordCount: 100</li>
1009 <li>ReadingTime: 1</li>
1010 <li>Len: 26</li>
1011
1012 </ul>
1013
1014 </body>
1015
1016 </html>`)
1017 b.AssertFileContent("public/metadata.html", `<h2>Translations metadata</h2>
1018 <ul>
1019
1020
1021 <li>Title: Title (zh), Summary (zh)</li>
1022 <li>Content: <p>这是一些内容</p>
1023 </li>
1024 <li>Plain: 这是一些内容
1025 </li>
1026 <li>PlainWords: [这是一些内容]</li>
1027 <li>Summary: Summary (zh)</li>
1028 <li>Truncated: false</li>
1029 <li>FuzzyWordCount: 100</li>
1030 <li>ReadingTime: 1</li>
1031 <li>Len: 26</li>
1032
1033 </ul>`)
1034 b.AssertFileContent("public/zh_cn/index.html", `<html>
1035
1036 <body>
1037
1038 <h2>Translations</h2>
1039 <ul>
1040
1041
1042 <li>Title: Title (en), Summary (en)</li>
1043 <li>Content: <p>Here is some content.</p>
1044 </li>
1045 <li>Plain: Here is some content.
1046 </li>
1047 <li>PlainWords: [Here is some content.]</li>
1048 <li>Summary: Summary (en)</li>
1049 <li>Truncated: false</li>
1050 <li>FuzzyWordCount: 100</li>
1051 <li>ReadingTime: 1</li>
1052 <li>Len: 29</li>
1053
1054 </ul>
1055
1056 </body>
1057
1058 </html>`)
1059 b.AssertFileContent("public/zh_cn/metadata.html", `<h2>Translations metadata</h2>
1060 <ul>
1061
1062
1063 <li>Title: Title (en), Summary (en)</li>
1064 <li>Content: <p>Here is some content.</p>
1065 </li>
1066 <li>Plain: Here is some content.
1067 </li>
1068 <li>PlainWords: [Here is some content.]</li>
1069 <li>Summary: Summary (en)</li>
1070 <li>Truncated: false</li>
1071 <li>FuzzyWordCount: 100</li>
1072 <li>ReadingTime: 1</li>
1073 <li>Len: 29</li>
1074
1075 </ul>`)
1076 }
1077
1078 func TestPageWithDate(t *testing.T) {
1079 t.Parallel()
1080
1081 files := `
1082 -- hugo.toml --
1083 baseURL = "http://example.com/"
1084 -- content/simple.md --
1085 ` + simplePageRFC3339Date + `
1086 `
1087 b := Test(t, files, TestOptSkipRender())
1088
1089 b.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 1)
1090
1091 p := b.H.Sites[0].RegularPages()[0]
1092 d, _ := time.Parse(time.RFC3339, "2013-05-17T16:59:30Z")
1093
1094 checkPageDate(t, p, d)
1095 }
1096
1097 func TestPageWithFrontMatterConfig(t *testing.T) {
1098 for _, dateHandler := range []string{":filename", ":fileModTime"} {
1099 t.Run(fmt.Sprintf("dateHandler=%q", dateHandler), func(t *testing.T) {
1100 t.Parallel()
1101
1102 pageTemplate := `
1103 ---
1104 title: Page
1105 weight: %d
1106 lastMod: 2018-02-28
1107 %s
1108 ---
1109 Content
1110 `
1111 files := `
1112 -- hugo.toml --
1113 baseURL = "http://example.com/"
1114 [frontmatter]
1115 date = ["` + dateHandler + `", "date"]
1116 -- content/section/2012-02-21-noslug.md --
1117 ` + fmt.Sprintf(pageTemplate, 1, "") + `
1118 -- content/section/2012-02-22-slug.md --
1119 ` + fmt.Sprintf(pageTemplate, 2, "slug: aslug") + `
1120 `
1121 b := Test(t, files, TestOptOsFs())
1122
1123 s := b.H.Sites[0]
1124 b.Assert(len(s.RegularPages()), qt.Equals, 2)
1125
1126 noSlug := s.RegularPages()[0]
1127 slug := s.RegularPages()[1]
1128
1129 b.Assert(noSlug.Lastmod().Day(), qt.Equals, 28)
1130
1131 switch strings.ToLower(dateHandler) {
1132 case ":filename":
1133 b.Assert(noSlug.Date().IsZero(), qt.Equals, false)
1134 b.Assert(slug.Date().IsZero(), qt.Equals, false)
1135 b.Assert(noSlug.Date().Year(), qt.Equals, 2012)
1136 b.Assert(slug.Date().Year(), qt.Equals, 2012)
1137 b.Assert(noSlug.Slug(), qt.Equals, "noslug")
1138 b.Assert(slug.Slug(), qt.Equals, "aslug")
1139 case ":filemodtime":
1140 // For fileModTime, we need to get the actual file mod time.
1141 // The IntegrationTestBuilder creates a temporary directory.
1142 // We need to get the path to the created files.
1143 // The `b.Fs` field gives access to the file system.
1144 c1Path := filepath.Join(b.Cfg.WorkingDir, "content", "section", "2012-02-21-noslug.md")
1145 c2Path := filepath.Join(b.Cfg.WorkingDir, "content", "section", "2012-02-22-slug.md")
1146
1147 c1fi, err := b.fs.Source.Stat(c1Path)
1148 b.Assert(err, qt.IsNil)
1149 c2fi, err := b.fs.Source.Stat(c2Path)
1150 b.Assert(err, qt.IsNil)
1151
1152 b.Assert(noSlug.Date().Year(), qt.Equals, c1fi.ModTime().Year())
1153 b.Assert(slug.Date().Year(), qt.Equals, c2fi.ModTime().Year())
1154 fallthrough
1155 default:
1156 b.Assert(noSlug.Slug(), qt.Equals, "")
1157 b.Assert(slug.Slug(), qt.Equals, "aslug")
1158
1159 }
1160 })
1161 }
1162 }
1163
1164 func TestWordCountWithAllCJKRunesWithoutHasCJKLanguage(t *testing.T) {
1165 t.Parallel()
1166 htesting.SkipSlowTestUnlessCI(t)
1167 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
1168 p := pages[0]
1169 if p.WordCount(context.Background()) != 8 {
1170 t.Fatalf("[%s] incorrect word count. expected %v, got %v", ext, 8, p.WordCount(context.Background()))
1171 }
1172 }
1173
1174 testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithAllCJKRunes)
1175 }
1176
1177 func TestWordCountWithAllCJKRunesHasCJKLanguage(t *testing.T) {
1178 t.Parallel()
1179 htesting.SkipSlowTestUnlessCI(t)
1180 settings := map[string]any{"hasCJKLanguage": true}
1181
1182 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
1183 p := pages[0]
1184 if p.WordCount(context.Background()) != 15 {
1185 t.Fatalf("[%s] incorrect word count, expected %v, got %v", ext, 15, p.WordCount(context.Background()))
1186 }
1187 }
1188 testAllMarkdownEnginesForPages(t, assertFunc, settings, simplePageWithAllCJKRunes)
1189 }
1190
1191 func TestWordCountWithMainEnglishWithCJKRunes(t *testing.T) {
1192 t.Parallel()
1193 htesting.SkipSlowTestUnlessCI(t)
1194 settings := map[string]any{"hasCJKLanguage": true}
1195
1196 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
1197 p := pages[0]
1198 if p.WordCount(context.Background()) != 74 {
1199 t.Fatalf("[%s] incorrect word count, expected %v, got %v", ext, 74, p.WordCount(context.Background()))
1200 }
1201 }
1202
1203 testAllMarkdownEnginesForPages(t, assertFunc, settings, simplePageWithMainEnglishWithCJKRunes)
1204 }
1205
1206 func TestWordCountWithIsCJKLanguageFalse(t *testing.T) {
1207 t.Parallel()
1208 htesting.SkipSlowTestUnlessCI(t)
1209 settings := map[string]any{
1210 "hasCJKLanguage": true,
1211 }
1212
1213 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
1214 p := pages[0]
1215 if p.WordCount(context.Background()) != 75 {
1216 t.Fatalf("[%s] incorrect word count for content '%s'. expected %v, got %v", ext, p.Plain(context.Background()), 74, p.WordCount(context.Background()))
1217 }
1218 }
1219
1220 testAllMarkdownEnginesForPages(t, assertFunc, settings, simplePageWithIsCJKLanguageFalse)
1221 }
1222
1223 func TestWordCount(t *testing.T) {
1224 t.Parallel()
1225 htesting.SkipSlowTestUnlessCI(t)
1226 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
1227 p := pages[0]
1228 if p.WordCount(context.Background()) != 483 {
1229 t.Fatalf("[%s] incorrect word count. expected %v, got %v", ext, 483, p.WordCount(context.Background()))
1230 }
1231
1232 if p.FuzzyWordCount(context.Background()) != 500 {
1233 t.Fatalf("[%s] incorrect word count. expected %v, got %v", ext, 500, p.FuzzyWordCount(context.Background()))
1234 }
1235
1236 if p.ReadingTime(context.Background()) != 3 {
1237 t.Fatalf("[%s] incorrect min read. expected %v, got %v", ext, 3, p.ReadingTime(context.Background()))
1238 }
1239 }
1240
1241 testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithLongContent)
1242 }
1243
1244 func TestPagePaths(t *testing.T) {
1245 t.Parallel()
1246
1247 siteParmalinksSetting := `
1248 [permalinks]
1249 post = ":year/:month/:day/:title/"
1250 `
1251
1252 tests := []struct {
1253 content string
1254 path string
1255 hasPermalink bool
1256 expected string
1257 }{
1258 {simplePage, "post/x.md", false, "/post/x/"},
1259 {simplePageWithURL, "post/x.md", false, "/simple/url/"},
1260 {simplePageWithSlug, "post/x.md", false, "/post/simple-slug/"},
1261 {simplePageWithDate, "post/x.md", true, "/2013/10/15/simple/"},
1262 {UTF8Page, "post/x.md", false, "/post/x/"},
1263 {UTF8PageWithURL, "post/x.md", false, "/%E3%83%A9%E3%83%BC%E3%83%A1%E3%83%B3/url/"},
1264 {UTF8PageWithSlug, "post/x.md", false, "/post/%E3%83%A9%E3%83%BC%E3%83%A1%E3%83%B3-slug/"},
1265 {UTF8PageWithDate, "post/x.md", true, "/2013/10/15/%E3%83%A9%E3%83%BC%E3%83%A1%E3%83%B3/"},
1266 }
1267
1268 for i, test := range tests {
1269 t.Run(fmt.Sprintf("Test%d", i), func(t *testing.T) {
1270 t.Parallel()
1271
1272 configContent := `baseURL = "http://example.com/"`
1273 if test.hasPermalink {
1274 configContent += siteParmalinksSetting
1275 }
1276
1277 files := `
1278 -- hugo.toml --
1279 ` + configContent + `
1280 -- content/` + test.path + ` --
1281 ` + test.content + `
1282 `
1283 b := Test(t, files, TestOptSkipRender())
1284
1285 s := b.H.Sites[0]
1286 b.Assert(len(s.RegularPages()), qt.Equals, 1)
1287 p := s.RegularPages()[0]
1288 b.Assert(p.RelPermalink(), qt.Equals, test.expected)
1289 })
1290 }
1291 }
1292
1293 func TestTranslationKey(t *testing.T) {
1294 files := `
1295 -- hugo.toml --
1296 disableKinds = ["taxonomy", "term"]
1297 defaultContentLanguage = "en"
1298 defaultContentLanguageInSubdir = true
1299 [languages]
1300 [languages.en]
1301 weight = 1
1302 [languages.nn]
1303 weight = 2
1304 -- content/sect/p1.en.md --
1305 ---
1306 translationkey: "adfasdf"
1307 title: "p1 en"
1308 ---
1309 -- content/sect/p1.nn.md --
1310 ---
1311 translationkey: "adfasdf"
1312 title: "p1 nn"
1313 ---
1314 -- layouts/single.html --
1315 Title: {{ .Title }}|TranslationKey: {{ .TranslationKey }}|
1316 Translations: {{ range .Translations }}{{ .Language.Lang }}|{{ end }}|
1317 AllTranslations: {{ range .AllTranslations }}{{ .Language.Lang }}|{{ end }}|
1318
1319 `
1320 b := Test(t, files)
1321 b.AssertFileContent(
1322 "public/en/sect/p1/index.html",
1323 "TranslationKey: adfasdf|",
1324 "AllTranslations: en|nn||",
1325 "Translations: nn||",
1326 )
1327
1328 b.AssertFileContent(
1329 "public/nn/sect/p1/index.html",
1330 "TranslationKey: adfasdf|",
1331 "Translations: en||",
1332 "AllTranslations: en|nn||",
1333 )
1334 }
1335
1336 func TestTranslationKeyTermPages(t *testing.T) {
1337 t.Parallel()
1338
1339 files := `
1340 -- hugo.toml --
1341 disableKinds = ['home','rss','section','sitemap','taxonomy']
1342 defaultContentLanguage = 'en'
1343 defaultContentLanguageInSubdir = true
1344 [languages.en]
1345 weight = 1
1346 [languages.pt]
1347 weight = 2
1348 [taxonomies]
1349 category = 'categories'
1350 -- layouts/list.html --
1351 {{ .IsTranslated }}|{{ range .Translations }}{{ .RelPermalink }}|{{ end }}
1352 -- layouts/single.html --
1353 {{ .Title }}|
1354 -- content/p1.en.md --
1355 ---
1356 title: p1 (en)
1357 categories: [music]
1358 ---
1359 -- content/p1.pt.md --
1360 ---
1361 title: p1 (pt)
1362 categories: [música]
1363 ---
1364 -- content/categories/music/_index.en.md --
1365 ---
1366 title: music
1367 translationKey: foo
1368 ---
1369 -- content/categories/música/_index.pt.md --
1370 ---
1371 title: música
1372 translationKey: foo
1373 ---
1374 `
1375
1376 b := Test(t, files)
1377 b.AssertFileContent("public/en/categories/music/index.html", "true|/pt/categories/m%C3%BAsica/|")
1378 b.AssertFileContent("public/pt/categories/música/index.html", "true|/en/categories/music/|")
1379 }
1380
1381 // Issue #11540.
1382 func TestTranslationKeyResourceSharing(t *testing.T) {
1383 files := `
1384 -- hugo.toml --
1385 disableKinds = ["taxonomy", "term"]
1386 defaultContentLanguage = "en"
1387 defaultContentLanguageInSubdir = true
1388 [languages]
1389 [languages.en]
1390 weight = 1
1391 [languages.nn]
1392 weight = 2
1393 -- content/sect/mybundle_en/index.en.md --
1394 ---
1395 translationkey: "adfasdf"
1396 title: "mybundle en"
1397 ---
1398 -- content/sect/mybundle_en/f1.txt --
1399 f1.en
1400 -- content/sect/mybundle_en/f2.txt --
1401 f2.en
1402 -- content/sect/mybundle_nn/index.nn.md --
1403 ---
1404 translationkey: "adfasdf"
1405 title: "mybundle nn"
1406 ---
1407 -- content/sect/mybundle_nn/f2.nn.txt --
1408 f2.nn
1409 -- layouts/single.html --
1410 Title: {{ .Title }}|TranslationKey: {{ .TranslationKey }}|
1411 Resources: {{ range .Resources }}{{ .RelPermalink }}|{{ .Content }}|{{ end }}|
1412
1413 `
1414 b := Test(t, files)
1415 b.AssertFileContent(
1416 "public/en/sect/mybundle_en/index.html",
1417 "TranslationKey: adfasdf|",
1418 "Resources: /en/sect/mybundle_en/f1.txt|f1.en|/en/sect/mybundle_en/f2.txt|f2.en||",
1419 )
1420
1421 b.AssertFileContent(
1422 "public/nn/sect/mybundle_nn/index.html",
1423 "TranslationKey: adfasdf|",
1424 "Title: mybundle nn|TranslationKey: adfasdf|\nResources: /en/sect/mybundle_en/f1.txt|f1.en|/nn/sect/mybundle_nn/f2.nn.txt|f2.nn||",
1425 )
1426 }
1427
1428 func TestTranslationKeyRotate(t *testing.T) {
1429 t.Parallel()
1430
1431 files := `
1432 -- hugo.toml --
1433 disableKinds = ['home','rss','section','sitemap','taxonomy']
1434 defaultContentLanguage = 'en'
1435 defaultContentLanguageInSubdir = true
1436 [languages]
1437 [languages.en]
1438 weight = 1
1439 [languages.pt]
1440 weight = 2
1441 -- content/foo.md --
1442 ---
1443 title: Foo
1444 translationkey: "mykey"
1445 ---
1446 -- content/bar.md --
1447 ---
1448 title: Bar
1449 translationkey: "mykey"
1450 ---
1451 -- layouts/all.html --
1452 Rotate(language): {{ with .Rotate "language" }}{{ range . }}{{ template "printp" . }}|{{ end }}{{ end }}$
1453 {{ define "printp" }}{{ .RelPermalink }}:{{ with .Site }}{{ template "prints" . }}{{ end }}{{ end }}
1454 {{ define "prints" }}/l:{{ .Language.Name }}/v:{{ .Version.Name }}/r:{{ .Role.Name }}{{ end }}
1455 `
1456
1457 b := Test(t, files)
1458
1459 b.AssertFileContent("public/en/foo/index.html", "Rotate(language): /en/bar/:/l:en/v:v1.0.0/r:guest|/en/foo/:/l:en/v:v1.0.0/r:guest|$")
1460 b.AssertFileContent("public/en/bar/index.html", "Rotate(language): /en/bar/:/l:en/v:v1.0.0/r:guest|/en/foo/:/l:en/v:v1.0.0/r:guest|$")
1461 }
1462
1463 func TestChompBOM(t *testing.T) {
1464 t.Parallel()
1465 const utf8BOM = "\xef\xbb\xbf"
1466
1467 files := `
1468 -- hugo.toml --
1469 baseURL = "http://example.com/"
1470 -- content/simple.md --
1471 ` + utf8BOM + simplePage + `
1472 `
1473 b := Test(t, files, TestOptSkipRender())
1474
1475 b.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 1)
1476
1477 p := b.H.Sites[0].RegularPages()[0]
1478
1479 checkPageTitle(t, p, "Simple")
1480 }
1481
1482 // https://github.com/gohugoio/hugo/issues/5381
1483 func TestPageManualSummary(t *testing.T) {
1484 t.Parallel()
1485
1486 files := `
1487 -- hugo.toml --
1488 baseURL = "http://example.com/"
1489 [security]
1490 allowContent = ['.*']
1491 -- content/page-md-shortcode.md --
1492 ---
1493 title: "Hugo"
1494 ---
1495 This is a {{< sc >}}.
1496 <!--more-->
1497 Content.
1498 -- content/page-md-only-shortcode.md --
1499 ---
1500 title: "Hugo"
1501 ---
1502 {{< sc >}}
1503 <!--more-->
1504 {{< sc >}}
1505 -- content/page-md-shortcode-same-line.md --
1506 ---
1507 title: "Hugo"
1508 ---
1509 This is a {{< sc >}}<!--more-->Same line.
1510 -- content/page-md-shortcode-same-line-after.md --
1511 ---
1512 title: "Hugo"
1513 ---
1514 Summary<!--more-->{{< sc >}}
1515 -- content/page-org-shortcode.org --
1516 #+TITLE: T1
1517 #+AUTHOR: A1
1518 #+DESCRIPTION: D1
1519 This is a {{< sc >}}.
1520 # more
1521 Content.
1522 -- content/page-org-variant1.org --
1523 #+TITLE: T1
1524 Summary.
1525
1526 # more
1527
1528 Content.
1529 -- layouts/_shortcodes/sc.html --
1530 a shortcode
1531 -- layouts/single.html --
1532 SUMMARY:{{ .Summary }}:END
1533 --------------------------
1534 CONTENT:{{ .Content }}
1535 `
1536 b := Test(t, files)
1537
1538 b.AssertFileContent(
1539 "public/page-md-shortcode/index.html",
1540 "SUMMARY:<p>This is a a shortcode.</p>:END",
1541 "CONTENT:<p>This is a a shortcode.</p>\n\n<p>Content.</p>\n",
1542 )
1543
1544 b.AssertFileContent(
1545 "public/page-md-shortcode-same-line/index.html",
1546 "SUMMARY:<p>This is a a shortcode</p>:END",
1547 "CONTENT:<p>This is a a shortcode</p>\n\n<p>Same line.</p>\n",
1548 )
1549
1550 b.AssertFileContent(
1551 "public/page-md-shortcode-same-line-after/index.html",
1552 "SUMMARY:<p>Summary</p>:END",
1553 "CONTENT:<p>Summary</p>\n\na shortcode",
1554 )
1555
1556 b.AssertFileContent(
1557 "public/page-org-shortcode/index.html",
1558 "SUMMARY:<p>\nThis is a a shortcode.\n</p>:END",
1559 "CONTENT:<p>\nThis is a a shortcode.\n</p>\n<p>\nContent.\t\n</p>\n",
1560 )
1561 b.AssertFileContent(
1562 "public/page-org-variant1/index.html",
1563 "SUMMARY:<p>\nSummary.\n</p>:END",
1564 "CONTENT:<p>\nSummary.\n</p>\n<p>\nContent.\t\n</p>\n",
1565 )
1566
1567 b.AssertFileContent(
1568 "public/page-md-only-shortcode/index.html",
1569 "SUMMARY:a shortcode:END",
1570 "CONTENT:a shortcode\n\na shortcode\n",
1571 )
1572 }
1573
1574 func TestHomePageWithNoTitle(t *testing.T) {
1575 t.Parallel()
1576
1577 files := `
1578 -- hugo.toml --
1579 baseURL = "http://example.com/"
1580 title = "Site Title"
1581 -- layouts/home.html --
1582 Title|{{ with .Title }}{{ . }}{{ end }}|
1583 -- content/_index.md --
1584 ---
1585 description: "No title for you!"
1586 ---
1587
1588 Content.
1589 `
1590 b := Test(t, files)
1591
1592 b.AssertFileContent("public/index.html", "Title||")
1593 }
1594
1595 func TestShouldBuild(t *testing.T) {
1596 past := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
1597 future := time.Date(2037, 11, 17, 20, 34, 58, 651387237, time.UTC)
1598 zero := time.Time{}
1599
1600 publishSettings := []struct {
1601 buildFuture bool
1602 buildExpired bool
1603 buildDrafts bool
1604 draft bool
1605 publishDate time.Time
1606 expiryDate time.Time
1607 out bool
1608 }{
1609 // publishDate and expiryDate
1610 {false, false, false, false, zero, zero, true},
1611 {false, false, false, false, zero, future, true},
1612 {false, false, false, false, past, zero, true},
1613 {false, false, false, false, past, future, true},
1614 {false, false, false, false, past, past, false},
1615 {false, false, false, false, future, future, false},
1616 {false, false, false, false, future, past, false},
1617
1618 // buildFuture and buildExpired
1619 {false, true, false, false, past, past, true},
1620 {true, true, false, false, past, past, true},
1621 {true, false, false, false, past, past, false},
1622 {true, false, false, false, future, future, true},
1623 {true, true, false, false, future, future, true},
1624 {false, true, false, false, future, past, false},
1625
1626 // buildDrafts and draft
1627 {true, true, false, true, past, future, false},
1628 {true, true, true, true, past, future, true},
1629 {true, true, true, true, past, future, true},
1630 }
1631
1632 for _, ps := range publishSettings {
1633 s := shouldBuild(ps.buildFuture, ps.buildExpired, ps.buildDrafts, ps.draft,
1634 ps.publishDate, ps.expiryDate)
1635 if s != ps.out {
1636 t.Errorf("AssertShouldBuild unexpected output with params: %+v", ps)
1637 }
1638 }
1639 }
1640
1641 func TestShouldBuildWithClock(t *testing.T) {
1642 htime.Clock = clocks.Start(time.Date(2021, 11, 17, 20, 34, 58, 651387237, time.UTC))
1643 t.Cleanup(func() { htime.Clock = clocks.System() })
1644 past := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
1645 future := time.Date(2037, 11, 17, 20, 34, 58, 651387237, time.UTC)
1646 zero := time.Time{}
1647
1648 publishSettings := []struct {
1649 buildFuture bool
1650 buildExpired bool
1651 buildDrafts bool
1652 draft bool
1653 publishDate time.Time
1654 expiryDate time.Time
1655 out bool
1656 }{
1657 // publishDate and expiryDate
1658 {false, false, false, false, zero, zero, true},
1659 {false, false, false, false, zero, future, true},
1660 {false, false, false, false, past, zero, true},
1661 {false, false, false, false, past, future, true},
1662 {false, false, false, false, past, past, false},
1663 {false, false, false, false, future, future, false},
1664 {false, false, false, false, future, past, false},
1665
1666 // buildFuture and buildExpired
1667 {false, true, false, false, past, past, true},
1668 {true, true, false, false, past, past, true},
1669 {true, false, false, false, past, past, false},
1670 {true, false, false, false, future, future, true},
1671 {true, true, false, false, future, future, true},
1672 {false, true, false, false, future, past, false},
1673
1674 // buildDrafts and draft
1675 {true, true, false, true, past, future, false},
1676 {true, true, true, true, past, future, true},
1677 {true, true, true, true, past, future, true},
1678 }
1679
1680 for _, ps := range publishSettings {
1681 s := shouldBuild(ps.buildFuture, ps.buildExpired, ps.buildDrafts, ps.draft,
1682 ps.publishDate, ps.expiryDate)
1683 if s != ps.out {
1684 t.Errorf("AssertShouldBuildWithClock unexpected output with params: %+v", ps)
1685 }
1686 }
1687 }
1688
1689 // See https://github.com/gohugoio/hugo/issues/9171
1690 // We redefined disablePathToLower in v0.121.0.
1691 func TestPagePathDisablePathToLower(t *testing.T) {
1692 files := `
1693 -- hugo.toml --
1694 baseURL = "http://example.com"
1695 disablePathToLower = true
1696 [permalinks]
1697 sect2 = "/:section/:contentbasename/"
1698 sect3 = "/:section/:title/"
1699 -- content/sect/p1.md --
1700 ---
1701 title: "Page1"
1702 ---
1703 p1.
1704 -- content/sect/p2.md --
1705 ---
1706 title: "Page2"
1707 slug: "PaGe2"
1708 ---
1709 p2.
1710 -- content/sect2/PaGe3.md --
1711 ---
1712 title: "Page3"
1713 ---
1714 -- content/seCt3/p4.md --
1715 ---
1716 title: "Pag.E4"
1717 slug: "PaGe4"
1718 ---
1719 p4.
1720 -- layouts/single.html --
1721 Single: {{ .Title}}|{{ .RelPermalink }}|{{ .Path }}|
1722 `
1723 b := Test(t, files)
1724 b.AssertFileContent("public/sect/p1/index.html", "Single: Page1|/sect/p1/|/sect/p1")
1725 b.AssertFileContent("public/sect/PaGe2/index.html", "Single: Page2|/sect/PaGe2/|/sect/p2")
1726 b.AssertFileContent("public/sect2/PaGe3/index.html", "Single: Page3|/sect2/PaGe3/|/sect2/page3|")
1727 b.AssertFileContent("public/sect3/Pag.E4/index.html", "Single: Pag.E4|/sect3/Pag.E4/|/sect3/p4|")
1728 }
1729
1730 func TestScratch(t *testing.T) {
1731 t.Parallel()
1732
1733 files := `
1734 -- hugo.toml --
1735 baseURL = "http://example.com/"
1736 -- layouts/home.html --
1737 {{ .Scratch.Set "b" "bv" }}
1738 B: {{ .Scratch.Get "b" }}
1739 -- layouts/_shortcodes/scratch.html --
1740 {{ .Scratch.Set "c" "cv" }}
1741 C: {{ .Scratch.Get "c" }}
1742 -- layouts/single.html --
1743 {{ .Content }}
1744 -- content/scratchme.md --
1745 ---
1746 title: Scratch Me!
1747 ---
1748
1749 {{< scratch >}}
1750 `
1751 b, err := TestE(t, files)
1752
1753 b.Assert(err, qt.IsNil)
1754
1755 b.AssertFileContent("public/index.html", "B: bv")
1756 b.AssertFileContent("public/scratchme/index.html", "C: cv")
1757 }
1758
1759 // Issue 13016.
1760 func TestScratchAliasToStore(t *testing.T) {
1761 t.Parallel()
1762
1763 files := `
1764 -- hugo.toml --
1765 disableKinds = ["taxonomy", "term", "page", "section"]
1766 disableLiveReload = true
1767 -- layouts/home.html --
1768 {{ .Scratch.Set "a" "b" }}
1769 {{ .Store.Set "c" "d" }}
1770 .Scratch eq .Store: {{ eq .Scratch .Store }}
1771 a: {{ .Store.Get "a" }}
1772 c: {{ .Scratch.Get "c" }}
1773
1774 `
1775
1776 b := Test(t, files)
1777
1778 b.AssertFileContent(
1779 "public/index.html",
1780 ".Scratch eq .Store: true",
1781 "a: b",
1782 "c: d",
1783 )
1784 }
1785
1786 func TestPageParam(t *testing.T) {
1787 t.Parallel()
1788
1789 files := `
1790 -- hugo.toml --
1791 baseURL = "https://example.org"
1792
1793 [params]
1794 [params.author]
1795 name = "Kurt Vonnegut"
1796 -- layouts/home.html --
1797
1798 {{ $withParam := .Site.GetPage "withparam" }}
1799 {{ $noParam := .Site.GetPage "noparam" }}
1800 {{ $withStringParam := .Site.GetPage "withstringparam" }}
1801
1802 Author page: {{ $withParam.Param "author.name" }}
1803 Author name page string: {{ $withStringParam.Param "author.name" }}|
1804 Author page string: {{ $withStringParam.Param "author" }}|
1805 Author project config: {{ $noParam.Param "author.name" }}
1806
1807 -- content/withparam.md --
1808 +++
1809 title = "With Param!"
1810 [author]
1811 name = "Ernest Miller Hemingway"
1812
1813 +++
1814 -- content/noparam.md --
1815 ---
1816 title: "No Param!"
1817 ---
1818 -- content/withstringparam.md --
1819 +++
1820 title = "With string Param!"
1821 author = "Jo Nesbø"
1822
1823 +++
1824 `
1825 b := Test(t, files)
1826
1827 b.AssertFileContent("public/index.html",
1828 "Author page: Ernest Miller Hemingway",
1829 "Author name page string: Kurt Vonnegut|",
1830 "Author page string: Jo Nesbø|",
1831 "Author project config: Kurt Vonnegut")
1832 }
1833
1834 func TestGoldmark(t *testing.T) {
1835 t.Parallel()
1836
1837 content := `
1838 +++
1839 title = "A Page!"
1840 +++
1841
1842 ## Shortcode {{% t %}} in header
1843
1844 ## Code Fense in Shortcode
1845
1846 {{% s %}}
1847 $$$bash {hl_lines=[1]}
1848 SHORT
1849 $$$
1850 {{% /s %}}
1851
1852 ## Code Fence
1853
1854 $$$bash {hl_lines=[1]}
1855 MARKDOWN
1856 $$$
1857
1858 Link with URL as text
1859
1860 [https://google.com](https://google.com)
1861
1862
1863 `
1864 content = strings.ReplaceAll(content, "$$$", "```")
1865
1866 files := `
1867 -- hugo.toml --
1868 baseURL = "https://example.org"
1869
1870 [markup]
1871 defaultMarkdownHandler="goldmark"
1872 [markup.goldmark]
1873 [markup.goldmark.renderer]
1874 unsafe = false
1875 [markup.highlight]
1876 noClasses=false
1877 -- layouts/single.html --
1878 Title: {{ .Title }}
1879 ToC: {{ .TableOfContents }}
1880 Content: {{ .Content }}
1881 -- layouts/_shortcodes/t.html --
1882 T-SHORT
1883 -- layouts/_shortcodes/s.html --
1884 ## Code
1885 {{ .Inner }}
1886 -- content/page.md --
1887 ` + content + `
1888 `
1889 b := Test(t, files)
1890
1891 b.AssertFileContent("public/page/index.html",
1892 `<nav id="TableOfContents">
1893 <li><a href="#shortcode-t-short-in-header">Shortcode T-SHORT in header</a></li>
1894 <code class="language-bash" data-lang="bash"><span class="line hl"><span class="cl">SHORT
1895 <code class="language-bash" data-lang="bash"><span class="line hl"><span class="cl">MARKDOWN
1896 <p><a href="https://google.com">https://google.com</a></p>
1897 `)
1898 }
1899
1900 func TestPageHashString(t *testing.T) {
1901 files := `
1902 -- hugo.toml --
1903 baseURL = "https://example.org"
1904 [languages]
1905 [languages.en]
1906 weight = 1
1907 title = "English"
1908 [languages.no]
1909 weight = 2
1910 title = "Norsk"
1911 -- content/p1.md --
1912 ---
1913 title: "p1"
1914 ---
1915 -- content/p2.md --
1916 ---
1917 title: "p2"
1918 ---
1919 `
1920
1921 b := Test(t, files)
1922
1923 p1 := b.H.Sites[0].RegularPages()[0]
1924 p2 := b.H.Sites[0].RegularPages()[1]
1925 sites := p1.Sites()
1926
1927 b.Assert(p1, qt.Not(qt.Equals), p2)
1928
1929 b.Assert(hashing.HashString(p1), qt.Not(qt.Equals), hashing.HashString(p2))
1930 b.Assert(hashing.HashString(sites[0]), qt.Not(qt.Equals), hashing.HashString(sites[1]))
1931 }
1932
1933 // Issue #11243
1934 func TestRenderWithoutArgument(t *testing.T) {
1935 t.Parallel()
1936
1937 files := `
1938 -- hugo.toml --
1939 -- layouts/home.html --
1940 {{ .Render }}
1941 `
1942
1943 b, err := TestE(t, files)
1944
1945 b.Assert(err, qt.IsNotNil)
1946 }
1947
1948 // See issue 15077.
1949 func TestRenderWithContext(t *testing.T) {
1950 t.Parallel()
1951
1952 files := `
1953 -- hugo.toml --
1954 -- content/p1.md --
1955 ---
1956 title: "P1"
1957 ---
1958 -- layouts/page.html --
1959 {{ .Render "li" }}|{{ .Render "li" (dict "Title" "Custom") }}
1960 -- layouts/li.html --
1961 Title: {{ .Title }}{{- /**/ -}}
1962 `
1963
1964 b := Test(t, files)
1965
1966 b.AssertFileContent("public/p1/index.html", "Title: P1|Title: Custom")
1967 }
1968
1969 // See issue 15077.
1970 func TestRenderWithContextErrors(t *testing.T) {
1971 t.Parallel()
1972
1973 filesTemplate := `
1974 -- hugo.toml --
1975 -- content/p1.md --
1976 ---
1977 title: "P1"
1978 ---
1979 -- layouts/li.html --
1980 li
1981 -- layouts/page.html --
1982 RENDER
1983 `
1984
1985 for _, test := range []struct {
1986 render string
1987 message string
1988 }{
1989 {`{{ .Render "li" "foo" "bar" }}`, `(?s).*too many arguments, expected VIEW \[CONTEXT\].*`},
1990 {`{{ .Render (slice "li") }}`, `(?s).*failed to convert view argument to string: unable to cast \[\]string{"li"} of type \[\]string to string.*`},
1991 } {
1992 files := strings.ReplaceAll(filesTemplate, "RENDER", test.render)
1993 b, err := TestE(t, files)
1994 b.Assert(err, qt.ErrorMatches, test.message)
1995 }
1996 }
1997
1998 // Issue #13021
1999 func TestAllStores(t *testing.T) {
2000 t.Parallel()
2001
2002 files := `
2003 -- hugo.toml --
2004 disableKinds = ["taxonomy", "term", "page", "section"]
2005 disableLiveReload = true
2006 -- content/_index.md --
2007 ---
2008 title: "Home"
2009 ---
2010 {{< s >}}
2011 -- layouts/_shortcodes/s.html --
2012 {{ if not (.Store.Get "Shortcode") }}{{ .Store.Set "Shortcode" (printf "sh-%s" $.Page.Title) }}{{ end }}
2013 Shortcode: {{ .Store.Get "Shortcode" }}|
2014 -- layouts/home.html --
2015 {{ .Content }}
2016 {{ if not (.Store.Get "Page") }}{{ .Store.Set "Page" (printf "p-%s" $.Title) }}{{ end }}
2017 {{ if not (hugo.Store.Get "Hugo") }}{{ hugo.Store.Set "Hugo" (printf "h-%s" $.Title) }}{{ end }}
2018 {{ if not (site.Store.Get "Site") }}{{ site.Store.Set "Site" (printf "s-%s" $.Title) }}{{ end }}
2019 Page: {{ .Store.Get "Page" }}|
2020 Hugo: {{ hugo.Store.Get "Hugo" }}|
2021 Site: {{ site.Store.Get "Site" }}|
2022 `
2023
2024 b := TestRunning(t, files)
2025
2026 b.AssertFileContent(
2027 "public/index.html",
2028 `
2029 Shortcode: sh-Home|
2030 Page: p-Home|
2031 Site: s-Home|
2032 Hugo: h-Home|
2033 `,
2034 )
2035
2036 b.EditFileReplaceAll("content/_index.md", "Home", "Homer").Build()
2037
2038 b.AssertFileContent(
2039 "public/index.html",
2040 `
2041 Shortcode: sh-Homer|
2042 Page: p-Homer|
2043 Site: s-Home|
2044 Hugo: h-Home|
2045 `,
2046 )
2047 }
2048
2049 // Issue 13538
2050 func TestHomePageIsLeafBundle(t *testing.T) {
2051 t.Parallel()
2052
2053 files := `
2054 -- hugo.toml --
2055 defaultContentLanguage = 'de'
2056 defaultContentLanguageInSubdir = true
2057 [security]
2058 allowContent = ['.*']
2059 [languages.de]
2060 weight = 1
2061 [languages.en]
2062 weight = 2
2063 -- layouts/all.html --
2064 {{ .Title }}
2065 -- content/index.de.md --
2066 ---
2067 title: home de
2068 ---
2069 -- content/index.en.org --
2070 ---
2071 title: home en
2072 ---
2073 `
2074
2075 b := Test(t, files, TestOptWarn())
2076
2077 b.AssertFileContent("public/de/index.html", "home de")
2078 b.AssertFileContent("public/en/index.html", "home en")
2079 b.AssertLogContains("Using index.de.md in your content's root directory is usually incorrect for your home page. You should use _index.de.md instead.")
2080 b.AssertLogContains("Using index.en.org in your content's root directory is usually incorrect for your home page. You should use _index.en.org instead.")
2081 }
2082
2083 func content(c resource.ContentProvider) string {
2084 cc, err := c.Content(context.Background())
2085 if err != nil {
2086 panic(err)
2087 }
2088
2089 ccs, err := cast.ToStringE(cc)
2090 if err != nil {
2091 panic(err)
2092 }
2093 return ccs
2094 }
2095
2096 // See issue 11574.
2097 func TestPageIsBranch(t *testing.T) {
2098 t.Parallel()
2099
2100 files := `
2101 -- hugo.toml --
2102 disableKinds = ["taxonomy", "term", "rss", "sitemap", "robotsTXT", "404"]
2103 -- content/_index.md --
2104 -- content/sect/_index.md --
2105 -- content/sect/p1.md --
2106 -- layouts/all.html --
2107 {{ .Kind }}|IsBranch={{ .IsBranch }}|IsPage={{ .IsPage }}
2108 `
2109
2110 b := Test(t, files)
2111
2112 b.AssertFileContent("public/index.html", "home|IsBranch=true|IsPage=false")
2113 b.AssertFileContent("public/sect/index.html", "section|IsBranch=true|IsPage=false")
2114 b.AssertFileContent("public/sect/p1/index.html", "page|IsBranch=false|IsPage=true")
2115 }
2116
2117 // See issue 11574.
2118 func TestPageIsNodeDeprecated(t *testing.T) {
2119 files := `
2120 -- hugo.toml --
2121 disableKinds = ["taxonomy", "term", "rss", "sitemap", "robotsTXT", "404"]
2122 -- content/_index.md --
2123 -- layouts/all.html --
2124 {{ .IsNode }}
2125 `
2126
2127 b := Test(t, files, TestOptInfo())
2128
2129 b.AssertLogContains(".Page.IsNode was deprecated")
2130 }
2131
2132 func BenchmarkIsTranslatedOneLanguage(b *testing.B) {
2133 // Set it reasonably high to get a balance between cached and uncached calls to IsTranslated.
2134 const numPages = 3000
2135
2136 var files strings.Builder
2137 files.WriteString(`
2138 -- hugo.toml --
2139 disableKinds = ["taxonomy", "term"]
2140 `)
2141 for i := range numPages {
2142 files.WriteString(fmt.Sprintf(`
2143 -- content/sect/p%d.md --`, i))
2144 }
2145
2146 bb := Test(b, files.String(), TestOptSkipRender())
2147 p := bb.H.Sites[0].RegularPages()
2148
2149 b.ResetTimer()
2150
2151 for i := range b.N {
2152 if p[i%numPages].IsTranslated() {
2153 b.Fatalf("Page %d should not be translated", i)
2154 }
2155 }
2156 }
2157
2158 // See issue 15052.
2159 func TestRenderViewNotFound(t *testing.T) {
2160 t.Parallel()
2161
2162 files := `
2163 -- hugo.toml --
2164 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
2165 -- layouts/home.html --
2166 {{ .Render "view_foo" }}
2167 {{ .Render "view_bar" }}
2168 -- layouts/view_foo.html --
2169 foo
2170 `
2171
2172 b, err := TestE(t, files)
2173 b.Assert(err, qt.ErrorMatches, `.*template "view_bar" not found.*`)
2174 }
2175
2176 // See issue 15057.
2177 func TestRenderCaseInsensitiveTemplateName(t *testing.T) {
2178 t.Parallel()
2179
2180 files := `
2181 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2182 -- content/s1/p1.md --
2183 ---
2184 title: p1
2185 ---
2186 -- content/s2/p2.md --
2187 ---
2188 title: p2
2189 layout: ab
2190 ---
2191 -- content/s2/p3.md --
2192 ---
2193 title: p3
2194 layout: aB
2195 ---
2196 -- content/s2/p4.md --
2197 ---
2198 title: p4
2199 layout: AB
2200 ---
2201 -- content/s3/p5.md --
2202 ---
2203 title: p5
2204 layout: cd
2205 ---
2206 -- content/s3/p6.md --
2207 ---
2208 title: p6
2209 layout: cD
2210 ---
2211 -- content/s3/p7.md --
2212 ---
2213 title: p7
2214 layout: CD
2215 ---
2216 -- content/s4/p8.md --
2217 ---
2218 title: p8
2219 layout: ef
2220 ---
2221 -- content/s4/p9.md --
2222 ---
2223 title: p9
2224 layout: eF
2225 ---
2226 -- content/s4/p10.md --
2227 ---
2228 title: p10
2229 layout: EF
2230 ---
2231 -- layouts/s1/p1/page.html --
2232 {{ .Render "ab" }}|{{ .Render "aB" }}|{{ .Render "AB" }}
2233 {{ .Render "cd" }}|{{ .Render "cD" }}|{{ .Render "CD" }}
2234 {{ .Render "ef" }}|{{ .Render "eF" }}|{{ .Render "EF" }}
2235 -- layouts/s1/p1/ab.html --
2236 ab{{- /**/ -}}
2237 -- layouts/s1/p1/cD.html --
2238 cD{{- /**/ -}}
2239 -- layouts/s1/p1/EF.html --
2240 EF{{- /**/ -}}
2241 -- layouts/s2/ab.html --
2242 ab
2243 -- layouts/s3/cD.html --
2244 cD
2245 -- layouts/s4/EF.html --
2246 EF
2247 `
2248
2249 b := Test(t, files)
2250
2251 b.AssertFileContent(
2252 "public/s1/p1/index.html",
2253 "ab|ab|ab",
2254 "cD|cD|cD",
2255 "EF|EF|EF",
2256 )
2257
2258 b.AssertFileContent("public/s2/p2/index.html", "ab")
2259 b.AssertFileContent("public/s2/p3/index.html", "ab")
2260 b.AssertFileContent("public/s2/p4/index.html", "ab")
2261
2262 b.AssertFileContent("public/s3/p5/index.html", "cD")
2263 b.AssertFileContent("public/s3/p6/index.html", "cD")
2264 b.AssertFileContent("public/s3/p7/index.html", "cD")
2265
2266 b.AssertFileContent("public/s4/p8/index.html", "EF")
2267 b.AssertFileContent("public/s4/p9/index.html", "EF")
2268 b.AssertFileContent("public/s4/p10/index.html", "EF")
2269 }
2270
2271 // See issue 15056.
2272 func TestRenderViewSubdir(t *testing.T) {
2273 t.Parallel()
2274
2275 files := `
2276 -- hugo.toml --
2277 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2278 -- content/s1/p1.md --
2279 ---
2280 title: p1
2281 ---
2282 -- layouts/s1/p1/page.html --
2283 {{ .Render "a" }}|{{ .Render "b" }}|{{ .Render "c" }}|{{ .Render "foo/d" }}|{{ .Render "foo/e" }}|{{ .Render "foo/f" }}|{{ .Render "sub/foo/g" }}
2284 -- layouts/s1/p1/a.html --
2285 a{{- /**/ -}}
2286 -- layouts/s1/b.html --
2287 b{{- /**/ -}}
2288 -- layouts/c.html --
2289 c{{- /**/ -}}
2290 -- layouts/s1/p1/foo/d.html --
2291 d{{- /**/ -}}
2292 -- layouts/s1/foo/e.html --
2293 e{{- /**/ -}}
2294 -- layouts/foo/f.html --
2295 f{{- /**/ -}}
2296 -- layouts/sub/foo/g.html --
2297 g{{- /**/ -}}
2298 `
2299
2300 b := Test(t, files)
2301 b.AssertFileContent("public/s1/p1/index.html", "a|b|c|d|e|f|g")
2302 }
2303
2304 // See issue 15056.
2305 func TestRenderViewSubdirRootPage(t *testing.T) {
2306 t.Parallel()
2307
2308 files := `
2309 -- hugo.toml --
2310 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2311 -- content/p1.md --
2312 ---
2313 title: p1
2314 ---
2315 -- layouts/page.html --
2316 {{ .Render "foo/a" }}
2317 -- layouts/foo/a.html --
2318 a{{- /**/ -}}
2319 `
2320
2321 b := Test(t, files)
2322 b.AssertFileContent("public/p1/index.html", "a")
2323 }
2324
2325 // See issue 15056.
2326 func TestRenderViewSubdirNotFound(t *testing.T) {
2327 t.Parallel()
2328
2329 files := `
2330 -- hugo.toml --
2331 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2332 -- content/s1/p1.md --
2333 ---
2334 title: p1
2335 ---
2336 -- layouts/s1/p1/page.html --
2337 {{ .Render "foo/missing" }}
2338 -- layouts/s1/p1/missing.html --
2339 should-not-match{{- /**/ -}}
2340 `
2341
2342 b, err := TestE(t, files)
2343 b.Assert(err, qt.ErrorMatches, `.*template "foo/missing" not found.*`)
2344 }
2345
2346 // See issue 15056.
2347 func TestRenderViewSubdirTrailingSlash(t *testing.T) {
2348 t.Parallel()
2349
2350 files := `
2351 -- hugo.toml --
2352 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2353 -- content/s1/p1.md --
2354 ---
2355 title: p1
2356 ---
2357 -- layouts/s1/p1/page.html --
2358 {{ .Render "foo/" }}
2359 -- layouts/s1/p1/foo.html --
2360 should-not-match{{- /**/ -}}
2361 `
2362
2363 b, err := TestE(t, files)
2364 b.Assert(err, qt.ErrorMatches, `.*template "foo/" not found.*`)
2365 }
2366
2367 // See issue 15056.
2368 func TestRenderViewSubdirCaseInsensitiveArgument(t *testing.T) {
2369 t.Parallel()
2370
2371 files := `
2372 -- hugo.toml --
2373 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2374 -- content/s1/p1.md --
2375 ---
2376 title: p1
2377 ---
2378 -- layouts/s1/p1/page.html --
2379 {{ .Render "Foo/Bar" }}
2380 -- layouts/s1/p1/foo/bar.html --
2381 bar{{- /**/ -}}
2382 `
2383
2384 b := Test(t, files)
2385 b.AssertFileContent("public/s1/p1/index.html", "bar")
2386 }
2387
2388 // See issue 15056.
2389 func TestRenderViewSubdirCaseInsensitivePath(t *testing.T) {
2390 t.Parallel()
2391
2392 files := `
2393 -- hugo.toml --
2394 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2395 -- content/s1/p1.md --
2396 ---
2397 title: p1
2398 ---
2399 -- layouts/s1/p1/page.html --
2400 {{ .Render "foo/bar" }}
2401 -- layouts/s1/p1/Foo/Bar.html --
2402 bar{{- /**/ -}}
2403 `
2404
2405 b := Test(t, files)
2406 b.AssertFileContent("public/s1/p1/index.html", "bar")
2407 }
2408
2409 // See issue 15056.
2410 func TestRenderViewSubdirWithUnderscore(t *testing.T) {
2411 t.Parallel()
2412
2413 files := `
2414 -- hugo.toml --
2415 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2416 -- content/p1.md --
2417 ---
2418 title: p1
2419 ---
2420 -- content/s1/p2.md --
2421 ---
2422 title: p2
2423 ---
2424 -- content/s1/p3.md --
2425 ---
2426 title: p3
2427 ---
2428 -- layouts/page.html --
2429 {{ .Title }}: {{ .Render "_views/a" }}
2430 -- layouts/_views/a.html --
2431 layouts/_views/a.html
2432 -- layouts/s1/_views/a.html --
2433 layouts/s1/_views/a.html
2434 -- layouts/s1/p3/_views/a.html --
2435 layouts/s1/p3/_views/a.html
2436 `
2437
2438 b := Test(t, files)
2439
2440 b.AssertFileContent("public/p1/index.html", "p1: layouts/_views/a.html")
2441 b.AssertFileContent("public/s1/p2/index.html", "p2: layouts/s1/_views/a.html") // fails
2442 b.AssertFileContent("public/s1/p3/index.html", "p3: layouts/s1/p3/_views/a.html") // fails
2443 }
2444
2445 func TestPageConttentWeight(t *testing.T) {
2446 files := `
2447 -- hugo.toml --
2448 -- content/mysection/page1.md --
2449 -- content/myothersection/page2.md --
2450 -- content/myothersection/_index.md --
2451 -- layouts/all.html --
2452 All.
2453 `
2454 b := Test(t, files)
2455
2456 check := func(p page.Page, ok bool) {
2457 cw := p.(contentNodeContentWeightProvider).contentWeight()
2458 b.Assert(ok, qt.Equals, cw > 0)
2459 }
2460
2461 s := b.H.Sites[0]
2462 for _, p := range s.RegularPages() {
2463 check(p, true)
2464 }
2465 check(s.home, false)
2466 mysection, _ := s.GetPage("mysection")
2467 check(mysection, false)
2468 myothersection, _ := s.GetPage("myothersection") // backed by a content file.
2469 check(myothersection, true)
2470 }
2471
2472 // See issue 15206.
2473 func TestReadingTimeAndFuzzyWordCountBoundaries(t *testing.T) {
2474 t.Parallel()
2475
2476 files := `
2477 -- hugo.toml --
2478 disableKinds = ["home", "section", "taxonomy", "term", "rss", "sitemap"]
2479 -- content/p99.md --
2480 ---
2481 title: p99
2482 ---
2483 ` + strings.Repeat("word ", 99) + `
2484 -- content/p100.md --
2485 ---
2486 title: p100
2487 ---
2488 ` + strings.Repeat("word ", 100) + `
2489 -- content/p101.md --
2490 ---
2491 title: p101
2492 ---
2493 ` + strings.Repeat("word ", 101) + `
2494 -- content/p211.md --
2495 ---
2496 title: p211
2497 ---
2498 ` + strings.Repeat("word ", 211) + `
2499 -- content/p212.md --
2500 ---
2501 title: p212
2502 ---
2503 ` + strings.Repeat("word ", 212) + `
2504 -- content/p213.md --
2505 ---
2506 title: p213
2507 ---
2508 ` + strings.Repeat("word ", 213) + `
2509 -- content/p499.md --
2510 ---
2511 title: p499
2512 isCJKLanguage: true
2513 ---
2514 ` + strings.Repeat("你", 499) + `
2515 -- content/p500.md --
2516 ---
2517 title: p500
2518 isCJKLanguage: true
2519 ---
2520 ` + strings.Repeat("你", 500) + `
2521 -- content/p501.md --
2522 ---
2523 title: p501
2524 isCJKLanguage: true
2525 ---
2526 ` + strings.Repeat("你", 501) + `
2527 -- layouts/page.html --
2528 {{ .WordCount }}|{{ .FuzzyWordCount }}|{{ .ReadingTime }}
2529 `
2530
2531 b := Test(t, files)
2532
2533 b.AssertFileContent("public/p99/index.html", "99|100|1")
2534 b.AssertFileContent("public/p100/index.html", "100|100|1")
2535 b.AssertFileContent("public/p101/index.html", "101|200|1")
2536
2537 b.AssertFileContent("public/p211/index.html", "211|300|1")
2538 b.AssertFileContent("public/p212/index.html", "212|300|1")
2539 b.AssertFileContent("public/p213/index.html", "213|300|2")
2540
2541 b.AssertFileContent("public/p499/index.html", "499|500|1")
2542 b.AssertFileContent("public/p500/index.html", "500|500|1")
2543 b.AssertFileContent("public/p501/index.html", "501|600|2")
2544 }