Debian Codegraph @

hugolib/rendershortcodes_test.go

raw
1 // Copyright 2024 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 hugolib
15
16 import (
17 "path/filepath"
18 "strings"
19 "testing"
20 )
21
22 func TestRenderShortcodesBasic(t *testing.T) {
23 t.Parallel()
24
25 files := `
26 -- hugo.toml --
27 disableKinds = ["home", "taxonomy", "term"]
28 -- content/p1.md --
29 ---
30 title: "p1"
31 ---
32 ## p1-h1
33 {{% include "p2" %}}
34 -- content/p2.md --
35 ---
36 title: "p2"
37 ---
38 ### p2-h1
39 {{< withhtml >}}
40 ### p2-h2
41 {{% withmarkdown %}}
42 ### p2-h3
43 {{% include "p3" %}}
44 -- content/p3.md --
45 ---
46 title: "p3"
47 ---
48 ### p3-h1
49 {{< withhtml >}}
50 ### p3-h2
51 {{% withmarkdown %}}
52 {{< level3 >}}
53 -- layouts/_shortcodes/include.html --
54 {{ $p := site.GetPage (.Get 0) }}
55 {{ $p.RenderShortcodes }}
56 -- layouts/_shortcodes/withhtml.html --
57 <div>{{ .Page.Title }} withhtml</div>
58 -- layouts/_shortcodes/withmarkdown.html --
59 #### {{ .Page.Title }} withmarkdown
60 -- layouts/_shortcodes/level3.html --
61 Level 3: {{ .Page.Title }}
62 -- layouts/single.html --
63 Fragments: {{ .Fragments.Identifiers }}|
64 HasShortcode Level 1: {{ .HasShortcode "include" }}|
65 HasShortcode Level 2: {{ .HasShortcode "withmarkdown" }}|
66 HasShortcode Level 3: {{ .HasShortcode "level3" }}|
67 HasShortcode not found: {{ .HasShortcode "notfound" }}|
68 Content: {{ .Content }}|
69 `
70
71 b := Test(t, files)
72
73 b.AssertNoRenderShortcodesArtifacts()
74 b.AssertFileContent("public/p1/index.html",
75 "Fragments: [p1-h1 p2-h1 p2-h2 p2-h3 p2-withmarkdown p3-h1 p3-h2 p3-withmarkdown]|",
76 "HasShortcode Level 1: true|",
77 "HasShortcode Level 2: true|",
78 "HasShortcode Level 3: true|",
79 "HasShortcode not found: false|",
80 )
81 }
82
83 func TestRenderShortcodesNestedMultipleOutputFormatTemplates(t *testing.T) {
84 t.Parallel()
85
86 files := `
87 -- hugo.toml --
88 disableKinds = ["home", "taxonomy", "term", "section", "rss", "sitemap", "robotsTXT", "404"]
89 [outputs]
90 page = ["html", "json"]
91 -- content/p1.md --
92 ---
93 title: "p1"
94 ---
95 ## p1-h1
96 {{% include "p2" %}}
97 -- content/p2.md --
98 ---
99 title: "p2"
100 ---
101 ### p2-h1
102 {{% myshort %}}
103 -- layouts/_shortcodes/include.html --
104 {{ $p := site.GetPage (.Get 0) }}
105 {{ $p.RenderShortcodes }}
106 -- layouts/_shortcodes/myshort.html --
107 Myshort HTML.
108 -- layouts/_shortcodes/myshort.json --
109 Myshort JSON.
110 -- layouts/single.html --
111 HTML: {{ .Content }}
112 -- layouts/single.json --
113 JSON: {{ .Content }}
114
115
116 `
117
118 b := Test(t, files)
119
120 b.AssertNoRenderShortcodesArtifacts()
121 b.AssertFileContent("public/p1/index.html", "Myshort HTML")
122 b.AssertFileContent("public/p1/index.json", "Myshort JSON")
123 }
124
125 func TestRenderShortcodesEditNested(t *testing.T) {
126 t.Parallel()
127
128 files := `
129 -- hugo.toml --
130 disableLiveReload = true
131 disableKinds = ["home", "taxonomy", "term", "section", "rss", "sitemap", "robotsTXT", "404"]
132 -- content/p1.md --
133 ---
134 title: "p1"
135 ---
136 ## p1-h1
137 {{% include "p2" %}}
138 -- content/p2.md --
139 ---
140 title: "p2"
141 ---
142 ### p2-h1
143 {{% myshort %}}
144 -- layouts/_shortcodes/include.html --
145 {{ $p := site.GetPage (.Get 0) }}
146 {{ $p.RenderShortcodes }}
147 -- layouts/_shortcodes/myshort.html --
148 Myshort Original.
149 -- layouts/single.html --
150 {{ .Content }}
151 `
152 b := TestRunning(t, files)
153 b.AssertNoRenderShortcodesArtifacts()
154 b.AssertFileContent("public/p1/index.html", "Myshort Original.")
155
156 b.EditFileReplaceAll("layouts/_shortcodes/myshort.html", "Original", "Edited").Build()
157 b.AssertNoRenderShortcodesArtifacts()
158 b.AssertFileContent("public/p1/index.html", "Myshort Edited.")
159 }
160
161 func TestRenderShortcodesEditIncludedPage(t *testing.T) {
162 t.Parallel()
163
164 files := `
165 -- hugo.toml --
166 disableLiveReload = true
167 disableKinds = ["home", "taxonomy", "term", "section", "rss", "sitemap", "robotsTXT", "404"]
168 -- content/p1.md --
169 ---
170 title: "p1"
171 ---
172 ## p1-h1
173 {{% include "p2" %}}
174 -- content/p2.md --
175 ---
176 title: "p2"
177 ---
178 ### Original
179 {{% myshort %}}
180 -- layouts/_shortcodes/include.html --
181 {{ $p := site.GetPage (.Get 0) }}
182 {{ $p.RenderShortcodes }}
183 -- layouts/_shortcodes/myshort.html --
184 Myshort Original.
185 -- layouts/single.html --
186 {{ .Content }}
187
188
189
190 `
191
192 b := TestRunning(t, files)
193
194 b.AssertNoRenderShortcodesArtifacts()
195 b.AssertFileContent("public/p1/index.html", "Original")
196
197 b.EditFileReplaceFunc("content/p2.md", func(s string) string {
198 return strings.Replace(s, "Original", "Edited", 1)
199 })
200 b.Build()
201 b.AssertNoRenderShortcodesArtifacts()
202 b.AssertFileContent("public/p1/index.html", "Edited")
203 }
204
205 func TestRenderShortcodesEditSectionContentWithShortcodeInIncludedPageIssue12458(t *testing.T) {
206 t.Parallel()
207
208 files := `
209 -- hugo.toml --
210 disableLiveReload = true
211 disableKinds = ["home", "taxonomy", "term", "rss", "sitemap", "robotsTXT", "404"]
212 -- content/mysection/_index.md --
213 ---
214 title: "My Section"
215 ---
216 ## p1-h1
217 {{% include "p2" %}}
218 -- content/mysection/p2.md --
219 ---
220 title: "p2"
221 ---
222 ### Original
223 {{% myshort %}}
224 -- layouts/_shortcodes/include.html --
225 {{ $p := .Page.GetPage (.Get 0) }}
226 {{ $p.RenderShortcodes }}
227 -- layouts/_shortcodes/myshort.html --
228 Myshort Original.
229 -- layouts/list.html --
230 {{ .Content }}
231
232
233
234 `
235 b := TestRunning(t, files)
236
237 b.AssertNoRenderShortcodesArtifacts()
238 b.AssertFileContent("public/mysection/index.html", "p1-h1")
239 b.EditFileReplaceAll("content/mysection/_index.md", "p1-h1", "p1-h1 Edited").Build()
240 b.AssertNoRenderShortcodesArtifacts()
241 b.AssertFileContent("public/mysection/index.html", "p1-h1 Edited")
242 }
243
244 func TestRenderShortcodesNestedPageContextIssue12356(t *testing.T) {
245 t.Parallel()
246
247 files := `
248 -- hugo.toml --
249 disableKinds = ["taxonomy", "term", "rss", "sitemap", "robotsTXT", "404"]
250 [security]
251 allowContent = ['.*']
252 -- layouts/_markup/render-image.html --
253 {{- with .PageInner.Resources.Get .Destination -}}Image: {{ .RelPermalink }}|{{- end -}}
254 -- layouts/_markup/render-link.html --
255 {{- with .PageInner.GetPage .Destination -}}Link: {{ .RelPermalink }}|{{- end -}}
256 -- layouts/_markup/render-heading.html --
257 Heading: {{ .PageInner.Title }}: {{ .PlainText }}|
258 -- layouts/_markup/render-codeblock.html --
259 CodeBlock: {{ .PageInner.Title }}: {{ .Type }}|
260 -- layouts/list.html --
261 Content:{{ .Content }}|
262 Fragments: {{ with .Fragments }}{{.Identifiers }}{{ end }}|
263 -- layouts/single.html --
264 Content:{{ .Content }}|
265 -- layouts/_shortcodes/include.html --
266 {{ with site.GetPage (.Get 0) }}
267 {{ .RenderShortcodes }}
268 {{ end }}
269 -- content/markdown/_index.md --
270 ---
271 title: "Markdown"
272 ---
273 # H1
274 |{{% include "/posts/p1" %}}|
275 ![kitten](pixel3.png "Pixel 3")
276
277 §§§go
278 fmt.Println("Hello")
279 §§§
280
281 -- content/markdown2/_index.md --
282 ---
283 title: "Markdown 2"
284 ---
285 |{{< include "/posts/p1" >}}|
286 -- content/html/_index.html --
287 ---
288 title: "HTML"
289 ---
290 |{{% include "/posts/p1" %}}|
291
292 -- content/posts/p1/index.md --
293 ---
294 title: "p1"
295 ---
296 ## H2-p1
297 ![kitten](pixel1.png "Pixel 1")
298 ![kitten](pixel2.png "Pixel 2")
299 [p2](p2)
300
301 §§§bash
302 echo "Hello"
303 §§§
304
305 -- content/posts/p2/index.md --
306 ---
307 title: "p2"
308 ---
309 -- content/posts/p1/pixel1.png --
310 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
311 -- content/posts/p1/pixel2.png --
312 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
313 -- content/markdown/pixel3.png --
314 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
315 -- content/html/pixel4.png --
316 iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==
317
318 `
319
320 b := Test(t, files)
321
322 b.AssertNoRenderShortcodesArtifacts()
323
324 b.AssertFileContent("public/markdown/index.html",
325 // Images.
326 "Image: /posts/p1/pixel1.png|\nImage: /posts/p1/pixel2.png|\n|\nImage: /markdown/pixel3.png|</p>\n|",
327 // Links.
328 "Link: /posts/p2/|",
329 // Code blocks
330 "CodeBlock: p1: bash|", "CodeBlock: Markdown: go|",
331 // Headings.
332 "Heading: Markdown: H1|", "Heading: p1: H2-p1|",
333 // Fragments.
334 "Fragments: [h1 h2-p1]|",
335 // Check that the special context markup is not rendered.
336 "! hugo_ctx",
337 )
338
339 b.AssertFileContent("public/markdown2/index.html", "! hugo_ctx", "Content:<p>|\n ![kitten](pixel1.png \"Pixel 1\")\n![kitten](pixel2.png \"Pixel 2\")\n|</p>\n|")
340
341 b.AssertFileContent("public/html/index.html", "! hugo_ctx")
342 }
343
344 // Issue 12854.
345 func TestRenderShortcodesWithHTML(t *testing.T) {
346 t.Parallel()
347
348 files := `
349 -- hugo.toml --
350 disableLiveReload = true
351 disableKinds = ["home", "taxonomy", "term"]
352 markup.goldmark.renderer.unsafe = true
353 -- content/p1.md --
354 ---
355 title: "p1"
356 ---
357 {{% include "p2" %}}
358 -- content/p2.md --
359 ---
360 title: "p2"
361 ---
362 Hello <b>world</b>. Some **bold** text. Some Unicode: 神真美好.
363 -- layouts/_shortcodes/include.html --
364 {{ with site.GetPage (.Get 0) }}
365 <div>{{ .RenderShortcodes }}</div>
366 {{ end }}
367 -- layouts/single.html --
368 {{ .Content }}
369 `
370
371 b := TestRunning(t, files, TestOptWarn())
372
373 b.AssertNoRenderShortcodesArtifacts()
374 b.AssertLogContains(filepath.ToSlash("WARN .RenderShortcodes detected inside HTML block in \"/content/p1.md\"; this may not be what you intended, see https://gohugo.io/methods/page/rendershortcodes/#limitations\nYou can suppress this warning by adding the following to your project configuration:\nignoreLogs = ['warning-rendershortcodes-in-html']"))
375 b.AssertFileContent("public/p1/index.html", "<div>Hello <b>world</b>. Some **bold** text. Some Unicode: 神真美好.\n</div>")
376 b.EditFileReplaceAll("content/p2.md", "Hello", "Hello Edited").Build()
377 b.AssertNoRenderShortcodesArtifacts()
378 b.AssertFileContent("public/p1/index.html", "<div>Hello Edited <b>world</b>. Some **bold** text. Some Unicode: 神真美好.\n</div>")
379 }
380
381 func TestRenderShortcodesIncludeMarkdownFileWithoutTrailingNewline(t *testing.T) {
382 t.Parallel()
383
384 files := `
385 -- hugo.toml --
386 disableLiveReload = true
387 disableKinds = ["home", "taxonomy", "term"]
388 markup.goldmark.renderer.unsafe = true
389 -- content/p1.md --
390 ---
391 title: "p1"
392 ---
393 Content p1 id-1000.{{% include "p2" %}}{{% include "p3" %}}
394
395 §§§ go
396 code_p1
397 §§§
398 §§§ go
399 code_p1_2
400 §§§
401
402 §§§ go
403 code_p1_3
404 §§§
405 -- content/p2.md --
406 ---
407 title: "p2"
408 ---
409 §§§ bash
410 code_p2
411 §§§
412 Foo.
413 -- content/p3.md --
414 ---
415 title: "p3"
416 ---
417 §§§ php
418 code_p3
419 §§§
420 -- layouts/_shortcodes/include.html --
421 {{ with site.GetPage (.Get 0) -}}
422 {{ .RenderShortcodes -}}
423 {{ end -}}
424 -- layouts/single.html --
425 {{ .Content }}
426 -- layouts/_markup/render-codeblock.html --
427 <code>{{ .Inner | safeHTML }}</code>
428 `
429
430 b := TestRunning(t, files, TestOptWarn())
431
432 b.AssertNoRenderShortcodesArtifacts()
433 b.AssertFileContentEquals("public/p1/index.html", "<p>Content p1 id-1000.</p>\n<code>code_p2</code><p>Foo.</p>\n<code>code_p3</code><code>code_p1</code><code>code_p1_2</code><code>code_p1_3</code>")
434 b.EditFileReplaceAll("content/p1.md", "id-1000.", "id-100.").Build()
435 b.AssertNoRenderShortcodesArtifacts()
436 b.AssertFileContentEquals("public/p1/index.html", "<p>Content p1 id-100.</p>\n<code>code_p2</code><p>Foo.</p>\n<code>code_p3</code><code>code_p1</code><code>code_p1_2</code><code>code_p1_3</code>")
437 b.EditFileReplaceAll("content/p2.md", "code_p2", "codep2").Build()
438 b.AssertNoRenderShortcodesArtifacts()
439 b.AssertFileContentEquals("public/p1/index.html", "<p>Content p1 id-100.</p>\n<code>codep2</code><p>Foo.</p>\n<code>code_p3</code><code>code_p1</code><code>code_p1_2</code><code>code_p1_3</code>")
440 b.EditFileReplaceAll("content/p3.md", "code_p3", "code_p3_edited").Build()
441 b.AssertNoRenderShortcodesArtifacts()
442 b.AssertFileContentEquals("public/p1/index.html", "<p>Content p1 id-100.</p>\n<code>codep2</code><p>Foo.</p>\n<code>code_p3_edited</code><code>code_p1</code><code>code_p1_2</code><code>code_p1_3</code>")
443 }
444
445 // Issue 13004.
446 func TestRenderShortcodesIncludeShortRefEdit(t *testing.T) {
447 t.Parallel()
448
449 files := `
450 -- hugo.toml --
451 disableLiveReload = true
452 disableKinds = ["home", "taxonomy", "term", "section", "rss", "sitemap", "robotsTXT", "404"]
453 -- content/first/p1.md --
454 ---
455 title: "p1"
456 ---
457 ## p1-h1
458 {{% include "p2" %}}
459 -- content/second/p2.md --
460 ---
461 title: "p2"
462 ---
463 ### p2-h1
464
465 This is some **markup**.
466 -- layouts/_shortcodes/include.html --
467 {{ $p := site.GetPage (.Get 0) -}}
468 {{ $p.RenderShortcodes -}}
469 -- layouts/single.html --
470 {{ .Content }}
471 `
472 b := TestRunning(t, files)
473 b.AssertNoRenderShortcodesArtifacts()
474 b.AssertFileContentEquals("public/first/p1/index.html", "<h2 id=\"p1-h1\">p1-h1</h2>\n<h3 id=\"p2-h1\">p2-h1</h3>\n<p>This is some <strong>markup</strong>.</p>\n")
475 b.EditFileReplaceAll("content/second/p2.md", "p2-h1", "p2-h1-edited").Build()
476 b.AssertNoRenderShortcodesArtifacts()
477 b.AssertFileContentEquals("public/first/p1/index.html", "<h2 id=\"p1-h1\">p1-h1</h2>\n<h3 id=\"p2-h1-edited\">p2-h1-edited</h3>\n<p>This is some <strong>markup</strong>.</p>\n")
478 }
479
480 // Issue 13051.
481 func TestRenderShortcodesEmptyParagraph(t *testing.T) {
482 t.Parallel()
483
484 files := `
485 -- hugo.toml --
486 disableKinds = ['section','rss','sitemap','taxonomy','term']
487 -- layouts/home.html --
488 {{ .Content }}
489 -- layouts/single.html --
490 {{ .Content }}
491 -- layouts/_shortcodes/include.html --
492 {{ with site.GetPage (.Get 0) }}
493 {{ .RenderShortcodes }}
494 {{ end }}
495 -- content/_index.md --
496 ---
497 title: home
498 ---
499
500 a
501
502 {{% include "/snippet" %}}
503
504 b
505
506 -- content/snippet.md --
507 ---
508 title: snippet
509 build:
510 render: never
511 list: never
512 ---
513
514 _emphasized_
515
516 not emphasized
517
518 `
519
520 b := Test(t, files)
521 b.AssertNoRenderShortcodesArtifacts()
522 b.AssertFileContentEquals("public/index.html",
523 "<p>a</p>\n<p><em>emphasized</em></p>\n<p>not emphasized</p>\n<p>b</p>\n",
524 )
525 }
526
527 // Issue 14732.
528 func TestRenderShortcodesStandalone(t *testing.T) {
529 t.Parallel()
530
531 files := `
532 -- hugo.toml --
533 disableKinds = ['section','rss','sitemap','taxonomy','term']
534 -- layouts/all.html --
535 RenderShortcodes: {{ .RenderShortcodes }}|
536 -- layouts/_shortcodes/headings.html --
537 ## Heading 1
538 ### Heading 2
539 {{ $p := site.GetPage "includeme" }}
540 RenderShortcodes2: {{ $p.RenderShortcodes }}|
541 -- content/p1.md --
542 ---
543 title: "p1"
544 ---
545 {{% headings "headings" %}}
546 -- content/includeme.md --
547 ---
548 title: "includeme"
549 ---
550 ## Heading 2
551 `
552
553 b := Test(t, files)
554
555 b.AssertNoRenderShortcodesArtifacts()
556 }
557
558 // Issue 12457.
559 func TestRenderShortcodesCodeBlock(t *testing.T) {
560 t.Parallel()
561
562 files := `
563 -- hugo.toml --
564 disableKinds = ['section','rss','sitemap','taxonomy','term']
565 -- layouts/_shortcodes/foo.html --
566 {{ $p := site.GetPage "includeme" }}
567 {{ $p.RenderShortcodes }}
568 -- layouts/home.html --
569 {{ .Content }}
570 -- content/_index.md --
571 ---
572 title: home
573 ---
574 {{% foo %}}
575 -- content/includeme.md --
576 ---
577 title: "includeme"
578 ---
579 Some markdown.
580 `
581
582 b := Test(t, files)
583 b.AssertNoRenderShortcodesArtifacts()
584 b.AssertFileContentEquals("public/index.html", "<p>Some markdown.</p>\n")
585 }