summaryrefslogtreecommitdiffstats
path: root/markup/goldmark/images/integration_test.go
blob: 8b0ba99c144e16196cc5fe2955abdf225524aefe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package images_test

import (
	"strings"
	"testing"

	"github.com/gohugoio/hugo/hugolib"
)

func TestDisableWrapStandAloneImageWithinParagraph(t *testing.T) {
	t.Parallel()

	filesTemplate := `
-- config.toml --
[markup.goldmark.renderer]
	unsafe = false
[markup.goldmark.parser]
wrapStandAloneImageWithinParagraph = CONFIG_VALUE
[markup.goldmark.parser.attribute]
	block = true
	title = true
-- content/p1.md --
---
title: "p1"
---

This is an inline image: ![Inline Image](/inline.jpg). Some more text.

![Block Image](/block.jpg)
{.b}


-- layouts/_default/single.html --
{{ .Content }}
`

	t.Run("With Hook, no wrap", func(t *testing.T) {
		files := strings.ReplaceAll(filesTemplate, "CONFIG_VALUE", "false")
		files = files + `-- layouts/_default/_markup/render-image.html --
{{ if .IsBlock }}
<figure class="{{ .Attributes.class }}">
	<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}|{{ .Ordinal }}" />
</figure>
{{ else }}
	<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}|{{ .Ordinal }}" />
{{ end }}
`
		b := hugolib.NewIntegrationTestBuilder(
			hugolib.IntegrationTestConfig{
				T:           t,
				TxtarString: files,
				NeedsOsFS:   false,
			},
		).Build()

		b.AssertFileContent("public/p1/index.html",
			"This is an inline image: \n\t<img src=\"/inline.jpg\" alt=\"Inline Image|0\" />\n. Some more text.</p>",
			"<figure class=\"b\">\n\t<img src=\"/block.jpg\" alt=\"Block Image|1\" />",
		)
	})

	t.Run("With Hook, wrap", func(t *testing.T) {
		files := strings.ReplaceAll(filesTemplate, "CONFIG_VALUE", "true")
		files = files + `-- layouts/_default/_markup/render-image.html --
{{ if .IsBlock }}
<figure class="{{ .Attributes.class }}">
	<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" />
</figure>
{{ else }}
	<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" />
{{ end }}
`
		b := hugolib.NewIntegrationTestBuilder(
			hugolib.IntegrationTestConfig{
				T:           t,
				TxtarString: files,
				NeedsOsFS:   false,
			},
		).Build()

		b.AssertFileContent("public/p1/index.html",
			"This is an inline image: \n\t<img src=\"/inline.jpg\" alt=\"Inline Image\" />\n. Some more text.</p>",
			"<p class=\"b\">\n\t<img src=\"/block.jpg\" alt=\"Block Image\" />\n</p>",
		)
	})

	t.Run("No Hook, no wrap", func(t *testing.T) {
		files := strings.ReplaceAll(filesTemplate, "CONFIG_VALUE", "false")
		b := hugolib.NewIntegrationTestBuilder(
			hugolib.IntegrationTestConfig{
				T:           t,
				TxtarString: files,
				NeedsOsFS:   false,
			},
		).Build()

		b.AssertFileContent("public/p1/index.html", "<p>This is an inline image: <img src=\"/inline.jpg\" alt=\"Inline Image\">. Some more text.</p>\n<img src=\"/block.jpg\" alt=\"Block Image\" class=\"b\">")
	})

	t.Run("No Hook, wrap", func(t *testing.T) {
		files := strings.ReplaceAll(filesTemplate, "CONFIG_VALUE", "true")
		b := hugolib.NewIntegrationTestBuilder(
			hugolib.IntegrationTestConfig{
				T:           t,
				TxtarString: files,
				NeedsOsFS:   false,
			},
		).Build()

		b.AssertFileContent("public/p1/index.html", "<p class=\"b\"><img src=\"/block.jpg\" alt=\"Block Image\"></p>")
	})
}