diff options
author | Giteabot <teabot@gitea.io> | 2024-06-22 05:03:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-22 00:03:31 +0300 |
commit | be302f302596a83ff6bd610a5177def245f96fde (patch) | |
tree | a22829a3c37fc87ec3a475e3eef6133ecae8abc4 /modules/markup/html.go | |
parent | a3529d662f1fd90d59314f518ad225f87f913972 (diff) | |
download | gitea-be302f302596a83ff6bd610a5177def245f96fde.tar.gz gitea-be302f302596a83ff6bd610a5177def245f96fde.zip |
Support relative paths to videos from Wiki pages (#31061) (#31453)
Backport #31061 by @sergeyvfx
This change fixes cases when a Wiki page refers to a video stored in the
Wiki repository using relative path. It follows the similar case which
has been already implemented for images.
Test plan:
- Create repository and Wiki page
- Clone the Wiki repository
- Add video to it, say `video.mp4`
- Modify the markdown file to refer to the video using `<video
src="video.mp4">`
- Commit the Wiki page
- Observe that the video is properly displayed
Co-authored-by: Sergey Sharybin <sergey.vfx@gmail.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'modules/markup/html.go')
-rw-r--r-- | modules/markup/html.go | 53 |
1 files changed, 11 insertions, 42 deletions
diff --git a/modules/markup/html.go b/modules/markup/html.go index 1eedf095a0..d0498074e1 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -88,6 +88,10 @@ func IsFullURLString(link string) bool { return fullURLPattern.MatchString(link) } +func IsNonEmptyRelativePath(link string) bool { + return link != "" && !IsFullURLString(link) && link[0] != '/' && link[0] != '?' && link[0] != '#' +} + // regexp for full links to issues/pulls var issueFullPattern *regexp.Regexp @@ -372,41 +376,6 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output return nil } -func handleNodeImg(ctx *RenderContext, img *html.Node) { - for i, attr := range img.Attr { - if attr.Key != "src" { - continue - } - - if attr.Val != "" && !IsFullURLString(attr.Val) && !strings.HasPrefix(attr.Val, "/") { - attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsWiki), attr.Val) - - // By default, the "<img>" tag should also be clickable, - // because frontend use `<img>` to paste the re-scaled image into the markdown, - // so it must match the default markdown image behavior. - hasParentAnchor := false - for p := img.Parent; p != nil; p = p.Parent { - if hasParentAnchor = p.Type == html.ElementNode && p.Data == "a"; hasParentAnchor { - break - } - } - if !hasParentAnchor { - imgA := &html.Node{Type: html.ElementNode, Data: "a", Attr: []html.Attribute{ - {Key: "href", Val: attr.Val}, - {Key: "target", Val: "_blank"}, - }} - parent := img.Parent - imgNext := img.NextSibling - parent.RemoveChild(img) - parent.InsertBefore(imgA, imgNext) - imgA.AppendChild(img) - } - } - attr.Val = camoHandleLink(attr.Val) - img.Attr[i] = attr - } -} - func visitNode(ctx *RenderContext, procs []processor, node *html.Node) *html.Node { // Add user-content- to IDs and "#" links if they don't already have them for idx, attr := range node.Attr { @@ -426,20 +395,20 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node) *html.Nod } } - // We ignore code and pre. switch node.Type { case html.TextNode: textNode(ctx, procs, node) case html.ElementNode: - if node.Data == "img" { - next := node.NextSibling - handleNodeImg(ctx, node) - return next + if node.Data == "code" || node.Data == "pre" { + // ignore code and pre nodes + return node.NextSibling + } else if node.Data == "img" { + return visitNodeImg(ctx, node) + } else if node.Data == "video" { + return visitNodeVideo(ctx, node) } else if node.Data == "a" { // Restrict text in links to emojis procs = emojiProcessors - } else if node.Data == "code" || node.Data == "pre" { - return node.NextSibling } else if node.Data == "i" { for _, attr := range node.Attr { if attr.Key != "class" { |