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 cnt := content(p)
418 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>")
419 }
420
421 func TestPageDatesTerms(t *testing.T) {
422 t.Parallel()
423
424 files := `
425 -- hugo.toml --
426 baseURL = "http://example.com/"
427 -- content/p1.md --
428 ---
429 title: p1
430 date: 2022-01-15
431 lastMod: 2022-01-16
432 tags: ["a", "b"]
433 categories: ["c", "d"]
434 ---
435 p1
436 -- content/p2.md --
437 ---
438 title: p2
439 date: 2017-01-16
440 lastMod: 2017-01-17
441 tags: ["a", "c"]
442 categories: ["c", "e"]
443 ---
444 p2
445 -- layouts/list.html --
446 {{ .Title }}|Date: {{ .Date.Format "2006-01-02" }}|Lastmod: {{ .Lastmod.Format "2006-01-02" }}|
447
448 `
449 b := Test(t, files)
450
451 b.AssertFileContent("public/categories/index.html", "Categories|Date: 2022-01-15|Lastmod: 2022-01-16|")
452 b.AssertFileContent("public/categories/c/index.html", "C|Date: 2022-01-15|Lastmod: 2022-01-16|")
453 b.AssertFileContent("public/categories/e/index.html", "E|Date: 2017-01-16|Lastmod: 2017-01-17|")
454 b.AssertFileContent("public/tags/index.html", "Tags|Date: 2022-01-15|Lastmod: 2022-01-16|")
455 b.AssertFileContent("public/tags/a/index.html", "A|Date: 2022-01-15|Lastmod: 2022-01-16|")
456 b.AssertFileContent("public/tags/c/index.html", "C|Date: 2017-01-16|Lastmod: 2017-01-17|")
457 }
458
459 func TestPageDatesAllKinds(t *testing.T) {
460 t.Parallel()
461
462 pageContent := `
463 ---
464 title: Page
465 date: 2017-01-15
466 tags: ["hugo"]
467 categories: ["cool stuff"]
468 ---
469 `
470
471 files := `
472 -- hugo.toml --
473 baseURL = "http://example.com/"
474 -- content/page.md --
475 ` + pageContent + `
476 -- content/blog/page.md --
477 ` + pageContent + `
478 `
479
480 b := Test(t, files)
481
482 b.Assert(len(b.H.Sites), qt.Equals, 1)
483 s := b.H.Sites[0]
484
485 checkDate := func(t time.Time, msg string) {
486 b.Helper()
487 b.Assert(t.Year(), qt.Equals, 2017, qt.Commentf(msg))
488 }
489
490 checkDated := func(d resource.Dated, msg string) {
491 b.Helper()
492 checkDate(d.Date(), "date: "+msg)
493 checkDate(d.Lastmod(), "lastmod: "+msg)
494 }
495 for _, p := range s.Pages() {
496 checkDated(p, p.Kind())
497 }
498 checkDate(s.Lastmod(), "site")
499 }
500
501 func TestPageDatesSections(t *testing.T) {
502 t.Parallel()
503
504 var files strings.Builder
505 files.WriteString(`
506 -- hugo.toml --
507 baseURL = "http://example.com/"
508 -- content/no-index/page.md --
509 ---
510 title: Page
511 date: 2017-01-15
512 ---
513 -- content/with-index-no-date/_index.md --
514 ---
515 title: No Date
516 ---
517 -- content/with-index-date/_index.md --
518 ---
519 title: Date
520 date: 2018-01-15
521 ---
522 -- content/with-index-date/p1.md --
523 ---
524 title: Date
525 date: 2018-01-15
526 ---
527 -- content/with-index-date/p2.md --
528 ---
529 title: Date
530 date: 2018-01-15
531 ---
532 `)
533 for i := 1; i <= 20; i++ {
534 files.WriteString(fmt.Sprintf(`
535 -- content/main-section/p%d.md --
536 ---
537 title: Date
538 date: 2012-01-12
539 ---
540 `, i))
541 }
542
543 b := Test(t, files.String())
544
545 b.Assert(len(b.H.Sites), qt.Equals, 1)
546 s := b.H.Sites[0]
547
548 checkDate := func(p page.Page, year int) {
549 b.Assert(p.Date().Year(), qt.Equals, year)
550 b.Assert(p.Lastmod().Year(), qt.Equals, year)
551 }
552
553 checkDate(s.getPageOldVersion("/"), 2018)
554 checkDate(s.getPageOldVersion("/no-index"), 2017)
555 b.Assert(s.getPageOldVersion("/with-index-no-date").Date().IsZero(), qt.Equals, true)
556 checkDate(s.getPageOldVersion("/with-index-date"), 2018)
557
558 b.Assert(s.Site().Lastmod().Year(), qt.Equals, 2018)
559 }
560
561 func TestPageSummary(t *testing.T) {
562 t.Parallel()
563 htesting.SkipSlowTestUnlessCI(t)
564 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
565 p := pages[0]
566 checkPageTitle(t, p, "SimpleWithoutSummaryDelimiter")
567 // Source is not AsciiDoc- or RST-compatible so don't test them
568 if ext != "ad" && ext != "rst" {
569 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)
570 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)
571 }
572 checkPageType(t, p, "page")
573 }
574
575 testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithoutSummaryDelimiter)
576 }
577
578 func TestPageWithDelimiter(t *testing.T) {
579 htesting.SkipSlowTestUnlessCI(t)
580 t.Parallel()
581 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
582 p := pages[0]
583 checkPageTitle(t, p, "Simple")
584 checkPageContent(t, p, normalizeExpected(ext, "<p>Summary Next Line</p>\n\n<p>Some more text</p>\n"), ext)
585 checkPageSummary(t, p, normalizeExpected(ext, "<p>Summary Next Line</p>"), ext)
586 checkPageType(t, p, "page")
587 }
588
589 testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithSummaryDelimiter)
590 }
591
592 func TestPageWithSummaryParameter(t *testing.T) {
593 t.Parallel()
594 htesting.SkipSlowTestUnlessCI(t)
595 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
596 p := pages[0]
597 checkPageTitle(t, p, "SimpleWithSummaryParameter")
598 checkPageContent(t, p, normalizeExpected(ext, "<p>Some text.</p>\n\n<p>Some more text.</p>\n"), ext)
599 // Summary is not AsciiDoc- or RST-compatible so don't test them
600 if ext != "ad" && ext != "rst" {
601 checkPageSummary(t, p, normalizeExpected(ext, "Page with summary parameter and <a href=\"http://www.example.com/\">a link</a>"), ext)
602 }
603 checkPageType(t, p, "page")
604 }
605
606 testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithSummaryParameter)
607 }
608
609 // Issue #3854
610 // Also see https://github.com/gohugoio/hugo/issues/3977
611 func TestPageWithDateFields(t *testing.T) {
612 t.Parallel()
613 htesting.SkipSlowTestUnlessCI(t)
614
615 c := qt.New(t)
616 pageWithDate := `---
617 title: P%d
618 weight: %d
619 %s: 2017-10-13
620 ---
621 Simple Page With Some Date`
622
623 hasDate := func(p page.Page) bool {
624 return p.Date().Year() == 2017
625 }
626
627 datePage := func(field string, weight int) string {
628 return fmt.Sprintf(pageWithDate, weight, weight, field)
629 }
630
631 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
632 c.Assert(len(pages) > 0, qt.Equals, true)
633 for _, p := range pages {
634 c.Assert(hasDate(p), qt.Equals, true)
635 }
636 }
637
638 fields := []string{"date", "publishdate", "pubdate", "published"}
639 pageContents := make([]string, len(fields))
640 for i, field := range fields {
641 pageContents[i] = datePage(field, i+1)
642 }
643
644 testAllMarkdownEnginesForPages(t, assertFunc, nil, pageContents...)
645 }
646
647 func TestPageRawContent(t *testing.T) {
648 files := `
649 -- hugo.toml --
650 -- content/basic.md --
651 ---
652 title: "basic"
653 ---
654 **basic**
655 -- content/empty.md --
656 ---
657 title: "empty"
658 ---
659 -- layouts/single.html --
660 |{{ .RawContent }}|
661 `
662
663 b := Test(t, files)
664
665 b.AssertFileContent("public/basic/index.html", "|**basic**|")
666 b.AssertFileContent("public/empty/index.html", "! title")
667 }
668
669 func TestTableOfContents(t *testing.T) {
670 t.Parallel()
671
672 files := `
673 -- hugo.toml --
674 baseURL = "http://example.com/"
675 -- content/tocpage.md --
676 ` + pageWithToC + `
677 `
678 b := Test(t, files, TestOptSkipRender())
679
680 b.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 1)
681
682 p := b.H.Sites[0].RegularPages()[0]
683
684 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>")
685 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>")
686 }
687
688 func TestPageWithMoreTag(t *testing.T) {
689 htesting.SkipSlowTestUnlessCI(t)
690 t.Parallel()
691 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
692 p := pages[0]
693 checkPageTitle(t, p, "Simple")
694 checkPageContent(t, p, normalizeExpected(ext, "<p>Summary Same Line</p>\n\n<p>Some more text</p>\n"))
695 checkPageSummary(t, p, normalizeExpected(ext, "<p>Summary Same Line</p>"))
696 checkPageType(t, p, "page")
697 }
698
699 testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithSummaryDelimiterSameLine)
700 }
701
702 func TestSummaryInFrontMatter(t *testing.T) {
703 t.Parallel()
704 Test(t, `
705 -- hugo.toml --
706 -- content/simple.md --
707 ---
708 title: Simple
709 summary: "Front **matter** summary"
710 ---
711 Simple Page
712 -- layouts/single.html --
713 Summary: {{ .Summary }}|Truncated: {{ .Truncated }}|
714
715 `).AssertFileContent("public/simple/index.html", "Summary: Front <strong>matter</strong> summary|", "Truncated: false")
716 }
717
718 func TestSummaryManualSplit(t *testing.T) {
719 t.Parallel()
720 Test(t, `
721 -- hugo.toml --
722 -- content/simple.md --
723 ---
724 title: Simple
725 ---
726 This is **summary**.
727 <!--more-->
728 This is **content**.
729 -- layouts/single.html --
730 Summary: {{ .Summary }}|Truncated: {{ .Truncated }}|
731 Content: {{ .Content }}|
732
733 `).AssertFileContent("public/simple/index.html",
734 "Summary: <p>This is <strong>summary</strong>.</p>|",
735 "Truncated: true|",
736 "Content: <p>This is <strong>summary</strong>.</p>\n<p>This is <strong>content</strong>.</p>|",
737 )
738 }
739
740 func TestSummaryManualSplitHTML(t *testing.T) {
741 t.Parallel()
742 Test(t, `
743 -- hugo.toml --
744 [security]
745 allowContent = ['.*']
746 -- content/simple.html --
747 ---
748 title: Simple
749 ---
750 <div>
751 This is <b>summary</b>.
752 </div>
753 <!--more-->
754 <div>
755 This is <b>content</b>.
756 </div>
757 -- layouts/single.html --
758 Summary: {{ .Summary }}|Truncated: {{ .Truncated }}|
759 Content: {{ .Content }}|
760
761 `).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>|")
762 }
763
764 func TestSummaryAuto(t *testing.T) {
765 t.Parallel()
766 Test(t, `
767 -- hugo.toml --
768 summaryLength = 10
769 -- content/simple.md --
770 ---
771 title: Simple
772 ---
773 This is **summary**.
774 This is **more summary**.
775 This is *even more summary**.
776 This is **more summary**.
777
778 This is **content**.
779 -- layouts/single.html --
780 Summary: {{ .Summary }}|Truncated: {{ .Truncated }}|
781 Content: {{ .Content }}|
782
783 `).AssertFileContent("public/simple/index.html",
784 "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>|",
785 "Truncated: true|",
786 "Content: <p>This is <strong>summary</strong>.")
787 }
788
789 // #2973
790 func TestSummaryWithHTMLTagsOnNextLine(t *testing.T) {
791 htesting.SkipSlowTestUnlessCI(t)
792 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
793 c := qt.New(t)
794 p := pages[0]
795 s := string(p.Summary(context.Background()))
796 c.Assert(s, qt.Contains, "Happy new year everyone!")
797 c.Assert(s, qt.Not(qt.Contains), "User interface")
798 }
799
800 testAllMarkdownEnginesForPages(t, assertFunc, nil, `---
801 title: Simple
802 ---
803 Happy new year everyone!
804
805 Here is the last report for commits in the year 2016. It covers hrev50718-hrev50829.
806
807 <!--more-->
808
809 <h3>User interface</h3>
810
811 `)
812 }
813
814 // Issue 9383
815 func TestRenderStringForRegularPageTranslations(t *testing.T) {
816 t.Parallel()
817
818 files := `
819 -- hugo.toml --
820 baseurl = "https://example.org/"
821 title = "My Site"
822
823 defaultContentLanguage = "ru"
824 defaultContentLanguageInSubdir = true
825
826 [languages.ru]
827 contentDir = 'content/ru'
828 weight = 1
829
830 [languages.en]
831 weight = 2
832 contentDir = 'content/en'
833
834 [outputs]
835 home = ["HTML", "JSON"]
836 -- layouts/home.html --
837 {{- range .Site.Home.Translations -}}
838 <p>{{- .RenderString "foo" -}}</p>
839 {{- end -}}
840 {{- range .Site.Home.AllTranslations -}}
841 <p>{{- .RenderString "bar" -}}</p>
842 {{- end -}}
843 -- layouts/single.html --
844 {{ .Content }}
845 -- layouts/index.json --
846 {"Title": "My Site"}
847 -- content/ru/a.md --
848 -- content/en/a.md --
849 `
850 b := Test(t, files)
851
852 b.AssertFileContent("public/ru/index.html", `
853 <p>foo</p>
854 <p>foo</p>
855 <p>bar</p>
856 <p>bar</p>
857 `)
858
859 b.AssertFileContent("public/en/index.html", `
860 <p>foo</p>
861 <p>foo</p>
862 <p>bar</p>
863 <p>bar</p>
864 `)
865 }
866
867 // Issue 8919
868 func TestContentProviderWithCustomOutputFormat(t *testing.T) {
869 t.Parallel()
870 htesting.SkipSlowTestUnlessCI(t)
871
872 files := `
873 -- hugo.toml --
874 baseURL = 'http://example.org/'
875 title = 'My New Hugo Project'
876
877 timeout = 600000 # ten minutes in case we want to pause and debug
878
879 defaultContentLanguage = "en"
880
881 [languages]
882 [languages.en]
883 title = "Repro"
884 label = "English"
885 contentDir = "content/en"
886
887 [languages.zh_CN]
888 title = "Repro"
889 label = "简体中文"
890 contentDir = "content/zh_CN"
891
892 [outputFormats]
893 [outputFormats.metadata]
894 baseName = "metadata"
895 mediaType = "text/html"
896 isPlainText = true
897 notAlternative = true
898
899 [outputs]
900 home = ["HTML", "metadata"]
901 -- layouts/home.metadata.html --
902 <h2>Translations metadata</h2>
903 <ul>
904 {{ $p := .Page }}
905 {{ range $p.Translations}}
906 <li>Title: {{ .Title }}, {{ .Summary }}</li>
907 <li>Content: {{ .Content }}</li>
908 <li>Plain: {{ .Plain }}</li>
909 <li>PlainWords: {{ .PlainWords }}</li>
910 <li>Summary: {{ .Summary }}</li>
911 <li>Truncated: {{ .Truncated }}</li>
912 <li>FuzzyWordCount: {{ .FuzzyWordCount }}</li>
913 <li>ReadingTime: {{ .ReadingTime }}</li>
914 <li>Len: {{ .Len }}</li>
915 {{ end }}
916 </ul>
917 -- layouts/baseof.html --
918 <html>
919
920 <body>
921 {{ block "main" . }}{{ end }}
922 </body>
923
924 </html>
925 -- layouts/home.html --
926 {{ define "main" }}
927 <h2>Translations</h2>
928 <ul>
929 {{ $p := .Page }}
930 {{ range $p.Translations}}
931 <li>Title: {{ .Title }}, {{ .Summary }}</li>
932 <li>Content: {{ .Content }}</li>
933 <li>Plain: {{ .Plain }}</li>
934 <li>PlainWords: {{ .PlainWords }}</li>
935 <li>Summary: {{ .Summary }}</li>
936 <li>Truncated: {{ .Truncated }}</li>
937 <li>FuzzyWordCount: {{ .FuzzyWordCount }}</li>
938 <li>ReadingTime: {{ .ReadingTime }}</li>
939 <li>Len: {{ .Len }}</li>
940 {{ end }}
941 </ul>
942 {{ end }}
943 -- content/en/_index.md --
944 ---
945 title: Title (en)
946 summary: Summary (en)
947 ---
948
949 Here is some content.
950 -- content/zh_CN/_index.md --
951 ---
952 title: Title (zh)
953 summary: Summary (zh)
954 ---
955
956 这是一些内容
957 `
958 b := Test(t, files)
959
960 b.AssertFileContent("public/index.html", `<html>
961
962 <body>
963
964 <h2>Translations</h2>
965 <ul>
966
967
968 <li>Title: Title (zh), Summary (zh)</li>
969 <li>Content: <p>这是一些内容</p>
970 </li>
971 <li>Plain: 这是一些内容
972 </li>
973 <li>PlainWords: [这是一些内容]</li>
974 <li>Summary: Summary (zh)</li>
975 <li>Truncated: false</li>
976 <li>FuzzyWordCount: 100</li>
977 <li>ReadingTime: 1</li>
978 <li>Len: 26</li>
979
980 </ul>
981
982 </body>
983
984 </html>`)
985 b.AssertFileContent("public/metadata.html", `<h2>Translations metadata</h2>
986 <ul>
987
988
989 <li>Title: Title (zh), Summary (zh)</li>
990 <li>Content: <p>这是一些内容</p>
991 </li>
992 <li>Plain: 这是一些内容
993 </li>
994 <li>PlainWords: [这是一些内容]</li>
995 <li>Summary: Summary (zh)</li>
996 <li>Truncated: false</li>
997 <li>FuzzyWordCount: 100</li>
998 <li>ReadingTime: 1</li>
999 <li>Len: 26</li>
1000
1001 </ul>`)
1002 b.AssertFileContent("public/zh_cn/index.html", `<html>
1003
1004 <body>
1005
1006 <h2>Translations</h2>
1007 <ul>
1008
1009
1010 <li>Title: Title (en), Summary (en)</li>
1011 <li>Content: <p>Here is some content.</p>
1012 </li>
1013 <li>Plain: Here is some content.
1014 </li>
1015 <li>PlainWords: [Here is some content.]</li>
1016 <li>Summary: Summary (en)</li>
1017 <li>Truncated: false</li>
1018 <li>FuzzyWordCount: 100</li>
1019 <li>ReadingTime: 1</li>
1020 <li>Len: 29</li>
1021
1022 </ul>
1023
1024 </body>
1025
1026 </html>`)
1027 b.AssertFileContent("public/zh_cn/metadata.html", `<h2>Translations metadata</h2>
1028 <ul>
1029
1030
1031 <li>Title: Title (en), Summary (en)</li>
1032 <li>Content: <p>Here is some content.</p>
1033 </li>
1034 <li>Plain: Here is some content.
1035 </li>
1036 <li>PlainWords: [Here is some content.]</li>
1037 <li>Summary: Summary (en)</li>
1038 <li>Truncated: false</li>
1039 <li>FuzzyWordCount: 100</li>
1040 <li>ReadingTime: 1</li>
1041 <li>Len: 29</li>
1042
1043 </ul>`)
1044 }
1045
1046 func TestPageWithDate(t *testing.T) {
1047 t.Parallel()
1048
1049 files := `
1050 -- hugo.toml --
1051 baseURL = "http://example.com/"
1052 -- content/simple.md --
1053 ` + simplePageRFC3339Date + `
1054 `
1055 b := Test(t, files, TestOptSkipRender())
1056
1057 b.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 1)
1058
1059 p := b.H.Sites[0].RegularPages()[0]
1060 d, _ := time.Parse(time.RFC3339, "2013-05-17T16:59:30Z")
1061
1062 checkPageDate(t, p, d)
1063 }
1064
1065 func TestPageWithFrontMatterConfig(t *testing.T) {
1066 for _, dateHandler := range []string{":filename", ":fileModTime"} {
1067 t.Run(fmt.Sprintf("dateHandler=%q", dateHandler), func(t *testing.T) {
1068 t.Parallel()
1069
1070 pageTemplate := `
1071 ---
1072 title: Page
1073 weight: %d
1074 lastMod: 2018-02-28
1075 %s
1076 ---
1077 Content
1078 `
1079 files := `
1080 -- hugo.toml --
1081 baseURL = "http://example.com/"
1082 [frontmatter]
1083 date = ["` + dateHandler + `", "date"]
1084 -- content/section/2012-02-21-noslug.md --
1085 ` + fmt.Sprintf(pageTemplate, 1, "") + `
1086 -- content/section/2012-02-22-slug.md --
1087 ` + fmt.Sprintf(pageTemplate, 2, "slug: aslug") + `
1088 `
1089 b := Test(t, files, TestOptOsFs())
1090
1091 s := b.H.Sites[0]
1092 b.Assert(len(s.RegularPages()), qt.Equals, 2)
1093
1094 noSlug := s.RegularPages()[0]
1095 slug := s.RegularPages()[1]
1096
1097 b.Assert(noSlug.Lastmod().Day(), qt.Equals, 28)
1098
1099 switch strings.ToLower(dateHandler) {
1100 case ":filename":
1101 b.Assert(noSlug.Date().IsZero(), qt.Equals, false)
1102 b.Assert(slug.Date().IsZero(), qt.Equals, false)
1103 b.Assert(noSlug.Date().Year(), qt.Equals, 2012)
1104 b.Assert(slug.Date().Year(), qt.Equals, 2012)
1105 b.Assert(noSlug.Slug(), qt.Equals, "noslug")
1106 b.Assert(slug.Slug(), qt.Equals, "aslug")
1107 case ":filemodtime":
1108 // For fileModTime, we need to get the actual file mod time.
1109 // The IntegrationTestBuilder creates a temporary directory.
1110 // We need to get the path to the created files.
1111 // The `b.Fs` field gives access to the file system.
1112 c1Path := filepath.Join(b.Cfg.WorkingDir, "content", "section", "2012-02-21-noslug.md")
1113 c2Path := filepath.Join(b.Cfg.WorkingDir, "content", "section", "2012-02-22-slug.md")
1114
1115 c1fi, err := b.fs.Source.Stat(c1Path)
1116 b.Assert(err, qt.IsNil)
1117 c2fi, err := b.fs.Source.Stat(c2Path)
1118 b.Assert(err, qt.IsNil)
1119
1120 b.Assert(noSlug.Date().Year(), qt.Equals, c1fi.ModTime().Year())
1121 b.Assert(slug.Date().Year(), qt.Equals, c2fi.ModTime().Year())
1122 fallthrough
1123 default:
1124 b.Assert(noSlug.Slug(), qt.Equals, "")
1125 b.Assert(slug.Slug(), qt.Equals, "aslug")
1126
1127 }
1128 })
1129 }
1130 }
1131
1132 func TestWordCountWithAllCJKRunesWithoutHasCJKLanguage(t *testing.T) {
1133 t.Parallel()
1134 htesting.SkipSlowTestUnlessCI(t)
1135 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
1136 p := pages[0]
1137 if p.WordCount(context.Background()) != 8 {
1138 t.Fatalf("[%s] incorrect word count. expected %v, got %v", ext, 8, p.WordCount(context.Background()))
1139 }
1140 }
1141
1142 testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithAllCJKRunes)
1143 }
1144
1145 func TestWordCountWithAllCJKRunesHasCJKLanguage(t *testing.T) {
1146 t.Parallel()
1147 htesting.SkipSlowTestUnlessCI(t)
1148 settings := map[string]any{"hasCJKLanguage": true}
1149
1150 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
1151 p := pages[0]
1152 if p.WordCount(context.Background()) != 15 {
1153 t.Fatalf("[%s] incorrect word count, expected %v, got %v", ext, 15, p.WordCount(context.Background()))
1154 }
1155 }
1156 testAllMarkdownEnginesForPages(t, assertFunc, settings, simplePageWithAllCJKRunes)
1157 }
1158
1159 func TestWordCountWithMainEnglishWithCJKRunes(t *testing.T) {
1160 t.Parallel()
1161 htesting.SkipSlowTestUnlessCI(t)
1162 settings := map[string]any{"hasCJKLanguage": true}
1163
1164 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
1165 p := pages[0]
1166 if p.WordCount(context.Background()) != 74 {
1167 t.Fatalf("[%s] incorrect word count, expected %v, got %v", ext, 74, p.WordCount(context.Background()))
1168 }
1169 }
1170
1171 testAllMarkdownEnginesForPages(t, assertFunc, settings, simplePageWithMainEnglishWithCJKRunes)
1172 }
1173
1174 func TestWordCountWithIsCJKLanguageFalse(t *testing.T) {
1175 t.Parallel()
1176 htesting.SkipSlowTestUnlessCI(t)
1177 settings := map[string]any{
1178 "hasCJKLanguage": true,
1179 }
1180
1181 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
1182 p := pages[0]
1183 if p.WordCount(context.Background()) != 75 {
1184 t.Fatalf("[%s] incorrect word count for content '%s'. expected %v, got %v", ext, p.Plain(context.Background()), 74, p.WordCount(context.Background()))
1185 }
1186 }
1187
1188 testAllMarkdownEnginesForPages(t, assertFunc, settings, simplePageWithIsCJKLanguageFalse)
1189 }
1190
1191 func TestWordCount(t *testing.T) {
1192 t.Parallel()
1193 htesting.SkipSlowTestUnlessCI(t)
1194 assertFunc := func(t *testing.T, ext string, pages page.Pages) {
1195 p := pages[0]
1196 if p.WordCount(context.Background()) != 483 {
1197 t.Fatalf("[%s] incorrect word count. expected %v, got %v", ext, 483, p.WordCount(context.Background()))
1198 }
1199
1200 if p.FuzzyWordCount(context.Background()) != 500 {
1201 t.Fatalf("[%s] incorrect word count. expected %v, got %v", ext, 500, p.FuzzyWordCount(context.Background()))
1202 }
1203
1204 if p.ReadingTime(context.Background()) != 3 {
1205 t.Fatalf("[%s] incorrect min read. expected %v, got %v", ext, 3, p.ReadingTime(context.Background()))
1206 }
1207 }
1208
1209 testAllMarkdownEnginesForPages(t, assertFunc, nil, simplePageWithLongContent)
1210 }
1211
1212 func TestPagePaths(t *testing.T) {
1213 t.Parallel()
1214
1215 siteParmalinksSetting := `
1216 [permalinks]
1217 post = ":year/:month/:day/:title/"
1218 `
1219
1220 tests := []struct {
1221 content string
1222 path string
1223 hasPermalink bool
1224 expected string
1225 }{
1226 {simplePage, "post/x.md", false, "/post/x/"},
1227 {simplePageWithURL, "post/x.md", false, "/simple/url/"},
1228 {simplePageWithSlug, "post/x.md", false, "/post/simple-slug/"},
1229 {simplePageWithDate, "post/x.md", true, "/2013/10/15/simple/"},
1230 {UTF8Page, "post/x.md", false, "/post/x/"},
1231 {UTF8PageWithURL, "post/x.md", false, "/%E3%83%A9%E3%83%BC%E3%83%A1%E3%83%B3/url/"},
1232 {UTF8PageWithSlug, "post/x.md", false, "/post/%E3%83%A9%E3%83%BC%E3%83%A1%E3%83%B3-slug/"},
1233 {UTF8PageWithDate, "post/x.md", true, "/2013/10/15/%E3%83%A9%E3%83%BC%E3%83%A1%E3%83%B3/"},
1234 }
1235
1236 for i, test := range tests {
1237 t.Run(fmt.Sprintf("Test%d", i), func(t *testing.T) {
1238 t.Parallel()
1239
1240 configContent := `baseURL = "http://example.com/"`
1241 if test.hasPermalink {
1242 configContent += siteParmalinksSetting
1243 }
1244
1245 files := `
1246 -- hugo.toml --
1247 ` + configContent + `
1248 -- content/` + test.path + ` --
1249 ` + test.content + `
1250 `
1251 b := Test(t, files, TestOptSkipRender())
1252
1253 s := b.H.Sites[0]
1254 b.Assert(len(s.RegularPages()), qt.Equals, 1)
1255 p := s.RegularPages()[0]
1256 b.Assert(p.RelPermalink(), qt.Equals, test.expected)
1257 })
1258 }
1259 }
1260
1261 func TestTranslationKey(t *testing.T) {
1262 files := `
1263 -- hugo.toml --
1264 disableKinds = ["taxonomy", "term"]
1265 defaultContentLanguage = "en"
1266 defaultContentLanguageInSubdir = true
1267 [languages]
1268 [languages.en]
1269 weight = 1
1270 [languages.nn]
1271 weight = 2
1272 -- content/sect/p1.en.md --
1273 ---
1274 translationkey: "adfasdf"
1275 title: "p1 en"
1276 ---
1277 -- content/sect/p1.nn.md --
1278 ---
1279 translationkey: "adfasdf"
1280 title: "p1 nn"
1281 ---
1282 -- layouts/single.html --
1283 Title: {{ .Title }}|TranslationKey: {{ .TranslationKey }}|
1284 Translations: {{ range .Translations }}{{ .Language.Lang }}|{{ end }}|
1285 AllTranslations: {{ range .AllTranslations }}{{ .Language.Lang }}|{{ end }}|
1286
1287 `
1288 b := Test(t, files)
1289 b.AssertFileContent("public/en/sect/p1/index.html",
1290 "TranslationKey: adfasdf|",
1291 "AllTranslations: en|nn||",
1292 "Translations: nn||",
1293 )
1294
1295 b.AssertFileContent("public/nn/sect/p1/index.html",
1296 "TranslationKey: adfasdf|",
1297 "Translations: en||",
1298 "AllTranslations: en|nn||",
1299 )
1300 }
1301
1302 func TestTranslationKeyTermPages(t *testing.T) {
1303 t.Parallel()
1304
1305 files := `
1306 -- hugo.toml --
1307 disableKinds = ['home','rss','section','sitemap','taxonomy']
1308 defaultContentLanguage = 'en'
1309 defaultContentLanguageInSubdir = true
1310 [languages.en]
1311 weight = 1
1312 [languages.pt]
1313 weight = 2
1314 [taxonomies]
1315 category = 'categories'
1316 -- layouts/list.html --
1317 {{ .IsTranslated }}|{{ range .Translations }}{{ .RelPermalink }}|{{ end }}
1318 -- layouts/single.html --
1319 {{ .Title }}|
1320 -- content/p1.en.md --
1321 ---
1322 title: p1 (en)
1323 categories: [music]
1324 ---
1325 -- content/p1.pt.md --
1326 ---
1327 title: p1 (pt)
1328 categories: [música]
1329 ---
1330 -- content/categories/music/_index.en.md --
1331 ---
1332 title: music
1333 translationKey: foo
1334 ---
1335 -- content/categories/música/_index.pt.md --
1336 ---
1337 title: música
1338 translationKey: foo
1339 ---
1340 `
1341
1342 b := Test(t, files)
1343 b.AssertFileContent("public/en/categories/music/index.html", "true|/pt/categories/m%C3%BAsica/|")
1344 b.AssertFileContent("public/pt/categories/música/index.html", "true|/en/categories/music/|")
1345 }
1346
1347 // Issue #11540.
1348 func TestTranslationKeyResourceSharing(t *testing.T) {
1349 files := `
1350 -- hugo.toml --
1351 disableKinds = ["taxonomy", "term"]
1352 defaultContentLanguage = "en"
1353 defaultContentLanguageInSubdir = true
1354 [languages]
1355 [languages.en]
1356 weight = 1
1357 [languages.nn]
1358 weight = 2
1359 -- content/sect/mybundle_en/index.en.md --
1360 ---
1361 translationkey: "adfasdf"
1362 title: "mybundle en"
1363 ---
1364 -- content/sect/mybundle_en/f1.txt --
1365 f1.en
1366 -- content/sect/mybundle_en/f2.txt --
1367 f2.en
1368 -- content/sect/mybundle_nn/index.nn.md --
1369 ---
1370 translationkey: "adfasdf"
1371 title: "mybundle nn"
1372 ---
1373 -- content/sect/mybundle_nn/f2.nn.txt --
1374 f2.nn
1375 -- layouts/single.html --
1376 Title: {{ .Title }}|TranslationKey: {{ .TranslationKey }}|
1377 Resources: {{ range .Resources }}{{ .RelPermalink }}|{{ .Content }}|{{ end }}|
1378
1379 `
1380 b := Test(t, files)
1381 b.AssertFileContent("public/en/sect/mybundle_en/index.html",
1382 "TranslationKey: adfasdf|",
1383 "Resources: /en/sect/mybundle_en/f1.txt|f1.en|/en/sect/mybundle_en/f2.txt|f2.en||",
1384 )
1385
1386 b.AssertFileContent("public/nn/sect/mybundle_nn/index.html",
1387 "TranslationKey: adfasdf|",
1388 "Title: mybundle nn|TranslationKey: adfasdf|\nResources: /en/sect/mybundle_en/f1.txt|f1.en|/nn/sect/mybundle_nn/f2.nn.txt|f2.nn||",
1389 )
1390 }
1391
1392 func TestTranslationKeyRotate(t *testing.T) {
1393 t.Parallel()
1394
1395 files := `
1396 -- hugo.toml --
1397 disableKinds = ['home','rss','section','sitemap','taxonomy']
1398 defaultContentLanguage = 'en'
1399 defaultContentLanguageInSubdir = true
1400 [languages]
1401 [languages.en]
1402 weight = 1
1403 [languages.pt]
1404 weight = 2
1405 -- content/foo.md --
1406 ---
1407 title: Foo
1408 translationkey: "mykey"
1409 ---
1410 -- content/bar.md --
1411 ---
1412 title: Bar
1413 translationkey: "mykey"
1414 ---
1415 -- layouts/all.html --
1416 Rotate(language): {{ with .Rotate "language" }}{{ range . }}{{ template "printp" . }}|{{ end }}{{ end }}$
1417 {{ define "printp" }}{{ .RelPermalink }}:{{ with .Site }}{{ template "prints" . }}{{ end }}{{ end }}
1418 {{ define "prints" }}/l:{{ .Language.Name }}/v:{{ .Version.Name }}/r:{{ .Role.Name }}{{ end }}
1419 `
1420
1421 b := Test(t, files)
1422
1423 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|$")
1424 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|$")
1425 }
1426
1427 func TestChompBOM(t *testing.T) {
1428 t.Parallel()
1429 const utf8BOM = "\xef\xbb\xbf"
1430
1431 files := `
1432 -- hugo.toml --
1433 baseURL = "http://example.com/"
1434 -- content/simple.md --
1435 ` + utf8BOM + simplePage + `
1436 `
1437 b := Test(t, files, TestOptSkipRender())
1438
1439 b.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 1)
1440
1441 p := b.H.Sites[0].RegularPages()[0]
1442
1443 checkPageTitle(t, p, "Simple")
1444 }
1445
1446 // https://github.com/gohugoio/hugo/issues/5381
1447 func TestPageManualSummary(t *testing.T) {
1448 t.Parallel()
1449
1450 files := `
1451 -- hugo.toml --
1452 baseURL = "http://example.com/"
1453 -- content/page-md-shortcode.md --
1454 ---
1455 title: "Hugo"
1456 ---
1457 This is a {{< sc >}}.
1458 <!--more-->
1459 Content.
1460 -- content/page-md-only-shortcode.md --
1461 ---
1462 title: "Hugo"
1463 ---
1464 {{< sc >}}
1465 <!--more-->
1466 {{< sc >}}
1467 -- content/page-md-shortcode-same-line.md --
1468 ---
1469 title: "Hugo"
1470 ---
1471 This is a {{< sc >}}<!--more-->Same line.
1472 -- content/page-md-shortcode-same-line-after.md --
1473 ---
1474 title: "Hugo"
1475 ---
1476 Summary<!--more-->{{< sc >}}
1477 -- content/page-org-shortcode.org --
1478 #+TITLE: T1
1479 #+AUTHOR: A1
1480 #+DESCRIPTION: D1
1481 This is a {{< sc >}}.
1482 # more
1483 Content.
1484 -- content/page-org-variant1.org --
1485 #+TITLE: T1
1486 Summary.
1487
1488 # more
1489
1490 Content.
1491 -- layouts/_shortcodes/sc.html --
1492 a shortcode
1493 -- layouts/single.html --
1494 SUMMARY:{{ .Summary }}:END
1495 --------------------------
1496 CONTENT:{{ .Content }}
1497 `
1498 b := Test(t, files)
1499
1500 b.AssertFileContent("public/page-md-shortcode/index.html",
1501 "SUMMARY:<p>This is a a shortcode.</p>:END",
1502 "CONTENT:<p>This is a a shortcode.</p>\n\n<p>Content.</p>\n",
1503 )
1504
1505 b.AssertFileContent("public/page-md-shortcode-same-line/index.html",
1506 "SUMMARY:<p>This is a a shortcode</p>:END",
1507 "CONTENT:<p>This is a a shortcode</p>\n\n<p>Same line.</p>\n",
1508 )
1509
1510 b.AssertFileContent("public/page-md-shortcode-same-line-after/index.html",
1511 "SUMMARY:<p>Summary</p>:END",
1512 "CONTENT:<p>Summary</p>\n\na shortcode",
1513 )
1514
1515 b.AssertFileContent("public/page-org-shortcode/index.html",
1516 "SUMMARY:<p>\nThis is a a shortcode.\n</p>:END",
1517 "CONTENT:<p>\nThis is a a shortcode.\n</p>\n<p>\nContent.\t\n</p>\n",
1518 )
1519 b.AssertFileContent("public/page-org-variant1/index.html",
1520 "SUMMARY:<p>\nSummary.\n</p>:END",
1521 "CONTENT:<p>\nSummary.\n</p>\n<p>\nContent.\t\n</p>\n",
1522 )
1523
1524 b.AssertFileContent("public/page-md-only-shortcode/index.html",
1525 "SUMMARY:a shortcode:END",
1526 "CONTENT:a shortcode\n\na shortcode\n",
1527 )
1528 }
1529
1530 func TestHomePageWithNoTitle(t *testing.T) {
1531 t.Parallel()
1532
1533 files := `
1534 -- hugo.toml --
1535 baseURL = "http://example.com/"
1536 title = "Site Title"
1537 -- layouts/home.html --
1538 Title|{{ with .Title }}{{ . }}{{ end }}|
1539 -- content/_index.md --
1540 ---
1541 description: "No title for you!"
1542 ---
1543
1544 Content.
1545 `
1546 b := Test(t, files)
1547
1548 b.AssertFileContent("public/index.html", "Title||")
1549 }
1550
1551 func TestShouldBuild(t *testing.T) {
1552 past := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
1553 future := time.Date(2037, 11, 17, 20, 34, 58, 651387237, time.UTC)
1554 zero := time.Time{}
1555
1556 publishSettings := []struct {
1557 buildFuture bool
1558 buildExpired bool
1559 buildDrafts bool
1560 draft bool
1561 publishDate time.Time
1562 expiryDate time.Time
1563 out bool
1564 }{
1565 // publishDate and expiryDate
1566 {false, false, false, false, zero, zero, true},
1567 {false, false, false, false, zero, future, true},
1568 {false, false, false, false, past, zero, true},
1569 {false, false, false, false, past, future, true},
1570 {false, false, false, false, past, past, false},
1571 {false, false, false, false, future, future, false},
1572 {false, false, false, false, future, past, false},
1573
1574 // buildFuture and buildExpired
1575 {false, true, false, false, past, past, true},
1576 {true, true, false, false, past, past, true},
1577 {true, false, false, false, past, past, false},
1578 {true, false, false, false, future, future, true},
1579 {true, true, false, false, future, future, true},
1580 {false, true, false, false, future, past, false},
1581
1582 // buildDrafts and draft
1583 {true, true, false, true, past, future, false},
1584 {true, true, true, true, past, future, true},
1585 {true, true, true, true, past, future, true},
1586 }
1587
1588 for _, ps := range publishSettings {
1589 s := shouldBuild(ps.buildFuture, ps.buildExpired, ps.buildDrafts, ps.draft,
1590 ps.publishDate, ps.expiryDate)
1591 if s != ps.out {
1592 t.Errorf("AssertShouldBuild unexpected output with params: %+v", ps)
1593 }
1594 }
1595 }
1596
1597 func TestShouldBuildWithClock(t *testing.T) {
1598 htime.Clock = clocks.Start(time.Date(2021, 11, 17, 20, 34, 58, 651387237, time.UTC))
1599 t.Cleanup(func() { htime.Clock = clocks.System() })
1600 past := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
1601 future := time.Date(2037, 11, 17, 20, 34, 58, 651387237, time.UTC)
1602 zero := time.Time{}
1603
1604 publishSettings := []struct {
1605 buildFuture bool
1606 buildExpired bool
1607 buildDrafts bool
1608 draft bool
1609 publishDate time.Time
1610 expiryDate time.Time
1611 out bool
1612 }{
1613 // publishDate and expiryDate
1614 {false, false, false, false, zero, zero, true},
1615 {false, false, false, false, zero, future, true},
1616 {false, false, false, false, past, zero, true},
1617 {false, false, false, false, past, future, true},
1618 {false, false, false, false, past, past, false},
1619 {false, false, false, false, future, future, false},
1620 {false, false, false, false, future, past, false},
1621
1622 // buildFuture and buildExpired
1623 {false, true, false, false, past, past, true},
1624 {true, true, false, false, past, past, true},
1625 {true, false, false, false, past, past, false},
1626 {true, false, false, false, future, future, true},
1627 {true, true, false, false, future, future, true},
1628 {false, true, false, false, future, past, false},
1629
1630 // buildDrafts and draft
1631 {true, true, false, true, past, future, false},
1632 {true, true, true, true, past, future, true},
1633 {true, true, true, true, past, future, true},
1634 }
1635
1636 for _, ps := range publishSettings {
1637 s := shouldBuild(ps.buildFuture, ps.buildExpired, ps.buildDrafts, ps.draft,
1638 ps.publishDate, ps.expiryDate)
1639 if s != ps.out {
1640 t.Errorf("AssertShouldBuildWithClock unexpected output with params: %+v", ps)
1641 }
1642 }
1643 }
1644
1645 // See https://github.com/gohugoio/hugo/issues/9171
1646 // We redefined disablePathToLower in v0.121.0.
1647 func TestPagePathDisablePathToLower(t *testing.T) {
1648 files := `
1649 -- hugo.toml --
1650 baseURL = "http://example.com"
1651 disablePathToLower = true
1652 [permalinks]
1653 sect2 = "/:section/:contentbasename/"
1654 sect3 = "/:section/:title/"
1655 -- content/sect/p1.md --
1656 ---
1657 title: "Page1"
1658 ---
1659 p1.
1660 -- content/sect/p2.md --
1661 ---
1662 title: "Page2"
1663 slug: "PaGe2"
1664 ---
1665 p2.
1666 -- content/sect2/PaGe3.md --
1667 ---
1668 title: "Page3"
1669 ---
1670 -- content/seCt3/p4.md --
1671 ---
1672 title: "Pag.E4"
1673 slug: "PaGe4"
1674 ---
1675 p4.
1676 -- layouts/single.html --
1677 Single: {{ .Title}}|{{ .RelPermalink }}|{{ .Path }}|
1678 `
1679 b := Test(t, files)
1680 b.AssertFileContent("public/sect/p1/index.html", "Single: Page1|/sect/p1/|/sect/p1")
1681 b.AssertFileContent("public/sect/PaGe2/index.html", "Single: Page2|/sect/PaGe2/|/sect/p2")
1682 b.AssertFileContent("public/sect2/PaGe3/index.html", "Single: Page3|/sect2/PaGe3/|/sect2/page3|")
1683 b.AssertFileContent("public/sect3/Pag.E4/index.html", "Single: Pag.E4|/sect3/Pag.E4/|/sect3/p4|")
1684 }
1685
1686 func TestScratch(t *testing.T) {
1687 t.Parallel()
1688
1689 files := `
1690 -- hugo.toml --
1691 baseURL = "http://example.com/"
1692 -- layouts/home.html --
1693 {{ .Scratch.Set "b" "bv" }}
1694 B: {{ .Scratch.Get "b" }}
1695 -- layouts/_shortcodes/scratch.html --
1696 {{ .Scratch.Set "c" "cv" }}
1697 C: {{ .Scratch.Get "c" }}
1698 -- layouts/single.html --
1699 {{ .Content }}
1700 -- content/scratchme.md --
1701 ---
1702 title: Scratch Me!
1703 ---
1704
1705 {{< scratch >}}
1706 `
1707 b, err := TestE(t, files)
1708
1709 b.Assert(err, qt.IsNil)
1710
1711 b.AssertFileContent("public/index.html", "B: bv")
1712 b.AssertFileContent("public/scratchme/index.html", "C: cv")
1713 }
1714
1715 // Issue 13016.
1716 func TestScratchAliasToStore(t *testing.T) {
1717 t.Parallel()
1718
1719 files := `
1720 -- hugo.toml --
1721 disableKinds = ["taxonomy", "term", "page", "section"]
1722 disableLiveReload = true
1723 -- layouts/home.html --
1724 {{ .Scratch.Set "a" "b" }}
1725 {{ .Store.Set "c" "d" }}
1726 .Scratch eq .Store: {{ eq .Scratch .Store }}
1727 a: {{ .Store.Get "a" }}
1728 c: {{ .Scratch.Get "c" }}
1729
1730 `
1731
1732 b := Test(t, files)
1733
1734 b.AssertFileContent("public/index.html",
1735 ".Scratch eq .Store: true",
1736 "a: b",
1737 "c: d",
1738 )
1739 }
1740
1741 func TestPageParam(t *testing.T) {
1742 t.Parallel()
1743
1744 files := `
1745 -- hugo.toml --
1746 baseURL = "https://example.org"
1747
1748 [params]
1749 [params.author]
1750 name = "Kurt Vonnegut"
1751 -- layouts/home.html --
1752
1753 {{ $withParam := .Site.GetPage "withparam" }}
1754 {{ $noParam := .Site.GetPage "noparam" }}
1755 {{ $withStringParam := .Site.GetPage "withstringparam" }}
1756
1757 Author page: {{ $withParam.Param "author.name" }}
1758 Author name page string: {{ $withStringParam.Param "author.name" }}|
1759 Author page string: {{ $withStringParam.Param "author" }}|
1760 Author project config: {{ $noParam.Param "author.name" }}
1761
1762 -- content/withparam.md --
1763 +++
1764 title = "With Param!"
1765 [author]
1766 name = "Ernest Miller Hemingway"
1767
1768 +++
1769 -- content/noparam.md --
1770 ---
1771 title: "No Param!"
1772 ---
1773 -- content/withstringparam.md --
1774 +++
1775 title = "With string Param!"
1776 author = "Jo Nesbø"
1777
1778 +++
1779 `
1780 b := Test(t, files)
1781
1782 b.AssertFileContent("public/index.html",
1783 "Author page: Ernest Miller Hemingway",
1784 "Author name page string: Kurt Vonnegut|",
1785 "Author page string: Jo Nesbø|",
1786 "Author project config: Kurt Vonnegut")
1787 }
1788
1789 func TestGoldmark(t *testing.T) {
1790 t.Parallel()
1791
1792 content := `
1793 +++
1794 title = "A Page!"
1795 +++
1796
1797 ## Shortcode {{% t %}} in header
1798
1799 ## Code Fense in Shortcode
1800
1801 {{% s %}}
1802 $$$bash {hl_lines=[1]}
1803 SHORT
1804 $$$
1805 {{% /s %}}
1806
1807 ## Code Fence
1808
1809 $$$bash {hl_lines=[1]}
1810 MARKDOWN
1811 $$$
1812
1813 Link with URL as text
1814
1815 [https://google.com](https://google.com)
1816
1817
1818 `
1819 content = strings.ReplaceAll(content, "$$$", "```")
1820
1821 files := `
1822 -- hugo.toml --
1823 baseURL = "https://example.org"
1824
1825 [markup]
1826 defaultMarkdownHandler="goldmark"
1827 [markup.goldmark]
1828 [markup.goldmark.renderer]
1829 unsafe = false
1830 [markup.highlight]
1831 noClasses=false
1832 -- layouts/single.html --
1833 Title: {{ .Title }}
1834 ToC: {{ .TableOfContents }}
1835 Content: {{ .Content }}
1836 -- layouts/_shortcodes/t.html --
1837 T-SHORT
1838 -- layouts/_shortcodes/s.html --
1839 ## Code
1840 {{ .Inner }}
1841 -- content/page.md --
1842 ` + content + `
1843 `
1844 b := Test(t, files)
1845
1846 b.AssertFileContent("public/page/index.html",
1847 `<nav id="TableOfContents">
1848 <li><a href="#shortcode-t-short-in-header">Shortcode T-SHORT in header</a></li>
1849 <code class="language-bash" data-lang="bash"><span class="line hl"><span class="cl">SHORT
1850 <code class="language-bash" data-lang="bash"><span class="line hl"><span class="cl">MARKDOWN
1851 <p><a href="https://google.com">https://google.com</a></p>
1852 `)
1853 }
1854
1855 func TestPageHashString(t *testing.T) {
1856 files := `
1857 -- hugo.toml --
1858 baseURL = "https://example.org"
1859 [languages]
1860 [languages.en]
1861 weight = 1
1862 title = "English"
1863 [languages.no]
1864 weight = 2
1865 title = "Norsk"
1866 -- content/p1.md --
1867 ---
1868 title: "p1"
1869 ---
1870 -- content/p2.md --
1871 ---
1872 title: "p2"
1873 ---
1874 `
1875
1876 b := Test(t, files)
1877
1878 p1 := b.H.Sites[0].RegularPages()[0]
1879 p2 := b.H.Sites[0].RegularPages()[1]
1880 sites := p1.Sites()
1881
1882 b.Assert(p1, qt.Not(qt.Equals), p2)
1883
1884 b.Assert(hashing.HashString(p1), qt.Not(qt.Equals), hashing.HashString(p2))
1885 b.Assert(hashing.HashString(sites[0]), qt.Not(qt.Equals), hashing.HashString(sites[1]))
1886 }
1887
1888 // Issue #11243
1889 func TestRenderWithoutArgument(t *testing.T) {
1890 t.Parallel()
1891
1892 files := `
1893 -- hugo.toml --
1894 -- layouts/home.html --
1895 {{ .Render }}
1896 `
1897
1898 b, err := TestE(t, files)
1899
1900 b.Assert(err, qt.IsNotNil)
1901 }
1902
1903 // Issue #13021
1904 func TestAllStores(t *testing.T) {
1905 t.Parallel()
1906
1907 files := `
1908 -- hugo.toml --
1909 disableKinds = ["taxonomy", "term", "page", "section"]
1910 disableLiveReload = true
1911 -- content/_index.md --
1912 ---
1913 title: "Home"
1914 ---
1915 {{< s >}}
1916 -- layouts/_shortcodes/s.html --
1917 {{ if not (.Store.Get "Shortcode") }}{{ .Store.Set "Shortcode" (printf "sh-%s" $.Page.Title) }}{{ end }}
1918 Shortcode: {{ .Store.Get "Shortcode" }}|
1919 -- layouts/home.html --
1920 {{ .Content }}
1921 {{ if not (.Store.Get "Page") }}{{ .Store.Set "Page" (printf "p-%s" $.Title) }}{{ end }}
1922 {{ if not (hugo.Store.Get "Hugo") }}{{ hugo.Store.Set "Hugo" (printf "h-%s" $.Title) }}{{ end }}
1923 {{ if not (site.Store.Get "Site") }}{{ site.Store.Set "Site" (printf "s-%s" $.Title) }}{{ end }}
1924 Page: {{ .Store.Get "Page" }}|
1925 Hugo: {{ hugo.Store.Get "Hugo" }}|
1926 Site: {{ site.Store.Get "Site" }}|
1927 `
1928
1929 b := TestRunning(t, files)
1930
1931 b.AssertFileContent("public/index.html",
1932 `
1933 Shortcode: sh-Home|
1934 Page: p-Home|
1935 Site: s-Home|
1936 Hugo: h-Home|
1937 `,
1938 )
1939
1940 b.EditFileReplaceAll("content/_index.md", "Home", "Homer").Build()
1941
1942 b.AssertFileContent("public/index.html",
1943 `
1944 Shortcode: sh-Homer|
1945 Page: p-Homer|
1946 Site: s-Home|
1947 Hugo: h-Home|
1948 `,
1949 )
1950 }
1951
1952 // Issue 13538
1953 func TestHomePageIsLeafBundle(t *testing.T) {
1954 t.Parallel()
1955
1956 files := `
1957 -- hugo.toml --
1958 defaultContentLanguage = 'de'
1959 defaultContentLanguageInSubdir = true
1960 [languages.de]
1961 weight = 1
1962 [languages.en]
1963 weight = 2
1964 -- layouts/all.html --
1965 {{ .Title }}
1966 -- content/index.de.md --
1967 ---
1968 title: home de
1969 ---
1970 -- content/index.en.org --
1971 ---
1972 title: home en
1973 ---
1974 `
1975
1976 b := Test(t, files, TestOptWarn())
1977
1978 b.AssertFileContent("public/de/index.html", "home de")
1979 b.AssertFileContent("public/en/index.html", "home en")
1980 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.")
1981 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.")
1982 }
1983
1984 func content(c resource.ContentProvider) string {
1985 cc, err := c.Content(context.Background())
1986 if err != nil {
1987 panic(err)
1988 }
1989
1990 ccs, err := cast.ToStringE(cc)
1991 if err != nil {
1992 panic(err)
1993 }
1994 return ccs
1995 }
1996
1997 // See issue 11574.
1998 func TestPageIsBranch(t *testing.T) {
1999 t.Parallel()
2000
2001 files := `
2002 -- hugo.toml --
2003 disableKinds = ["taxonomy", "term", "rss", "sitemap", "robotsTXT", "404"]
2004 -- content/_index.md --
2005 -- content/sect/_index.md --
2006 -- content/sect/p1.md --
2007 -- layouts/all.html --
2008 {{ .Kind }}|IsBranch={{ .IsBranch }}|IsPage={{ .IsPage }}
2009 `
2010
2011 b := Test(t, files)
2012
2013 b.AssertFileContent("public/index.html", "home|IsBranch=true|IsPage=false")
2014 b.AssertFileContent("public/sect/index.html", "section|IsBranch=true|IsPage=false")
2015 b.AssertFileContent("public/sect/p1/index.html", "page|IsBranch=false|IsPage=true")
2016 }
2017
2018 // See issue 11574.
2019 func TestPageIsNodeDeprecated(t *testing.T) {
2020 files := `
2021 -- hugo.toml --
2022 disableKinds = ["taxonomy", "term", "rss", "sitemap", "robotsTXT", "404"]
2023 -- content/_index.md --
2024 -- layouts/all.html --
2025 {{ .IsNode }}
2026 `
2027
2028 b := Test(t, files, TestOptInfo())
2029
2030 b.AssertLogContains(".Page.IsNode was deprecated")
2031 }
2032
2033 func BenchmarkIsTranslatedOneLanguage(b *testing.B) {
2034 // Set it reasonably high to get a balance between cached and uncached calls to IsTranslated.
2035 const numPages = 3000
2036
2037 var files strings.Builder
2038 files.WriteString(`
2039 -- hugo.toml --
2040 disableKinds = ["taxonomy", "term"]
2041 `)
2042 for i := range numPages {
2043 files.WriteString(fmt.Sprintf(`
2044 -- content/sect/p%d.md --`, i))
2045 }
2046
2047 bb := Test(b, files.String(), TestOptSkipRender())
2048 p := bb.H.Sites[0].RegularPages()
2049
2050 b.ResetTimer()
2051
2052 for i := range b.N {
2053 if p[i%numPages].IsTranslated() {
2054 b.Fatalf("Page %d should not be translated", i)
2055 }
2056 }
2057 }
2058
2059 // See issue 15052.
2060 func TestRenderViewNotFound(t *testing.T) {
2061 t.Parallel()
2062
2063 files := `
2064 -- hugo.toml --
2065 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
2066 -- layouts/home.html --
2067 {{ .Render "view_foo" }}
2068 {{ .Render "view_bar" }}
2069 -- layouts/view_foo.html --
2070 foo
2071 `
2072
2073 b, err := TestE(t, files)
2074 b.Assert(err, qt.ErrorMatches, `.*template "view_bar" not found.*`)
2075 }
2076
2077 // See issue 15057.
2078 func TestRenderCaseInsensitiveTemplateName(t *testing.T) {
2079 t.Parallel()
2080
2081 files := `
2082 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2083 -- content/s1/p1.md --
2084 ---
2085 title: p1
2086 ---
2087 -- content/s2/p2.md --
2088 ---
2089 title: p2
2090 layout: ab
2091 ---
2092 -- content/s2/p3.md --
2093 ---
2094 title: p3
2095 layout: aB
2096 ---
2097 -- content/s2/p4.md --
2098 ---
2099 title: p4
2100 layout: AB
2101 ---
2102 -- content/s3/p5.md --
2103 ---
2104 title: p5
2105 layout: cd
2106 ---
2107 -- content/s3/p6.md --
2108 ---
2109 title: p6
2110 layout: cD
2111 ---
2112 -- content/s3/p7.md --
2113 ---
2114 title: p7
2115 layout: CD
2116 ---
2117 -- content/s4/p8.md --
2118 ---
2119 title: p8
2120 layout: ef
2121 ---
2122 -- content/s4/p9.md --
2123 ---
2124 title: p9
2125 layout: eF
2126 ---
2127 -- content/s4/p10.md --
2128 ---
2129 title: p10
2130 layout: EF
2131 ---
2132 -- layouts/s1/p1/page.html --
2133 {{ .Render "ab" }}|{{ .Render "aB" }}|{{ .Render "AB" }}
2134 {{ .Render "cd" }}|{{ .Render "cD" }}|{{ .Render "CD" }}
2135 {{ .Render "ef" }}|{{ .Render "eF" }}|{{ .Render "EF" }}
2136 -- layouts/s1/p1/ab.html --
2137 ab{{- /**/ -}}
2138 -- layouts/s1/p1/cD.html --
2139 cD{{- /**/ -}}
2140 -- layouts/s1/p1/EF.html --
2141 EF{{- /**/ -}}
2142 -- layouts/s2/ab.html --
2143 ab
2144 -- layouts/s3/cD.html --
2145 cD
2146 -- layouts/s4/EF.html --
2147 EF
2148 `
2149
2150 b := Test(t, files)
2151
2152 b.AssertFileContent("public/s1/p1/index.html",
2153 "ab|ab|ab",
2154 "cD|cD|cD",
2155 "EF|EF|EF",
2156 )
2157
2158 b.AssertFileContent("public/s2/p2/index.html", "ab")
2159 b.AssertFileContent("public/s2/p3/index.html", "ab")
2160 b.AssertFileContent("public/s2/p4/index.html", "ab")
2161
2162 b.AssertFileContent("public/s3/p5/index.html", "cD")
2163 b.AssertFileContent("public/s3/p6/index.html", "cD")
2164 b.AssertFileContent("public/s3/p7/index.html", "cD")
2165
2166 b.AssertFileContent("public/s4/p8/index.html", "EF")
2167 b.AssertFileContent("public/s4/p9/index.html", "EF")
2168 b.AssertFileContent("public/s4/p10/index.html", "EF")
2169 }
2170
2171 // See issue 15056.
2172 func TestRenderViewSubdir(t *testing.T) {
2173 t.Parallel()
2174
2175 files := `
2176 -- hugo.toml --
2177 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2178 -- content/s1/p1.md --
2179 ---
2180 title: p1
2181 ---
2182 -- layouts/s1/p1/page.html --
2183 {{ .Render "a" }}|{{ .Render "b" }}|{{ .Render "c" }}|{{ .Render "foo/d" }}|{{ .Render "foo/e" }}|{{ .Render "foo/f" }}|{{ .Render "sub/foo/g" }}
2184 -- layouts/s1/p1/a.html --
2185 a{{- /**/ -}}
2186 -- layouts/s1/b.html --
2187 b{{- /**/ -}}
2188 -- layouts/c.html --
2189 c{{- /**/ -}}
2190 -- layouts/s1/p1/foo/d.html --
2191 d{{- /**/ -}}
2192 -- layouts/s1/foo/e.html --
2193 e{{- /**/ -}}
2194 -- layouts/foo/f.html --
2195 f{{- /**/ -}}
2196 -- layouts/sub/foo/g.html --
2197 g{{- /**/ -}}
2198 `
2199
2200 b := Test(t, files)
2201 b.AssertFileContent("public/s1/p1/index.html", "a|b|c|d|e|f|g")
2202 }
2203
2204 // See issue 15056.
2205 func TestRenderViewSubdirRootPage(t *testing.T) {
2206 t.Parallel()
2207
2208 files := `
2209 -- hugo.toml --
2210 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2211 -- content/p1.md --
2212 ---
2213 title: p1
2214 ---
2215 -- layouts/page.html --
2216 {{ .Render "foo/a" }}
2217 -- layouts/foo/a.html --
2218 a{{- /**/ -}}
2219 `
2220
2221 b := Test(t, files)
2222 b.AssertFileContent("public/p1/index.html", "a")
2223 }
2224
2225 // See issue 15056.
2226 func TestRenderViewSubdirNotFound(t *testing.T) {
2227 t.Parallel()
2228
2229 files := `
2230 -- hugo.toml --
2231 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2232 -- content/s1/p1.md --
2233 ---
2234 title: p1
2235 ---
2236 -- layouts/s1/p1/page.html --
2237 {{ .Render "foo/missing" }}
2238 -- layouts/s1/p1/missing.html --
2239 should-not-match{{- /**/ -}}
2240 `
2241
2242 b, err := TestE(t, files)
2243 b.Assert(err, qt.ErrorMatches, `.*template "foo/missing" not found.*`)
2244 }
2245
2246 // See issue 15056.
2247 func TestRenderViewSubdirTrailingSlash(t *testing.T) {
2248 t.Parallel()
2249
2250 files := `
2251 -- hugo.toml --
2252 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2253 -- content/s1/p1.md --
2254 ---
2255 title: p1
2256 ---
2257 -- layouts/s1/p1/page.html --
2258 {{ .Render "foo/" }}
2259 -- layouts/s1/p1/foo.html --
2260 should-not-match{{- /**/ -}}
2261 `
2262
2263 b, err := TestE(t, files)
2264 b.Assert(err, qt.ErrorMatches, `.*template "foo/" not found.*`)
2265 }
2266
2267 // See issue 15056.
2268 func TestRenderViewSubdirCaseInsensitiveArgument(t *testing.T) {
2269 t.Parallel()
2270
2271 files := `
2272 -- hugo.toml --
2273 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2274 -- content/s1/p1.md --
2275 ---
2276 title: p1
2277 ---
2278 -- layouts/s1/p1/page.html --
2279 {{ .Render "Foo/Bar" }}
2280 -- layouts/s1/p1/foo/bar.html --
2281 bar{{- /**/ -}}
2282 `
2283
2284 b := Test(t, files)
2285 b.AssertFileContent("public/s1/p1/index.html", "bar")
2286 }
2287
2288 // See issue 15056.
2289 func TestRenderViewSubdirCaseInsensitivePath(t *testing.T) {
2290 t.Parallel()
2291
2292 files := `
2293 -- hugo.toml --
2294 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2295 -- content/s1/p1.md --
2296 ---
2297 title: p1
2298 ---
2299 -- layouts/s1/p1/page.html --
2300 {{ .Render "foo/bar" }}
2301 -- layouts/s1/p1/Foo/Bar.html --
2302 bar{{- /**/ -}}
2303 `
2304
2305 b := Test(t, files)
2306 b.AssertFileContent("public/s1/p1/index.html", "bar")
2307 }
2308
2309 // See issue 15056.
2310 func TestRenderViewSubdirWithUnderscore(t *testing.T) {
2311 t.Parallel()
2312
2313 files := `
2314 -- hugo.toml --
2315 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
2316 -- content/p1.md --
2317 ---
2318 title: p1
2319 ---
2320 -- content/s1/p2.md --
2321 ---
2322 title: p2
2323 ---
2324 -- content/s1/p3.md --
2325 ---
2326 title: p3
2327 ---
2328 -- layouts/page.html --
2329 {{ .Title }}: {{ .Render "_views/a" }}
2330 -- layouts/_views/a.html --
2331 layouts/_views/a.html
2332 -- layouts/s1/_views/a.html --
2333 layouts/s1/_views/a.html
2334 -- layouts/s1/p3/_views/a.html --
2335 layouts/s1/p3/_views/a.html
2336 `
2337
2338 b := Test(t, files)
2339
2340 b.AssertFileContent("public/p1/index.html", "p1: layouts/_views/a.html")
2341 b.AssertFileContent("public/s1/p2/index.html", "p2: layouts/s1/_views/a.html") // fails
2342 b.AssertFileContent("public/s1/p3/index.html", "p3: layouts/s1/p3/_views/a.html") // fails
2343 }
2344
2345 func TestPageConttentWeight(t *testing.T) {
2346 files := `
2347 -- hugo.toml --
2348 -- content/mysection/page1.md --
2349 -- content/myothersection/page2.md --
2350 -- content/myothersection/_index.md --
2351 -- layouts/all.html --
2352 All.
2353 `
2354 b := Test(t, files)
2355
2356 check := func(p page.Page, ok bool) {
2357 cw := p.(contentNodeContentWeightProvider).contentWeight()
2358 b.Assert(ok, qt.Equals, cw > 0)
2359 }
2360
2361 s := b.H.Sites[0]
2362 for _, p := range s.RegularPages() {
2363 check(p, true)
2364 }
2365 check(s.home, false)
2366 mysection, _ := s.GetPage("mysection")
2367 check(mysection, false)
2368 myothersection, _ := s.GetPage("myothersection") // backed by a content file.
2369 check(myothersection, true)
2370 }