github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/tpl/page/integration_test.go (about) 1 // Copyright 2023 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 page_test 15 16 import ( 17 "fmt" 18 "strings" 19 "testing" 20 21 "github.com/gohugoio/hugo/hugolib" 22 ) 23 24 func TestThatPageIsAvailableEverywhere(t *testing.T) { 25 t.Parallel() 26 27 filesTemplate := ` 28 -- config.toml -- 29 baseURL = 'http://example.com/' 30 disableKinds = ["taxonomy", "term"] 31 enableInlineShortcodes = true 32 paginate = 1 33 enableRobotsTXT = true 34 LANG_CONFIG 35 -- content/_index.md -- 36 --- 37 title: "Home" 38 aliases: ["/homealias/"] 39 --- 40 {{< shortcode "Angled Brackets" >}} 41 {{% shortcode "Percentage" %}} 42 43 {{< outer >}} 44 {{< inner >}} 45 {{< /outer >}} 46 47 {{< foo.inline >}}{{ if page.IsHome }}Shortcode Inline OK.{{ end }}{{< /foo.inline >}} 48 49 ## Heading 50 51 [I'm an inline-style link](https://www.google.com) 52 53  54 55 $$$bash 56 echo "hello"; 57 $$$ 58 59 -- content/p1.md -- 60 -- content/p2/index.md -- 61 -- content/p2/p2_1.md -- 62 --- 63 title: "P2_1" 64 --- 65 {{< foo.inline >}}{{ if page.IsHome }}Shortcode in bundled page OK.{{ else}}Failed.{{ end }}{{< /foo.inline >}} 66 -- content/p3.md -- 67 -- layouts/_default/_markup/render-heading.html -- 68 {{ if page.IsHome }} 69 Heading OK. 70 {{ end }} 71 -- layouts/_default/_markup/render-image.html -- 72 {{ if page.IsHome }} 73 Image OK. 74 {{ end }} 75 -- layouts/_default/_markup/render-link.html -- 76 {{ if page.IsHome }} 77 Link OK. 78 {{ end }} 79 -- layouts/_default/myview.html 80 {{ if page.IsHome }} 81 Render OK. 82 {{ end }} 83 -- layouts/_default/_markup/render-codeblock.html -- 84 {{ if page.IsHome }} 85 Codeblock OK. 86 {{ end }} 87 -- layouts/_default/single.html -- 88 Single. 89 -- layouts/index.html -- 90 {{ if eq page . }}Page OK.{{ end }} 91 {{ $r := "{{ if page.IsHome }}ExecuteAsTemplate OK.{{ end }}" | resources.FromString "foo.html" | resources.ExecuteAsTemplate "foo.html" . }} 92 {{ $r.Content }} 93 {{ .RenderString "{{< renderstring.inline >}}{{ if page.IsHome }}RenderString OK.{{ end }}{{< /renderstring.inline >}}}}"}} 94 {{ .Render "myview" }} 95 {{ .Content }} 96 partial: {{ partials.Include "foo.html" . }} 97 {{ $pag := (.Paginate site.RegularPages) }} 98 PageNumber: {{ $pag.PageNumber }}/{{ $pag.TotalPages }}| 99 {{ $p2 := site.GetPage "p2" }} 100 {{ $p2_1 := index $p2.Resources 0 }} 101 Bundled page: {{ $p2_1.Content }} 102 -- layouts/alias.html -- 103 {{ if eq page .Page }}Alias OK.{{ else }}Failed.{{ end }} 104 -- layouts/404.html -- 105 {{ if eq page . }}404 Page OK.{{ else }}Failed.{{ end }} 106 -- layouts/partials/foo.html -- 107 {{ if page.IsHome }}Partial OK.{{ else }}Failed.{{ end }} 108 -- layouts/shortcodes/outer.html -- 109 {{ .Inner }} 110 -- layouts/shortcodes/inner.html -- 111 {{ if page.IsHome }}Shortcode Inner OK.{{ else }}Failed.{{ end }} 112 -- layouts/shortcodes/shortcode.html -- 113 {{ if page.IsHome }}Shortcode {{ .Get 0 }} OK.{{ else }}Failed.{{ end }} 114 -- layouts/sitemap.xml -- 115 HRE?{{ if eq page . }}Sitemap OK.{{ else }}Failed.{{ end }} 116 -- layouts/robots.txt -- 117 {{ if eq page . }}Robots OK.{{ else }}Failed.{{ end }} 118 -- layouts/sitemapindex.xml -- 119 {{ if not page }}SitemapIndex OK.{{ else }}Failed.{{ end }} 120 121 ` 122 123 for _, multilingual := range []bool{false, true} { 124 t.Run(fmt.Sprintf("multilingual-%t", multilingual), func(t *testing.T) { 125 // Fenced code blocks. 126 files := strings.ReplaceAll(filesTemplate, "$$$", "```") 127 128 if multilingual { 129 files = strings.ReplaceAll(files, "LANG_CONFIG", ` 130 [languages] 131 [languages.en] 132 weight = 1 133 [languages.no] 134 weight = 2 135 `) 136 } else { 137 files = strings.ReplaceAll(files, "LANG_CONFIG", "") 138 } 139 140 b := hugolib.NewIntegrationTestBuilder( 141 hugolib.IntegrationTestConfig{ 142 T: t, 143 TxtarString: files, 144 }, 145 ).Build() 146 147 b.AssertFileContent("public/index.html", ` 148 Heading OK. 149 Image OK. 150 Link OK. 151 Codeblock OK. 152 Page OK. 153 Partial OK. 154 Shortcode Angled Brackets OK. 155 Shortcode Percentage OK. 156 Shortcode Inner OK. 157 Shortcode Inline OK. 158 ExecuteAsTemplate OK. 159 RenderString OK. 160 Render OK. 161 Shortcode in bundled page OK. 162 `) 163 164 b.AssertFileContent("public/404.html", `404 Page OK.`) 165 b.AssertFileContent("public/robots.txt", `Robots OK.`) 166 b.AssertFileContent("public/homealias/index.html", `Alias OK.`) 167 b.AssertFileContent("public/page/1/index.html", `Alias OK.`) 168 b.AssertFileContent("public/page/2/index.html", `Page OK.`) 169 if multilingual { 170 b.AssertFileContent("public/sitemap.xml", `SitemapIndex OK.`) 171 } else { 172 b.AssertFileContent("public/sitemap.xml", `Sitemap OK.`) 173 } 174 175 }) 176 177 } 178 179 } 180 181 // Issue 10791. 182 func TestPageTableOfContentsInShortcode(t *testing.T) { 183 t.Parallel() 184 185 files := ` 186 -- config.toml -- 187 baseURL = 'http://example.com/' 188 disableKinds = ["taxonomy", "term"] 189 -- content/p1.md -- 190 --- 191 title: "P1" 192 --- 193 {{< toc >}} 194 195 # Heading 1 196 -- layouts/shortcodes/toc.html -- 197 {{ page.TableOfContents }} 198 -- layouts/_default/single.html -- 199 {{ .Content }} 200 ` 201 202 b := hugolib.NewIntegrationTestBuilder( 203 hugolib.IntegrationTestConfig{ 204 T: t, 205 TxtarString: files, 206 }, 207 ).Build() 208 209 b.AssertFileContent("public/p1/index.html", "<nav id=\"TableOfContents\"></nav> \n<h1 id=\"heading-1\">Heading 1</h1>") 210 211 }