| 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 tplimpl_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"strings" |
| 18 |
"testing" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/hugolib" |
| 21 |
) |
| 22 |
|
| 23 |
func TestEmbeddedLinkRenderHook(t *testing.T) { |
| 24 |
t.Parallel() |
| 25 |
|
| 26 |
files := ` |
| 27 |
-- hugo.toml -- |
| 28 |
disableKinds = ['rss','sitemap','taxonomy','term'] |
| 29 |
[markup.goldmark.renderHooks.link] |
| 30 |
useEmbedded = 'always' |
| 31 |
-- layouts/list.html -- |
| 32 |
{{ .Content }} |
| 33 |
-- layouts/single.html -- |
| 34 |
{{ .Content }} |
| 35 |
-- assets/a.txt -- |
| 36 |
irrelevant |
| 37 |
-- content/_index.md -- |
| 38 |
--- |
| 39 |
title: home |
| 40 |
--- |
| 41 |
-- content/s1/_index.md -- |
| 42 |
--- |
| 43 |
title: s1 |
| 44 |
--- |
| 45 |
-- content/s1/p1.md -- |
| 46 |
--- |
| 47 |
title: s1/p1 |
| 48 |
--- |
| 49 |
-- content/s1/p2/index.md -- |
| 50 |
--- |
| 51 |
title: s1/p2 |
| 52 |
--- |
| 53 |
[500](a.txt) // global resource |
| 54 |
[510](b.txt) // page resource |
| 55 |
[520](./b.txt) // page resource |
| 56 |
-- content/s1/p2/b.txt -- |
| 57 |
irrelevant |
| 58 |
-- content/s1/p3.md -- |
| 59 |
--- |
| 60 |
title: s1/p3 |
| 61 |
--- |
| 62 |
// Remote |
| 63 |
[10](https://a.org) |
| 64 |
|
| 65 |
// fragment |
| 66 |
[100](/#foo) |
| 67 |
[110](#foo) |
| 68 |
[120](p1#foo) |
| 69 |
[130](p1/#foo) |
| 70 |
|
| 71 |
// section page |
| 72 |
[200](s1) |
| 73 |
[210](/s1) |
| 74 |
[220](../s1) |
| 75 |
[230](s1/) |
| 76 |
[240](/s1/) |
| 77 |
[250](../s1/) |
| 78 |
|
| 79 |
// regular page |
| 80 |
[300](p1) |
| 81 |
[310](/s1/p1) |
| 82 |
[320](../s1/p1) |
| 83 |
[330](p1/) |
| 84 |
[340](/s1/p1/) |
| 85 |
[350](../s1/p1/) |
| 86 |
|
| 87 |
// leaf bundle |
| 88 |
[400](p2) |
| 89 |
[410](/s1/p2) |
| 90 |
[420](../s1/p2) |
| 91 |
[430](p2/) |
| 92 |
[440](/s1/p2/) |
| 93 |
[450](../s1/p2/) |
| 94 |
|
| 95 |
// empty |
| 96 |
[]() |
| 97 |
` |
| 98 |
|
| 99 |
b := hugolib.Test(t, files) |
| 100 |
|
| 101 |
b.AssertFileContent("public/s1/p3/index.html", |
| 102 |
`<a href="https://a.org">10</a>`, |
| 103 |
`<a href="/#foo">100</a>`, |
| 104 |
`<a href="/s1/p3/#foo">110</a>`, |
| 105 |
`<a href="/s1/p1/#foo">120</a>`, |
| 106 |
`<a href="/s1/p1/#foo">130</a>`, |
| 107 |
|
| 108 |
`<a href="/s1/">200</a>`, |
| 109 |
`<a href="/s1/">210</a>`, |
| 110 |
`<a href="/s1/">220</a>`, |
| 111 |
`<a href="/s1/">230</a>`, |
| 112 |
`<a href="/s1/">240</a>`, |
| 113 |
`<a href="/s1/">250</a>`, |
| 114 |
|
| 115 |
`<a href="/s1/p1/">300</a>`, |
| 116 |
`<a href="/s1/p1/">310</a>`, |
| 117 |
`<a href="/s1/p1/">320</a>`, |
| 118 |
`<a href="/s1/p1/">330</a>`, |
| 119 |
`<a href="/s1/p1/">340</a>`, |
| 120 |
`<a href="/s1/p1/">350</a>`, |
| 121 |
|
| 122 |
`<a href="/s1/p2/">400</a>`, |
| 123 |
`<a href="/s1/p2/">410</a>`, |
| 124 |
`<a href="/s1/p2/">420</a>`, |
| 125 |
`<a href="/s1/p2/">430</a>`, |
| 126 |
`<a href="/s1/p2/">440</a>`, |
| 127 |
`<a href="/s1/p2/">450</a>`, |
| 128 |
|
| 129 |
`<a href=""></a>`, |
| 130 |
) |
| 131 |
|
| 132 |
b.AssertFileContent("public/s1/p2/index.html", |
| 133 |
`<a href="/a.txt">500</a>`, |
| 134 |
`<a href="/s1/p2/b.txt">510</a>`, |
| 135 |
`<a href="/s1/p2/b.txt">520</a>`, |
| 136 |
) |
| 137 |
} |
| 138 |
|
| 139 |
// Issue 12203 |
| 140 |
// Issue 12468 |
| 141 |
// Issue 12514 |
| 142 |
func TestEmbeddedImageRenderHook(t *testing.T) { |
| 143 |
t.Parallel() |
| 144 |
|
| 145 |
files := ` |
| 146 |
-- hugo.toml -- |
| 147 |
baseURL = 'https://example.org/dir/' |
| 148 |
disableKinds = ['home','rss','section','sitemap','taxonomy','term'] |
| 149 |
[markup.goldmark.extensions.typographer] |
| 150 |
disable = true |
| 151 |
[markup.goldmark.parser] |
| 152 |
wrapStandAloneImageWithinParagraph = false |
| 153 |
[markup.goldmark.parser.attribute] |
| 154 |
block = false |
| 155 |
[markup.goldmark.renderHooks.image] |
| 156 |
useEmbedded = 'always' |
| 157 |
-- content/p1/index.md -- |
| 158 |
![]() |
| 159 |
|
| 160 |
 |
| 161 |
|
| 162 |
 |
| 163 |
|
| 164 |
 |
| 165 |
{.foo #bar} |
| 166 |
|
| 167 |
 |
| 168 |
{id="\"><script>alert()</script>"} |
| 169 |
-- content/p1/pixel.png -- |
| 170 |
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg== |
| 171 |
-- layouts/single.html -- |
| 172 |
{{ .Content }} |
| 173 |
` |
| 174 |
|
| 175 |
b := hugolib.Test(t, files) |
| 176 |
b.AssertFileContent("public/p1/index.html", |
| 177 |
`<img src="" alt="">`, |
| 178 |
`<img src="/dir/p1/pixel.png" alt="alt1">`, |
| 179 |
`<img src="/dir/p1/pixel.png" alt="alt2-&<>'" title="&<>'">`, |
| 180 |
`<img src="/dir/p1/pixel.png?a=b&c=d#fragment" alt="alt3">`, |
| 181 |
`<img src="/dir/p1/pixel.png" alt="alt4">`, |
| 182 |
) |
| 183 |
|
| 184 |
files = strings.Replace(files, "block = false", "block = true", -1) |
| 185 |
|
| 186 |
b = hugolib.Test(t, files) |
| 187 |
b.AssertFileContent("public/p1/index.html", |
| 188 |
`<img src="" alt="">`, |
| 189 |
`<img src="/dir/p1/pixel.png" alt="alt1">`, |
| 190 |
`<img src="/dir/p1/pixel.png" alt="alt2-&<>'" title="&<>'">`, |
| 191 |
`<img src="/dir/p1/pixel.png?a=b&c=d#fragment" alt="alt3" class="foo" id="bar">`, |
| 192 |
`<img src="/dir/p1/pixel.png" alt="alt4" id=""><script>alert()</script>">`, |
| 193 |
) |
| 194 |
} |
| 195 |
|
| 196 |
// Issue 13535 |
| 197 |
func TestEmbeddedLinkAndImageRenderHookConfig(t *testing.T) { |
| 198 |
t.Parallel() |
| 199 |
|
| 200 |
files := ` |
| 201 |
-- hugo.toml -- |
| 202 |
disableKinds = ['home','rss','section','sitemap','taxonomy','term'] |
| 203 |
|
| 204 |
[markup.goldmark] |
| 205 |
duplicateResourceFiles = false |
| 206 |
|
| 207 |
[markup.goldmark.renderHooks.image] |
| 208 |
#KEY_VALUE |
| 209 |
|
| 210 |
[markup.goldmark.renderHooks.link] |
| 211 |
#KEY_VALUE |
| 212 |
|
| 213 |
#LANGUAGES |
| 214 |
-- content/s1/p1/index.md -- |
| 215 |
--- |
| 216 |
title: p1 |
| 217 |
--- |
| 218 |
[p2](p2) |
| 219 |
|
| 220 |
[a](a.txt) |
| 221 |
|
| 222 |
 |
| 223 |
-- content/s1/p1/a.txt -- |
| 224 |
-- content/s1/p1/b.jpg -- |
| 225 |
-- content/s1/p2.md -- |
| 226 |
--- |
| 227 |
title: p2 |
| 228 |
--- |
| 229 |
-- layouts/all.html -- |
| 230 |
{{ .Content }} |
| 231 |
` |
| 232 |
|
| 233 |
const customHooks string = ` |
| 234 |
-- layouts/s1/_markup/render-link.html -- |
| 235 |
custom link render hook: {{ .Text }}|{{ .Destination }} |
| 236 |
-- layouts/s1/_markup/render-image.html -- |
| 237 |
custom image render hook: {{ .Text }}|{{ .Destination }} |
| 238 |
` |
| 239 |
|
| 240 |
const languages string = ` |
| 241 |
[languages.en] |
| 242 |
[languages.fr] |
| 243 |
` |
| 244 |
|
| 245 |
const ( |
| 246 |
fileToCheck = "public/s1/p1/index.html" |
| 247 |
wantCustom string = "<p>custom link render hook: p2|p2</p>\n<p>custom link render hook: a|a.txt</p>\n<p>custom image render hook: b|b.jpg</p>" |
| 248 |
wantEmbedded string = "<p><a href=\"/s1/p2/\">p2</a></p>\n<p><a href=\"/s1/p1/a.txt\">a</a></p>\n<p><img src=\"/s1/p1/b.jpg\" alt=\"b\"></p>" |
| 249 |
wantGoldmark string = "<p><a href=\"p2\">p2</a></p>\n<p><a href=\"a.txt\">a</a></p>\n<p><img src=\"b.jpg\" alt=\"b\"></p>" |
| 250 |
) |
| 251 |
|
| 252 |
tests := []struct { |
| 253 |
id string // the test id |
| 254 |
isMultilingual bool // whether the site is multilingual single-host |
| 255 |
hasCustomHooks bool // whether the site has custom link and image render hooks |
| 256 |
keyValuePair string // the useEmbedded key-value pair |
| 257 |
want string // the expected content of public/s1/p1/index.html |
| 258 |
}{ |
| 259 |
{"01", false, false, "", wantGoldmark}, // monolingual |
| 260 |
{"02", false, false, "useEmbedded = 'always'", wantEmbedded}, // monolingual, useEmbedded = 'always' |
| 261 |
{"03", false, false, "useEmbedded = 'auto'", wantGoldmark}, // monolingual, useEmbedded = 'auto' |
| 262 |
{"04", false, false, "useEmbedded = 'fallback'", wantEmbedded}, // monolingual, useEmbedded = 'fallback' |
| 263 |
{"05", false, false, "useEmbedded = 'never'", wantGoldmark}, // monolingual, useEmbedded = 'never' |
| 264 |
{"06", false, true, "", wantCustom}, // monolingual, with custom hooks |
| 265 |
{"07", false, true, "useEmbedded = 'always'", wantEmbedded}, // monolingual, with custom hooks, useEmbedded = 'always' |
| 266 |
{"08", false, true, "useEmbedded = 'auto'", wantCustom}, // monolingual, with custom hooks, useEmbedded = 'auto' |
| 267 |
{"09", false, true, "useEmbedded = 'fallback'", wantCustom}, // monolingual, with custom hooks, useEmbedded = 'fallback' |
| 268 |
{"10", false, true, "useEmbedded = 'never'", wantCustom}, // monolingual, with custom hooks, useEmbedded = 'never' |
| 269 |
{"11", true, false, "", wantEmbedded}, // multilingual |
| 270 |
{"12", true, false, "useEmbedded = 'always'", wantEmbedded}, // multilingual, useEmbedded = 'always' |
| 271 |
{"13", true, false, "useEmbedded = 'auto'", wantEmbedded}, // multilingual, useEmbedded = 'auto' |
| 272 |
{"14", true, false, "useEmbedded = 'fallback'", wantEmbedded}, // multilingual, useEmbedded = 'fallback' |
| 273 |
{"15", true, false, "useEmbedded = 'never'", wantGoldmark}, // multilingual, useEmbedded = 'never' |
| 274 |
{"16", true, true, "", wantCustom}, // multilingual, with custom hooks |
| 275 |
{"17", true, true, "useEmbedded = 'always'", wantEmbedded}, // multilingual, with custom hooks, useEmbedded = 'always' |
| 276 |
{"18", true, true, "useEmbedded = 'auto'", wantCustom}, // multilingual, with custom hooks, useEmbedded = 'auto' |
| 277 |
{"19", true, true, "useEmbedded = 'fallback'", wantCustom}, // multilingual, with custom hooks, useEmbedded = 'fallback' |
| 278 |
{"20", true, true, "useEmbedded = 'never'", wantCustom}, // multilingual, with custom hooks, useEmbedded = 'never' |
| 279 |
} |
| 280 |
|
| 281 |
for _, tt := range tests { |
| 282 |
t.Run(tt.id, func(t *testing.T) { |
| 283 |
t.Parallel() |
| 284 |
|
| 285 |
f := files |
| 286 |
if tt.isMultilingual { |
| 287 |
f = strings.ReplaceAll(f, "#LANGUAGES", languages) |
| 288 |
} |
| 289 |
if tt.hasCustomHooks { |
| 290 |
f = f + customHooks |
| 291 |
} |
| 292 |
f = strings.ReplaceAll(f, "#KEY_VALUE", tt.keyValuePair) |
| 293 |
|
| 294 |
b := hugolib.Test(t, f) |
| 295 |
b.AssertFileContent(fileToCheck, tt.want) |
| 296 |
}) |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
func TestRenderHookMultilingual(t *testing.T) { |
| 301 |
t.Parallel() |
| 302 |
|
| 303 |
files := ` |
| 304 |
-- hugo.toml -- |
| 305 |
baseURL = 'https://example.org/' |
| 306 |
defaultContentLanguage = 'en' |
| 307 |
[markup.goldmark.renderHooks.image] |
| 308 |
useEmbedded = 'never' |
| 309 |
[languages.en] |
| 310 |
weight = 1 |
| 311 |
[languages.tr] |
| 312 |
weight = 2 |
| 313 |
-- content/p1.en.md -- |
| 314 |
--- |
| 315 |
title: p1 |
| 316 |
--- |
| 317 |
 |
| 318 |
-- content/p1.tr.md -- |
| 319 |
--- |
| 320 |
title: p1 |
| 321 |
--- |
| 322 |
 |
| 323 |
-- layouts/_markup/render-image.tr.html -- |
| 324 |
TR-IMAGE |
| 325 |
-- layouts/_default/single.html -- |
| 326 |
{{ .Content }} |
| 327 |
` |
| 328 |
|
| 329 |
b := hugolib.Test(t, files) |
| 330 |
|
| 331 |
b.AssertFileContent("public/p1/index.html", "<img src=\"img.jpg\" alt=\"alt\">") |
| 332 |
b.AssertFileContent("public/tr/p1/index.html", "TR-IMAGE") |
| 333 |
} |
| 334 |
|
| 335 |
// Hooks: |
| 336 |
// table |
| 337 |
// passthrough |
| 338 |
// link |
| 339 |
// image |
| 340 |
// heading |
| 341 |
// codeblock |
| 342 |
// blockquote |
| 343 |
func TestRenderHooksPosition(t *testing.T) { |
| 344 |
t.Parallel() |
| 345 |
|
| 346 |
files := ` |
| 347 |
-- hugo.toml -- |
| 348 |
[markup] |
| 349 |
[markup.goldmark] |
| 350 |
[markup.goldmark.extensions] |
| 351 |
[markup.goldmark.extensions.passthrough] |
| 352 |
enable = true |
| 353 |
[markup.goldmark.extensions.passthrough.delimiters] |
| 354 |
block = [['\[', '\]'], ['$$', '$$']] |
| 355 |
inline = [['\(', '\)']] |
| 356 |
-- layouts/_markup/render-table.html -- |
| 357 |
HOOK_CONTENT |
| 358 |
-- layouts/_markup/render-passthrough.html -- |
| 359 |
HOOK_CONTENT |
| 360 |
-- layouts/_markup/render-link.html -- |
| 361 |
HOOK_CONTENT |
| 362 |
-- layouts/_markup/render-image.html -- |
| 363 |
HOOK_CONTENT |
| 364 |
-- layouts/_markup/render-heading.html -- |
| 365 |
HOOK_CONTENT |
| 366 |
-- layouts/_markup/render-codeblock.html -- |
| 367 |
HOOK_CONTENT |
| 368 |
-- layouts/_markup/render-blockquote.html -- |
| 369 |
HOOK_CONTENT |
| 370 |
-- layouts/shortcodes/myheading.html -- |
| 371 |
{{ $s := .Get 0 -}} |
| 372 |
|
| 373 |
|
| 374 |
{{ printf "## %s" $s }} |
| 375 |
-- layouts/shortcodes/mylink.html -- |
| 376 |
{{ $s := .Get 0 -}} |
| 377 |
|
| 378 |
A |
| 379 |
|
| 380 |
B |
| 381 |
|
| 382 |
{{ printf "[%s](%s)" $s $s }} |
| 383 |
|
| 384 |
|
| 385 |
C |
| 386 |
-- layouts/single.html -- |
| 387 |
{{ .Content }} |
| 388 |
-- content/p1.md -- |
| 389 |
--- |
| 390 |
title: "p1" |
| 391 |
--- |
| 392 |
|
| 393 |
[p1](/p1) |
| 394 |
[p2](/p2) |
| 395 |
7 |
| 396 |
8 |
| 397 |
{{% mylink "p3" %}} |
| 398 |
10 |
| 399 |
11 |
| 400 |
12 |
| 401 |
## My Heading 2 |
| 402 |
14 |
| 403 |
### My Heading 3 |
| 404 |
16 |
| 405 |
{{% myheading "h4" %}} |
| 406 |
18 |
| 407 |
## Table 1 |
| 408 |
20 |
| 409 |
| Month | Savings | |
| 410 |
| -------- | ------- | |
| 411 |
| January | $250 | |
| 412 |
| February | $80 | |
| 413 |
| March | $420 | |
| 414 |
|
| 415 |
> blockquote 1 |
| 416 |
|
| 417 |
Foo. |
| 418 |
|
| 419 |
> blockquote 2 |
| 420 |
|
| 421 |
|
| 422 |
### Code block |
| 423 |
|
| 424 |
§§§go |
| 425 |
fmt.Println("hello") |
| 426 |
§§§ |
| 427 |
|
| 428 |
## PassThrough |
| 429 |
|
| 430 |
\[block1\] |
| 431 |
|
| 432 |
\[block2\] |
| 433 |
|
| 434 |
This is an \(inline\) passthrough element with opening and closing inline delimiters. |
| 435 |
|
| 436 |
|
| 437 |
` |
| 438 |
|
| 439 |
files = strings.ReplaceAll(files, "HOOK_CONTENT", ` |
| 440 |
{{ $pos := .Position }} |
| 441 |
{{ printf "%T" . }}|{{ path.Base $pos.Filename }}|{{ printf "%d:%d" $pos.LineNumber $pos.ColumnNumber }}|{{ $.Ordinal }}| |
| 442 |
`) |
| 443 |
b := hugolib.Test(t, files) |
| 444 |
|
| 445 |
b.AssertFileContent("public/p1/index.html", |
| 446 |
"goldmark.linkContext|p1.md|5:1|0|", |
| 447 |
"goldmark.linkContext|p1.md|6:2|1|", |
| 448 |
"goldmark.linkContext|p1.md|9:1|2|", |
| 449 |
"goldmark.headingContext|p1.md|13:1|0|", |
| 450 |
"goldmark.headingContext|p1.md|15:1|1|", |
| 451 |
"goldmark.headingContext|p1.md|17:1|2|", |
| 452 |
"tables.tableContext|p1.md|20:1|0|", |
| 453 |
"blockquotes.blockquoteContext|p1.md|27:1|0|", |
| 454 |
"blockquotes.blockquoteContext|p1.md|31:1|1|", |
| 455 |
"codeblocks.codeBlockContext|p1.md|36:1|0|", |
| 456 |
"passthrough.passthroughContext|p1.md|42:1|0|", |
| 457 |
"passthrough.passthroughContext|p1.md|44:2|1|", |
| 458 |
"passthrough.passthroughContext|p1.md|46:12|2|", |
| 459 |
) |
| 460 |
} |
| 461 |
|
| 462 |
func TestRenderHooksPositionRenderString(t *testing.T) { |
| 463 |
t.Parallel() |
| 464 |
|
| 465 |
files := ` |
| 466 |
-- hugo.toml -- |
| 467 |
-- assets/a.txt -- |
| 468 |
|
| 469 |
## Heading |
| 470 |
|
| 471 |
[link](b.txt) |
| 472 |
|
| 473 |
-- assets/b.txt -- |
| 474 |
{{% myshortcode %}} |
| 475 |
{{< myshortcode >}} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
[link](a.txt) |
| 480 |
-- layouts/shortcodes/myshortcode.html -- |
| 481 |
My Shortcode. |
| 482 |
# This is a heading in the shortcode. |
| 483 |
Some text. |
| 484 |
-- layouts/_markup/render-link.html -- |
| 485 |
{{ $pos := .Position }} |
| 486 |
{{ printf "%T" . }}|{{ path.Join $pos.Filename }}|{{ printf "%d:%d" $pos.LineNumber $pos.ColumnNumber }}|{{ $.Ordinal }}| |
| 487 |
-- layouts/all.html -- |
| 488 |
{{ $a := resources.Get "a.txt" }} |
| 489 |
a: {{ .RenderString $a.Content }} |
| 490 |
b: {{ .RenderString (resources.Get "b.txt").Content }} |
| 491 |
-- content/p1.md -- |
| 492 |
|
| 493 |
|
| 494 |
` |
| 495 |
|
| 496 |
b := hugolib.Test(t, files) |
| 497 |
b.AssertFileContent("public/p1/index.html", |
| 498 |
"/content/p1.md (rendered from string)|4:1|0|", |
| 499 |
"/content/p1.md (rendered from string)|6:3|0|", |
| 500 |
) |
| 501 |
} |