Skip to content

Commit

Permalink
Support relative paths to videos from Wiki pages
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sergeyvfx committed May 23, 2024
1 parent 7ab0988 commit 2da0e37
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,17 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node) {
attr.Val = camoHandleLink(attr.Val)
node.Attr[i] = attr
}
} else if node.Data == "video" {
for i, attr := range node.Attr {
if attr.Key != "src" {
continue
}
if len(attr.Val) > 0 && !IsFullURLString(attr.Val) {
attr.Val = util.URLJoin(ctx.Links.ResolveMediaLink(ctx.IsWiki), attr.Val)
}
attr.Val = camoHandleLink(attr.Val)
node.Attr[i] = attr
}
} else if node.Data == "a" {
// Restrict text in links to emojis
procs = emojiProcessors
Expand Down
40 changes: 40 additions & 0 deletions modules/markup/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,46 @@ func TestRender_RelativeImages(t *testing.T) {
`<img src="`+util.URLJoin(rawwiki, "icon.png")+`"/>`)
}

func TestRender_RelativeVideos(t *testing.T) {
setting.AppURL = markup.TestAppURL

test := func(input, expected, expectedWiki string) {
buffer, err := markdown.RenderString(&markup.RenderContext{
Ctx: git.DefaultContext,
Links: markup.Links{
Base: markup.TestRepoURL,
BranchPath: "master",
},
Metas: localMetas,
}, input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
buffer, err = markdown.RenderString(&markup.RenderContext{
Ctx: git.DefaultContext,
Links: markup.Links{
Base: markup.TestRepoURL,
},
Metas: localMetas,
IsWiki: true,
}, input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(string(buffer)))
}

rawwiki := util.URLJoin(markup.TestRepoURL, "wiki", "raw")
mediatree := util.URLJoin(markup.TestRepoURL, "media", "master")

test(
`<video src="Link">`,
`<video src="`+util.URLJoin(mediatree, "Link")+`"></video>`,
`<video src="`+util.URLJoin(rawwiki, "Link")+`"></video>`)

test(
`<video src="./video.mp4">`,
`<video src="`+util.URLJoin(mediatree, "video.mp4")+`"></video>`,
`<video src="`+util.URLJoin(rawwiki, "video.mp4")+`"></video>`)
}

func Test_ParseClusterFuzz(t *testing.T) {
setting.AppURL = markup.TestAppURL

Expand Down

0 comments on commit 2da0e37

Please sign in to comment.