-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b6eb46
commit 8dec66e
Showing
10 changed files
with
58 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package markup | ||
|
||
import ( | ||
"path" | ||
|
||
"code.gitea.io/gitea/modules/util" | ||
) | ||
|
||
func ResolveLink(ctx *RenderContext, link, userContentAnchorPrefix string) (result string, resolved bool) { | ||
isAnchorFragment := link != "" && link[0] == '#' | ||
if !isAnchorFragment && !IsFullURLString(link) { | ||
linkBase := ctx.Links.Base | ||
if ctx.IsWiki { | ||
if ext := path.Ext(link); ext == "" || ext == ".-" { | ||
linkBase = ctx.Links.WikiLink() // the link is for a wiki page | ||
} else if DetectMarkupTypeByFileName(link) != "" { | ||
linkBase = ctx.Links.WikiLink() // the link is renderable as a wiki page | ||
} else { | ||
linkBase = ctx.Links.WikiRawLink() // otherwise, use a raw link instead to view&download medias | ||
} | ||
} else if ctx.Links.BranchPath != "" || ctx.Links.TreePath != "" { | ||
// if there is no BranchPath, then the link will be something like "/owner/repo/src/{the-file-path}" | ||
// and then this link will be handled by the "legacy-ref" code and be redirected to the default branch like "/owner/repo/src/branch/main/{the-file-path}" | ||
linkBase = ctx.Links.SrcLink() | ||
} | ||
link, resolved = util.URLJoin(linkBase, link), true | ||
} | ||
if isAnchorFragment && userContentAnchorPrefix != "" { | ||
link, resolved = userContentAnchorPrefix+link[1:], true | ||
} | ||
return link, resolved | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -635,7 +635,7 @@ [email protected] | |
<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) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters