Debian Codegraph @

markup/goldmark/goldmark_integration_test.go

raw
1 // Copyright 2021 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 goldmark_test
15
16 import (
17 "fmt"
18 "strings"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22
23 "github.com/gohugoio/hugo/hugolib"
24 )
25
26 // Issue 9463
27 func TestAttributeExclusion(t *testing.T) {
28 t.Parallel()
29
30 files := `
31 -- hugo.toml --
32 [markup.goldmark.renderer]
33 unsafe = false
34 [markup.goldmark.parser.attribute]
35 block = true
36 title = true
37 -- content/p1.md --
38 ---
39 title: "p1"
40 ---
41 ## Heading {class="a" onclick="alert('heading')"}
42
43 > Blockquote
44 {class="b" ondblclick="alert('blockquote')"}
45
46 ~~~bash {id="c" onmouseover="alert('code fence')" LINENOS=true}
47 foo
48 ~~~
49 -- layouts/single.html --
50 {{ .Content }}
51 `
52
53 b := hugolib.Test(t, files)
54
55 b.AssertFileContent("public/p1/index.html", `
56 <h2 class="a" id="heading">
57 <blockquote class="b">
58 <div class="highlight" id="c">
59 `)
60 }
61
62 // Issue 9511
63 func TestAttributeExclusionWithRenderHook(t *testing.T) {
64 t.Parallel()
65
66 files := `
67 -- content/p1.md --
68 ---
69 title: "p1"
70 ---
71 ## Heading {onclick="alert('renderhook')" data-foo="bar"}
72 -- layouts/single.html --
73 {{ .Content }}
74 -- layouts/_markup/render-heading.html --
75 <h{{ .Level }}
76 {{- range $k, $v := .Attributes -}}
77 {{- printf " %s=%q" $k $v | safeHTMLAttr -}}
78 {{- end -}}
79 >{{ .Text }}</h{{ .Level }}>
80 `
81
82 b := hugolib.Test(t, files)
83
84 b.AssertFileContent("public/p1/index.html", `
85 <h2 data-foo="bar" id="heading">Heading</h2>
86 `)
87 }
88
89 func TestAttributesDefaultRenderer(t *testing.T) {
90 t.Parallel()
91
92 files := `
93 -- content/p1.md --
94 ---
95 title: "p1"
96 ---
97 ## Heading Attribute Which Needs Escaping { class="a < b" }
98 -- layouts/single.html --
99 {{ .Content }}
100 `
101
102 b := hugolib.Test(t, files)
103
104 b.AssertFileContent("public/p1/index.html", `
105 class="a &lt; b"
106 `)
107 }
108
109 // Issue 9558.
110 func TestAttributesHookNoEscape(t *testing.T) {
111 t.Parallel()
112
113 files := `
114 -- content/p1.md --
115 ---
116 title: "p1"
117 ---
118 ## Heading Attribute Which Needs Escaping { class="Smith & Wesson" }
119 -- layouts/_markup/render-heading.html --
120 plain: |{{- range $k, $v := .Attributes -}}{{ $k }}: {{ $v }}|{{ end }}|
121 safeHTML: |{{- range $k, $v := .Attributes -}}{{ $k }}: {{ $v | safeHTML }}|{{ end }}|
122 -- layouts/single.html --
123 {{ .Content }}
124 `
125
126 b := hugolib.Test(t, files)
127
128 b.AssertFileContent("public/p1/index.html", `
129 plain: |class: Smith &amp; Wesson|id: heading-attribute-which-needs-escaping|
130 safeHTML: |class: Smith & Wesson|id: heading-attribute-which-needs-escaping|
131 `)
132 }
133
134 // Issue 9504
135 func TestLinkInTitle(t *testing.T) {
136 t.Parallel()
137
138 files := `
139 -- hugo.toml --
140 -- content/p1.md --
141 ---
142 title: "p1"
143 ---
144 ## Hello [Test](https://example.com)
145 -- layouts/single.html --
146 {{ .Content }}
147 -- layouts/_markup/render-heading.html --
148 <h{{ .Level }} id="{{ .Anchor | safeURL }}">
149 {{ .Text }}
150 <a class="anchor" href="#{{ .Anchor | safeURL }}">#</a>
151 </h{{ .Level }}>
152 -- layouts/_markup/render-link.html --
153 <a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}>{{ .Text }}</a>
154
155 `
156
157 b := hugolib.Test(t, files)
158
159 b.AssertFileContent("public/p1/index.html",
160 "<h2 id=\"hello-test\">\n Hello <a href=\"https://example.com\">Test</a>\n\n <a class=\"anchor\" href=\"#hello-test\">#</a>\n</h2>",
161 )
162 }
163
164 func TestHighlight(t *testing.T) {
165 t.Parallel()
166
167 files := `
168 -- hugo.toml --
169 [markup]
170 [markup.highlight]
171 anchorLineNos = false
172 codeFences = true
173 guessSyntax = false
174 hl_Lines = ''
175 lineAnchors = ''
176 lineNoStart = 1
177 lineNos = false
178 lineNumbersInTable = true
179 noClasses = false
180 style = 'monokai'
181 tabWidth = 4
182 -- layouts/single.html --
183 {{ .Content }}
184 -- content/p1.md --
185 ---
186 title: "p1"
187 ---
188
189 ## Code Fences
190
191 §§§bash
192 LINE1
193 §§§
194
195 ## Code Fences No Lexer
196
197 §§§moo
198 LINE1
199 §§§
200
201 ## Code Fences Simple Attributes
202
203 §§A§bash { .myclass id="myid" }
204 LINE1
205 §§A§
206
207 ## Code Fences Line Numbers
208
209 §§§bash {linenos=table,hl_lines=[8,"15-17"],linenostart=199}
210 LINE1
211 LINE2
212 LINE3
213 LINE4
214 LINE5
215 LINE6
216 LINE7
217 LINE8
218 §§§
219
220
221
222
223 `
224
225 b := hugolib.Test(t, files)
226
227 b.AssertFileContent("public/p1/index.html",
228 "<div class=\"highlight\"><pre tabindex=\"0\" class=\"chroma\"><code class=\"language-bash\" data-lang=\"bash\"><span class=\"line\"><span class=\"cl\">LINE1\n</span></span></code></pre></div>",
229 "Code Fences No Lexer</h2>\n<pre tabindex=\"0\"><code class=\"language-moo\" data-lang=\"moo\">LINE1\n</code></pre>",
230 "lnt",
231 )
232 }
233
234 func BenchmarkRenderHooks(b *testing.B) {
235 var files strings.Builder
236 files.WriteString(`
237 -- hugo.toml --
238 -- layouts/_markup/render-heading.html --
239 <h{{ .Level }} id="{{ .Anchor | safeURL }}">
240 {{ .Text }}
241 <a class="anchor" href="#{{ .Anchor | safeURL }}">#</a>
242 </h{{ .Level }}>
243 -- layouts/_markup/render-link.html --
244 <a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}>{{ .Text }}</a>
245 -- layouts/single.html --
246 {{ .Content }}
247 `)
248
249 content := `
250
251 ## Hello1 [Test](https://example.com)
252
253 A.
254
255 ## Hello2 [Test](https://example.com)
256
257 B.
258
259 ## Hello3 [Test](https://example.com)
260
261 C.
262
263 ## Hello4 [Test](https://example.com)
264
265 D.
266
267 [Test](https://example.com)
268
269 ## Hello5
270
271
272 `
273
274 for i := 1; i < 100; i++ {
275 files.WriteString(fmt.Sprintf("\n-- content/posts/p%d.md --\n"+content, i+1))
276 }
277
278 cfg := hugolib.IntegrationTestConfig{
279 T: b,
280 TxtarString: files.String(),
281 }
282
283 for b.Loop() {
284 b.StopTimer()
285 bb := hugolib.NewIntegrationTestBuilder(cfg)
286 b.StartTimer()
287 bb.Build()
288 }
289 }
290
291 func BenchmarkCodeblocks(b *testing.B) {
292 var filesTemplate strings.Builder
293 filesTemplate.WriteString(`
294 -- hugo.toml --
295 [markup]
296 [markup.highlight]
297 anchorLineNos = false
298 codeFences = true
299 guessSyntax = false
300 hl_Lines = ''
301 lineAnchors = ''
302 lineNoStart = 1
303 lineNos = false
304 lineNumbersInTable = true
305 noClasses = true
306 style = 'monokai'
307 tabWidth = 4
308 -- layouts/single.html --
309 {{ .Content }}
310 `)
311
312 content := `
313
314 FENCEgo
315 package main
316 import "fmt"
317 func main() {
318 fmt.Println("hello world")
319 }
320 FENCE
321
322 FENCEunknownlexer
323 hello
324 FENCE
325 `
326
327 content = strings.ReplaceAll(content, "FENCE", "```")
328
329 for i := 1; i < 100; i++ {
330 filesTemplate.WriteString(fmt.Sprintf("\n-- content/posts/p%d.md --\n"+content, i+1))
331 }
332
333 runBenchmark := func(files string, b *testing.B) {
334 cfg := hugolib.IntegrationTestConfig{
335 T: b,
336 TxtarString: files,
337 }
338
339 for b.Loop() {
340 b.StopTimer()
341 bb := hugolib.NewIntegrationTestBuilder(cfg)
342 b.StartTimer()
343 bb.Build()
344 }
345 }
346
347 b.Run("Default", func(b *testing.B) {
348 runBenchmark(filesTemplate.String(), b)
349 })
350
351 b.Run("Hook no higlight", func(b *testing.B) {
352 files := filesTemplate.String() + `
353 -- layouts/_markup/render-codeblock.html --
354 {{ .Inner }}
355 `
356
357 runBenchmark(files, b)
358 })
359 }
360
361 // Iisse #8959
362 func TestHookInfiniteRecursion(t *testing.T) {
363 t.Parallel()
364
365 for _, renderFunc := range []string{"markdownify", ".Page.RenderString"} {
366 t.Run(renderFunc, func(t *testing.T) {
367 files := `
368 -- hugo.toml --
369 -- layouts/_markup/render-link.html --
370 <a href="{{ .Destination | safeURL }}">{{ .Text | RENDERFUNC }}</a>
371 -- layouts/single.html --
372 {{ .Content }}
373 -- content/p1.md --
374 ---
375 title: "p1"
376 ---
377
378 https://example.org
379
380 [email protected]
381
382
383 `
384
385 files = strings.ReplaceAll(files, "RENDERFUNC", renderFunc)
386
387 b, err := hugolib.TestE(t, files)
388
389 b.Assert(err, qt.IsNotNil)
390 b.Assert(err.Error(), qt.Contains, "text is already rendered, repeating it may cause infinite recursion")
391 })
392 }
393 }
394
395 // Issue 9594
396 func TestQuotesInImgAltAttr(t *testing.T) {
397 t.Parallel()
398
399 files := `
400 -- hugo.toml --
401 [markup.goldmark.extensions]
402 typographer = false
403 -- content/p1.md --
404 ---
405 title: "p1"
406 ---
407 !["a"](b.jpg)
408 -- layouts/single.html --
409 {{ .Content }}
410 `
411
412 b := hugolib.Test(t, files)
413
414 b.AssertFileContent("public/p1/index.html", `
415 <img src="b.jpg" alt="&quot;a&quot;">
416 `)
417 }
418
419 func TestLinkifyProtocol(t *testing.T) {
420 t.Parallel()
421
422 runTest := func(protocol string, withHook bool) *hugolib.IntegrationTestBuilder {
423 files := `
424 -- hugo.toml --
425 [markup.goldmark]
426 [markup.goldmark.extensions]
427 linkify = true
428 linkifyProtocol = "PROTOCOL"
429 -- content/p1.md --
430 ---
431 title: "p1"
432 ---
433 Link no procol: www.example.org
434 Link http procol: http://www.example.org
435 Link https procol: https://www.example.org
436
437 -- layouts/single.html --
438 {{ .Content }}
439 `
440 files = strings.ReplaceAll(files, "PROTOCOL", protocol)
441
442 if withHook {
443 files += `-- layouts/_markup/render-link.html --
444 <a href="{{ .Destination | safeURL }}">{{ .Text }}</a>`
445 }
446
447 return hugolib.Test(t, files)
448 }
449
450 for _, withHook := range []bool{false, true} {
451
452 b := runTest("https", withHook)
453
454 b.AssertFileContent("public/p1/index.html",
455 "Link no procol: <a href=\"https://www.example.org\">www.example.org</a>",
456 "Link http procol: <a href=\"http://www.example.org\">http://www.example.org</a>",
457 "Link https procol: <a href=\"https://www.example.org\">https://www.example.org</a></p>",
458 )
459
460 b = runTest("http", withHook)
461
462 b.AssertFileContent("public/p1/index.html",
463 "Link no procol: <a href=\"http://www.example.org\">www.example.org</a>",
464 "Link http procol: <a href=\"http://www.example.org\">http://www.example.org</a>",
465 "Link https procol: <a href=\"https://www.example.org\">https://www.example.org</a></p>",
466 )
467
468 b = runTest("gopher", withHook)
469
470 b.AssertFileContent("public/p1/index.html",
471 "Link no procol: <a href=\"gopher://www.example.org\">www.example.org</a>",
472 "Link http procol: <a href=\"http://www.example.org\">http://www.example.org</a>",
473 "Link https procol: <a href=\"https://www.example.org\">https://www.example.org</a></p>",
474 )
475
476 }
477 }
478
479 func TestGoldmarkBugs(t *testing.T) {
480 t.Parallel()
481
482 files := `
483 -- hugo.toml --
484 [markup.goldmark.renderer]
485 unsafe = true
486 -- content/p1.md --
487 ---
488 title: "p1"
489 ---
490
491 ## Issue 9650
492
493 a <!-- b --> c
494
495 ## Issue 9658
496
497 - This is a list item <!-- Comment: an innocent-looking comment -->
498
499
500 -- layouts/single.html --
501 {{ .Content }}
502 `
503
504 b := hugolib.Test(t, files)
505
506 b.AssertFileContentExact("public/p1/index.html",
507 // Issue 9650
508 "<p>a <!-- b --> c</p>",
509 // Issue 9658 (crash)
510 "<li>This is a list item <!-- Comment: an innocent-looking comment --></li>",
511 )
512 }
513
514 // Issue 13286
515 func TestImageAltApostrophesWithTypographer(t *testing.T) {
516 t.Parallel()
517
518 files := `
519 -- hugo.toml --
520 [markup.goldmark.extensions.typographer]
521 disable = false
522 [markup.goldmark.renderHooks.image]
523 useEmbedded = 'always'
524 -- content/p1.md --
525 ---
526 title: "p1"
527 ---
528
529 ## Image
530
531 ![A's is > than B's](some-image.png)
532
533
534 -- layouts/single.html --
535 {{ .Content }}
536 `
537
538 b := hugolib.Test(t, files)
539
540 b.AssertFileContentExact("public/p1/index.html",
541 // Note that this markup is slightly different than the one produced by the default Goldmark renderer,
542 // see issue 13292.
543 "alt=\"A’s is &gt; than B’s\">",
544 )
545 }
546
547 // Issue #7332
548 // Issue #11587
549 func TestGoldmarkEmojiExtension(t *testing.T) {
550 t.Parallel()
551
552 files := `
553 -- hugo.toml --
554 enableEmoji = true
555 -- content/p1.md --
556 ---
557 title: "p1"
558 ---
559 ~~~text
560 :x:
561 ~~~
562
563 {{% include "/p2" %}}
564
565 {{< sc1 >}}:smiley:{{< /sc1 >}}
566
567 {{< sc2 >}}:+1:{{< /sc2 >}}
568
569 {{% sc3 %}}:-1:{{% /sc3 %}}
570
571 -- content/p2.md --
572 ---
573 title: "p2"
574 ---
575 :heavy_check_mark:
576 -- layouts/_shortcodes/include.html --
577 {{ $p := site.GetPage (.Get 0) }}
578 {{ $p.RenderShortcodes }}
579 -- layouts/_shortcodes/sc1.html --
580 sc1_begin|{{ .Inner }}|sc1_end
581 -- layouts/_shortcodes/sc2.html --
582 sc2_begin|{{ .Inner | .Page.RenderString }}|sc2_end
583 -- layouts/_shortcodes/sc3.html --
584 sc3_begin|{{ .Inner }}|sc3_end
585 -- layouts/single.html --
586 {{ .Content }}
587 `
588
589 b := hugolib.Test(t, files)
590
591 b.AssertFileContentExact("public/p1/index.html",
592 // Issue #7332
593 "<span>:x:\n</span>",
594 // Issue #11587
595 "<p>&#x2714;&#xfe0f;</p>",
596 // Should not be converted to emoji
597 "sc1_begin|:smiley:|sc1_end",
598 // Should be converted to emoji
599 "sc2_begin|&#x1f44d;|sc2_end",
600 // Should be converted to emoji
601 "sc3_begin|&#x1f44e;|sc3_end",
602 )
603 }
604
605 func TestEmojiDisabled(t *testing.T) {
606 t.Parallel()
607
608 files := `
609 -- hugo.toml --
610 enableEmoji = false
611 -- content/p1.md --
612 ---
613 title: "p1"
614 ---
615 :x:
616 -- layouts/single.html --
617 {{ .Content }}
618 `
619
620 b := hugolib.Test(t, files)
621
622 b.AssertFileContentExact("public/p1/index.html", "<p>:x:</p>")
623 }
624
625 func TestEmojiDefaultConfig(t *testing.T) {
626 t.Parallel()
627
628 files := `
629 -- content/p1.md --
630 ---
631 title: "p1"
632 ---
633 :x:
634 -- layouts/single.html --
635 {{ .Content }}
636 `
637
638 b := hugolib.Test(t, files)
639
640 b.AssertFileContentExact("public/p1/index.html", "<p>:x:</p>")
641 }
642
643 // Issue #5748
644 func TestGoldmarkTemplateDelims(t *testing.T) {
645 t.Parallel()
646
647 files := `
648 -- hugo.toml --
649 [minify]
650 minifyOutput = true
651 [minify.tdewolff.html]
652 templateDelims = ["<?php","?>"]
653 -- layouts/home.html --
654 <div class="foo">
655 {{ safeHTML "<?php" }}
656 echo "hello";
657 {{ safeHTML "?>" }}
658 </div>
659 `
660
661 b := hugolib.Test(t, files)
662 b.AssertFileContent("public/index.html", "<div class=foo><?php\necho \"hello\";\n?>\n</div>")
663 }
664
665 // Issue #10894
666 func TestPassthroughInlineFences(t *testing.T) {
667 t.Parallel()
668
669 files := `
670 -- hugo.toml --
671 [markup.goldmark.extensions.passthrough]
672 enable = true
673 [markup.goldmark.extensions.passthrough.delimiters]
674 inline = [['$', '$'], ['\(', '\)']]
675 -- content/p1.md --
676 ---
677 title: "p1"
678 ---
679 ## LaTeX test
680
681 Inline equation that would be mangled by default parser: $a^*=x-b^*$
682
683 -- layouts/single.html --
684 {{ .Content }}
685 `
686
687 b := hugolib.Test(t, files)
688 b.AssertFileContent("public/p1/index.html", `
689 $a^*=x-b^*$
690 `)
691 }
692
693 func TestPassthroughBlockFences(t *testing.T) {
694 t.Parallel()
695
696 files := `
697 -- hugo.toml --
698 [markup.goldmark.extensions.passthrough]
699 enable = true
700 [markup.goldmark.extensions.passthrough.delimiters]
701 block = [['$$', '$$']]
702 -- content/p1.md --
703 ---
704 title: "p1"
705 ---
706 ## LaTeX test
707
708 Block equation that would be mangled by default parser:
709
710 $$a^*=x-b^*$$
711
712 -- layouts/single.html --
713 {{ .Content }}
714 `
715
716 b := hugolib.Test(t, files)
717 b.AssertFileContent("public/p1/index.html", `
718 $$a^*=x-b^*$$
719 `)
720 }
721
722 func TestPassthroughWithAlternativeFences(t *testing.T) {
723 t.Parallel()
724
725 files := `
726 -- hugo.toml --
727 [markup.goldmark.extensions.passthrough]
728 enable = true
729 [markup.goldmark.extensions.passthrough.delimiters]
730 inline = [['(((', ')))']]
731 block = [['%!%', '%!%']]
732 -- content/p1.md --
733 ---
734 title: "p1"
735 ---
736 ## LaTeX test
737
738 Inline equation that would be mangled by default parser: (((a^*=x-b^*)))
739 Inline equation that should be mangled by default parser: $a^*=x-b^*$
740
741 Block equation that would be mangled by default parser:
742
743 %!%
744 a^*=x-b^*
745 %!%
746
747 -- layouts/single.html --
748 {{ .Content }}
749 `
750
751 b := hugolib.Test(t, files)
752 b.AssertFileContent("public/p1/index.html", `
753 (((a^*=x-b^*)))
754 `)
755 b.AssertFileContent("public/p1/index.html", `
756 $a^<em>=x-b^</em>$
757 `)
758 b.AssertFileContent("public/p1/index.html", `
759 %!%
760 a^*=x-b^*
761 %!%
762 `)
763 }
764
765 func TestExtrasExtension(t *testing.T) {
766 t.Parallel()
767
768 files := `
769 -- hugo.toml --
770 disableKinds = ['page','rss','section','sitemap','taxonomy','term']
771 [markup.goldmark.extensions]
772 strikethrough = false
773 [markup.goldmark.extensions.extras.delete]
774 enable = false
775 [markup.goldmark.extensions.extras.insert]
776 enable = false
777 [markup.goldmark.extensions.extras.mark]
778 enable = false
779 [markup.goldmark.extensions.extras.subscript]
780 enable = false
781 [markup.goldmark.extensions.extras.superscript]
782 enable = false
783 -- layouts/home.html --
784 {{ .Content }}
785 -- content/_index.md --
786 ---
787 title: home
788 ---
789 ~~delete~~
790
791 ++insert++
792
793 ==mark==
794
795 H~2~0
796
797 1^st^
798 `
799
800 b := hugolib.Test(t, files)
801
802 b.AssertFileContent("public/index.html",
803 "<p>~~delete~~</p>",
804 "<p>++insert++</p>",
805 "<p>==mark==</p>",
806 "<p>H~2~0</p>",
807 "<p>1^st^</p>",
808 )
809
810 files = strings.ReplaceAll(files, "enable = false", "enable = true")
811
812 b = hugolib.Test(t, files)
813
814 b.AssertFileContent("public/index.html",
815 "<p><del>delete</del></p>",
816 "<p><ins>insert</ins></p>",
817 "<p><mark>mark</mark></p>",
818 "<p>H<sub>2</sub>0</p>",
819 "<p>1<sup>st</sup></p>",
820 )
821 }
822
823 // Issue 12997.
824 func TestGoldmarkRawHTMLWarningBlocks(t *testing.T) {
825 files := `
826 -- hugo.toml --
827 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
828 markup.goldmark.renderer.unsafe = false
829 -- content/p1.md --
830 ---
831 title: "p1"
832 ---
833 <div>Some raw HTML</div>
834 -- layouts/single.html --
835 {{ .Content }}
836 `
837
838 b := hugolib.Test(t, files, hugolib.TestOptWarn())
839
840 b.AssertFileContent("public/p1/index.html", "<!-- raw HTML omitted -->")
841 b.AssertLogContains("WARN Raw HTML omitted while rendering \"/content/p1.md\"; see https://gohugo.io/getting-started/configuration-markup/#rendererunsafe\nYou can suppress this warning by adding the following to your project configuration:\nignoreLogs = ['warning-goldmark-raw-html']")
842
843 b = hugolib.Test(t, strings.ReplaceAll(files, "markup.goldmark.renderer.unsafe = false", "markup.goldmark.renderer.unsafe = true"), hugolib.TestOptWarn())
844 b.AssertFileContent("public/p1/index.html", "! <!-- raw HTML omitted -->")
845 b.AssertLogContains("! WARN")
846 }
847
848 func TestGoldmarkRawHTMLWarningInline(t *testing.T) {
849 files := `
850 -- hugo.toml --
851 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
852 markup.goldmark.renderer.unsafe = false
853 -- content/p1.md --
854 ---
855 title: "p1"
856 ---
857 <em>raw HTML</em>
858 -- layouts/single.html --
859 {{ .Content }}
860 `
861
862 b := hugolib.Test(t, files, hugolib.TestOptWarn())
863
864 b.AssertFileContent("public/p1/index.html", "<!-- raw HTML omitted -->")
865 b.AssertLogContains("WARN Raw HTML omitted while rendering \"/content/p1.md\"; see https://gohugo.io/getting-started/configuration-markup/#rendererunsafe\nYou can suppress this warning by adding the following to your project configuration:\nignoreLogs = ['warning-goldmark-raw-html']")
866
867 b = hugolib.Test(t, strings.ReplaceAll(files, "markup.goldmark.renderer.unsafe = false", "markup.goldmark.renderer.unsafe = true"), hugolib.TestOptWarn())
868 b.AssertFileContent("public/p1/index.html", "! <!-- raw HTML omitted -->")
869 b.AssertLogContains("! WARN")
870 }
871
872 // See https://github.com/gohugoio/hugo/issues/13278#issuecomment-2603280548
873 func TestGoldmarkRawHTMLCommentNoWarning(t *testing.T) {
874 files := `
875 -- hugo.toml --
876 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
877 markup.goldmark.renderer.unsafe = false
878 -- content/p1.md --
879 ---
880 title: "p1"
881 ---
882 # HTML comments
883
884 ## Simple
885 <!-- This is a comment -->
886
887 <!-- This is a comment indented -->
888
889 **Hello**<!-- This is a comment indented with markup surrounding. -->_world_.
890 ## With HTML
891
892 <!-- <p>This is another paragraph </p> -->
893
894 ## With HTML and JS
895
896 <!-- <script>alert('hello');</script> -->
897
898 ## With Block
899
900 <!--
901 <p>Look at this cool image:</p>
902 <img border="0" src="pic_trulli.jpg" alt="Trulli">
903 -->
904
905 ## XSS
906
907 <!-- --><script>alert("I just escaped the HTML comment")</script><!-- -->
908
909
910 ## More
911
912 This is a <!--hidden--> word.
913
914 This is a <!-- hidden--> word.
915
916 This is a <!-- hidden --> word.
917
918 This is a <!--
919 hidden --> word.
920
921 This is a <!--
922 hidden
923 --> word.
924
925
926 -- layouts/single.html --
927 {{ .Content }}
928 `
929
930 b := hugolib.Test(t, files, hugolib.TestOptWarn())
931
932 b.AssertFileContent("public/p1/index.html",
933 "! <!-- raw HTML omitted -->",
934 "! <!-- hidden -->",
935 "! <!-- This is a comment -->",
936 "! script",
937 )
938 b.AssertLogContains("! WARN")
939
940 b = hugolib.Test(t, strings.ReplaceAll(files, "markup.goldmark.renderer.unsafe = false", "markup.goldmark.renderer.unsafe = true"), hugolib.TestOptWarn())
941 b.AssertFileContent("public/p1/index.html",
942 "! <!-- raw HTML omitted -->",
943 "<!-- hidden -->",
944 "<!-- This is a comment -->",
945 )
946 b.AssertLogContains("! WARN")
947 }
948
949 func TestFootnoteExtension(t *testing.T) {
950 t.Parallel()
951
952 files := `
953 -- hugo.toml --
954 disableKinds = ['home','rss','section','sitemap','taxonomy','term']
955 [markup.goldmark.extensions.footnote]
956 enable = false
957 enableAutoIDPrefix = false
958 TBD = 'back'
959 -- layouts/all.html --
960 {{ .Content }}
961 -- content/p1.md --
962 ---
963 title: p1
964 ---
965 foo[^1] and bar[^2]
966
967 [^1]: footnote one
968 [^2]: footnote two
969 -- content/p2.md --
970 ---
971 title: p2
972 ---
973 foo[^1] and bar[^2]
974
975 [^1]: footnote one
976 [^2]: footnote two
977 -- content/_content.gotmpl --
978 {{ range slice 3 4 }}
979 {{ $page := dict
980 "content" (dict "mediaType" "text/markdown" "value" "foo[^1] and bar[^2]\n\n[^1]: footnote one\n[^2]: footnote two")
981 "path" (printf "p%d" .)
982 "title" (printf "p%d" .)
983 }}
984 {{ $.AddPage $page }}
985 {{ end }}
986 `
987
988 // test enableAutoIDPrefix
989
990 want := "<p>foo[^1] and bar[^2]</p>\n<p>[^1]: footnote one\n[^2]: footnote two</p>"
991 b := hugolib.Test(t, files)
992 b.AssertFileContent("public/p1/index.html", want)
993 b.AssertFileContent("public/p2/index.html", want)
994 b.AssertFileContent("public/p3/index.html", want)
995 b.AssertFileContent("public/p4/index.html", want)
996
997 files = strings.ReplaceAll(files, "enable = false", "enable = true")
998 want = "<p>foo<sup id=\"fnref:1\"><a href=\"#fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup> and bar<sup id=\"fnref:2\"><a href=\"#fn:2\" class=\"footnote-ref\" role=\"doc-noteref\">2</a></sup></p>\n<div class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"fn:1\">\n<p>footnote one&#160;<a href=\"#fnref:1\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;</a></p>\n</li>\n<li id=\"fn:2\">\n<p>footnote two&#160;<a href=\"#fnref:2\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;</a></p>\n</li>\n</ol>\n</div>"
999 b = hugolib.Test(t, files)
1000 b.AssertFileContent("public/p1/index.html", want)
1001 b.AssertFileContent("public/p2/index.html", want)
1002 b.AssertFileContent("public/p3/index.html", want)
1003 b.AssertFileContent("public/p4/index.html", want)
1004
1005 files = strings.ReplaceAll(files, "enableAutoIDPrefix = false", "enableAutoIDPrefix = true")
1006 b = hugolib.Test(t, files)
1007 b.AssertFileContent("public/p1/index.html",
1008 "<p>foo<sup id=\"hb5cdcabc9e678612fnref:1\"><a href=\"#hb5cdcabc9e678612fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup> and bar<sup id=\"hb5cdcabc9e678612fnref:2\"><a href=\"#hb5cdcabc9e678612fn:2\" class=\"footnote-ref\" role=\"doc-noteref\">2</a></sup></p>\n<div class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"hb5cdcabc9e678612fn:1\">\n<p>footnote one&#160;<a href=\"#hb5cdcabc9e678612fnref:1\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;</a></p>\n</li>\n<li id=\"hb5cdcabc9e678612fn:2\">\n<p>footnote two&#160;<a href=\"#hb5cdcabc9e678612fnref:2\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;</a></p>\n</li>\n</ol>\n</div>",
1009 )
1010 b.AssertFileContent("public/p2/index.html",
1011 "<p>foo<sup id=\"h58e8265a0c07b195fnref:1\"><a href=\"#h58e8265a0c07b195fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup> and bar<sup id=\"h58e8265a0c07b195fnref:2\"><a href=\"#h58e8265a0c07b195fn:2\" class=\"footnote-ref\" role=\"doc-noteref\">2</a></sup></p>\n<div class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"h58e8265a0c07b195fn:1\">\n<p>footnote one&#160;<a href=\"#h58e8265a0c07b195fnref:1\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;</a></p>\n</li>\n<li id=\"h58e8265a0c07b195fn:2\">\n<p>footnote two&#160;<a href=\"#h58e8265a0c07b195fnref:2\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;</a></p>\n</li>\n</ol>\n</div>",
1012 )
1013 b.AssertFileContent("public/p3/index.html",
1014 "<p>foo<sup id=\"h0aab769290d7e233fnref:1\"><a href=\"#h0aab769290d7e233fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup> and bar<sup id=\"h0aab769290d7e233fnref:2\"><a href=\"#h0aab769290d7e233fn:2\" class=\"footnote-ref\" role=\"doc-noteref\">2</a></sup></p>\n<div class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"h0aab769290d7e233fn:1\">\n<p>footnote one&#160;<a href=\"#h0aab769290d7e233fnref:1\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;</a></p>\n</li>\n<li id=\"h0aab769290d7e233fn:2\">\n<p>footnote two&#160;<a href=\"#h0aab769290d7e233fnref:2\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;</a></p>\n</li>\n</ol>\n</div>",
1015 )
1016 b.AssertFileContent("public/p4/index.html",
1017 "<p>foo<sup id=\"ha35b794ad6e8626cfnref:1\"><a href=\"#ha35b794ad6e8626cfn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup> and bar<sup id=\"ha35b794ad6e8626cfnref:2\"><a href=\"#ha35b794ad6e8626cfn:2\" class=\"footnote-ref\" role=\"doc-noteref\">2</a></sup></p>\n<div class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"ha35b794ad6e8626cfn:1\">\n<p>footnote one&#160;<a href=\"#ha35b794ad6e8626cfnref:1\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;</a></p>\n</li>\n<li id=\"ha35b794ad6e8626cfn:2\">\n<p>footnote two&#160;<a href=\"#ha35b794ad6e8626cfnref:2\" class=\"footnote-backref\" role=\"doc-backlink\">&#x21a9;&#xfe0e;</a></p>\n</li>\n</ol>\n</div>",
1018 )
1019
1020 // test backlinkHTML
1021
1022 files = strings.ReplaceAll(files, "TBD", "backlinkHTML")
1023 b = hugolib.Test(t, files)
1024 b.AssertFileContent("public/p1/index.html",
1025 "<p>foo<sup id=\"hb5cdcabc9e678612fnref:1\"><a href=\"#hb5cdcabc9e678612fn:1\" class=\"footnote-ref\" role=\"doc-noteref\">1</a></sup> and bar<sup id=\"hb5cdcabc9e678612fnref:2\"><a href=\"#hb5cdcabc9e678612fn:2\" class=\"footnote-ref\" role=\"doc-noteref\">2</a></sup></p>\n<div class=\"footnotes\" role=\"doc-endnotes\">\n<hr>\n<ol>\n<li id=\"hb5cdcabc9e678612fn:1\">\n<p>footnote one&#160;<a href=\"#hb5cdcabc9e678612fnref:1\" class=\"footnote-backref\" role=\"doc-backlink\">back</a></p>\n</li>\n<li id=\"hb5cdcabc9e678612fn:2\">\n<p>footnote two&#160;<a href=\"#hb5cdcabc9e678612fnref:2\" class=\"footnote-backref\" role=\"doc-backlink\">back</a></p>\n</li>\n</ol>\n</div>",
1026 )
1027 }
1028
1029 func TestRenderLinkDefaultDangerous(t *testing.T) {
1030 t.Parallel()
1031
1032 files := `
1033 -- content/p1.md --
1034 ---
1035 title: "p1"
1036 ---
1037 Link: [Click me](&#106;avascript:alert(1))
1038 Image: ![alt](&#106;avascript:alert(2))
1039 -- layouts/all.html --
1040 Content: {{ .Content }}
1041 `
1042
1043 b := hugolib.Test(t, files)
1044
1045 b.AssertFileContent("public/p1/index.html",
1046 `! alert(1)"`,
1047 `! alert(2)"`,
1048 )
1049 }
1050
1051 // Issue 14715
1052 func TestRenderLinkDefaultAmpersand(t *testing.T) {
1053 t.Parallel()
1054
1055 files := `
1056 -- content/_index.md --
1057 ---
1058 title: "Home"
1059 ---
1060 [foo](https://a.com/?a=1&b=2)
1061 -- layouts/home.html --
1062 {{ .Content }}
1063 `
1064
1065 b := hugolib.Test(t, files)
1066
1067 b.AssertFileContent("public/index.html",
1068 `<a href="https://a.com/?a=1&amp;b=2">foo</a>`,
1069 )
1070 }