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 // #2973
792 func TestSummaryWithHTMLTagsOnNextLine(t *testing.T) {
793 htesting.SkipSlowTestUnlessCI(t)
794 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
795 c := qt.New(t)
796 p := pages[0]
797 s := string(p.Summary(context.Background()))
798 c.Assert(s, qt.Contains, "Happy new year everyone!")
799 c.Assert(s, qt.Not(qt.Contains), "User interface")
800 }
801
802 testAllMarkdownEnginesForPages(t, assertFunc, nil, `---
803 title: Simple
804 ---
805 Happy new year everyone!
806
807 Here is the last report for commits in the year 2016. It covers hrev50718-hrev50829.
808
809 <!--more-->
810
811 <h3>User interface</h3>
812
813 `)
814 }
815
816 // Issue 9383
817 func TestRenderStringForRegularPageTranslations(t *testing.T) {
818 t.Parallel()
819
820 files := `
821 -- hugo.toml --
822 baseurl = "https://example.org/"
823 title = "My Site"
824
825 defaultContentLanguage = "ru"
826 defaultContentLanguageInSubdir = true
827
828 [languages.ru]
829 contentDir = 'content/ru'
830 weight = 1
831
832 [languages.en]
833 weight = 2
834 contentDir = 'content/en'
835
836 [outputs]
837 home = ["HTML", "JSON"]
838 -- layouts/home.html --
839 {{- range .Site.Home.Translations -}}
840 <p>{{- .RenderString "foo" -}}</p>
841 {{- end -}}
842 {{- range .Site.Home.AllTranslations -}}
843 <p>{{- .RenderString "bar" -}}</p>
844 {{- end -}}
845 -- layouts/single.html --
846 {{ .Content }}
847 -- layouts/index.json --
848 {"Title": "My Site"}
849 -- content/ru/a.md --
850 -- content/en/a.md --
851 `
852 b := Test(t, files)
853
854 b.AssertFileContent("public/ru/index.html", `
855 <p>foo</p>
856 <p>foo</p>
857 <p>bar</p>
858 <p>bar</p>
859 `)
860
861 b.AssertFileContent("public/en/index.html", `
862 <p>foo</p>
863 <p>foo</p>
864 <p>bar</p>
865 <p>bar</p>
866 `)
867 }
868
869 // Issue 8919
870 func TestContentProviderWithCustomOutputFormat(t *testing.T) {
871 t.Parallel()
872 htesting.SkipSlowTestUnlessCI(t)
873
874 files := `
875 -- hugo.toml --
876 baseURL = 'http://example.org/'
877 title = 'My New Hugo Project'
878
879 timeout = 600000 # ten minutes in case we want to pause and debug
880
881 defaultContentLanguage = "en"
882
883 [languages]
884 [languages.en]
885 title = "Repro"
886 label = "English"
887 contentDir = "content/en"
888
889 [languages.zh_CN]
890 title = "Repro"
891 label = "简体中文"
892 contentDir = "content/zh_CN"
893
894 [outputFormats]
895 [outputFormats.metadata]
896 baseName = "metadata"
897 mediaType = "text/html"
898 isPlainText = true
899 notAlternative = true
900
901 [outputs]
902 home = ["HTML", "metadata"]
903 -- layouts/home.metadata.html --
904 <h2>Translations metadata</h2>
905 <ul>
906 {{ $p := .Page }}
907 {{ range $p.Translations}}
908 <li>Title: {{ .Title }}, {{ .Summary }}</li>
909 <li>Content: {{ .Content }}</li>
910 <li>Plain: {{ .Plain }}</li>
911 <li>PlainWords: {{ .PlainWords }}</li>
912 <li>Summary: {{ .Summary }}</li>
913 <li>Truncated: {{ .Truncated }}</li>
914 <li>FuzzyWordCount: {{ .FuzzyWordCount }}</li>
915 <li>ReadingTime: {{ .ReadingTime }}</li>
916 <li>Len: {{ .Len }}</li>
917 {{ end }}
918 </ul>
919 -- layouts/baseof.html --
920 <html>
921
922 <body>
923 {{ block "main" . }}{{ end }}
924 </body>
925
926 </html>
927 -- layouts/home.html --
928 {{ define "main" }}
929 <h2>Translations</h2>
930 <ul>
931 {{ $p := .Page }}
932 {{ range $p.Translations}}
933 <li>Title: {{ .Title }}, {{ .Summary }}</li>
934 <li>Content: {{ .Content }}</li>
935 <li>Plain: {{ .Plain }}</li>
936 <li>PlainWords: {{ .PlainWords }}</li>
937 <li>Summary: {{ .Summary }}</li>
938 <li>Truncated: {{ .Truncated }}</li>
939 <li>FuzzyWordCount: {{ .FuzzyWordCount }}</li>
940 <li>ReadingTime: {{ .ReadingTime }}</li>
941 <li>Len: {{ .Len }}</li>
942 {{ end }}
943 </ul>
944 {{ end }}
945 -- content/en/_index.md --
946 ---
947 title: Title (en)
948 summary: Summary (en)
949 ---
950
951 Here is some content.
952 -- content/zh_CN/_index.md --
953 ---
954 title: Title (zh)
955 summary: Summary (zh)
956 ---
957
958 这是一些内容
959 `
960 b := Test(t, files)
961
962 b.AssertFileContent("public/index.html", `<html>
963
964 <body>
965
966 <h2>Translations</h2>
967 <ul>
968
969
970 <li>Title: Title (zh), Summary (zh)</li>
971 <li>Content: <p>这是一些内容</p>
972 </li>
973 <li>Plain: 这是一些内容
974 </li>
975 <li>PlainWords: [这是一些内容]</li>
976 <li>Summary: Summary (zh)</li>
977 <li>Truncated: false</li>
978 <li>FuzzyWordCount: 100</li>
979 <li>ReadingTime: 1</li>
980 <li>Len: 26</li>
981
982 </ul>
983
984 </body>
985
986 </html>`)
987 b.AssertFileContent("public/metadata.html", `<h2>Translations metadata</h2>
988 <ul>
989
990
991 <li>Title: Title (zh), Summary (zh)</li>
992 <li>Content: <p>这是一些内容</p>
993 </li>
994 <li>Plain: 这是一些内容
995 </li>
996 <li>PlainWords: [这是一些内容]</li>
997 <li>Summary: Summary (zh)</li>
998 <li>Truncated: false</li>
999 <li>FuzzyWordCount: 100</li>
1000 <li>ReadingTime: 1</li>
1001 <li>Len: 26</li>
1002
1003 </ul>`)
1004 b.AssertFileContent("public/zh_cn/index.html", `<html>
1005
1006 <body>
1007
1008 <h2>Translations</h2>
1009 <ul>
1010
1011
1012 <li>Title: Title (en), Summary (en)</li>
1013 <li>Content: <p>Here is some content.</p>
1014 </li>
1015 <li>Plain: Here is some content.
1016 </li>
1017 <li>PlainWords: [Here is some content.]</li>
1018 <li>Summary: Summary (en)</li>
1019 <li>Truncated: false</li>
1020 <li>FuzzyWordCount: 100</li>
1021 <li>ReadingTime: 1</li>
1022 <li>Len: 29</li>
1023
1024 </ul>
1025
1026 </body>
1027
1028 </html>`)
1029 b.AssertFileContent("public/zh_cn/metadata.html", `<h2>Translations metadata</h2>
1030 <ul>
1031
1032
1033 <li>Title: Title (en), Summary (en)</li>
1034 <li>Content: <p>Here is some content.</p>
1035 </li>
1036 <li>Plain: Here is some content.
1037 </li>
1038 <li>PlainWords: [Here is some content.]</li>
1039 <li>Summary: Summary (en)</li>
1040 <li>Truncated: false</li>
1041 <li>FuzzyWordCount: 100</li>
1042 <li>ReadingTime: 1</li>
1043 <li>Len: 29</li>
1044
1045 </ul>`)
1046 }
1047
1048 func TestPageWithDate(t *testing.T) {
1049 t.Parallel()
1050
1051 files := `
1052 -- hugo.toml --
1053 baseURL = "http://example.com/"
1054 -- content/simple.md --
1055 ` + simplePageRFC3339Date + `
1056 `
1057 b := Test(t, files, TestOptSkipRender())
1058
1059 b.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 1)
1060
1061 p := b.H.Sites[0].RegularPages()[0]
1062 d, _ := time.Parse(time.RFC3339, "2013-05-17T16:59:30Z")
1063
1064 checkPageDate(t, p, d)
1065 }
1066
1067 func TestPageWithFrontMatterConfig(t *testing.T) {
1068 for _, dateHandler := range []string{":filename", ":fileModTime"} {
1069 t.Run(fmt.Sprintf("dateHandler=%q", dateHandler), func(t *testing.T) {
1070 t.Parallel()
1071
1072 pageTemplate := `
1073 ---
1074 title: Page
1075 weight: %d
1076 lastMod: 2018-02-28
1077 %s
1078 ---
1079 Content
1080 `
1081 files := `
1082 -- hugo.toml --
1083 baseURL = "http://example.com/"
1084 [frontmatter]
1085 date = ["` + dateHandler + `", "date"]
1086 -- content/section/2012-02-21-noslug.md --
1087 ` + fmt.Sprintf(pageTemplate, 1, "") + `
1088 -- content/section/2012-02-22-slug.md --
1089 ` + fmt.Sprintf(pageTemplate, 2, "slug: aslug") + `
1090 `
1091 b := Test(t, files, TestOptOsFs())
1092
1093 s := b.H.Sites[0]
1094 b.Assert(len(s.RegularPages()), qt.Equals, 2)
1095
1096 noSlug := s.RegularPages()[0]
1097 slug := s.RegularPages()[1]
1098
1099 b.Assert(noSlug.Lastmod().Day(), qt.Equals, 28)
1100
1101 switch strings.ToLower(dateHandler) {
1102 case ":filename":
1103 b.Assert(noSlug.Date().IsZero(), qt.Equals, false)
1104 b.Assert(slug.Date().IsZero(), qt.Equals, false)
1105 b.Assert(noSlug.Date().Year(), qt.Equals, 2012)
1106 b.Assert(slug.Date().Year(), qt.Equals, 2012)
1107 b.Assert(noSlug.Slug(), qt.Equals, "noslug")
1108 b.Assert(slug.Slug(), qt.Equals, "aslug")
1109 case ":filemodtime":
1110 // For fileModTime, we need to get the actual file mod time.
1111 // The IntegrationTestBuilder creates a temporary directory.
1112 // We need to get the path to the created files.
1113 // The `b.Fs` field gives access to the file system.
1114 c1Path := filepath.Join(b.Cfg.WorkingDir, "content", "section", "2012-02-21-noslug.md")
1115 c2Path := filepath.Join(b.Cfg.WorkingDir, "content", "section", "2012-02-22-slug.md")
1116
1117 c1fi, err := b.fs.Source.Stat(c1Path)
1118 b.Assert(err, qt.IsNil)
1119 c2fi, err := b.fs.Source.Stat(c2Path)
1120 b.Assert(err, qt.IsNil)
1121
1122 b.Assert(noSlug.Date().Year(), qt.Equals, c1fi.ModTime().Year())
1123 b.Assert(slug.Date().Year(), qt.Equals, c2fi.ModTime().Year())
1124 fallthrough
1125 default:
1126 b.Assert(noSlug.Slug(), qt.Equals, "")
1127 b.Assert(slug.Slug(), qt.Equals, "aslug")
1128
1129 }
1130 })
1131 }
1132 }
1133
1134 func TestWordCountWithAllCJKRunesWithoutHasCJKLanguage(t *testing.T) {
1135 t.Parallel()
1136 htesting.SkipSlowTestUnlessCI(t)
1137 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
1138 p := pages[0]
1139 if p.WordCount(context.Background()) != 8 {
1140 t.Fatalf("[%s] incorrect word count. expected %v, got %v", ext, 8, p.WordCount(context.Background()))
1141 }
1142 }
1143
1144 testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithAllCJKRunes)
1145 }
1146
1147 func TestWordCountWithAllCJKRunesHasCJKLanguage(t *testing.T) {
1148 t.Parallel()
1149 htesting.SkipSlowTestUnlessCI(t)
1150 settings := map[string]any{"hasCJKLanguage": true}
1151
1152 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
1153 p := pages[0]
1154 if p.WordCount(context.Background()) != 15 {
1155 t.Fatalf("[%s] incorrect word count, expected %v, got %v", ext, 15, p.WordCount(context.Background()))
1156 }
1157 }
1158 testAllMarkdownEnginesForPages(t, assertFunc, settings, simplePageWithAllCJKRunes)
1159 }
1160
1161 func TestWordCountWithMainEnglishWithCJKRunes(t *testing.T) {
1162 t.Parallel()
1163 htesting.SkipSlowTestUnlessCI(t)
1164 settings := map[string]any{"hasCJKLanguage": true}
1165
1166 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
1167 p := pages[0]
1168 if p.WordCount(context.Background()) != 74 {
1169 t.Fatalf("[%s] incorrect word count, expected %v, got %v", ext, 74, p.WordCount(context.Background()))
1170 }
1171 }
1172
1173 testAllMarkdownEnginesForPages(t, assertFunc, settings, simplePageWithMainEnglishWithCJKRunes)
1174 }
1175
1176 func TestWordCountWithIsCJKLanguageFalse(t *testing.T) {
1177 t.Parallel()
1178 htesting.SkipSlowTestUnlessCI(t)
1179 settings := map[string]any{
1180 "hasCJKLanguage": true,
1181 }
1182
1183 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
1184 p := pages[0]
1185 if p.WordCount(context.Background()) != 75 {
1186 t.Fatalf("[%s] incorrect word count for content '%s'. expected %v, got %v", ext, p.Plain(context.Background()), 74, p.WordCount(context.Background()))
1187 }
1188 }
1189
1190 testAllMarkdownEnginesForPages(t, assertFunc, settings, simplePageWithIsCJKLanguageFalse)
1191 }
1192
1193 func TestWordCount(t *testing.T) {
1194 t.Parallel()
1195 htesting.SkipSlowTestUnlessCI(t)
1196 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
1197 p := pages[0]
1198 if p.WordCount(context.Background()) != 483 {
1199 t.Fatalf("[%s] incorrect word count. expected %v, got %v", ext, 483, p.WordCount(context.Background()))
1200 }
1201
1202 if p.FuzzyWordCount(context.Background()) != 500 {
1203 t.Fatalf("[%s] incorrect word count. expected %v, got %v", ext, 500, p.FuzzyWordCount(context.Background()))
1204 }
1205
1206 if p.ReadingTime(context.Background()) != 3 {
1207 t.Fatalf("[%s] incorrect min read. expected %v, got %v", ext, 3, p.ReadingTime(context.Background()))
1208 }
1209 }
1210
1211 testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithLongContent)
1212 }
1213
1214 func TestPagePaths(t *testing.T) {
1215 t.Parallel()
1216
1217 siteParmalinksSetting := `
1218 [permalinks]
1219 post = ":year/:month/:day/:title/"
1220 `
1221
1222 tests := []struct {
1223 content string
1224 path string
1225 hasPermalink bool
1226 expected string
1227 }{
1228 {simplePage, "post/x.md", false, "/post/x/"},
1229 {simplePageWithURL, "post/x.md", false, "/simple/url/"},
1230 {simplePageWithSlug, "post/x.md", false, "/post/simple-slug/"},
1231 {simplePageWithDate, "post/x.md", true, "/2013/10/15/simple/"},
1232 {UTF8Page, "post/x.md", false, "/post/x/"},
1233 {UTF8PageWithURL, "post/x.md", false, "/%E3%83%A9%E3%83%BC%E3%83%A1%E3%83%B3/url/"},
1234 {UTF8PageWithSlug, "post/x.md", false, "/post/%E3%83%A9%E3%83%BC%E3%83%A1%E3%83%B3-slug/"},
1235 {UTF8PageWithDate, "post/x.md", true, "/2013/10/15/%E3%83%A9%E3%83%BC%E3%83%A1%E3%83%B3/"},
1236 }
1237
1238 for i, test := range tests {
1239 t.Run(fmt.Sprintf("Test%d", i), func(t *testing.T) {
1240 t.Parallel()
1241
1242 configContent := `baseURL = "http://example.com/"`
1243 if test.hasPermalink {
1244 configContent += siteParmalinksSetting
1245 }
1246
1247 files := `
1248 -- hugo.toml --
1249 ` + configContent + `
1250 -- content/` + test.path + ` --
1251 ` + test.content + `
1252 `
1253 b := Test(t, files, TestOptSkipRender())
1254
1255 s := b.H.Sites[0]
1256 b.Assert(len(s.RegularPages()), qt.Equals, 1)
1257 p := s.RegularPages()[0]
1258 b.Assert(p.RelPermalink(), qt.Equals, test.expected)
1259 })
1260 }
1261 }
1262
1263 func TestTranslationKey(t *testing.T) {
1264 files := `
1265 -- hugo.toml --
1266 disableKinds = ["taxonomy", "term"]
1267 defaultContentLanguage = "en"
1268 defaultContentLanguageInSubdir = true
1269 [languages]
1270 [languages.en]
1271 weight = 1
1272 [languages.nn]
1273 weight = 2
1274 -- content/sect/p1.en.md --
1275 ---
1276 translationkey: "adfasdf"
1277 title: "p1 en"
1278 ---
1279 -- content/sect/p1.nn.md --
1280 ---
1281 translationkey: "adfasdf"
1282 title: "p1 nn"
1283 ---
1284 -- layouts/single.html --
1285 Title: {{ .Title }}|TranslationKey: {{ .TranslationKey }}|
1286 Translations: {{ range .Translations }}{{ .Language.Lang }}|{{ end }}|
1287 AllTranslations: {{ range .AllTranslations }}{{ .Language.Lang }}|{{ end }}|
1288
1289 `
1290 b := Test(t, files)
1291 b.AssertFileContent(
1292 "public/en/sect/p1/index.html",
1293 "TranslationKey: adfasdf|",
1294 "AllTranslations: en|nn||",
1295 "Translations: nn||",
1296 )
1297
1298 b.AssertFileContent(
1299 "public/nn/sect/p1/index.html",
1300 "TranslationKey: adfasdf|",
1301 "Translations: en||",
1302 "AllTranslations: en|nn||",
1303 )
1304 }
1305
1306 func TestTranslationKeyTermPages(t *testing.T) {
1307 t.Parallel()
1308
1309 files := `
1310 -- hugo.toml --
1311 disableKinds = ['home','rss','section','sitemap','taxonomy']
1312 defaultContentLanguage = 'en'
1313 defaultContentLanguageInSubdir = true
1314 [languages.en]
1315 weight = 1
1316 [languages.pt]
1317 weight = 2
1318 [taxonomies]
1319 category = 'categories'
1320 -- layouts/list.html --
1321 {{ .IsTranslated }}|{{ range .Translations }}{{ .RelPermalink }}|{{ end }}
1322 -- layouts/single.html --
1323 {{ .Title }}|
1324 -- content/p1.en.md --
1325 ---
1326 title: p1 (en)
1327 categories: [music]
1328 ---
1329 -- content/p1.pt.md --
1330 ---
1331 title: p1 (pt)
1332 categories: [música]
1333 ---
1334 -- content/categories/music/_index.en.md --
1335 ---
1336 title: music
1337 translationKey: foo
1338 ---
1339 -- content/categories/música/_index.pt.md --
1340 ---
1341 title: música
1342 translationKey: foo
1343 ---
1344 `
1345
1346 b := Test(t, files)
1347 b.AssertFileContent("public/en/categories/music/index.html", "true|/pt/categories/m%C3%BAsica/|")
1348 b.AssertFileContent("public/pt/categories/música/index.html", "true|/en/categories/music/|")
1349 }
1350
1351 // Issue #11540.
1352 func TestTranslationKeyResourceSharing(t *testing.T) {
1353 files := `
1354 -- hugo.toml --
1355 disableKinds = ["taxonomy", "term"]
1356 defaultContentLanguage = "en"
1357 defaultContentLanguageInSubdir = true
1358 [languages]
1359 [languages.en]
1360 weight = 1
1361 [languages.nn]
1362 weight = 2
1363 -- content/sect/mybundle_en/index.en.md --
1364 ---
1365 translationkey: "adfasdf"
1366 title: "mybundle en"
1367 ---
1368 -- content/sect/mybundle_en/f1.txt --
1369 f1.en
1370 -- content/sect/mybundle_en/f2.txt --
1371 f2.en
1372 -- content/sect/mybundle_nn/index.nn.md --
1373 ---
1374 translationkey: "adfasdf"
1375 title: "mybundle nn"
1376 ---
1377 -- content/sect/mybundle_nn/f2.nn.txt --
1378 f2.nn
1379 -- layouts/single.html --
1380 Title: {{ .Title }}|TranslationKey: {{ .TranslationKey }}|
1381 Resources: {{ range .Resources }}{{ .RelPermalink }}|{{ .Content }}|{{ end }}|
1382
1383 `
1384 b := Test(t, files)
1385 b.AssertFileContent(
1386 "public/en/sect/mybundle_en/index.html",
1387 "TranslationKey: adfasdf|",
1388 "Resources: /en/sect/mybundle_en/f1.txt|f1.en|/en/sect/mybundle_en/f2.txt|f2.en||",
1389 )
1390
1391 b.AssertFileContent(
1392 "public/nn/sect/mybundle_nn/index.html",
1393 "TranslationKey: adfasdf|",
1394 "Title: mybundle nn|TranslationKey: adfasdf|\nResources: /en/sect/mybundle_en/f1.txt|f1.en|/nn/sect/mybundle_nn/f2.nn.txt|f2.nn||",
1395 )
1396 }
1397
1398 func TestTranslationKeyRotate(t *testing.T) {
1399 t.Parallel()
1400
1401 files := `
1402 -- hugo.toml --
1403 disableKinds = ['home','rss','section','sitemap','taxonomy']
1404 defaultContentLanguage = 'en'
1405 defaultContentLanguageInSubdir = true
1406 [languages]
1407 [languages.en]
1408 weight = 1
1409 [languages.pt]
1410 weight = 2
1411 -- content/foo.md --
1412 ---
1413 title: Foo
1414 translationkey: "mykey"
1415 ---
1416 -- content/bar.md --
1417 ---
1418 title: Bar
1419 translationkey: "mykey"
1420 ---
1421 -- layouts/all.html --
1422 Rotate(language): {{ with .Rotate "language" }}{{ range . }}{{ template "printp" . }}|{{ end }}{{ end }}$
1423 {{ define "printp" }}{{ .RelPermalink }}:{{ with .Site }}{{ template "prints" . }}{{ end }}{{ end }}
1424 {{ define "prints" }}/l:{{ .Language.Name }}/v:{{ .Version.Name }}/r:{{ .Role.Name }}{{ end }}
1425 `
1426
1427 b := Test(t, files)
1428
1429 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|$")
1430 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|$")
1431 }
1432
1433 func TestChompBOM(t *testing.T) {
1434 t.Parallel()
1435 const utf8BOM = "\xef\xbb\xbf"
1436
1437 files := `
1438 -- hugo.toml --
1439 baseURL = "http://example.com/"
1440 -- content/simple.md --
1441 ` + utf8BOM + simplePage + `
1442 `
1443 b := Test(t, files, TestOptSkipRender())
1444
1445 b.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 1)
1446
1447 p := b.H.Sites[0].RegularPages()[0]
1448
1449 checkPageTitle(t, p, "Simple")
1450 }
1451
1452 // https://github.com/gohugoio/hugo/issues/5381
1453 func TestPageManualSummary(t *testing.T) {
1454 t.Parallel()
1455
1456 files := `
1457 -- hugo.toml --
1458 baseURL = "http://example.com/"
1459 [security]
1460 allowContent = ['.*']
1461 -- content/page-md-shortcode.md --
1462 ---
1463 title: "Hugo"
1464 ---
1465 This is a {{< sc >}}.
1466 <!--more-->
1467 Content.
1468 -- content/page-md-only-shortcode.md --
1469 ---
1470 title: "Hugo"
1471 ---
1472 {{< sc >}}
1473 <!--more-->
1474 {{< sc >}}
1475 -- content/page-md-shortcode-same-line.md --
1476 ---
1477 title: "Hugo"
1478 ---
1479 This is a {{< sc >}}<!--more-->Same line.
1480 -- content/page-md-shortcode-same-line-after.md --
1481 ---
1482 title: "Hugo"
1483 ---
1484 Summary<!--more-->{{< sc >}}
1485 -- content/page-org-shortcode.org --
1486 #+TITLE: T1
1487 #+AUTHOR: A1
1488 #+DESCRIPTION: D1
1489 This is a {{< sc >}}.
1490 # more
1491 Content.
1492 -- content/page-org-variant1.org --
1493 #+TITLE: T1
1494 Summary.
1495
1496 # more
1497
1498 Content.
1499 -- layouts/_shortcodes/sc.html --
1500 a shortcode
1501 -- layouts/single.html --
1502 SUMMARY:{{ .Summary }}:END
1503 --------------------------
1504 CONTENT:{{ .Content }}
1505 `
1506 b := Test(t, files)
1507
1508 b.AssertFileContent(
1509 "public/page-md-shortcode/index.html",
1510 "SUMMARY:<p>This is a a shortcode.</p>:END",
1511 "CONTENT:<p>This is a a shortcode.</p>\n\n<p>Content.</p>\n",
1512 )
1513
1514 b.AssertFileContent(
1515 "public/page-md-shortcode-same-line/index.html",
1516 "SUMMARY:<p>This is a a shortcode</p>:END",
1517 "CONTENT:<p>This is a a shortcode</p>\n\n<p>Same line.</p>\n",
1518 )
1519
1520 b.AssertFileContent(
1521 "public/page-md-shortcode-same-line-after/index.html",
1522 "SUMMARY:<p>Summary</p>:END",
1523 "CONTENT:<p>Summary</p>\n\na shortcode",
1524 )
1525
1526 b.AssertFileContent(
1527 "public/page-org-shortcode/index.html",
1528 "SUMMARY:<p>\nThis is a a shortcode.\n</p>:END",
1529 "CONTENT:<p>\nThis is a a shortcode.\n</p>\n<p>\nContent.\t\n</p>\n",
1530 )
1531 b.AssertFileContent(
1532 "public/page-org-variant1/index.html",
1533 "SUMMARY:<p>\nSummary.\n</p>:END",
1534 "CONTENT:<p>\nSummary.\n</p>\n<p>\nContent.\t\n</p>\n",
1535 )
1536
1537 b.AssertFileContent(
1538 "public/page-md-only-shortcode/index.html",
1539 "SUMMARY:a shortcode:END",
1540 "CONTENT:a shortcode\n\na shortcode\n",
1541 )
1542 }
1543
1544 func TestHomePageWithNoTitle(t *testing.T) {
1545 t.Parallel()
1546
1547 files := `
1548 -- hugo.toml --
1549 baseURL = "http://example.com/"
1550 title = "Site Title"
1551 -- layouts/home.html --
1552 Title|{{ with .Title }}{{ . }}{{ end }}|
1553 -- content/_index.md --
1554 ---
1555 description: "No title for you!"
1556 ---
1557
1558 Content.
1559 `
1560 b := Test(t, files)
1561
1562 b.AssertFileContent("public/index.html", "Title||")
1563 }
1564
1565 func TestShouldBuild(t *testing.T) {
1566 past := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
1567 future := time.Date(2037, 11, 17, 20, 34, 58, 651387237, time.UTC)
1568 zero := time.Time{}
1569
1570 publishSettings := []struct {
1571 buildFuture bool
1572 buildExpired bool
1573 buildDrafts bool
1574 draft bool
1575 publishDate time.Time
1576 expiryDate time.Time
1577 out bool
1578 }{
1579 // publishDate and expiryDate
1580 {false, false, false, false, zero, zero, true},
1581 {false, false, false, false, zero, future, true},
1582 {false, false, false, false, past, zero, true},
1583 {false, false, false, false, past, future, true},
1584 {false, false, false, false, past, past, false},
1585 {false, false, false, false, future, future, false},
1586 {false, false, false, false, future, past, false},
1587
1588 // buildFuture and buildExpired
1589 {false, true, false, false, past, past, true},
1590 {true, true, false, false, past, past, true},
1591 {true, false, false, false, past, past, false},
1592 {true, false, false, false, future, future, true},
1593 {true, true, false, false, future, future, true},
1594 {false, true, false, false, future, past, false},
1595
1596 // buildDrafts and draft
1597 {true, true, false, true, past, future, false},
1598 {true, true, true, true, past, future, true},
1599 {true, true, true, true, past, future, true},
1600 }
1601
1602 for _, ps := range publishSettings {
1603 s := shouldBuild(ps.buildFuture, ps.buildExpired, ps.buildDrafts, ps.draft,
1604 ps.publishDate, ps.expiryDate)
1605 if s != ps.out {
1606 t.Errorf("AssertShouldBuild unexpected output with params: %+v", ps)
1607 }
1608 }
1609 }
1610
1611 func TestShouldBuildWithClock(t *testing.T) {
1612 htime.Clock = clocks.Start(time.Date(2021, 11, 17, 20, 34, 58, 651387237, time.UTC))
1613 t.Cleanup(func() { htime.Clock = clocks.System() })
1614 past := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
1615 future := time.Date(2037, 11, 17, 20, 34, 58, 651387237, time.UTC)
1616 zero := time.Time{}
1617
1618 publishSettings := []struct {
1619 buildFuture bool
1620 buildExpired bool
1621 buildDrafts bool
1622 draft bool
1623 publishDate time.Time
1624 expiryDate time.Time
1625 out bool
1626 }{
1627 // publishDate and expiryDate
1628 {false, false, false, false, zero, zero, true},
1629 {false, false, false, false, zero, future, true},
1630 {false, false, false, false, past, zero, true},
1631 {false, false, false, false, past, future, true},
1632 {false, false, false, false, past, past, false},
1633 {false, false, false, false, future, future, false},
1634 {false, false, false, false, future, past, false},
1635
1636 // buildFuture and buildExpired
1637 {false, true, false, false, past, past, true},
1638 {true, true, false, false, past, past, true},
1639 {true, false, false, false, past, past, false},
1640 {true, false, false, false, future, future, true},
1641 {true, true, false, false, future, future, true},
1642 {false, true, false, false, future, past, false},
1643
1644 // buildDrafts and draft
1645 {true, true, false, true, past, future, false},
1646 {true, true, true, true, past, future, true},
1647 {true, true, true, true, past, future, true},
1648 }
1649
1650 for _, ps := range publishSettings {
1651 s := shouldBuild(ps.buildFuture, ps.buildExpired, ps.buildDrafts, ps.draft,
1652 ps.publishDate, ps.expiryDate)
1653 if s != ps.out {
1654 t.Errorf("AssertShouldBuildWithClock unexpected output with params: %+v", ps)
1655 }
1656 }
1657 }
1658
1659 // See https://github.com/gohugoio/hugo/issues/9171
1660 // We redefined disablePathToLower in v0.121.0.
1661 func TestPagePathDisablePathToLower(t *testing.T) {
1662 files := `
1663 -- hugo.toml --
1664 baseURL = "http://example.com"
1665 disablePathToLower = true
1666 [permalinks]
1667 sect2 = "/:section/:contentbasename/"
1668 sect3 = "/:section/:title/"
1669 -- content/sect/p1.md --
1670 ---
1671 title: "Page1"
1672 ---
1673 p1.
1674 -- content/sect/p2.md --
1675 ---
1676 title: "Page2"
1677 slug: "PaGe2"
1678 ---
1679 p2.
1680 -- content/sect2/PaGe3.md --
1681 ---
1682 title: "Page3"
1683 ---
1684 -- content/seCt3/p4.md --
1685 ---
1686 title: "Pag.E4"
1687 slug: "PaGe4"
1688 ---
1689 p4.
1690 -- layouts/single.html --
1691 Single: {{ .Title}}|{{ .RelPermalink }}|{{ .Path }}|
1692 `
1693 b := Test(t, files)
1694 b.AssertFileContent("public/sect/p1/index.html", "Single: Page1|/sect/p1/|/sect/p1")
1695 b.AssertFileContent("public/sect/PaGe2/index.html", "Single: Page2|/sect/PaGe2/|/sect/p2")
1696 b.AssertFileContent("public/sect2/PaGe3/index.html", "Single: Page3|/sect2/PaGe3/|/sect2/page3|")
1697 b.AssertFileContent("public/sect3/Pag.E4/index.html", "Single: Pag.E4|/sect3/Pag.E4/|/sect3/p4|")
1698 }
1699
1700 func TestScratch(t *testing.T) {
1701 t.Parallel()
1702
1703 files := `
1704 -- hugo.toml --
1705 baseURL = "http://example.com/"
1706 -- layouts/home.html --
1707 {{ .Scratch.Set "b" "bv" }}
1708 B: {{ .Scratch.Get "b" }}
1709 -- layouts/_shortcodes/scratch.html --
1710 {{ .Scratch.Set "c" "cv" }}
1711 C: {{ .Scratch.Get "c" }}
1712 -- layouts/single.html --
1713 {{ .Content }}
1714 -- content/scratchme.md --
1715 ---
1716 title: Scratch Me!
1717 ---
1718
1719 {{< scratch >}}
1720 `
1721 b, err := TestE(t, files)
1722
1723 b.Assert(err, qt.IsNil)
1724
1725 b.AssertFileContent("public/index.html", "B: bv")
1726 b.AssertFileContent("public/scratchme/index.html", "C: cv")
1727 }
1728
1729 // Issue 13016.
1730 func TestScratchAliasToStore(t *testing.T) {
1731 t.Parallel()
1732
1733 files := `
1734 -- hugo.toml --
1735 disableKinds = ["taxonomy", "term", "page", "section"]
1736 disableLiveReload = true
1737 -- layouts/home.html --
1738 {{ .Scratch.Set "a" "b" }}
1739 {{ .Store.Set "c" "d" }}
1740 .Scratch eq .Store: {{ eq .Scratch .Store }}
1741 a: {{ .Store.Get "a" }}
1742 c: {{ .Scratch.Get "c" }}
1743
1744 `
1745
1746 b := Test(t, files)
1747
1748 b.AssertFileContent(
1749 "public/index.html",
1750 ".Scratch eq .Store: true",
1751 "a: b",
1752 "c: d",
1753 )
1754 }
1755
1756 func TestPageParam(t *testing.T) {
1757 t.Parallel()
1758
1759 files := `
1760 -- hugo.toml --
1761 baseURL = "https://example.org"
1762
1763 [params]
1764 [params.author]
1765 name = "Kurt Vonnegut"
1766 -- layouts/home.html --
1767
1768 {{ $withParam := .Site.GetPage "withparam" }}
1769 {{ $noParam := .Site.GetPage "noparam" }}
1770 {{ $withStringParam := .Site.GetPage "withstringparam" }}
1771
1772 Author page: {{ $withParam.Param "author.name" }}
1773 Author name page string: {{ $withStringParam.Param "author.name" }}|
1774 Author page string: {{ $withStringParam.Param "author" }}|
1775 Author project config: {{ $noParam.Param "author.name" }}
1776
1777 -- content/withparam.md --
1778 +++
1779 title = "With Param!"
1780 [author]
1781 name = "Ernest Miller Hemingway"
1782
1783 +++
1784 -- content/noparam.md --
1785 ---
1786 title: "No Param!"
1787 ---
1788 -- content/withstringparam.md --
1789 +++
1790 title = "With string Param!"
1791 author = "Jo Nesbø"
1792
1793 +++
1794 `
1795 b := Test(t, files)
1796
1797 b.AssertFileContent("public/index.html",
1798 "Author page: Ernest Miller Hemingway",
1799 "Author name page string: Kurt Vonnegut|",
1800 "Author page string: Jo Nesbø|",
1801 "Author project config: Kurt Vonnegut")
1802 }
1803
1804 func TestGoldmark(t *testing.T) {
1805 t.Parallel()
1806
1807 content := `
1808 +++
1809 title = "A Page!"
1810 +++
1811
1812 ## Shortcode {{% t %}} in header
1813
1814 ## Code Fense in Shortcode
1815
1816 {{% s %}}
1817 $$$bash {hl_lines=[1]}
1818 SHORT
1819 $$$
1820 {{% /s %}}
1821
1822 ## Code Fence
1823
1824 $$$bash {hl_lines=[1]}
1825 MARKDOWN
1826 $$$
1827
1828 Link with URL as text
1829
1830 [https://google.com](https://google.com)
1831
1832
1833 `
1834 content = strings.ReplaceAll(content, "$$$", "```")
1835
1836 files := `
1837 -- hugo.toml --
1838 baseURL = "https://example.org"
1839
1840 [markup]
1841 defaultMarkdownHandler="goldmark"
1842 [markup.goldmark]
1843 [markup.goldmark.renderer]
1844 unsafe = false
1845 [markup.highlight]
1846 noClasses=false
1847 -- layouts/single.html --
1848 Title: {{ .Title }}
1849 ToC: {{ .TableOfContents }}
1850 Content: {{ .Content }}
1851 -- layouts/_shortcodes/t.html --
1852 T-SHORT
1853 -- layouts/_shortcodes/s.html --
1854 ## Code
1855 {{ .Inner }}
1856 -- content/page.md --
1857 ` + content + `
1858 `
1859 b := Test(t, files)
1860
1861 b.AssertFileContent("public/page/index.html",
1862 `<nav id="TableOfContents">
1863 <li><a href="#shortcode-t-short-in-header">Shortcode T-SHORT in header</a></li>
1864 <code class="language-bash" data-lang="bash"><span class="line hl"><span class="cl">SHORT
1865 <code class="language-bash" data-lang="bash"><span class="line hl"><span class="cl">MARKDOWN
1866 <p><a href="https://google.com">https://google.com</a></p>
1867 `)
1868 }
1869
1870 func TestPageHashString(t *testing.T) {
1871 files := `
1872 -- hugo.toml --
1873 baseURL = "https://example.org"
1874 [languages]
1875 [languages.en]
1876 weight = 1
1877 title = "English"
1878 [languages.no]
1879 weight = 2
1880 title = "Norsk"
1881 -- content/p1.md --
1882 ---
1883 title: "p1"
1884 ---
1885 -- content/p2.md --
1886 ---
1887 title: "p2"
1888 ---
1889 `
1890
1891 b := Test(t, files)
1892
1893 p1 := b.H.Sites[0].RegularPages()[0]
1894 p2 := b.H.Sites[0].RegularPages()[1]
1895 sites := p1.Sites()
1896
1897 b.Assert(p1, qt.Not(qt.Equals), p2)
1898
1899 b.Assert(hashing.HashString(p1), qt.Not(qt.Equals), hashing.HashString(p2))
1900 b.Assert(hashing.HashString(sites[0]), qt.Not(qt.Equals), hashing.HashString(sites[1]))
1901 }
1902
1903 // Issue #11243
1904 func TestRenderWithoutArgument(t *testing.T) {
1905 t.Parallel()
1906
1907 files := `
1908 -- hugo.toml --
1909 -- layouts/home.html --
1910 {{ .Render }}
1911 `
1912
1913 b, err := TestE(t, files)
1914
1915 b.Assert(err, qt.IsNotNil)
1916 }
1917
1918 // See issue 15077.
1919 func TestRenderWithContext(t *testing.T) {
1920 t.Parallel()
1921
1922 files := `
1923 -- hugo.toml --
1924 -- content/p1.md --
1925 ---
1926 title: "P1"
1927 ---
1928 -- layouts/page.html --
1929 {{ .Render "li" }}|{{ .Render "li" (dict "Title" "Custom") }}
1930 -- layouts/li.html --
1931 Title: {{ .Title }}{{- /**/ -}}
1932 `
1933
1934 b := Test(t, files)
1935
1936 b.AssertFileContent("public/p1/index.html", "Title: P1|Title: Custom")
1937 }
1938
1939 // See issue 15077.
1940 func TestRenderWithContextErrors(t *testing.T) {
1941 t.Parallel()
1942
1943 filesTemplate := `
1944 -- hugo.toml --
1945 -- content/p1.md --
1946 ---
1947 title: "P1"
1948 ---
1949 -- layouts/li.html --
1950 li
1951 -- layouts/page.html --
1952 RENDER
1953 `
1954
1955 for _, test := range []struct {
1956 render string
1957 message string
1958 }{
1959 {`{{ .Render "li" "foo" "bar" }}`, `(?s).*too many arguments, expected VIEW \[CONTEXT\].*`},
1960 {`{{ .Render (slice "li") }}`, `(?s).*failed to convert view argument to string: unable to cast \[\]string{"li"} of type \[\]string to string.*`},
1961 } {
1962 files := strings.ReplaceAll(filesTemplate, "RENDER", test.render)
1963 b, err := TestE(t, files)
1964 b.Assert(err, qt.ErrorMatches, test.message)
1965 }
1966 }
1967
1968 // Issue #13021
1969 func TestAllStores(t *testing.T) {
1970 t.Parallel()
1971
1972 files := `
1973 -- hugo.toml --
1974 disableKinds = ["taxonomy", "term", "page", "section"]
1975 disableLiveReload = true
1976 -- content/_index.md --
1977 ---
1978 title: "Home"
1979 ---
1980 {{< s >}}
1981 -- layouts/_shortcodes/s.html --
1982 {{ if not (.Store.Get "Shortcode") }}{{ .Store.Set "Shortcode" (printf "sh-%s" $.Page.Title) }}{{ end }}
1983 Shortcode: {{ .Store.Get "Shortcode" }}|
1984 -- layouts/home.html --
1985 {{ .Content }}
1986 {{ if not (.Store.Get "Page") }}{{ .Store.Set "Page" (printf "p-%s" $.Title) }}{{ end }}
1987 {{ if not (hugo.Store.Get "Hugo") }}{{ hugo.Store.Set "Hugo" (printf "h-%s" $.Title) }}{{ end }}
1988 {{ if not (site.Store.Get "Site") }}{{ site.Store.Set "Site" (printf "s-%s" $.Title) }}{{ end }}
1989 Page: {{ .Store.Get "Page" }}|
1990 Hugo: {{ hugo.Store.Get "Hugo" }}|
1991 Site: {{ site.Store.Get "Site" }}|
1992 `
1993
1994 b := TestRunning(t, files)
1995
1996 b.AssertFileContent(
1997 "public/index.html",
1998 `
1999 Shortcode: sh-Home|
2000 Page: p-Home|
2001 Site: s-Home|
2002 Hugo: h-Home|
2003 `,
2004 )
2005
2006 b.EditFileReplaceAll("content/_index.md", "Home", "Homer").Build()
2007
2008 b.AssertFileContent(
2009 "public/index.html",
2010 `
2011 Shortcode: sh-Homer|
2012 Page: p-Homer|
2013 Site: s-Home|
2014 Hugo: h-Home|
2015 `,
2016 )
2017 }
2018
2019 // Issue 13538
2020 func TestHomePageIsLeafBundle(t *testing.T) {
2021 t.Parallel()
2022
2023 files := `
2024 -- hugo.toml --
2025 defaultContentLanguage = 'de'
2026 defaultContentLanguageInSubdir = true
2027 [security]
2028 allowContent = ['.*']
2029 [languages.de]
2030 weight = 1
2031 [languages.en]
2032 weight = 2
2033 -- layouts/all.html --
2034 {{ .Title }}
2035 -- content/index.de.md --
2036 ---
2037 title: home de
2038 ---
2039 -- content/index.en.org --
2040 ---
2041 title: home en
2042 ---
2043 `
2044
2045 b := Test(t, files, TestOptWarn())
2046
2047 b.AssertFileContent("public/de/index.html", "home de")
2048 b.AssertFileContent("public/en/index.html", "home en")
2049 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.")
2050 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.")
2051 }
2052
2053 func content(c resource.ContentProvider) string {
2054 cc, err := c.Content(context.Background())
2055 if err != nil {
2056 panic(err)
2057 }
2058
2059 ccs, err := cast.ToStringE(cc)
2060 if err != nil {
2061 panic(err)
2062 }
2063 return ccs
2064 }
2065
2066 // See issue 11574.
2067 func TestPageIsBranch(t *testing.T) {
2068 t.Parallel()
2069
2070 files := `
2071 -- hugo.toml --
2072 disableKinds = ["taxonomy", "term", "rss", "sitemap", "robotsTXT", "404"]
2073 -- content/_index.md --
2074 -- content/sect/_index.md --
2075 -- content/sect/p1.md --
2076 -- layouts/all.html --
2077 {{ .Kind }}|IsBranch={{ .IsBranch }}|IsPage={{ .IsPage }}
2078 `
2079
2080 b := Test(t, files)
2081
2082 b.AssertFileContent("public/index.html", "home|IsBranch=true|IsPage=false")
2083 b.AssertFileContent("public/sect/index.html", "section|IsBranch=true|IsPage=false")
2084 b.AssertFileContent("public/sect/p1/index.html", "page|IsBranch=false|IsPage=true")
2085 }
2086
2087 // See issue 11574.
2088 func TestPageIsNodeDeprecated(t *testing.T) {
2089 files := `
2090 -- hugo.toml --
2091 disableKinds = ["taxonomy", "term", "rss", "sitemap", "robotsTXT", "404"]
2092 -- content/_index.md --
2093 -- layouts/all.html --
2094 {{ .IsNode }}
2095 `
2096
2097 b := Test(t, files, TestOptInfo())
2098
2099 b.AssertLogContains(".Page.IsNode was deprecated")
2100 }
2101
2102 func BenchmarkIsTranslatedOneLanguage(b *testing.B) {
2103 // Set it reasonably high to get a balance between cached and uncached calls to IsTranslated.
2104 const numPages = 3000
2105
2106 var files strings.Builder
2107 files.WriteString(`
2108 -- hugo.toml --
2109 disableKinds = ["taxonomy", "term"]
2110 `)
2111 for i := range numPages {
2112 files.WriteString(fmt.Sprintf(`
2113 -- content/sect/p%d.md --`, i))
2114 }
2115
2116 bb := Test(b, files.String(), TestOptSkipRender())
2117 p := bb.H.Sites[0].RegularPages()
2118
2119 b.ResetTimer()
2120
2121 for i := range b.N {
2122 if p[i%numPages].IsTranslated() {
2123 b.Fatalf("Page %d should not be translated", i)
2124 }
2125 }
2126 }
2127
2128 // See issue 15052.
2129 func TestRenderViewNotFound(t *testing.T) {
2130 t.Parallel()
2131
2132 files := `
2133 -- hugo.toml --
2134 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
2135 -- layouts/home.html --
2136 {{ .Render "view_foo" }}
2137 {{ .Render "view_bar" }}
2138 -- layouts/view_foo.html --
2139 foo
2140 `
2141
2142 b, err := TestE(t, files)
2143 b.Assert(err, qt.ErrorMatches, `.*template "view_bar" not found.*`)
2144 }
2145
2146 // See issue 15057.
2147 func TestRenderCaseInsensitiveTemplateName(t *testing.T) {
2148 t.Parallel()
2149
2150 files := `
2151 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2152 -- content/s1/p1.md --
2153 ---
2154 title: p1
2155 ---
2156 -- content/s2/p2.md --
2157 ---
2158 title: p2
2159 layout: ab
2160 ---
2161 -- content/s2/p3.md --
2162 ---
2163 title: p3
2164 layout: aB
2165 ---
2166 -- content/s2/p4.md --
2167 ---
2168 title: p4
2169 layout: AB
2170 ---
2171 -- content/s3/p5.md --
2172 ---
2173 title: p5
2174 layout: cd
2175 ---
2176 -- content/s3/p6.md --
2177 ---
2178 title: p6
2179 layout: cD
2180 ---
2181 -- content/s3/p7.md --
2182 ---
2183 title: p7
2184 layout: CD
2185 ---
2186 -- content/s4/p8.md --
2187 ---
2188 title: p8
2189 layout: ef
2190 ---
2191 -- content/s4/p9.md --
2192 ---
2193 title: p9
2194 layout: eF
2195 ---
2196 -- content/s4/p10.md --
2197 ---
2198 title: p10
2199 layout: EF
2200 ---
2201 -- layouts/s1/p1/page.html --
2202 {{ .Render "ab" }}|{{ .Render "aB" }}|{{ .Render "AB" }}
2203 {{ .Render "cd" }}|{{ .Render "cD" }}|{{ .Render "CD" }}
2204 {{ .Render "ef" }}|{{ .Render "eF" }}|{{ .Render "EF" }}
2205 -- layouts/s1/p1/ab.html --
2206 ab{{- /**/ -}}
2207 -- layouts/s1/p1/cD.html --
2208 cD{{- /**/ -}}
2209 -- layouts/s1/p1/EF.html --
2210 EF{{- /**/ -}}
2211 -- layouts/s2/ab.html --
2212 ab
2213 -- layouts/s3/cD.html --
2214 cD
2215 -- layouts/s4/EF.html --
2216 EF
2217 `
2218
2219 b := Test(t, files)
2220
2221 b.AssertFileContent(
2222 "public/s1/p1/index.html",
2223 "ab|ab|ab",
2224 "cD|cD|cD",
2225 "EF|EF|EF",
2226 )
2227
2228 b.AssertFileContent("public/s2/p2/index.html", "ab")
2229 b.AssertFileContent("public/s2/p3/index.html", "ab")
2230 b.AssertFileContent("public/s2/p4/index.html", "ab")
2231
2232 b.AssertFileContent("public/s3/p5/index.html", "cD")
2233 b.AssertFileContent("public/s3/p6/index.html", "cD")
2234 b.AssertFileContent("public/s3/p7/index.html", "cD")
2235
2236 b.AssertFileContent("public/s4/p8/index.html", "EF")
2237 b.AssertFileContent("public/s4/p9/index.html", "EF")
2238 b.AssertFileContent("public/s4/p10/index.html", "EF")
2239 }
2240
2241 // See issue 15056.
2242 func TestRenderViewSubdir(t *testing.T) {
2243 t.Parallel()
2244
2245 files := `
2246 -- hugo.toml --
2247 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2248 -- content/s1/p1.md --
2249 ---
2250 title: p1
2251 ---
2252 -- layouts/s1/p1/page.html --
2253 {{ .Render "a" }}|{{ .Render "b" }}|{{ .Render "c" }}|{{ .Render "foo/d" }}|{{ .Render "foo/e" }}|{{ .Render "foo/f" }}|{{ .Render "sub/foo/g" }}
2254 -- layouts/s1/p1/a.html --
2255 a{{- /**/ -}}
2256 -- layouts/s1/b.html --
2257 b{{- /**/ -}}
2258 -- layouts/c.html --
2259 c{{- /**/ -}}
2260 -- layouts/s1/p1/foo/d.html --
2261 d{{- /**/ -}}
2262 -- layouts/s1/foo/e.html --
2263 e{{- /**/ -}}
2264 -- layouts/foo/f.html --
2265 f{{- /**/ -}}
2266 -- layouts/sub/foo/g.html --
2267 g{{- /**/ -}}
2268 `
2269
2270 b := Test(t, files)
2271 b.AssertFileContent("public/s1/p1/index.html", "a|b|c|d|e|f|g")
2272 }
2273
2274 // See issue 15056.
2275 func TestRenderViewSubdirRootPage(t *testing.T) {
2276 t.Parallel()
2277
2278 files := `
2279 -- hugo.toml --
2280 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2281 -- content/p1.md --
2282 ---
2283 title: p1
2284 ---
2285 -- layouts/page.html --
2286 {{ .Render "foo/a" }}
2287 -- layouts/foo/a.html --
2288 a{{- /**/ -}}
2289 `
2290
2291 b := Test(t, files)
2292 b.AssertFileContent("public/p1/index.html", "a")
2293 }
2294
2295 // See issue 15056.
2296 func TestRenderViewSubdirNotFound(t *testing.T) {
2297 t.Parallel()
2298
2299 files := `
2300 -- hugo.toml --
2301 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2302 -- content/s1/p1.md --
2303 ---
2304 title: p1
2305 ---
2306 -- layouts/s1/p1/page.html --
2307 {{ .Render "foo/missing" }}
2308 -- layouts/s1/p1/missing.html --
2309 should-not-match{{- /**/ -}}
2310 `
2311
2312 b, err := TestE(t, files)
2313 b.Assert(err, qt.ErrorMatches, `.*template "foo/missing" not found.*`)
2314 }
2315
2316 // See issue 15056.
2317 func TestRenderViewSubdirTrailingSlash(t *testing.T) {
2318 t.Parallel()
2319
2320 files := `
2321 -- hugo.toml --
2322 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2323 -- content/s1/p1.md --
2324 ---
2325 title: p1
2326 ---
2327 -- layouts/s1/p1/page.html --
2328 {{ .Render "foo/" }}
2329 -- layouts/s1/p1/foo.html --
2330 should-not-match{{- /**/ -}}
2331 `
2332
2333 b, err := TestE(t, files)
2334 b.Assert(err, qt.ErrorMatches, `.*template "foo/" not found.*`)
2335 }
2336
2337 // See issue 15056.
2338 func TestRenderViewSubdirCaseInsensitiveArgument(t *testing.T) {
2339 t.Parallel()
2340
2341 files := `
2342 -- hugo.toml --
2343 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2344 -- content/s1/p1.md --
2345 ---
2346 title: p1
2347 ---
2348 -- layouts/s1/p1/page.html --
2349 {{ .Render "Foo/Bar" }}
2350 -- layouts/s1/p1/foo/bar.html --
2351 bar{{- /**/ -}}
2352 `
2353
2354 b := Test(t, files)
2355 b.AssertFileContent("public/s1/p1/index.html", "bar")
2356 }
2357
2358 // See issue 15056.
2359 func TestRenderViewSubdirCaseInsensitivePath(t *testing.T) {
2360 t.Parallel()
2361
2362 files := `
2363 -- hugo.toml --
2364 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2365 -- content/s1/p1.md --
2366 ---
2367 title: p1
2368 ---
2369 -- layouts/s1/p1/page.html --
2370 {{ .Render "foo/bar" }}
2371 -- layouts/s1/p1/Foo/Bar.html --
2372 bar{{- /**/ -}}
2373 `
2374
2375 b := Test(t, files)
2376 b.AssertFileContent("public/s1/p1/index.html", "bar")
2377 }
2378
2379 // See issue 15056.
2380 func TestRenderViewSubdirWithUnderscore(t *testing.T) {
2381 t.Parallel()
2382
2383 files := `
2384 -- hugo.toml --
2385 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2386 -- content/p1.md --
2387 ---
2388 title: p1
2389 ---
2390 -- content/s1/p2.md --
2391 ---
2392 title: p2
2393 ---
2394 -- content/s1/p3.md --
2395 ---
2396 title: p3
2397 ---
2398 -- layouts/page.html --
2399 {{ .Title }}: {{ .Render "_views/a" }}
2400 -- layouts/_views/a.html --
2401 layouts/_views/a.html
2402 -- layouts/s1/_views/a.html --
2403 layouts/s1/_views/a.html
2404 -- layouts/s1/p3/_views/a.html --
2405 layouts/s1/p3/_views/a.html
2406 `
2407
2408 b := Test(t, files)
2409
2410 b.AssertFileContent("public/p1/index.html", "p1: layouts/_views/a.html")
2411 b.AssertFileContent("public/s1/p2/index.html", "p2: layouts/s1/_views/a.html") // fails
2412 b.AssertFileContent("public/s1/p3/index.html", "p3: layouts/s1/p3/_views/a.html") // fails
2413 }
2414
2415 func TestPageConttentWeight(t *testing.T) {
2416 files := `
2417 -- hugo.toml --
2418 -- content/mysection/page1.md --
2419 -- content/myothersection/page2.md --
2420 -- content/myothersection/_index.md --
2421 -- layouts/all.html --
2422 All.
2423 `
2424 b := Test(t, files)
2425
2426 check := func(p page.Page, ok bool) {
2427 cw := p.(contentNodeContentWeightProvider).contentWeight()
2428 b.Assert(ok, qt.Equals, cw > 0)
2429 }
2430
2431 s := b.H.Sites[0]
2432 for _, p := range s.RegularPages() {
2433 check(p, true)
2434 }
2435 check(s.home, false)
2436 mysection, _ := s.GetPage("mysection")
2437 check(mysection, false)
2438 myothersection, _ := s.GetPage("myothersection") // backed by a content file.
2439 check(myothersection, true)
2440 }
2441
2442 // See issue 15206.
2443 func TestReadingTimeAndFuzzyWordCountBoundaries(t *testing.T) {
2444 t.Parallel()
2445
2446 files := `
2447 -- hugo.toml --
2448 disableKinds = ["home", "section", "taxonomy", "term", "rss", "sitemap"]
2449 -- content/p99.md --
2450 ---
2451 title: p99
2452 ---
2453 ` + strings.Repeat("word ", 99) + `
2454 -- content/p100.md --
2455 ---
2456 title: p100
2457 ---
2458 ` + strings.Repeat("word ", 100) + `
2459 -- content/p101.md --
2460 ---
2461 title: p101
2462 ---
2463 ` + strings.Repeat("word ", 101) + `
2464 -- content/p211.md --
2465 ---
2466 title: p211
2467 ---
2468 ` + strings.Repeat("word ", 211) + `
2469 -- content/p212.md --
2470 ---
2471 title: p212
2472 ---
2473 ` + strings.Repeat("word ", 212) + `
2474 -- content/p213.md --
2475 ---
2476 title: p213
2477 ---
2478 ` + strings.Repeat("word ", 213) + `
2479 -- content/p499.md --
2480 ---
2481 title: p499
2482 isCJKLanguage: true
2483 ---
2484 ` + strings.Repeat("你", 499) + `
2485 -- content/p500.md --
2486 ---
2487 title: p500
2488 isCJKLanguage: true
2489 ---
2490 ` + strings.Repeat("你", 500) + `
2491 -- content/p501.md --
2492 ---
2493 title: p501
2494 isCJKLanguage: true
2495 ---
2496 ` + strings.Repeat("你", 501) + `
2497 -- layouts/page.html --
2498 {{ .WordCount }}|{{ .FuzzyWordCount }}|{{ .ReadingTime }}
2499 `
2500
2501 b := Test(t, files)
2502
2503 b.AssertFileContent("public/p99/index.html", "99|100|1")
2504 b.AssertFileContent("public/p100/index.html", "100|100|1")
2505 b.AssertFileContent("public/p101/index.html", "101|200|1")
2506
2507 b.AssertFileContent("public/p211/index.html", "211|300|1")
2508 b.AssertFileContent("public/p212/index.html", "212|300|1")
2509 b.AssertFileContent("public/p213/index.html", "213|300|2")
2510
2511 b.AssertFileContent("public/p499/index.html", "499|500|1")
2512 b.AssertFileContent("public/p500/index.html", "500|500|1")
2513 b.AssertFileContent("public/p501/index.html", "501|600|2")
2514 }