forked from gohugoio/hugo
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
markup/goldmark: Add removeSurroundingParagraph for Markdown images
This both: * Removes any surrounding paragraph nodes * And transfers any attributes from the surrounding paragraph down to the image node Closes gohugoio#8362 Fixes gohugoio#10492
- Loading branch information
Showing
6 changed files
with
158 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package images_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gohugoio/hugo/hugolib" | ||
) | ||
|
||
func TestRemoveSurroundingParagraph(t *testing.T) { | ||
t.Parallel() | ||
|
||
filesTemplate := ` | ||
-- config.toml -- | ||
[markup.goldmark.renderer] | ||
unsafe = false | ||
[markup.goldmark.parser] | ||
[markup.goldmark.parser.image] | ||
removeSurroundingParagraph = true | ||
[markup.goldmark.parser.attribute] | ||
block = true | ||
title = true | ||
-- content/p1.md -- | ||
--- | ||
title: "p1" | ||
--- | ||
![Image](/img.jpg) | ||
{.b} | ||
-- layouts/_default/single.html -- | ||
{{ .Content }} | ||
` | ||
|
||
t.Run("With Hook", func(t *testing.T) { | ||
files := filesTemplate + `-- layouts/_default/_markup/render-image.html -- | ||
<figure class="{{ .Attributes.class }}"> | ||
<img src="{{ .Destination | safeURL }}" alt="{{ .Text }}" /> | ||
</figure> | ||
` | ||
b := hugolib.NewIntegrationTestBuilder( | ||
hugolib.IntegrationTestConfig{ | ||
T: t, | ||
TxtarString: files, | ||
NeedsOsFS: false, | ||
}, | ||
).Build() | ||
|
||
b.AssertFileContent("public/p1/index.html", "<figure class=\"b\">\n <img src=\"/img.jpg\" alt=\"Image\" />\n</figure>") | ||
}) | ||
|
||
t.Run("No Hook", func(t *testing.T) { | ||
b := hugolib.NewIntegrationTestBuilder( | ||
hugolib.IntegrationTestConfig{ | ||
T: t, | ||
TxtarString: filesTemplate, | ||
NeedsOsFS: false, | ||
}, | ||
).Build() | ||
|
||
b.AssertFileContent("public/p1/index.html", `<img src="/img.jpg" alt="Image" class="b">`) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package images | ||
|
||
import ( | ||
"github.com/yuin/goldmark" | ||
"github.com/yuin/goldmark/ast" | ||
"github.com/yuin/goldmark/parser" | ||
"github.com/yuin/goldmark/text" | ||
"github.com/yuin/goldmark/util" | ||
) | ||
|
||
type ( | ||
imagesExtension struct{} | ||
) | ||
|
||
func New() goldmark.Extender { | ||
return &imagesExtension{} | ||
} | ||
|
||
func (e *imagesExtension) Extend(m goldmark.Markdown) { | ||
m.Parser().AddOptions( | ||
parser.WithASTTransformers( | ||
util.Prioritized(&Transformer{}, 300), | ||
), | ||
) | ||
} | ||
|
||
type Transformer struct { | ||
} | ||
|
||
// Transform transforms the provided Markdown AST. | ||
func (t *Transformer) Transform(doc *ast.Document, reader text.Reader, pctx parser.Context) { | ||
ast.Walk(doc, func(node ast.Node, enter bool) (ast.WalkStatus, error) { | ||
if !enter { | ||
return ast.WalkContinue, nil | ||
} | ||
|
||
if n, ok := node.(*ast.Image); ok { | ||
if p := n.Parent(); p != nil { | ||
if p.Kind() == ast.KindParagraph { | ||
for _, attr := range p.Attributes() { | ||
// Transfer any attribute set down to the image. | ||
// Image elements does not support attributes on its own, | ||
// so it's safe to just set without checking first. | ||
n.SetAttribute(attr.Name, attr.Value) | ||
} | ||
grandParent := p.Parent() | ||
grandParent.ReplaceChild(grandParent, p, n) | ||
} | ||
} | ||
} | ||
|
||
return ast.WalkContinue, nil | ||
|
||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters