code.gitea.io/gitea@v1.22.3/modules/markup/html_node.go (about) 1 // Copyright 2024 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package markup 5 6 import ( 7 "code.gitea.io/gitea/modules/util" 8 9 "golang.org/x/net/html" 10 ) 11 12 func visitNodeImg(ctx *RenderContext, img *html.Node) (next *html.Node) { 13 next = img.NextSibling 14 for i, attr := range img.Attr { 15 if attr.Key != "src" { 16 continue 17 } 18 19 if IsNonEmptyRelativePath(attr.Val) { 20 attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsWiki), attr.Val) 21 22 // By default, the "<img>" tag should also be clickable, 23 // because frontend use `<img>` to paste the re-scaled image into the markdown, 24 // so it must match the default markdown image behavior. 25 hasParentAnchor := false 26 for p := img.Parent; p != nil; p = p.Parent { 27 if hasParentAnchor = p.Type == html.ElementNode && p.Data == "a"; hasParentAnchor { 28 break 29 } 30 } 31 if !hasParentAnchor { 32 imgA := &html.Node{Type: html.ElementNode, Data: "a", Attr: []html.Attribute{ 33 {Key: "href", Val: attr.Val}, 34 {Key: "target", Val: "_blank"}, 35 }} 36 parent := img.Parent 37 imgNext := img.NextSibling 38 parent.RemoveChild(img) 39 parent.InsertBefore(imgA, imgNext) 40 imgA.AppendChild(img) 41 } 42 } 43 attr.Val = camoHandleLink(attr.Val) 44 img.Attr[i] = attr 45 } 46 return next 47 } 48 49 func visitNodeVideo(ctx *RenderContext, node *html.Node) (next *html.Node) { 50 next = node.NextSibling 51 for i, attr := range node.Attr { 52 if attr.Key != "src" { 53 continue 54 } 55 if IsNonEmptyRelativePath(attr.Val) { 56 attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsWiki), attr.Val) 57 } 58 attr.Val = camoHandleLink(attr.Val) 59 node.Attr[i] = attr 60 } 61 return next 62 }