@@ -10,6 +10,7 @@ import (
10
10
"regexp"
11
11
"strings"
12
12
13
+ "code.gitea.io/gitea/modules/log"
13
14
"code.gitea.io/gitea/modules/markup"
14
15
"code.gitea.io/gitea/modules/markup/common"
15
16
"code.gitea.io/gitea/modules/setting"
@@ -101,11 +102,41 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
101
102
parent := n .Parent ()
102
103
// Create a link around image only if parent is not already a link
103
104
if _ , ok := parent .(* ast.Link ); ! ok && parent != nil {
105
+ next := n .NextSibling ()
106
+
107
+ // Create a link wrapper
104
108
wrap := ast .NewLink ()
105
109
wrap .Destination = link
106
110
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
107
132
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
+
109
140
}
110
141
case * ast.Link :
111
142
// Links need their href to munged to be a real value
0 commit comments