Skip to content

Commit

Permalink
fix: media links in org files not liked to media files (#12997)
Browse files Browse the repository at this point in the history
* 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 <art27@cantab.net>

Co-authored-by: zeripath <art27@cantab.net>
  • Loading branch information
Pranav Nachnekar and zeripath authored Oct 1, 2020
1 parent 1d2553a commit 1827f89
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions modules/markup/orgmode/orgmode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"fmt"
"html"
"strings"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
Expand Down Expand Up @@ -94,10 +95,23 @@ func (r *Renderer) WriteRegularLink(l org.RegularLink) {
}
switch l.Kind() {
case "image":
r.WriteString(fmt.Sprintf(`<img src="%s" alt="%s" title="%s" />`, link, description, description))
imageSrc := getMediaURL(link)
fmt.Fprintf(r, `<img src="%s" alt="%s" title="%s" />`, imageSrc, description, description)
case "video":
r.WriteString(fmt.Sprintf(`<video src="%s" title="%s">%s</video>`, link, description, description))
videoSrc := getMediaURL(link)
fmt.Fprintf(r, `<video src="%s" title="%s">%s</video>`, videoSrc, description, description)
default:
r.WriteString(fmt.Sprintf(`<a href="%s" title="%s">%s</a>`, link, description, description))
fmt.Fprintf(r, `<a href="%s" title="%s">%s</a>`, 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
}

0 comments on commit 1827f89

Please sign in to comment.