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