@@ -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
0 commit comments