Skip to content

Commit 49b8716

Browse files
Support relative paths to videos from Wiki pages (#31061)
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: wxiaoguang <[email protected]>
1 parent 996037f commit 49b8716

File tree

3 files changed

+83
-43
lines changed

3 files changed

+83
-43
lines changed

modules/markup/html.go

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ func IsFullURLString(link string) bool {
8888
return fullURLPattern.MatchString(link)
8989
}
9090

91+
func IsNonEmptyRelativePath(link string) bool {
92+
return link != "" && !IsFullURLString(link) && link[0] != '/' && link[0] != '?' && link[0] != '#'
93+
}
94+
9195
// regexp for full links to issues/pulls
9296
var issueFullPattern *regexp.Regexp
9397

@@ -358,41 +362,6 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
358362
return nil
359363
}
360364

361-
func handleNodeImg(ctx *RenderContext, img *html.Node) {
362-
for i, attr := range img.Attr {
363-
if attr.Key != "src" {
364-
continue
365-
}
366-
367-
if attr.Val != "" && !IsFullURLString(attr.Val) && !strings.HasPrefix(attr.Val, "/") {
368-
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsWiki), attr.Val)
369-
370-
// By default, the "<img>" tag should also be clickable,
371-
// because frontend use `<img>` to paste the re-scaled image into the markdown,
372-
// so it must match the default markdown image behavior.
373-
hasParentAnchor := false
374-
for p := img.Parent; p != nil; p = p.Parent {
375-
if hasParentAnchor = p.Type == html.ElementNode && p.Data == "a"; hasParentAnchor {
376-
break
377-
}
378-
}
379-
if !hasParentAnchor {
380-
imgA := &html.Node{Type: html.ElementNode, Data: "a", Attr: []html.Attribute{
381-
{Key: "href", Val: attr.Val},
382-
{Key: "target", Val: "_blank"},
383-
}}
384-
parent := img.Parent
385-
imgNext := img.NextSibling
386-
parent.RemoveChild(img)
387-
parent.InsertBefore(imgA, imgNext)
388-
imgA.AppendChild(img)
389-
}
390-
}
391-
attr.Val = camoHandleLink(attr.Val)
392-
img.Attr[i] = attr
393-
}
394-
}
395-
396365
func visitNode(ctx *RenderContext, procs []processor, node *html.Node) *html.Node {
397366
// Add user-content- to IDs and "#" links if they don't already have them
398367
for idx, attr := range node.Attr {
@@ -412,20 +381,20 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node) *html.Nod
412381
}
413382
}
414383

415-
// We ignore code and pre.
416384
switch node.Type {
417385
case html.TextNode:
418386
processTextNodes(ctx, procs, node)
419387
case html.ElementNode:
420-
if node.Data == "img" {
421-
next := node.NextSibling
422-
handleNodeImg(ctx, node)
423-
return next
388+
if node.Data == "code" || node.Data == "pre" {
389+
// ignore code and pre nodes
390+
return node.NextSibling
391+
} else if node.Data == "img" {
392+
return visitNodeImg(ctx, node)
393+
} else if node.Data == "video" {
394+
return visitNodeVideo(ctx, node)
424395
} else if node.Data == "a" {
425396
// Restrict text in links to emojis
426397
procs = emojiProcessors
427-
} else if node.Data == "code" || node.Data == "pre" {
428-
return node.NextSibling
429398
} else if node.Data == "i" {
430399
for _, attr := range node.Attr {
431400
if attr.Key != "class" {

modules/markup/html_node.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

modules/markup/html_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ func TestRender_ShortLinks(t *testing.T) {
522522
`<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`)
523523
}
524524

525-
func TestRender_RelativeImages(t *testing.T) {
525+
func TestRender_RelativeMedias(t *testing.T) {
526526
render := func(input string, isWiki bool, links markup.Links) string {
527527
buffer, err := markdown.RenderString(&markup.RenderContext{
528528
Ctx: git.DefaultContext,
@@ -548,6 +548,15 @@ func TestRender_RelativeImages(t *testing.T) {
548548

549549
out = render(`<img src="/LINK">`, true, markup.Links{Base: "/test-owner/test-repo", BranchPath: "test-branch"})
550550
assert.Equal(t, `<img src="/LINK"/>`, out)
551+
552+
out = render(`<video src="LINK">`, false, markup.Links{Base: "/test-owner/test-repo"})
553+
assert.Equal(t, `<video src="/test-owner/test-repo/LINK"></video>`, out)
554+
555+
out = render(`<video src="LINK">`, true, markup.Links{Base: "/test-owner/test-repo"})
556+
assert.Equal(t, `<video src="/test-owner/test-repo/wiki/raw/LINK"></video>`, out)
557+
558+
out = render(`<video src="/LINK">`, false, markup.Links{Base: "/test-owner/test-repo"})
559+
assert.Equal(t, `<video src="/LINK"></video>`, out)
551560
}
552561

553562
func Test_ParseClusterFuzz(t *testing.T) {

0 commit comments

Comments
 (0)