mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-26 22:04:32 +02:00
markup/goldmark: Add removeSurroundingParagraph for Markdown images
* Removes any surrounding paragraph nodes * And transfers any attributes from the surrounding paragraph down to the image node * Adds IsBlock and Ordinal (zero based) field to the image context passed to the image render hooks IsBlock is set to true if `wrapStandAloneImageWithinParagraph = false` and the image's parent node has only one child. Closes #8362 Fixes #10492 Fixes #10494 Fixes #10501
This commit is contained in:
113
markup/goldmark/links/integration_test.go
Normal file
113
markup/goldmark/links/integration_test.go
Normal file
@@ -0,0 +1,113 @@
|
||||
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: . Some more text.
|
||||
|
||||

|
||||
{.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>")
|
||||
})
|
||||
|
||||
}
|
Reference in New Issue
Block a user