| 1 |
// Copyright 2026 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 tplimpl_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"strings" |
| 18 |
"testing" |
| 19 |
|
| 20 |
qt "github.com/frankban/quicktest" |
| 21 |
|
| 22 |
"github.com/gohugoio/hugo/hugolib" |
| 23 |
) |
| 24 |
|
| 25 |
func TestDisqusTemplate(t *testing.T) { |
| 26 |
t.Parallel() |
| 27 |
|
| 28 |
files := ` |
| 29 |
-- hugo.toml -- |
| 30 |
disableKinds = ['page','section','rss','sitemap','taxonomy','term'] |
| 31 |
[services.disqus] |
| 32 |
shortname = 'foo' |
| 33 |
[privacy.disqus] |
| 34 |
disable = false |
| 35 |
-- layouts/home.html -- |
| 36 |
{{ .Title }} |
| 37 |
{{ partial "disqus.html" . }} |
| 38 |
-- content/_index.md -- |
| 39 |
+++ |
| 40 |
title = 'Home' |
| 41 |
[params] |
| 42 |
disqus_identifier = 'my-identifier' |
| 43 |
disqus_title = 'My Title' |
| 44 |
disqus_url = 'https://example.com/my-page/' |
| 45 |
+++ |
| 46 |
` |
| 47 |
|
| 48 |
b := hugolib.Test(t, files) |
| 49 |
|
| 50 |
b.AssertFileContent( |
| 51 |
"public/index.html", |
| 52 |
`s.src = '//' + "foo" + '.disqus.com/embed.js';`, |
| 53 |
`this.page.identifier = 'my-identifier';`, |
| 54 |
`this.page.title = 'My Title';`, |
| 55 |
`this.page.url = 'https:\/\/example.com\/my-page\/';`, |
| 56 |
) |
| 57 |
|
| 58 |
// disable = true: no output |
| 59 |
f := strings.ReplaceAll(files, "disable = false", "disable = true") |
| 60 |
b = hugolib.Test(t, f) |
| 61 |
b.AssertFileContent("public/index.html", `! disqus_thread`) |
| 62 |
|
| 63 |
// No shortname: no output |
| 64 |
f = strings.ReplaceAll(files, "shortname = 'foo'", "shortname = ''") |
| 65 |
b = hugolib.Test(t, f) |
| 66 |
b.AssertFileContent("public/index.html", `! disqus_thread`) |
| 67 |
} |
| 68 |
|
| 69 |
func TestGoogleAnalyticsTemplate(t *testing.T) { |
| 70 |
t.Parallel() |
| 71 |
|
| 72 |
files := ` |
| 73 |
-- hugo.toml -- |
| 74 |
disableKinds = ['page','section','rss','sitemap','taxonomy','term'] |
| 75 |
[privacy.googleAnalytics] |
| 76 |
disable = false |
| 77 |
DNT |
| 78 |
[services.googleAnalytics] |
| 79 |
id = 'G-0123456789' |
| 80 |
-- layouts/home.html -- |
| 81 |
{{ .Title }} |
| 82 |
{{ partial "google_analytics.html" . }} |
| 83 |
-- content/_index.md -- |
| 84 |
--- |
| 85 |
title: Home |
| 86 |
--- |
| 87 |
` |
| 88 |
|
| 89 |
// Default respectDoNotTrack value |
| 90 |
f := strings.ReplaceAll(files, "DNT", "") |
| 91 |
b := hugolib.Test(t, f) |
| 92 |
b.AssertFileContent( |
| 93 |
"public/index.html", |
| 94 |
`<script async src="https://www.googletagmanager.com/gtag/js?id=G-0123456789"></script>`, |
| 95 |
`if ( true )`, |
| 96 |
) |
| 97 |
|
| 98 |
// respectDoNotTrack = true |
| 99 |
f = strings.ReplaceAll(files, "DNT", "respectDoNotTrack = true") |
| 100 |
b = hugolib.Test(t, f) |
| 101 |
b.AssertFileContent( |
| 102 |
"public/index.html", |
| 103 |
`<script async src="https://www.googletagmanager.com/gtag/js?id=G-0123456789"></script>`, |
| 104 |
`if ( true )`, |
| 105 |
) |
| 106 |
|
| 107 |
// respectDoNotTrack = false |
| 108 |
f = strings.ReplaceAll(files, "DNT", "respectDoNotTrack = false") |
| 109 |
b = hugolib.Test(t, f) |
| 110 |
b.AssertFileContent( |
| 111 |
"public/index.html", |
| 112 |
`<script async src="https://www.googletagmanager.com/gtag/js?id=G-0123456789"></script>`, |
| 113 |
`if ( false )`, |
| 114 |
) |
| 115 |
|
| 116 |
// disable = true: no output |
| 117 |
f = strings.ReplaceAll(files, "DNT", "") |
| 118 |
f = strings.ReplaceAll(f, "disable = false", "disable = true") |
| 119 |
b = hugolib.Test(t, f) |
| 120 |
b.AssertFileContent("public/index.html", `! googletagmanager`) |
| 121 |
|
| 122 |
// No ID: no output |
| 123 |
f = strings.ReplaceAll(files, "DNT", "") |
| 124 |
f = strings.ReplaceAll(f, "id = 'G-0123456789'", "id = ''") |
| 125 |
b = hugolib.Test(t, f) |
| 126 |
b.AssertFileContent("public/index.html", `! googletagmanager`) |
| 127 |
|
| 128 |
// UA- prefix ID: warning logged, no script output |
| 129 |
f = strings.ReplaceAll(files, "DNT", "") |
| 130 |
f = strings.ReplaceAll(f, "G-0123456789", "UA-12345678-1") |
| 131 |
b = hugolib.Test(t, f) |
| 132 |
b.AssertFileContent("public/index.html", `! googletagmanager`) |
| 133 |
} |
| 134 |
|
| 135 |
func TestOpengraphTemplate(t *testing.T) { |
| 136 |
t.Parallel() |
| 137 |
|
| 138 |
files := ` |
| 139 |
-- hugo.toml -- |
| 140 |
baseURL = "/" |
| 141 |
title = 'My Site' |
| 142 |
capitalizeListTitles = false |
| 143 |
disableKinds = ['section','rss','sitemap','taxonomy'] |
| 144 |
locale = 'en-US' |
| 145 |
[markup.goldmark.renderer] |
| 146 |
unsafe = true |
| 147 |
[params] |
| 148 |
description = "m <em>n</em> and **o** can't." |
| 149 |
[params.social] |
| 150 |
facebook_admin = 'foo' |
| 151 |
[taxonomies] |
| 152 |
series = 'series' |
| 153 |
tag = 'tags' |
| 154 |
-- layouts/home.html -- |
| 155 |
{{ partial "opengraph.html" . }} |
| 156 |
-- layouts/page.html -- |
| 157 |
{{ partial "opengraph.html" . }} |
| 158 |
-- layouts/term.html -- |
| 159 |
{{ partial "opengraph.html" . }} |
| 160 |
-- content/_index.md -- |
| 161 |
--- |
| 162 |
title: Home |
| 163 |
--- |
| 164 |
-- content/s1/p1.md -- |
| 165 |
--- |
| 166 |
title: p1 |
| 167 |
date: 2024-04-24T08:00:00-07:00 |
| 168 |
lastmod: 2024-04-24T11:00:00-07:00 |
| 169 |
images: [a.jpg,b.jpg] |
| 170 |
audio: [c.mp3,d.mp3] |
| 171 |
videos: [e.mp4,f.mp4] |
| 172 |
series: [series-1] |
| 173 |
tags: [t1,t2] |
| 174 |
--- |
| 175 |
a <em>b</em> and **c** can't. |
| 176 |
-- content/s1/p2.md -- |
| 177 |
--- |
| 178 |
title: p2 |
| 179 |
series: [series-1] |
| 180 |
--- |
| 181 |
d <em>e</em> and **f** can't. |
| 182 |
<!--more--> |
| 183 |
-- content/s1/p3.md -- |
| 184 |
--- |
| 185 |
title: p3 |
| 186 |
series: [series-1] |
| 187 |
summary: g <em>h</em> and **i** can't. |
| 188 |
--- |
| 189 |
-- content/s1/p4.md -- |
| 190 |
--- |
| 191 |
title: p4 |
| 192 |
series: [series-1] |
| 193 |
description: j <em>k</em> and **l** can't. |
| 194 |
--- |
| 195 |
-- content/s1/p5.md -- |
| 196 |
--- |
| 197 |
title: p5 |
| 198 |
series: [series-1] |
| 199 |
--- |
| 200 |
-- content/s1/p6.md -- |
| 201 |
--- |
| 202 |
title: p6 |
| 203 |
series: [series-1] |
| 204 |
locale: fr-FR |
| 205 |
--- |
| 206 |
-- content/s1/p7.md -- |
| 207 |
--- |
| 208 |
title: '' |
| 209 |
--- |
| 210 |
-- content/p0.md -- |
| 211 |
--- |
| 212 |
title: p0 |
| 213 |
--- |
| 214 |
` |
| 215 |
|
| 216 |
b := hugolib.Test(t, files) |
| 217 |
|
| 218 |
b.AssertFileContent( |
| 219 |
"public/s1/p1/index.html", ` |
| 220 |
<meta property="og:url" content="/s1/p1/"> |
| 221 |
<meta property="og:site_name" content="My Site"> |
| 222 |
<meta property="og:title" content="p1"> |
| 223 |
<meta property="og:description" content="a b and c can’t."> |
| 224 |
<meta property="og:locale" content="en_US"> |
| 225 |
<meta property="og:type" content="article"> |
| 226 |
<meta property="article:section" content="s1"> |
| 227 |
<meta property="article:published_time" content="2024-04-24T08:00:00-07:00"> |
| 228 |
<meta property="article:modified_time" content="2024-04-24T11:00:00-07:00"> |
| 229 |
<meta property="article:tag" content="t1"> |
| 230 |
<meta property="article:tag" content="t2"> |
| 231 |
<meta property="og:image" content="/a.jpg"> |
| 232 |
<meta property="og:image" content="/b.jpg"> |
| 233 |
<meta property="og:audio" content="/c.mp3"> |
| 234 |
<meta property="og:audio" content="/d.mp3"> |
| 235 |
<meta property="og:video" content="/e.mp4"> |
| 236 |
<meta property="og:video" content="/f.mp4"> |
| 237 |
<meta property="og:see_also" content="/s1/p2/"> |
| 238 |
<meta property="og:see_also" content="/s1/p3/"> |
| 239 |
<meta property="og:see_also" content="/s1/p4/"> |
| 240 |
<meta property="og:see_also" content="/s1/p5/"> |
| 241 |
<meta property="fb:admins" content="foo"> |
| 242 |
`, |
| 243 |
) |
| 244 |
|
| 245 |
// Description from content excerpt |
| 246 |
b.AssertFileContent( |
| 247 |
"public/s1/p2/index.html", |
| 248 |
`<meta property="og:description" content="d e and f can’t.">`, |
| 249 |
) |
| 250 |
|
| 251 |
// Description from front matter summary |
| 252 |
b.AssertFileContent( |
| 253 |
"public/s1/p3/index.html", |
| 254 |
`<meta property="og:description" content="g h and i can’t.">`, |
| 255 |
) |
| 256 |
|
| 257 |
// The markdown is intentionally not rendered to HTML. |
| 258 |
b.AssertFileContent( |
| 259 |
"public/s1/p4/index.html", |
| 260 |
`<meta property="og:description" content="j k and **l** can't.">`, |
| 261 |
) |
| 262 |
|
| 263 |
// The markdown is intentionally not rendered to HTML. |
| 264 |
b.AssertFileContent( |
| 265 |
"public/s1/p5/index.html", |
| 266 |
`<meta property="og:description" content="m n and **o** can't.">`, |
| 267 |
) |
| 268 |
|
| 269 |
// og:locale from page front matter |
| 270 |
b.AssertFileContent( |
| 271 |
"public/s1/p6/index.html", |
| 272 |
`<meta property="og:locale" content="fr_FR">`, |
| 273 |
) |
| 274 |
|
| 275 |
// No article:section for root-level page |
| 276 |
b.AssertFileContent( |
| 277 |
"public/p0/index.html", |
| 278 |
`<meta property="og:type" content="article">`, |
| 279 |
`! article:section`, |
| 280 |
) |
| 281 |
|
| 282 |
// Term page |
| 283 |
b.AssertFileContent( |
| 284 |
"public/series/series-1/index.html", |
| 285 |
`<meta property="og:url" content="/series/series-1/">`, |
| 286 |
`<meta property="og:title" content="series-1">`, |
| 287 |
`<meta property="og:type" content="website">`, |
| 288 |
) |
| 289 |
|
| 290 |
// site.Title fallback when page title is empty |
| 291 |
b.AssertFileContent("public/s1/p7/index.html", `<meta property="og:title" content="My Site">`) |
| 292 |
|
| 293 |
// facebook_app_id takes precedence over facebook_admin |
| 294 |
f := strings.ReplaceAll(files, "facebook_admin = 'foo'", "facebook_app_id = 'bar'") |
| 295 |
b = hugolib.Test(t, f) |
| 296 |
b.AssertFileContent( |
| 297 |
"public/s1/p1/index.html", |
| 298 |
`<meta property="fb:app_id" content="bar">`, |
| 299 |
`! fb:admins`, |
| 300 |
) |
| 301 |
|
| 302 |
// Issue 14433 |
| 303 |
b.AssertFileContent( |
| 304 |
"public/index.html", |
| 305 |
`<meta property="og:title" content="Home">`, |
| 306 |
`<meta property="og:locale" content="en_US">`, |
| 307 |
`<meta property="og:type" content="website">`, |
| 308 |
`! false`, |
| 309 |
) |
| 310 |
|
| 311 |
// No description: no meta |
| 312 |
f = strings.ReplaceAll(files, `description = "m <em>n</em> and **o** can't."`, "") |
| 313 |
b = hugolib.Test(t, f) |
| 314 |
b.AssertFileContent("public/s1/p5/index.html", `! og:description`) |
| 315 |
} |
| 316 |
|
| 317 |
func TestPaginationTemplate(t *testing.T) { |
| 318 |
t.Parallel() |
| 319 |
|
| 320 |
files := ` |
| 321 |
-- hugo.toml -- |
| 322 |
pagination.pagerSize = 1 |
| 323 |
-- layouts/home.html -- |
| 324 |
{{ .Paginate (where site.RegularPages "Section" "s1") }}PARTIAL |
| 325 |
-- content/s1/p01.md -- |
| 326 |
--- |
| 327 |
title: p01 |
| 328 |
--- |
| 329 |
-- content/s1/p02.md -- |
| 330 |
--- |
| 331 |
title: p02 |
| 332 |
--- |
| 333 |
-- content/s1/p03.md -- |
| 334 |
--- |
| 335 |
title: p03 |
| 336 |
--- |
| 337 |
-- content/s1/p04.md -- |
| 338 |
--- |
| 339 |
title: p04 |
| 340 |
--- |
| 341 |
-- content/s1/p05.md -- |
| 342 |
--- |
| 343 |
title: p05 |
| 344 |
--- |
| 345 |
-- content/s1/p06.md -- |
| 346 |
--- |
| 347 |
title: p06 |
| 348 |
--- |
| 349 |
-- content/s1/p07.md -- |
| 350 |
--- |
| 351 |
title: p07 |
| 352 |
--- |
| 353 |
-- content/s1/p08.md -- |
| 354 |
--- |
| 355 |
title: p08 |
| 356 |
--- |
| 357 |
-- content/s1/p09.md -- |
| 358 |
--- |
| 359 |
title: p09 |
| 360 |
--- |
| 361 |
-- content/s1/p10.md -- |
| 362 |
--- |
| 363 |
title: p10 |
| 364 |
--- |
| 365 |
` |
| 366 |
|
| 367 |
test := func(variant string, expectedOutput string) { |
| 368 |
b := hugolib.Test(t, strings.ReplaceAll(files, "PARTIAL", variant)) |
| 369 |
b.AssertFileContent("public/index.html", expectedOutput) |
| 370 |
} |
| 371 |
|
| 372 |
expectedOutputDefaultFormat := "Pager 1\n <ul class=\"pagination pagination-default\">\n <li class=\"page-item disabled\">\n <a aria-disabled=\"true\" aria-label=\"First\" class=\"page-link\" role=\"button\" tabindex=\"-1\"><span aria-hidden=\"true\">««</span></a>\n </li>\n <li class=\"page-item disabled\">\n <a aria-disabled=\"true\" aria-label=\"Previous\" class=\"page-link\" role=\"button\" tabindex=\"-1\"><span aria-hidden=\"true\">«</span></a>\n </li>\n <li class=\"page-item active\">\n <a aria-current=\"page\" aria-label=\"Page 1\" class=\"page-link\" role=\"button\">1</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/2/\" aria-label=\"Page 2\" class=\"page-link\" role=\"button\">2</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/3/\" aria-label=\"Page 3\" class=\"page-link\" role=\"button\">3</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/4/\" aria-label=\"Page 4\" class=\"page-link\" role=\"button\">4</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/5/\" aria-label=\"Page 5\" class=\"page-link\" role=\"button\">5</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/2/\" aria-label=\"Next\" class=\"page-link\" role=\"button\"><span aria-hidden=\"true\">»</span></a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/10/\" aria-label=\"Last\" class=\"page-link\" role=\"button\"><span aria-hidden=\"true\">»»</span></a>\n </li>\n </ul>" |
| 373 |
expectedOutputTerseFormat := "Pager 1\n <ul class=\"pagination pagination-terse\">\n <li class=\"page-item active\">\n <a aria-current=\"page\" aria-label=\"Page 1\" class=\"page-link\" role=\"button\">1</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/2/\" aria-label=\"Page 2\" class=\"page-link\" role=\"button\">2</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/3/\" aria-label=\"Page 3\" class=\"page-link\" role=\"button\">3</a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/2/\" aria-label=\"Next\" class=\"page-link\" role=\"button\"><span aria-hidden=\"true\">»</span></a>\n </li>\n <li class=\"page-item\">\n <a href=\"/page/10/\" aria-label=\"Last\" class=\"page-link\" role=\"button\"><span aria-hidden=\"true\">»»</span></a>\n </li>\n </ul>" |
| 374 |
|
| 375 |
test(`{{ partial "pagination.html" . }}`, expectedOutputDefaultFormat) |
| 376 |
test(`{{ partial "pagination.html" (dict "page" .) }}`, expectedOutputDefaultFormat) |
| 377 |
test(`{{ partial "pagination.html" (dict "page" . "format" "default") }}`, expectedOutputDefaultFormat) |
| 378 |
test(`{{ partial "pagination.html" (dict "page" . "format" "terse") }}`, expectedOutputTerseFormat) |
| 379 |
|
| 380 |
// Default format, last page: active First and Prev, disabled Next and Last |
| 381 |
f := strings.ReplaceAll(files, "PARTIAL", `{{ partial "pagination.html" . }}`) |
| 382 |
b := hugolib.Test(t, f) |
| 383 |
b.AssertFileContent( |
| 384 |
"public/page/10/index.html", |
| 385 |
`<a href="/" aria-label="First" class="page-link" role="button">`, |
| 386 |
`<a href="/page/9/" aria-label="Previous" class="page-link" role="button">`, |
| 387 |
`aria-disabled="true" aria-label="Next"`, |
| 388 |
`aria-disabled="true" aria-label="Last"`, |
| 389 |
) |
| 390 |
|
| 391 |
// Terse format, last page: First and Prev shown, Next and Last hidden |
| 392 |
f = strings.ReplaceAll(files, "PARTIAL", `{{ partial "pagination.html" (dict "page" . "format" "terse") }}`) |
| 393 |
b = hugolib.Test(t, f) |
| 394 |
b.AssertFileContent( |
| 395 |
"public/page/10/index.html", |
| 396 |
`<a href="/" aria-label="First" class="page-link" role="button">`, |
| 397 |
`<a href="/page/9/" aria-label="Previous" class="page-link" role="button">`, |
| 398 |
`! aria-label="Next"`, |
| 399 |
`! aria-label="Last"`, |
| 400 |
) |
| 401 |
|
| 402 |
// TotalPages <= 1: no output |
| 403 |
f = ` |
| 404 |
-- hugo.toml -- |
| 405 |
-- layouts/home.html -- |
| 406 |
{{ .Paginate (where site.RegularPages "Section" "s1") }}{{ partial "pagination.html" . }} |
| 407 |
-- content/s1/p1.md -- |
| 408 |
--- |
| 409 |
title: p1 |
| 410 |
--- |
| 411 |
` |
| 412 |
b = hugolib.Test(t, f) |
| 413 |
b.AssertFileContent("public/index.html", `! class="pagination"`) |
| 414 |
|
| 415 |
// Unsupported format: build error |
| 416 |
f = strings.ReplaceAll(files, "PARTIAL", `{{ partial "pagination.html" (dict "page" . "format" "fancy") }}`) |
| 417 |
b, err := hugolib.TestE(t, f) |
| 418 |
b.Assert(err, qt.IsNotNil) |
| 419 |
|
| 420 |
// Map without page key: build error |
| 421 |
f = strings.ReplaceAll(files, "PARTIAL", `{{ partial "pagination.html" (dict "format" "default") }}`) |
| 422 |
b, err = hugolib.TestE(t, f) |
| 423 |
b.Assert(err, qt.IsNotNil) |
| 424 |
} |
| 425 |
|
| 426 |
// Issue 12432 |
| 427 |
func TestSchemaTemplate(t *testing.T) { |
| 428 |
t.Parallel() |
| 429 |
|
| 430 |
files := ` |
| 431 |
-- hugo.toml -- |
| 432 |
baseURL = "/" |
| 433 |
capitalizeListTitles = false |
| 434 |
disableKinds = ['rss','sitemap'] |
| 435 |
[markup.goldmark.renderer] |
| 436 |
unsafe = true |
| 437 |
[params] |
| 438 |
description = "m <em>n</em> and **o** can't." |
| 439 |
[taxonomies] |
| 440 |
tag = 'tags' |
| 441 |
-- layouts/list.html -- |
| 442 |
{{ partial "schema.html" . }} |
| 443 |
-- layouts/single.html -- |
| 444 |
{{ partial "schema.html" . }} |
| 445 |
-- content/s1/p1.md -- |
| 446 |
--- |
| 447 |
title: p1 |
| 448 |
date: 2024-04-24T08:00:00-07:00 |
| 449 |
lastmod: 2024-04-24T11:00:00-07:00 |
| 450 |
images: [a.jpg,b.jpg] |
| 451 |
tags: [t1,t2] |
| 452 |
--- |
| 453 |
a <em>b</em> and **c** can't. |
| 454 |
-- content/s1/p2.md -- |
| 455 |
--- |
| 456 |
title: p2 |
| 457 |
--- |
| 458 |
d <em>e</em> and **f** can't. |
| 459 |
<!--more--> |
| 460 |
-- content/s1/p3.md -- |
| 461 |
--- |
| 462 |
title: p3 |
| 463 |
summary: g <em>h</em> and **i** can't. |
| 464 |
--- |
| 465 |
-- content/s1/p4.md -- |
| 466 |
--- |
| 467 |
title: p4 |
| 468 |
description: j <em>k</em> and **l** can't. |
| 469 |
--- |
| 470 |
-- content/s1/p5.md -- |
| 471 |
--- |
| 472 |
title: p5 |
| 473 |
--- |
| 474 |
-- content/s1/p6.md -- |
| 475 |
--- |
| 476 |
title: p6 |
| 477 |
keywords: [k1,k2] |
| 478 |
--- |
| 479 |
` |
| 480 |
|
| 481 |
b := hugolib.Test(t, files) |
| 482 |
|
| 483 |
b.AssertFileContent( |
| 484 |
"public/s1/p1/index.html", ` |
| 485 |
<meta itemprop="name" content="p1"> |
| 486 |
<meta itemprop="description" content="a b and c can’t."> |
| 487 |
<meta itemprop="datePublished" content="2024-04-24T08:00:00-07:00"> |
| 488 |
<meta itemprop="dateModified" content="2024-04-24T11:00:00-07:00"> |
| 489 |
<meta itemprop="wordCount" content="5"> |
| 490 |
<meta itemprop="image" content="/a.jpg"> |
| 491 |
<meta itemprop="image" content="/b.jpg"> |
| 492 |
<meta itemprop="keywords" content="t1,t2"> |
| 493 |
`, |
| 494 |
) |
| 495 |
|
| 496 |
// Description from content excerpt |
| 497 |
b.AssertFileContent( |
| 498 |
"public/s1/p2/index.html", |
| 499 |
`<meta itemprop="description" content="d e and f can’t.">`, |
| 500 |
) |
| 501 |
|
| 502 |
// Description from front matter summary |
| 503 |
b.AssertFileContent( |
| 504 |
"public/s1/p3/index.html", |
| 505 |
`<meta itemprop="description" content="g h and i can’t.">`, |
| 506 |
) |
| 507 |
|
| 508 |
// The markdown is intentionally not rendered to HTML. |
| 509 |
b.AssertFileContent( |
| 510 |
"public/s1/p4/index.html", |
| 511 |
`<meta itemprop="description" content="j k and **l** can't.">`, |
| 512 |
) |
| 513 |
|
| 514 |
// The markdown is intentionally not rendered to HTML. |
| 515 |
b.AssertFileContent( |
| 516 |
"public/s1/p5/index.html", |
| 517 |
`<meta itemprop="description" content="m n and **o** can't.">`, |
| 518 |
) |
| 519 |
|
| 520 |
// Front matter keywords |
| 521 |
b.AssertFileContent( |
| 522 |
"public/s1/p6/index.html", |
| 523 |
`<meta itemprop="keywords" content="k1,k2">`, |
| 524 |
) |
| 525 |
|
| 526 |
// No keywords: no meta |
| 527 |
b.AssertFileContent("public/s1/p5/index.html", `! itemprop="keywords"`) |
| 528 |
|
| 529 |
// Keywords taxonomy |
| 530 |
f := strings.ReplaceAll(files, "tag = 'tags'", "tag = 'tags'\nkeyword = 'keywords'") |
| 531 |
b = hugolib.Test(t, f) |
| 532 |
b.AssertFileContent("public/s1/p6/index.html", `<meta itemprop="keywords" content="k1,k2">`) |
| 533 |
|
| 534 |
// Any taxonomy fallback |
| 535 |
f = strings.ReplaceAll(files, "tag = 'tags'", "series = 'series'") |
| 536 |
f = strings.ReplaceAll(f, "tags: [t1,t2]", "series: [s1]") |
| 537 |
b = hugolib.Test(t, f) |
| 538 |
b.AssertFileContent("public/s1/p1/index.html", `<meta itemprop="keywords" content="s1">`) |
| 539 |
|
| 540 |
// No description: no meta |
| 541 |
f = strings.ReplaceAll(files, `description = "m <em>n</em> and **o** can't."`, "") |
| 542 |
b = hugolib.Test(t, f) |
| 543 |
b.AssertFileContent("public/s1/p5/index.html", `! itemprop="description"`) |
| 544 |
|
| 545 |
// name falls back to site.Title when page title is empty |
| 546 |
f = ` |
| 547 |
-- hugo.toml -- |
| 548 |
title = 'My Site' |
| 549 |
disableKinds = ['rss','sitemap'] |
| 550 |
-- layouts/single.html -- |
| 551 |
{{ partial "schema.html" . }} |
| 552 |
-- content/p1.md -- |
| 553 |
--- |
| 554 |
title: '' |
| 555 |
--- |
| 556 |
` |
| 557 |
b = hugolib.Test(t, f) |
| 558 |
b.AssertFileContent("public/p1/index.html", `<meta itemprop="name" content="My Site">`) |
| 559 |
} |
| 560 |
|
| 561 |
// Issue 12433 |
| 562 |
func TestTwitterCardsTemplate(t *testing.T) { |
| 563 |
t.Parallel() |
| 564 |
|
| 565 |
files := ` |
| 566 |
-- hugo.toml -- |
| 567 |
capitalizeListTitles = false |
| 568 |
disableKinds = ['rss','sitemap','taxonomy','term'] |
| 569 |
[markup.goldmark.renderer] |
| 570 |
unsafe = true |
| 571 |
[params] |
| 572 |
description = "m <em>n</em> and **o** can't." |
| 573 |
[params.social] |
| 574 |
twitter = 'foo' |
| 575 |
-- layouts/list.html -- |
| 576 |
{{ partial "twitter_cards.html" . }} |
| 577 |
-- layouts/single.html -- |
| 578 |
{{ partial "twitter_cards.html" . }} |
| 579 |
-- content/s1/p1.md -- |
| 580 |
--- |
| 581 |
title: p1 |
| 582 |
images: [a.jpg,b.jpg] |
| 583 |
--- |
| 584 |
a <em>b</em> and **c** can't. |
| 585 |
-- content/s1/p2.md -- |
| 586 |
--- |
| 587 |
title: p2 |
| 588 |
--- |
| 589 |
d <em>e</em> and **f** can't. |
| 590 |
<!--more--> |
| 591 |
-- content/s1/p3.md -- |
| 592 |
--- |
| 593 |
title: p3 |
| 594 |
summary: g <em>h</em> and **i** can't. |
| 595 |
--- |
| 596 |
-- content/s1/p4.md -- |
| 597 |
--- |
| 598 |
title: p4 |
| 599 |
description: j <em>k</em> and **l** can't. |
| 600 |
--- |
| 601 |
-- content/s1/p5.md -- |
| 602 |
--- |
| 603 |
title: p5 |
| 604 |
--- |
| 605 |
-- content/s1/p6.md -- |
| 606 |
--- |
| 607 |
title: '' |
| 608 |
--- |
| 609 |
` |
| 610 |
|
| 611 |
b := hugolib.Test(t, files) |
| 612 |
|
| 613 |
b.AssertFileContent( |
| 614 |
"public/s1/p1/index.html", ` |
| 615 |
<meta name="twitter:card" content="summary_large_image"> |
| 616 |
<meta name="twitter:image" content="https://example.org/a.jpg"> |
| 617 |
<meta name="twitter:title" content="p1"> |
| 618 |
<meta name="twitter:description" content="a b and c can’t."> |
| 619 |
<meta name="twitter:site" content="@foo"> |
| 620 |
`, |
| 621 |
) |
| 622 |
|
| 623 |
// Description from content excerpt |
| 624 |
b.AssertFileContent( |
| 625 |
"public/s1/p2/index.html", |
| 626 |
`<meta name="twitter:card" content="summary">`, |
| 627 |
`<meta name="twitter:description" content="d e and f can’t.">`, |
| 628 |
) |
| 629 |
|
| 630 |
// Description from front matter summary |
| 631 |
b.AssertFileContent( |
| 632 |
"public/s1/p3/index.html", |
| 633 |
`<meta name="twitter:description" content="g h and i can’t.">`, |
| 634 |
) |
| 635 |
|
| 636 |
// The markdown is intentionally not rendered to HTML. |
| 637 |
b.AssertFileContent( |
| 638 |
"public/s1/p4/index.html", |
| 639 |
`<meta name="twitter:description" content="j k and **l** can't.">`, |
| 640 |
) |
| 641 |
|
| 642 |
// The markdown is intentionally not rendered to HTML. |
| 643 |
b.AssertFileContent( |
| 644 |
"public/s1/p5/index.html", |
| 645 |
`<meta name="twitter:description" content="m n and **o** can't.">`, |
| 646 |
) |
| 647 |
|
| 648 |
// No title: no meta |
| 649 |
b.AssertFileContent("public/s1/p6/index.html", `! twitter:title`) |
| 650 |
|
| 651 |
// site.Title fallback when page title is empty |
| 652 |
f := strings.ReplaceAll(files, "capitalizeListTitles = false", "title = 'My Site'\ncapitalizeListTitles = false") |
| 653 |
b = hugolib.Test(t, f) |
| 654 |
b.AssertFileContent("public/s1/p6/index.html", `<meta name="twitter:title" content="My Site">`) |
| 655 |
|
| 656 |
// site.Params.title fallback when page title and site title are empty |
| 657 |
f = strings.ReplaceAll(files, "[params]\n", "[params]\ntitle = 'My Params Site'\n") |
| 658 |
b = hugolib.Test(t, f) |
| 659 |
b.AssertFileContent("public/s1/p6/index.html", `<meta name="twitter:title" content="My Params Site">`) |
| 660 |
|
| 661 |
// No description: no meta |
| 662 |
f = strings.ReplaceAll(files, `description = "m <em>n</em> and **o** can't."`, "") |
| 663 |
b = hugolib.Test(t, f) |
| 664 |
b.AssertFileContent("public/s1/p6/index.html", `! twitter:description`) |
| 665 |
|
| 666 |
// @-prefixed twitter handle: no double @ |
| 667 |
f = strings.ReplaceAll(files, "twitter = 'foo'", "twitter = '@foo'") |
| 668 |
b = hugolib.Test(t, f) |
| 669 |
b.AssertFileContent("public/s1/p1/index.html", `<meta name="twitter:site" content="@foo">`) |
| 670 |
|
| 671 |
// site.Params.social not a map: no twitter:site |
| 672 |
f = strings.ReplaceAll(files, "[params.social]\ntwitter = 'foo'", "social = 'foo'") |
| 673 |
b = hugolib.Test(t, f) |
| 674 |
b.AssertFileContent("public/s1/p1/index.html", `! twitter:site`) |
| 675 |
|
| 676 |
// twitter handle empty: no twitter:site |
| 677 |
f = strings.ReplaceAll(files, "twitter = 'foo'", "twitter = ''") |
| 678 |
b = hugolib.Test(t, f) |
| 679 |
b.AssertFileContent("public/s1/p1/index.html", `! twitter:site`) |
| 680 |
} |
| 681 |
|
| 682 |
func TestGetPageImagesTemplate(t *testing.T) { |
| 683 |
t.Parallel() |
| 684 |
|
| 685 |
files := ` |
| 686 |
-- hugo.toml -- |
| 687 |
baseURL = "https://example.org" |
| 688 |
disableKinds = ['home','rss','section','sitemap','taxonomy','term'] |
| 689 |
[params] |
| 690 |
images = ["a.jpg", "b.jpg"] |
| 691 |
-- assets/c.jpg -- |
| 692 |
Yy5qcGc= |
| 693 |
-- layouts/single.html -- |
| 694 |
{{ .Title}} |
| 695 |
{{ $images := partial "_funcs/get-page-images.html" . }} |
| 696 |
{{ range $images }} |
| 697 |
{{ .Permalink }} |
| 698 |
{{ end }} |
| 699 |
-- content/p1/index.md -- |
| 700 |
--- |
| 701 |
title: p1 |
| 702 |
--- |
| 703 |
-- content/p1/d-featured.jpg -- |
| 704 |
ZC1mZWF0dXJlZC5qcGc= |
| 705 |
-- content/p2/index.md -- |
| 706 |
--- |
| 707 |
title: p2 |
| 708 |
--- |
| 709 |
-- content/p2/e-cover.jpg -- |
| 710 |
ZS1jb3Zlci5qcGc= |
| 711 |
-- content/p3/index.md -- |
| 712 |
--- |
| 713 |
title: p3 |
| 714 |
--- |
| 715 |
-- content/p3/f-thumbnail.jpg -- |
| 716 |
Zi10aHVtYm5haWwuanBn |
| 717 |
-- content/p4.md -- |
| 718 |
--- |
| 719 |
title: p4 |
| 720 |
images: ["g.jpg"] |
| 721 |
--- |
| 722 |
-- content/p5.md -- |
| 723 |
--- |
| 724 |
title: p5 |
| 725 |
images: ["https://example.com/h.png"] |
| 726 |
--- |
| 727 |
-- content/p6/index.md -- |
| 728 |
--- |
| 729 |
title: p6 |
| 730 |
images: ["i.jpg"] |
| 731 |
--- |
| 732 |
-- content/p6/i.jpg -- |
| 733 |
aS5qcGc= |
| 734 |
-- content/p7.md -- |
| 735 |
--- |
| 736 |
title: p7 |
| 737 |
images: ["c.jpg"] |
| 738 |
--- |
| 739 |
-- content/p8.md -- |
| 740 |
--- |
| 741 |
title: p8 |
| 742 |
--- |
| 743 |
` |
| 744 |
|
| 745 |
b := hugolib.Test(t, files) |
| 746 |
|
| 747 |
// Featured image resource (*feature* glob match) |
| 748 |
b.AssertFileContent("public/p1/index.html", `https://example.org/p1/d-featured.jpg`) |
| 749 |
b.AssertFileContent("public/p1/d-featured.jpg", "d-featured.jpg") |
| 750 |
|
| 751 |
// Cover image resource (*cover* glob match) |
| 752 |
b.AssertFileContent("public/p2/index.html", `https://example.org/p2/e-cover.jpg`) |
| 753 |
b.AssertFileContent("public/p2/e-cover.jpg", "e-cover.jpg") |
| 754 |
|
| 755 |
// Thumbnail image resource (*thumbnail* glob match) |
| 756 |
b.AssertFileContent("public/p3/index.html", `https://example.org/p3/f-thumbnail.jpg`) |
| 757 |
b.AssertFileContent("public/p3/f-thumbnail.jpg", "f-thumbnail.jpg") |
| 758 |
|
| 759 |
// Explicit images param: non-resource relative path (absURL fallback) |
| 760 |
b.AssertFileContent("public/p4/index.html", `https://example.org/g.jpg`) |
| 761 |
|
| 762 |
// Explicit images param: external URL |
| 763 |
b.AssertFileContent("public/p5/index.html", `https://example.com/h.png`) |
| 764 |
|
| 765 |
// Explicit images param: page resource |
| 766 |
b.AssertFileContent("public/p6/index.html", `https://example.org/p6/i.jpg`) |
| 767 |
b.AssertFileContent("public/p6/i.jpg", "i.jpg") |
| 768 |
|
| 769 |
// Explicit images param: global resource |
| 770 |
b.AssertFileContent("public/p7/index.html", `https://example.org/c.jpg`) |
| 771 |
b.AssertFileContent("public/c.jpg", "c.jpg") |
| 772 |
|
| 773 |
// Site params fallback: only the first site image is used |
| 774 |
b.AssertFileContent("public/p8/index.html", `https://example.org/a.jpg`, `! b.jpg`) |
| 775 |
|
| 776 |
// Site params fallback with no site images: no output |
| 777 |
f := strings.ReplaceAll(files, `images = ["a.jpg", "b.jpg"]`, `images = []`) |
| 778 |
b = hugolib.Test(t, f) |
| 779 |
b.AssertFileContent("public/p8/index.html", `! https://`) |
| 780 |
} |