mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	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 <art27@cantab.net> Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
		@@ -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(`<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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user