diff --git a/modules/markup/html_commit.go b/modules/markup/html_commit.go index fe7a034967abe..bd1e61d7de330 100644 --- a/modules/markup/html_commit.go +++ b/modules/markup/html_commit.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/references" + "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" "golang.org/x/net/html" @@ -102,6 +103,11 @@ func fullHashPatternProcessor(ctx *RenderContext, node *html.Node) { if ret.QueryHash != "" { text += " (" + ret.QueryHash + ")" } + // only turn commit links to the current instance into hash link + if !strings.HasPrefix(strings.ToLower(ret.FullURL), strings.ToLower(setting.AppURL)) { + node = node.NextSibling + continue + } replaceContent(node, ret.PosStart, ret.PosEnd, createCodeLink(ret.FullURL, text, "commit")) node = node.NextSibling.NextSibling } diff --git a/modules/markup/html_internal_test.go b/modules/markup/html_internal_test.go index 467cc509d0b03..ca2579c8ead10 100644 --- a/modules/markup/html_internal_test.go +++ b/modules/markup/html_internal_test.go @@ -299,7 +299,7 @@ func TestRender_AutoLink(t *testing.T) { // render other commit URLs tmp = "https://external-link.gitea.io/go-gitea/gitea/commit/d8a994ef243349f321568f9e36d5c3f444b99cae#diff-2" - test(tmp, "d8a994ef24 (diff-2)") + test(tmp, ""+tmp+"") } func TestRender_FullIssueURLs(t *testing.T) { diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 08b050baae15c..32dbc11742dbf 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -71,6 +71,8 @@ func TestRender_Commits(t *testing.T) { } func TestRender_CrossReferences(t *testing.T) { + setting.AppURL = markup.TestAppURL + defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)() test := func(input, expected string) { rctx := markup.NewTestRenderContext(markup.TestAppURL, localMetas).WithRelativePath("a.md") @@ -99,6 +101,11 @@ func TestRender_CrossReferences(t *testing.T) { `

gogitea/some-repo-name#12345

`) inputURL := "https://host/a/b/commit/0123456789012345678901234567890123456789/foo.txt?a=b#L2-L3" + test( + inputURL, + `

`+inputURL+`

`) + + inputURL = setting.AppURL + "a/b/commit/0123456789012345678901234567890123456789/foo.txt?a=b#L2-L3" test( inputURL, `

0123456789/foo.txt (L2-L3)

`) diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index 4eb01bcc2da52..35fc2de5fbdbd 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -484,20 +484,20 @@ foo: bar func TestRenderLinks(t *testing.T) { input := ` space @mention-user${SPACE}${SPACE} /just/a/path.bin -https://example.com/file.bin +http://localhost:3000/file.bin [local link](file.bin) -[remote link](https://example.com) +[remote link](http://localhost:3000) [[local link|file.bin]] -[[remote link|https://example.com]] +[[remote link|http://localhost:3000]] ![local image](image.jpg) ![local image](path/file) ![local image](/path/file) -![remote image](https://example.com/image.jpg) +![remote image](http://localhost:3000/image.jpg) [[local image|image.jpg]] -[[remote link|https://example.com/image.jpg]] -https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash +[[remote link|http://localhost:3000/image.jpg]] +http://localhost:3000/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare -https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb +http://localhost:3000/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit :+1: mail@domain.com @@ -508,20 +508,20 @@ mail@domain.com input = strings.ReplaceAll(input, "${SPACE}", " ") // replace ${SPACE} with " ", to avoid some editor's auto-trimming expected := `

space @mention-user
/just/a/path.bin -https://example.com/file.bin +http://localhost:3000/file.bin local link -remote link +remote link local link -remote link +remote link local image local image local image -remote image +remote image local image -remote link -88fc37a3c0...12fc37a3c0 (hash) +remote link +88fc37a3c0...12fc37a3c0 (hash) com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare -88fc37a3c0 +88fc37a3c0 com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit 👍 mail@domain.com