diff --git a/modules/markup/html_commit.go b/modules/markup/html_commit.go
index 0a9b329589e..c319374a38d 100644
--- a/modules/markup/html_commit.go
+++ b/modules/markup/html_commit.go
@@ -8,6 +8,7 @@ import (
"strings"
"code.gitea.io/gitea/modules/base"
+ "code.gitea.io/gitea/modules/httplib"
"code.gitea.io/gitea/modules/references"
"code.gitea.io/gitea/modules/util"
@@ -121,6 +122,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 !httplib.IsCurrentGiteaSiteURL(ctx, ret.FullURL) {
+ node = node.NextSibling
+ continue
+ }
replaceContent(node, ret.PosStart, ret.PosEnd, createCodeLink(ret.FullURL, text, "commit"))
node = node.NextSibling.NextSibling
}
@@ -167,6 +173,12 @@ func comparePatternProcessor(ctx *RenderContext, node *html.Node) {
}
}
+ // only turn compare links to the current instance into hash link
+ if !httplib.IsCurrentGiteaSiteURL(ctx, urlFull) {
+ node = node.NextSibling
+ continue
+ }
+
text := text1 + textDots + text2
if hash != "" {
text += " (" + hash + ")"
diff --git a/modules/markup/html_internal_test.go b/modules/markup/html_internal_test.go
index 467cc509d0b..ca2579c8ead 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 76013ccd13d..5f873d29852 100644
--- a/modules/markup/html_test.go
+++ b/modules/markup/html_test.go
@@ -71,6 +71,7 @@ func TestRender_Commits(t *testing.T) {
}
func TestRender_CrossReferences(t *testing.T) {
+ defer testModule.MockVariableValue(&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")
@@ -98,17 +99,17 @@ func TestRender_CrossReferences(t *testing.T) {
util.URLJoin(markup.TestAppURL, "gogitea", "some-repo-name", "issues", "12345"),
`
783b039...da951ce`, res.String())
+ assert.Equal(t, `783b039...da951ce`, res.String())
}
func TestIsFullURL(t *testing.T) {
diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go
index 47b293e1e9b..8d6b3b3c80e 100644
--- a/modules/markup/markdown/markdown_test.go
+++ b/modules/markup/markdown/markdown_test.go
@@ -483,6 +483,9 @@ foo: bar
}
func TestRenderLinks(t *testing.T) {
+ defer test.MockVariableValue(&setting.AppURL, AppURL)()
+ defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
+
input := ` space @mention-user${SPACE}${SPACE}
/just/a/path.bin
https://example.com/file.bin
@@ -520,9 +523,9 @@ mail@domain.com
-88fc37a3c0...12fc37a3c0 (hash)
+https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
-88fc37a3c0
+https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
👍
mail@domain.com
@@ -530,10 +533,21 @@ com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
#123
space
`
- defer test.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
result, err := markdown.RenderString(markup.NewTestRenderContext(localMetas), input)
assert.NoError(t, err)
assert.Equal(t, expected, string(result))
+
+ t.Run("LocalCommitAndCompare", func(t *testing.T) {
+ input := `http://localhost:3000/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
+http://localhost:3000/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash`
+
+ expected := `
+`
+ result, err := markdown.RenderString(markup.NewTestRenderContext(localMetas), input)
+ assert.NoError(t, err)
+ assert.Equal(t, expected, string(result))
+ })
}
func TestMarkdownLink(t *testing.T) {