aboutsummaryrefslogtreecommitdiffstats
path: root/modules/markup/markdown
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2024-06-18 11:09:20 +0800
committerGitHub <noreply@github.com>2024-06-18 03:09:20 +0000
commit21783a5752f518e579d8fe48b33504a16674ee17 (patch)
tree20fb13001f82c85d1cb9fc3773b343c21891d569 /modules/markup/markdown
parent37a4b233a0a4ca516b90e0c8e15d8dafb8d13358 (diff)
downloadgitea-21783a5752f518e579d8fe48b33504a16674ee17.tar.gz
gitea-21783a5752f518e579d8fe48b33504a16674ee17.zip
Fix rendered wiki page link (#31398)
Fix #31395
Diffstat (limited to 'modules/markup/markdown')
-rw-r--r--modules/markup/markdown/markdown_test.go10
-rw-r--r--modules/markup/markdown/transform_link.go29
2 files changed, 7 insertions, 32 deletions
diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go
index 8c41ec12e3..9a8c39df0a 100644
--- a/modules/markup/markdown/markdown_test.go
+++ b/modules/markup/markdown/markdown_test.go
@@ -635,7 +635,7 @@ mail@domain.com
<a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a><br/>
<a href="/file.bin" rel="nofollow">local link</a><br/>
<a href="https://example.com" rel="nofollow">remote link</a><br/>
-<a href="/src/file.bin" rel="nofollow">local link</a><br/>
+<a href="/file.bin" rel="nofollow">local link</a><br/>
<a href="https://example.com" rel="nofollow">remote link</a><br/>
<a href="/image.jpg" target="_blank" rel="nofollow noopener"><img src="/image.jpg" alt="local image"/></a><br/>
<a href="/path/file" target="_blank" rel="nofollow noopener"><img src="/path/file" alt="local image"/></a><br/>
@@ -691,7 +691,7 @@ space</p>
<a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a><br/>
<a href="https://gitea.io/file.bin" rel="nofollow">local link</a><br/>
<a href="https://example.com" rel="nofollow">remote link</a><br/>
-<a href="https://gitea.io/src/file.bin" rel="nofollow">local link</a><br/>
+<a href="https://gitea.io/file.bin" rel="nofollow">local link</a><br/>
<a href="https://example.com" rel="nofollow">remote link</a><br/>
<a href="https://gitea.io/image.jpg" target="_blank" rel="nofollow noopener"><img src="https://gitea.io/image.jpg" alt="local image"/></a><br/>
<a href="https://gitea.io/path/file" target="_blank" rel="nofollow noopener"><img src="https://gitea.io/path/file" alt="local image"/></a><br/>
@@ -749,7 +749,7 @@ space</p>
<a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a><br/>
<a href="/relative/path/file.bin" rel="nofollow">local link</a><br/>
<a href="https://example.com" rel="nofollow">remote link</a><br/>
-<a href="/relative/path/src/file.bin" rel="nofollow">local link</a><br/>
+<a href="/relative/path/file.bin" rel="nofollow">local link</a><br/>
<a href="https://example.com" rel="nofollow">remote link</a><br/>
<a href="/relative/path/image.jpg" target="_blank" rel="nofollow noopener"><img src="/relative/path/image.jpg" alt="local image"/></a><br/>
<a href="/relative/path/path/file" target="_blank" rel="nofollow noopener"><img src="/relative/path/path/file" alt="local image"/></a><br/>
@@ -866,7 +866,7 @@ space</p>
Expected: `<p>space @mention-user<br/>
/just/a/path.bin<br/>
<a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a><br/>
-<a href="/user/repo/file.bin" rel="nofollow">local link</a><br/>
+<a href="/user/repo/src/sub/folder/file.bin" rel="nofollow">local link</a><br/>
<a href="https://example.com" rel="nofollow">remote link</a><br/>
<a href="/user/repo/src/sub/folder/file.bin" rel="nofollow">local link</a><br/>
<a href="https://example.com" rel="nofollow">remote link</a><br/>
@@ -984,7 +984,7 @@ space</p>
for i, c := range cases {
result, err := markdown.RenderString(&markup.RenderContext{Ctx: context.Background(), Links: c.Links, IsWiki: c.IsWiki}, input)
assert.NoError(t, err, "Unexpected error in testcase: %v", i)
- assert.Equal(t, template.HTML(c.Expected), result, "Unexpected result in testcase %v", i)
+ assert.Equal(t, c.Expected, string(result), "Unexpected result in testcase %v", i)
}
}
diff --git a/modules/markup/markdown/transform_link.go b/modules/markup/markdown/transform_link.go
index 527a5dfc44..38fbf693ab 100644
--- a/modules/markup/markdown/transform_link.go
+++ b/modules/markup/markdown/transform_link.go
@@ -4,38 +4,13 @@
package markdown
import (
- "path/filepath"
-
"code.gitea.io/gitea/modules/markup"
- giteautil "code.gitea.io/gitea/modules/util"
"github.com/yuin/goldmark/ast"
)
func (g *ASTTransformer) transformLink(ctx *markup.RenderContext, v *ast.Link) {
- // Links need their href to munged to be a real value
- link := v.Destination
- isAnchorFragment := len(link) > 0 && link[0] == '#'
- if !isAnchorFragment && !markup.IsFullURLBytes(link) {
- base := ctx.Links.Base
- if ctx.IsWiki {
- if filepath.Ext(string(link)) == "" {
- // This link doesn't have a file extension - assume a regular wiki link
- base = ctx.Links.WikiLink()
- } else if markup.Type(string(link)) != "" {
- // If it's a file type we can render, use a regular wiki link
- base = ctx.Links.WikiLink()
- } else {
- // Otherwise, use a raw link instead
- base = ctx.Links.WikiRawLink()
- }
- } else if ctx.Links.HasBranchInfo() {
- base = ctx.Links.SrcLink()
- }
- link = []byte(giteautil.URLJoin(base, string(link)))
- }
- if isAnchorFragment {
- link = []byte("#user-content-" + string(link)[1:])
+ if link, resolved := markup.ResolveLink(ctx, string(v.Destination), "#user-content-"); resolved {
+ v.Destination = []byte(link)
}
- v.Destination = link
}