| 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 requiredF 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 |
"fmt" |
| 18 |
"strings" |
| 19 |
"testing" |
| 20 |
|
| 21 |
qt "github.com/frankban/quicktest" |
| 22 |
) |
| 23 |
|
| 24 |
func TestRenderHooksRSS(t *testing.T) { |
| 25 |
files := ` |
| 26 |
-- hugo.toml -- |
| 27 |
baseURL = "https://example.org" |
| 28 |
disableKinds = ["taxonomy", "term"] |
| 29 |
-- layouts/home.html -- |
| 30 |
{{ $p := site.GetPage "p1.md" }} |
| 31 |
{{ $p2 := site.GetPage "p2.md" }} |
| 32 |
P1: {{ $p.Content }} |
| 33 |
P2: {{ $p2.Content }} |
| 34 |
-- layouts/index.xml -- |
| 35 |
{{ $p2 := site.GetPage "p2.md" }} |
| 36 |
{{ $p3 := site.GetPage "p3.md" }} |
| 37 |
P2: {{ $p2.Content }} |
| 38 |
P3: {{ $p3.Content }} |
| 39 |
-- layouts/_markup/render-link.html -- |
| 40 |
html-link: {{ .Destination | safeURL }}| |
| 41 |
-- layouts/_markup/render-link.rss.xml -- |
| 42 |
xml-link: {{ .Destination | safeURL }}| |
| 43 |
-- layouts/_markup/render-heading.html -- |
| 44 |
html-heading: {{ .Text }}| |
| 45 |
-- layouts/_markup/render-heading.rss.xml -- |
| 46 |
xml-heading: {{ .Text }}| |
| 47 |
-- content/p1.md -- |
| 48 |
--- |
| 49 |
title: "p1" |
| 50 |
--- |
| 51 |
P1. [I'm an inline-style link](https://www.gohugo.io) |
| 52 |
|
| 53 |
# Heading in p1 |
| 54 |
|
| 55 |
-- content/p2.md -- |
| 56 |
--- |
| 57 |
title: "p2" |
| 58 |
--- |
| 59 |
P2. [I'm an inline-style link](https://www.bep.is) |
| 60 |
|
| 61 |
# Heading in p2 |
| 62 |
|
| 63 |
-- content/p3.md -- |
| 64 |
--- |
| 65 |
title: "p3" |
| 66 |
outputs: ["rss"] |
| 67 |
--- |
| 68 |
P3. [I'm an inline-style link](https://www.example.org) |
| 69 |
` |
| 70 |
b := Test(t, files) |
| 71 |
|
| 72 |
b.AssertFileContent("public/index.html", ` |
| 73 |
P1: <p>P1. html-link: https://www.gohugo.io|</p> |
| 74 |
html-heading: Heading in p1| |
| 75 |
html-heading: Heading in p2| |
| 76 |
`) |
| 77 |
b.AssertFileContent("public/index.xml", ` |
| 78 |
P2: <p>P2. xml-link: https://www.bep.is|</p> |
| 79 |
P3: <p>P3. xml-link: https://www.example.org|</p> |
| 80 |
xml-heading: Heading in p2| |
| 81 |
`) |
| 82 |
} |
| 83 |
|
| 84 |
// Issue 13242. |
| 85 |
func TestRenderHooksRSSOnly(t *testing.T) { |
| 86 |
files := ` |
| 87 |
-- hugo.toml -- |
| 88 |
baseURL = "https://example.org" |
| 89 |
disableKinds = ["taxonomy", "term"] |
| 90 |
-- layouts/home.html -- |
| 91 |
{{ $p := site.GetPage "p1.md" }} |
| 92 |
{{ $p2 := site.GetPage "p2.md" }} |
| 93 |
P1: {{ $p.Content }} |
| 94 |
P2: {{ $p2.Content }} |
| 95 |
-- layouts/index.xml -- |
| 96 |
{{ $p2 := site.GetPage "p2.md" }} |
| 97 |
{{ $p3 := site.GetPage "p3.md" }} |
| 98 |
P2: {{ $p2.Content }} |
| 99 |
P3: {{ $p3.Content }} |
| 100 |
-- layouts/_markup/render-link.rss.xml -- |
| 101 |
xml-link: {{ .Destination | safeURL }}| |
| 102 |
-- layouts/_markup/render-heading.rss.xml -- |
| 103 |
xml-heading: {{ .Text }}| |
| 104 |
-- content/p1.md -- |
| 105 |
--- |
| 106 |
title: "p1" |
| 107 |
--- |
| 108 |
P1. [I'm an inline-style link](https://www.gohugo.io) |
| 109 |
|
| 110 |
# Heading in p1 |
| 111 |
|
| 112 |
-- content/p2.md -- |
| 113 |
--- |
| 114 |
title: "p2" |
| 115 |
--- |
| 116 |
P2. [I'm an inline-style link](https://www.bep.is) |
| 117 |
|
| 118 |
# Heading in p2 |
| 119 |
|
| 120 |
-- content/p3.md -- |
| 121 |
--- |
| 122 |
title: "p3" |
| 123 |
outputs: ["rss"] |
| 124 |
--- |
| 125 |
P3. [I'm an inline-style link](https://www.example.org) |
| 126 |
` |
| 127 |
b := Test(t, files) |
| 128 |
|
| 129 |
b.AssertFileContent("public/index.html", ` |
| 130 |
P1: <p>P1. <a href="https://www.gohugo.io">I’m an inline-style link</a></p> |
| 131 |
<h1 id="heading-in-p1">Heading in p1</h1> |
| 132 |
<h1 id="heading-in-p2">Heading in p2</h1> |
| 133 |
`) |
| 134 |
|
| 135 |
b.AssertFileContent("public/index.xml", ` |
| 136 |
P2: <p>P2. xml-link: https://www.bep.is|</p> |
| 137 |
P3: <p>P3. xml-link: https://www.example.org|</p> |
| 138 |
xml-heading: Heading in p2| |
| 139 |
`) |
| 140 |
} |
| 141 |
|
| 142 |
// https://github.com/gohugoio/hugo/issues/6629 |
| 143 |
func TestRenderLinkWithMarkupInText(t *testing.T) { |
| 144 |
t.Parallel() |
| 145 |
|
| 146 |
files := ` |
| 147 |
-- hugo.toml -- |
| 148 |
baseURL="https://example.org" |
| 149 |
[markup] |
| 150 |
[markup.goldmark] |
| 151 |
[markup.goldmark.renderer] |
| 152 |
unsafe = true |
| 153 |
-- layouts/home.html -- |
| 154 |
{{ $p := site.GetPage "p1.md" }} |
| 155 |
P1: {{ $p.Content }} |
| 156 |
-- layouts/_markup/render-link.html -- |
| 157 |
html-link: {{ .Destination | safeURL }}|Text: {{ .Text }}|Plain: {{ .PlainText | safeHTML }} |
| 158 |
-- layouts/_markup/render-image.html -- |
| 159 |
html-image: {{ .Destination | safeURL }}|Text: {{ .Text }}|Plain: {{ .PlainText | safeHTML }} |
| 160 |
-- content/p1.md -- |
| 161 |
--- |
| 162 |
title: "p1" |
| 163 |
--- |
| 164 |
|
| 165 |
START: [**should be bold**](https://gohugo.io)END |
| 166 |
|
| 167 |
Some regular **markup**. |
| 168 |
|
| 169 |
Image: |
| 170 |
|
| 171 |
END |
| 172 |
` |
| 173 |
|
| 174 |
b := Test(t, files) |
| 175 |
|
| 176 |
b.AssertFileContent("public/index.html", ` |
| 177 |
P1: <p>START: html-link: https://gohugo.io|Text: <strong>should be bold</strong>|Plain: should be boldEND</p> |
| 178 |
<p>Some regular <strong>markup</strong>.</p> |
| 179 |
<p>html-image: image.jpg|Text: Hello<br> Goodbye|Plain: Hello GoodbyeEND</p> |
| 180 |
`) |
| 181 |
} |
| 182 |
|
| 183 |
func TestRenderHookContentFragmentsOnSelf(t *testing.T) { |
| 184 |
files := ` |
| 185 |
-- hugo.toml -- |
| 186 |
baseURL = "https://example.org" |
| 187 |
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"] |
| 188 |
-- content/p1.md -- |
| 189 |
--- |
| 190 |
title: "p1" |
| 191 |
--- |
| 192 |
|
| 193 |
## A {#z} |
| 194 |
## B |
| 195 |
## C |
| 196 |
|
| 197 |
-- content/p2.md -- |
| 198 |
--- |
| 199 |
title: "p2" |
| 200 |
--- |
| 201 |
|
| 202 |
## D |
| 203 |
## E |
| 204 |
## F |
| 205 |
|
| 206 |
-- layouts/_markup/render-heading.html -- |
| 207 |
Heading: {{ .Text }}| |
| 208 |
{{ with .Page }} |
| 209 |
Self Fragments: {{ .Fragments.Identifiers }}| |
| 210 |
{{ end }} |
| 211 |
{{ with (site.GetPage "p1.md") }} |
| 212 |
P1 Fragments: {{ .Fragments.Identifiers }}| |
| 213 |
{{ end }} |
| 214 |
-- layouts/single.html -- |
| 215 |
{{ .Content}} |
| 216 |
` |
| 217 |
|
| 218 |
b := Test(t, files) |
| 219 |
|
| 220 |
b.AssertFileContent("public/p1/index.html", ` |
| 221 |
Self Fragments: [b c z] |
| 222 |
P1 Fragments: [b c z] |
| 223 |
`) |
| 224 |
b.AssertFileContent("public/p2/index.html", ` |
| 225 |
Self Fragments: [d e f] |
| 226 |
P1 Fragments: [b c z] |
| 227 |
`) |
| 228 |
} |
| 229 |
|
| 230 |
func TestDefaultRenderHooksMultilingual(t *testing.T) { |
| 231 |
files := ` |
| 232 |
-- hugo.toml -- |
| 233 |
baseURL = "https://example.org" |
| 234 |
disableKinds = ["taxonomy", "term", "RSS", "sitemap", "robotsTXT"] |
| 235 |
defaultContentLanguage = "nn" |
| 236 |
defaultContentLanguageInSubdir = true |
| 237 |
[markup] |
| 238 |
[markup.goldmark] |
| 239 |
duplicateResourceFiles = false |
| 240 |
[markup.goldmark.renderhooks] |
| 241 |
[markup.goldmark.renderhooks.link] |
| 242 |
#useEmbedded = 'never' |
| 243 |
[markup.goldmark.renderhooks.image] |
| 244 |
#useEmbedded = 'never' |
| 245 |
[languages] |
| 246 |
[languages.en] |
| 247 |
weight = 1 |
| 248 |
[languages.nn] |
| 249 |
weight = 2 |
| 250 |
-- content/p1/index.md -- |
| 251 |
--- |
| 252 |
title: "p1" |
| 253 |
--- |
| 254 |
[P2](p2) |
| 255 |
 |
| 256 |
-- content/p2/index.md -- |
| 257 |
--- |
| 258 |
title: "p2" |
| 259 |
--- |
| 260 |
[P1](p1) |
| 261 |
 |
| 262 |
-- content/p1/index.en.md -- |
| 263 |
--- |
| 264 |
title: "p1 en" |
| 265 |
--- |
| 266 |
[P2](p2) |
| 267 |
 |
| 268 |
-- content/p2/index.en.md -- |
| 269 |
--- |
| 270 |
title: "p2 en" |
| 271 |
--- |
| 272 |
[P1](p1) |
| 273 |
 |
| 274 |
|
| 275 |
-- content/p1/pixel.nn.png -- |
| 276 |
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg== |
| 277 |
-- content/p2/pixel.png -- |
| 278 |
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg== |
| 279 |
-- layouts/single.html -- |
| 280 |
{{ .Title }}|{{ .Content }}|$ |
| 281 |
|
| 282 |
` |
| 283 |
|
| 284 |
t.Run("Default multilingual", func(t *testing.T) { |
| 285 |
b := Test(t, files) |
| 286 |
|
| 287 |
b.AssertFileContent("public/nn/p1/index.html", |
| 288 |
"p1|<p><a href=\"/nn/p2/\">P2</a\n></p>", "<img src=\"/nn/p1/pixel.nn.png\" alt=\"Pixel\">") |
| 289 |
b.AssertFileContent("public/en/p1/index.html", |
| 290 |
"p1 en|<p><a href=\"/en/p2/\">P2</a\n></p>", "<img src=\"/nn/p1/pixel.nn.png\" alt=\"Pixel\">") |
| 291 |
}) |
| 292 |
|
| 293 |
t.Run("Disabled", func(t *testing.T) { |
| 294 |
b := Test(t, strings.ReplaceAll(files, "#useEmbedded = 'never'", "useEmbedded = 'never'")) |
| 295 |
|
| 296 |
b.AssertFileContent("public/nn/p1/index.html", |
| 297 |
"p1|<p><a href=\"p2\">P2</a>", "<img src=\"pixel.png\" alt=\"Pixel\">") |
| 298 |
}) |
| 299 |
} |
| 300 |
|
| 301 |
func TestRenderHooksDefaultEscape(t *testing.T) { |
| 302 |
files := ` |
| 303 |
-- hugo.toml -- |
| 304 |
[markup.goldmark.extensions.typographer] |
| 305 |
disable = true |
| 306 |
[markup.goldmark.renderHooks.image] |
| 307 |
useEmbedded = 'WHEN' |
| 308 |
[markup.goldmark.renderHooks.link] |
| 309 |
useEmbedded = 'WHEN' |
| 310 |
[markup.goldmark.parser] |
| 311 |
wrapStandAloneImageWithinParagraph = false |
| 312 |
[markup.goldmark.parser.attribute] |
| 313 |
block = true |
| 314 |
title = true |
| 315 |
|
| 316 |
-- content/_index.md -- |
| 317 |
--- |
| 318 |
title: "Home" |
| 319 |
--- |
| 320 |
Link: [text-"<>&](/destination-"<> 'title-"<>&') |
| 321 |
|
| 322 |
Image:  |
| 323 |
{class="><script>alert()</script>" id="baz"} |
| 324 |
|
| 325 |
-- layouts/home.html -- |
| 326 |
{{ .Content }} |
| 327 |
` |
| 328 |
|
| 329 |
for _, when := range []string{"auto", "never", "always", "fallback"} { |
| 330 |
t.Run(fmt.Sprint(when), func(t *testing.T) { |
| 331 |
t.Parallel() |
| 332 |
b := Test(t, strings.ReplaceAll(files, "WHEN", fmt.Sprint(when))) |
| 333 |
|
| 334 |
// The escaping is slightly different between the two. |
| 335 |
if when == "always" || when == "fallback" { |
| 336 |
b.AssertFileContent("public/index.html", |
| 337 |
"Link: <a href=\"/destination-%22%3C%3E\" title=\"title-"<>&\">text-"<>&</a>", |
| 338 |
"img src=\"/destination-%22%3C%3E\" alt=\"alt-"<>&\" title=\"title-"<>&\">", |
| 339 |
"><script>", |
| 340 |
) |
| 341 |
} else { |
| 342 |
b.AssertFileContent("public/index.html", |
| 343 |
"Link: <a href=\"/destination-%22%3C%3E\" title=\"title-"<>&\">text-"<>&</a>", |
| 344 |
"Image: <img src=\"/destination-%22%3C%3E\" alt=\"alt-"<>&\" title=\"title-"<>&\">", |
| 345 |
) |
| 346 |
} |
| 347 |
}) |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
// Issue 13410. |
| 352 |
func TestRenderHooksMultilineTitlePlainText(t *testing.T) { |
| 353 |
t.Parallel() |
| 354 |
|
| 355 |
files := ` |
| 356 |
-- hugo.toml -- |
| 357 |
-- content/p1.md -- |
| 358 |
--- |
| 359 |
title: "p1" |
| 360 |
--- |
| 361 |
|
| 362 |
First line. |
| 363 |
Second line. |
| 364 |
---------------- |
| 365 |
-- layouts/_markup/render-heading.html -- |
| 366 |
Plain text: {{ .PlainText }}|Text: {{ .Text }}| |
| 367 |
-- layouts/single.html -- |
| 368 |
Content: {{ .Content}}| |
| 369 |
} |
| 370 |
` |
| 371 |
b := Test(t, files) |
| 372 |
|
| 373 |
b.AssertFileContent("public/p1/index.html", |
| 374 |
"Content: Plain text: First line.\nSecond line.|", |
| 375 |
"|Text: First line.\nSecond line.||\n", |
| 376 |
) |
| 377 |
} |
| 378 |
|
| 379 |
func TestContentOutputReuseRenderHooksAndShortcodesHTMLOnly(t *testing.T) { |
| 380 |
files := ` |
| 381 |
-- hugo.toml -- |
| 382 |
-- layouts/home.html -- |
| 383 |
HTML: {{ .Title }}|{{ .Content }}| |
| 384 |
-- layouts/index.xml -- |
| 385 |
XML: {{ .Title }}|{{ .Content }}| |
| 386 |
-- layouts/_markup/render-heading.html -- |
| 387 |
Render heading. |
| 388 |
-- layouts/_shortcodes/myshortcode.html -- |
| 389 |
My shortcode. |
| 390 |
-- content/_index.md -- |
| 391 |
--- |
| 392 |
title: "Home" |
| 393 |
--- |
| 394 |
## Heading |
| 395 |
|
| 396 |
{{< myshortcode >}} |
| 397 |
` |
| 398 |
b := Test(t, files) |
| 399 |
|
| 400 |
s := b.H.Sites[0] |
| 401 |
b.Assert(s.home.pageOutputTemplateVariationsState.Load(), qt.Equals, uint32(1)) |
| 402 |
b.AssertFileContent("public/index.html", "HTML: Home|Render heading.\nMy shortcode.\n|") |
| 403 |
b.AssertFileContent("public/index.xml", "XML: Home|Render heading.\nMy shortcode.\n|") |
| 404 |
} |
| 405 |
|
| 406 |
func TestContentOutputNoReuseRenderHooksInBothHTMLAnXML(t *testing.T) { |
| 407 |
files := ` |
| 408 |
-- hugo.toml -- |
| 409 |
disableKinds = ["taxonomy", "term"] |
| 410 |
-- layouts/home.html -- |
| 411 |
HTML: {{ .Title }}|{{ .Content }}| |
| 412 |
-- layouts/index.xml -- |
| 413 |
XML: {{ .Title }}|{{ .Content }}| |
| 414 |
-- layouts/_markup/render-heading.html -- |
| 415 |
Render heading. |
| 416 |
-- layouts/_markup/render-heading.xml -- |
| 417 |
Render heading XML. |
| 418 |
-- content/_index.md -- |
| 419 |
--- |
| 420 |
title: "Home" |
| 421 |
--- |
| 422 |
## Heading |
| 423 |
|
| 424 |
|
| 425 |
` |
| 426 |
b := Test(t, files) |
| 427 |
|
| 428 |
s := b.H.Sites[0] |
| 429 |
b.Assert(s.home.pageOutputTemplateVariationsState.Load() > 1, qt.IsTrue) |
| 430 |
b.AssertFileContentExact("public/index.xml", "XML: Home|Render heading XML.|") |
| 431 |
b.AssertFileContentExact("public/index.html", "HTML: Home|Render heading.|") |
| 432 |
} |
| 433 |
|
| 434 |
func TestContentOutputNoReuseShortcodesInBothHTMLAnXML(t *testing.T) { |
| 435 |
files := ` |
| 436 |
-- hugo.toml -- |
| 437 |
disableKinds = ["taxonomy", "term"] |
| 438 |
-- layouts/home.html -- |
| 439 |
HTML: {{ .Title }}|{{ .Content }}| |
| 440 |
-- layouts/index.xml -- |
| 441 |
XML: {{ .Title }}|{{ .Content }}| |
| 442 |
-- layouts/_markup/render-heading.html -- |
| 443 |
Render heading. |
| 444 |
|
| 445 |
-- layouts/_shortcodes/myshortcode.html -- |
| 446 |
My shortcode HTML. |
| 447 |
-- layouts/_shortcodes/myshortcode.xml -- |
| 448 |
My shortcode XML. |
| 449 |
-- content/_index.md -- |
| 450 |
--- |
| 451 |
title: "Home" |
| 452 |
--- |
| 453 |
## Heading |
| 454 |
|
| 455 |
{{< myshortcode >}} |
| 456 |
|
| 457 |
|
| 458 |
` |
| 459 |
b := Test(t, files) |
| 460 |
|
| 461 |
b.AssertFileContentExact("public/index.xml", "My shortcode XML.") |
| 462 |
b.AssertFileContentExact("public/index.html", "My shortcode HTML.") |
| 463 |
s := b.H.Sites[0] |
| 464 |
b.Assert(s.home.pageOutputTemplateVariationsState.Load() > 1, qt.IsTrue) |
| 465 |
} |