Skip to content

Commit 332090f

Browse files
authored
Merge branch 'master' into format
2 parents f6a31bc + 164e35e commit 332090f

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

modules/markup/markdown/goldmark.go

+32-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"regexp"
1111
"strings"
1212

13+
"code.gitea.io/gitea/modules/log"
1314
"code.gitea.io/gitea/modules/markup"
1415
"code.gitea.io/gitea/modules/markup/common"
1516
"code.gitea.io/gitea/modules/setting"
@@ -101,11 +102,41 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
101102
parent := n.Parent()
102103
// Create a link around image only if parent is not already a link
103104
if _, ok := parent.(*ast.Link); !ok && parent != nil {
105+
next := n.NextSibling()
106+
107+
// Create a link wrapper
104108
wrap := ast.NewLink()
105109
wrap.Destination = link
106110
wrap.Title = v.Title
111+
112+
// Duplicate the current image node
113+
image := ast.NewImage(ast.NewLink())
114+
image.Destination = link
115+
image.Title = v.Title
116+
for _, attr := range v.Attributes() {
117+
image.SetAttribute(attr.Name, attr.Value)
118+
}
119+
for child := v.FirstChild(); child != nil; {
120+
next := child.NextSibling()
121+
image.AppendChild(image, child)
122+
child = next
123+
}
124+
125+
// Append our duplicate image to the wrapper link
126+
wrap.AppendChild(wrap, image)
127+
128+
// Wire in the next sibling
129+
wrap.SetNextSibling(next)
130+
131+
// Replace the current node with the wrapper link
107132
parent.ReplaceChild(parent, n, wrap)
108-
wrap.AppendChild(wrap, n)
133+
134+
// But most importantly ensure the next sibling is still on the old image too
135+
v.SetNextSibling(next)
136+
137+
} else {
138+
log.Debug("ast.Image: %s has parent: %v", link, parent)
139+
109140
}
110141
case *ast.Link:
111142
// Links need their href to munged to be a real value

modules/markup/markdown/markdown_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,14 @@ func TestRender_RenderParagraphs(t *testing.T) {
308308
test(t, "A\n\nB\nC\n", 2)
309309
test(t, "A\n\n\nB\nC\n", 2)
310310
}
311+
312+
func TestRenderSiblingImages_Issue12925(t *testing.T) {
313+
testcase := `![image1](/image1)
314+
![image2](/image2)
315+
`
316+
expected := `<p><a href="/image1" rel="nofollow"><img src="/image1" alt="image1"></a><br>
317+
<a href="/image2" rel="nofollow"><img src="/image2" alt="image2"></a></p>
318+
`
319+
res := string(RenderRaw([]byte(testcase), "", false))
320+
assert.Equal(t, expected, res)
321+
}

0 commit comments

Comments
 (0)