]> source.dussan.org Git - gitea.git/commitdiff
Links in markdown should be absolute to the repository not the server (#15088)
authorzeripath <art27@cantab.net>
Sat, 10 Apr 2021 16:26:28 +0000 (17:26 +0100)
committerGitHub <noreply@github.com>
Sat, 10 Apr 2021 16:26:28 +0000 (00:26 +0800)
* Links in markdown should be absolute to the repository not the server

Fix #15075

Signed-off-by: Andrew Thornton <art27@cantab.net>
* match github

Signed-off-by: Andrew Thornton <art27@cantab.net>
* add testcase

Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: 6543 <6543@obermui.de>
modules/markup/markdown/goldmark.go
modules/markup/markdown/markdown_test.go

index 07e2e36e1da44e8cb8218b684719e6ded23076cd..ad77177db439af4fa316ccc9753bb1481a037831 100644 (file)
@@ -98,7 +98,8 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
                                }
                                prefix = strings.Replace(prefix, "/src/", "/media/", 1)
 
-                               lnk := string(link)
+                               lnk := strings.TrimLeft(string(link), "/")
+
                                lnk = giteautil.URLJoin(prefix, lnk)
                                link = []byte(lnk)
                        }
index 5b3ef21fb66b9c0724c86141d0ec4983e4d1db83..0e340763aedd199821a90f286014eb09285dac2c 100644 (file)
@@ -93,6 +93,19 @@ func TestRender_Images(t *testing.T) {
        test(
                "[!["+title+"]("+url+")]("+href+")",
                `<p><a href="`+href+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
+
+       url = "/../../.images/src/02/train.jpg"
+       test(
+               "!["+title+"]("+url+")",
+               `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
+
+       test(
+               "[["+title+"|"+url+"]]",
+               `<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" title="`+title+`" alt="`+title+`"/></a></p>`)
+       test(
+               "[!["+title+"]("+url+")]("+href+")",
+               `<p><a href="`+href+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
+
 }
 
 func testAnswers(baseURLContent, baseURLImages string) []string {