From 1827f892de52ececac2ca5dc397a11bfbcc0da43 Mon Sep 17 00:00:00 2001 From: Pranav Nachnekar Date: Thu, 1 Oct 2020 20:52:34 +0530 Subject: [PATCH] fix: media links in org files not liked to media files (#12997) * fix: media links in org files not liked to media files * fix: write directly to io.Writer r as suggested by code review Co-authored-by: zeripath Co-authored-by: zeripath --- modules/markup/orgmode/orgmode.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/modules/markup/orgmode/orgmode.go b/modules/markup/orgmode/orgmode.go index 86222cc88..ddd445aba 100644 --- a/modules/markup/orgmode/orgmode.go +++ b/modules/markup/orgmode/orgmode.go @@ -8,6 +8,7 @@ import ( "bytes" "fmt" "html" + "strings" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" @@ -94,10 +95,23 @@ func (r *Renderer) WriteRegularLink(l org.RegularLink) { } switch l.Kind() { case "image": - r.WriteString(fmt.Sprintf(`%s`, link, description, description)) + imageSrc := getMediaURL(link) + fmt.Fprintf(r, `%s`, imageSrc, description, description) case "video": - r.WriteString(fmt.Sprintf(``, link, description, description)) + videoSrc := getMediaURL(link) + fmt.Fprintf(r, ``, videoSrc, description, description) default: - r.WriteString(fmt.Sprintf(`%s`, link, description, description)) + fmt.Fprintf(r, `%s`, link, description, description) } } + +func getMediaURL(l []byte) string { + srcURL := string(l) + + // Check if link is valid + if len(srcURL) > 0 && !markup.IsLink(l) { + srcURL = strings.Replace(srcURL, "/src/", "/media/", 1) + } + + return srcURL +}