loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Use relative links for commits, mentions, and issues in markdown (#29427)

Fixes #29404

Use relative links for
- commits
- mentions
- issues

---------

Co-authored-by: silverwind <me@silverwind.io>
(cherry picked from commit 85c59d6c21e10ef9d3ccf11713548f50e47e920f)

authored by

KN4CK3R
silverwind
and committed by
Earl Warren
024bfb7f e2abfdc3

+65 -26
+6 -6
modules/markup/html.go
··· 609 609 if ok && strings.Contains(mention, "/") { 610 610 mentionOrgAndTeam := strings.Split(mention, "/") 611 611 if mentionOrgAndTeam[0][1:] == ctx.Metas["org"] && strings.Contains(teams, ","+strings.ToLower(mentionOrgAndTeam[1])+",") { 612 - replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, "org", ctx.Metas["org"], "teams", mentionOrgAndTeam[1]), mention, "mention")) 612 + replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(ctx.Links.Prefix(), "org", ctx.Metas["org"], "teams", mentionOrgAndTeam[1]), mention, "mention")) 613 613 node = node.NextSibling.NextSibling 614 614 start = 0 615 615 continue ··· 620 620 mentionedUsername := mention[1:] 621 621 622 622 if DefaultProcessorHelper.IsUsernameMentionable != nil && DefaultProcessorHelper.IsUsernameMentionable(ctx.Ctx, mentionedUsername) { 623 - replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, mentionedUsername), mention, "mention")) 623 + replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(ctx.Links.Prefix(), mentionedUsername), mention, "mention")) 624 624 node = node.NextSibling.NextSibling 625 625 } else { 626 626 node = node.NextSibling ··· 898 898 path = "pulls" 899 899 } 900 900 if ref.Owner == "" { 901 - link = createLink(util.URLJoin(setting.AppURL, ctx.Metas["user"], ctx.Metas["repo"], path, ref.Issue), reftext, "ref-issue") 901 + link = createLink(util.URLJoin(ctx.Links.Prefix(), ctx.Metas["user"], ctx.Metas["repo"], path, ref.Issue), reftext, "ref-issue") 902 902 } else { 903 - link = createLink(util.URLJoin(setting.AppURL, ref.Owner, ref.Name, path, ref.Issue), reftext, "ref-issue") 903 + link = createLink(util.URLJoin(ctx.Links.Prefix(), ref.Owner, ref.Name, path, ref.Issue), reftext, "ref-issue") 904 904 } 905 905 } 906 906 ··· 939 939 } 940 940 941 941 reftext := ref.Owner + "/" + ref.Name + "@" + base.ShortSha(ref.CommitSha) 942 - link := createLink(util.URLJoin(setting.AppSubURL, ref.Owner, ref.Name, "commit", ref.CommitSha), reftext, "commit") 942 + link := createLink(util.URLJoin(ctx.Links.Prefix(), ref.Owner, ref.Name, "commit", ref.CommitSha), reftext, "commit") 943 943 944 944 replaceContent(node, ref.RefLocation.Start, ref.RefLocation.End, link) 945 945 node = node.NextSibling.NextSibling ··· 1166 1166 continue 1167 1167 } 1168 1168 1169 - link := util.URLJoin(setting.AppURL, ctx.Metas["user"], ctx.Metas["repo"], "commit", hash) 1169 + link := util.URLJoin(ctx.Links.Prefix(), ctx.Metas["user"], ctx.Metas["repo"], "commit", hash) 1170 1170 replaceContent(node, m[2], m[3], createCodeLink(link, base.ShortSha(hash), "commit")) 1171 1171 start = 0 1172 1172 node = node.NextSibling.NextSibling
+1
modules/markup/html_internal_test.go
··· 287 287 } 288 288 289 289 func testRenderIssueIndexPattern(t *testing.T, input, expected string, ctx *RenderContext) { 290 + ctx.Links.AbsolutePrefix = true 290 291 if ctx.Links.Base == "" { 291 292 ctx.Links.Base = TestRepoURL 292 293 }
+6 -3
modules/markup/html_test.go
··· 43 43 Ctx: git.DefaultContext, 44 44 RelativePath: ".md", 45 45 Links: markup.Links{ 46 - Base: markup.TestRepoURL, 46 + AbsolutePrefix: true, 47 + Base: markup.TestRepoURL, 47 48 }, 48 49 Metas: localMetas, 49 50 }, input) ··· 96 97 Ctx: git.DefaultContext, 97 98 RelativePath: "a.md", 98 99 Links: markup.Links{ 99 - Base: setting.AppSubURL, 100 + AbsolutePrefix: true, 101 + Base: setting.AppSubURL, 100 102 }, 101 103 Metas: localMetas, 102 104 }, input) ··· 579 581 err := markup.PostProcess(&markup.RenderContext{ 580 582 Ctx: git.DefaultContext, 581 583 Links: markup.Links{ 582 - Base: "https://example.com", 584 + AbsolutePrefix: true, 585 + Base: "https://example.com", 583 586 }, 584 587 Metas: localMetas, 585 588 }, strings.NewReader(input), &res)
+3 -3
modules/markup/markdown/markdown_test.go
··· 131 131 <li><a href="` + baseURLContent + `/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li> 132 132 <li><a href="` + baseURLContent + `/Tips" rel="nofollow">Tips</a></li> 133 133 </ul> 134 - <p>See commit <a href="http://localhost:3000/gogits/gogs/commit/65f1bf27bc" rel="nofollow"><code>65f1bf27bc</code></a></p> 134 + <p>See commit <a href="/gogits/gogs/commit/65f1bf27bc" rel="nofollow"><code>65f1bf27bc</code></a></p> 135 135 <p>Ideas and codes</p> 136 136 <ul> 137 - <li>Bezier widget (by <a href="` + AppURL + `r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/ocornut/imgui/issues/786" class="ref-issue" rel="nofollow">ocornut/imgui#786</a></li> 138 - <li>Bezier widget (by <a href="` + AppURL + `r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/gogits/gogs/issues/786" class="ref-issue" rel="nofollow">#786</a></li> 137 + <li>Bezier widget (by <a href="/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/ocornut/imgui/issues/786" class="ref-issue" rel="nofollow">ocornut/imgui#786</a></li> 138 + <li>Bezier widget (by <a href="/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/gogits/gogs/issues/786" class="ref-issue" rel="nofollow">#786</a></li> 139 139 <li>Node graph editors <a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">https://github.com/ocornut/imgui/issues/306</a></li> 140 140 <li><a href="` + baseURLContent + `/memory_editor_example" rel="nofollow">Memory Editor</a></li> 141 141 <li><a href="` + baseURLContent + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
+11 -3
modules/markup/renderer.go
··· 82 82 } 83 83 84 84 type Links struct { 85 - Base string 86 - BranchPath string 87 - TreePath string 85 + AbsolutePrefix bool 86 + Base string 87 + BranchPath string 88 + TreePath string 89 + } 90 + 91 + func (l *Links) Prefix() string { 92 + if l.AbsolutePrefix { 93 + return setting.AppURL 94 + } 95 + return setting.AppSubURL 88 96 } 89 97 90 98 func (l *Links) HasBranchInfo() bool {
+7 -7
modules/templates/util_render_test.go
··· 122 122 com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit 123 123 <span class="emoji" aria-label="thumbs up">👍</span> 124 124 <a href="mailto:mail@domain.com" class="mailto">mail@domain.com</a> 125 - <a href="http://localhost:3000/mention-user" class="mention">@mention-user</a> test 126 - <a href="http://localhost:3000/user13/repo11/issues/123" class="ref-issue">#123</a> 125 + <a href="/mention-user" class="mention">@mention-user</a> test 126 + <a href="/user13/repo11/issues/123" class="ref-issue">#123</a> 127 127 space` 128 128 129 129 assert.EqualValues(t, expected, RenderCommitBody(context.Background(), testInput, testMetas)) 130 130 } 131 131 132 132 func TestRenderCommitMessage(t *testing.T) { 133 - expected := `space <a href="http://localhost:3000/mention-user" class="mention">@mention-user</a> ` 133 + expected := `space <a href="/mention-user" class="mention">@mention-user</a> ` 134 134 135 135 assert.EqualValues(t, expected, RenderCommitMessage(context.Background(), testInput, testMetas)) 136 136 } 137 137 138 138 func TestRenderCommitMessageLinkSubject(t *testing.T) { 139 - expected := `<a href="https://example.com/link" class="default-link muted">space </a><a href="http://localhost:3000/mention-user" class="mention">@mention-user</a>` 139 + expected := `<a href="https://example.com/link" class="default-link muted">space </a><a href="/mention-user" class="mention">@mention-user</a>` 140 140 141 141 assert.EqualValues(t, expected, RenderCommitMessageLinkSubject(context.Background(), testInput, "https://example.com/link", testMetas)) 142 142 } ··· 160 160 <span class="emoji" aria-label="thumbs up">👍</span> 161 161 mail@domain.com 162 162 @mention-user test 163 - <a href="http://localhost:3000/user13/repo11/issues/123" class="ref-issue">#123</a> 163 + <a href="/user13/repo11/issues/123" class="ref-issue">#123</a> 164 164 space 165 165 ` 166 166 assert.EqualValues(t, expected, RenderIssueTitle(context.Background(), testInput, testMetas)) 167 167 } 168 168 169 169 func TestRenderMarkdownToHtml(t *testing.T) { 170 - expected := `<p>space <a href="http://localhost:3000/mention-user" rel="nofollow">@mention-user</a><br/> 170 + expected := `<p>space <a href="/mention-user" rel="nofollow">@mention-user</a><br/> 171 171 /just/a/path.bin 172 172 <a href="https://example.com/file.bin" rel="nofollow">https://example.com/file.bin</a> 173 173 <a href="/file.bin" rel="nofollow">local link</a> ··· 184 184 com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit 185 185 <span class="emoji" aria-label="thumbs up">👍</span> 186 186 <a href="mailto:mail@domain.com" rel="nofollow">mail@domain.com</a> 187 - <a href="http://localhost:3000/mention-user" rel="nofollow">@mention-user</a> test 187 + <a href="/mention-user" rel="nofollow">@mention-user</a> test 188 188 #123 189 189 space</p> 190 190 `
+4 -2
routers/common/markup.go
··· 34 34 if err := markdown.RenderRaw(&markup.RenderContext{ 35 35 Ctx: ctx, 36 36 Links: markup.Links{ 37 - Base: urlPrefix, 37 + AbsolutePrefix: true, 38 + Base: urlPrefix, 38 39 }, 39 40 }, strings.NewReader(text), ctx.Resp); err != nil { 40 41 ctx.Error(http.StatusInternalServerError, err.Error()) ··· 79 80 if err := markup.Render(&markup.RenderContext{ 80 81 Ctx: ctx, 81 82 Links: markup.Links{ 82 - Base: urlPrefix, 83 + AbsolutePrefix: true, 84 + Base: urlPrefix, 83 85 }, 84 86 Metas: meta, 85 87 IsWiki: wiki,
+2 -1
services/mailer/mail.go
··· 222 222 body, err := markdown.RenderString(&markup.RenderContext{ 223 223 Ctx: ctx, 224 224 Links: markup.Links{ 225 - Base: ctx.Issue.Repo.HTMLURL(), 225 + AbsolutePrefix: true, 226 + Base: ctx.Issue.Repo.HTMLURL(), 226 227 }, 227 228 Metas: ctx.Issue.Repo.ComposeMetas(ctx), 228 229 }, ctx.Content)
+25 -1
services/mailer/mail_test.go
··· 8 8 "context" 9 9 "fmt" 10 10 "html/template" 11 + "io" 12 + "mime/quotedprintable" 11 13 "regexp" 12 14 "strings" 13 15 "testing" ··· 19 21 repo_model "code.gitea.io/gitea/models/repo" 20 22 "code.gitea.io/gitea/models/unittest" 21 23 user_model "code.gitea.io/gitea/models/user" 24 + "code.gitea.io/gitea/modules/markup" 22 25 "code.gitea.io/gitea/modules/setting" 23 26 24 27 "github.com/stretchr/testify/assert" ··· 67 70 func TestComposeIssueCommentMessage(t *testing.T) { 68 71 doer, _, issue, comment := prepareMailerTest(t) 69 72 73 + markup.Init(&markup.ProcessorHelper{ 74 + IsUsernameMentionable: func(ctx context.Context, username string) bool { 75 + return username == doer.Name 76 + }, 77 + }) 78 + 70 79 setting.IncomingEmail.Enabled = true 71 80 defer func() { setting.IncomingEmail.Enabled = false }() 72 81 ··· 77 86 msgs, err := composeIssueCommentMessages(&mailCommentContext{ 78 87 Context: context.TODO(), // TODO: use a correct context 79 88 Issue: issue, Doer: doer, ActionType: activities_model.ActionCommentIssue, 80 - Content: "test body", Comment: comment, 89 + Content: fmt.Sprintf("test @%s %s#%d body", doer.Name, issue.Repo.FullName(), issue.Index), 90 + Comment: comment, 81 91 }, "en-US", recipients, false, "issue comment") 82 92 assert.NoError(t, err) 83 93 assert.Len(t, msgs, 2) ··· 96 106 assert.Equal(t, "<user2/repo1/issues/1/comment/2@localhost>", gomailMsg.GetHeader("Message-ID")[0], "Message-ID header doesn't match") 97 107 assert.Equal(t, "<mailto:"+replyTo+">", gomailMsg.GetHeader("List-Post")[0]) 98 108 assert.Len(t, gomailMsg.GetHeader("List-Unsubscribe"), 2) // url + mailto 109 + 110 + var buf bytes.Buffer 111 + gomailMsg.WriteTo(&buf) 112 + 113 + b, err := io.ReadAll(quotedprintable.NewReader(&buf)) 114 + assert.NoError(t, err) 115 + 116 + // text/plain 117 + assert.Contains(t, string(b), fmt.Sprintf(`( %s )`, doer.HTMLURL())) 118 + assert.Contains(t, string(b), fmt.Sprintf(`( %s )`, issue.HTMLURL())) 119 + 120 + // text/html 121 + assert.Contains(t, string(b), fmt.Sprintf(`href="%s"`, doer.HTMLURL())) 122 + assert.Contains(t, string(b), fmt.Sprintf(`href="%s"`, issue.HTMLURL())) 99 123 } 100 124 101 125 func TestComposeIssueMessage(t *testing.T) {