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.

Merge pull request 'fix: improve the display of PR & issue short links' (#5075) from solomonv/pr-short-link-text-fixes into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/5075
Reviewed-by: Gusted <gusted@noreply.codeberg.org>

Gusted 904e1239 e5edd6ff

+18 -42
+13 -37
modules/markup/html.go
··· 93 93 // Once for to prevent races 94 94 var issueFullPatternOnce sync.Once 95 95 96 - // regexp for full links to hash comment in pull request files changed tab 97 - var filesChangedFullPattern *regexp.Regexp 98 - 99 - // Once for to prevent races 100 - var filesChangedFullPatternOnce sync.Once 101 - 102 96 func getIssueFullPattern() *regexp.Regexp { 103 97 issueFullPatternOnce.Do(func() { 104 98 // example: https://domain/org/repo/pulls/27#hash 105 99 issueFullPattern = regexp.MustCompile(regexp.QuoteMeta(setting.AppURL) + 106 - `[\w_.-]+/[\w_.-]+/(?:issues|pulls)/((?:\w{1,10}-)?[1-9][0-9]*)([\?|#](\S+)?)?\b`) 100 + `(?P<user>[\w_.-]+)\/(?P<repo>[\w_.-]+)\/(?:issues|pulls)\/(?P<num>(?:\w{1,10}-)?[1-9][0-9]*)(?P<subpath>\/[\w_.-]+)?(?:(?P<comment>#(?:issue|issuecomment)-\d+)|(?:[\?#](?:\S+)?))?\b`) 107 101 }) 108 102 return issueFullPattern 109 - } 110 - 111 - func getFilesChangedFullPattern() *regexp.Regexp { 112 - filesChangedFullPatternOnce.Do(func() { 113 - // example: https://domain/org/repo/pulls/27/files#hash 114 - filesChangedFullPattern = regexp.MustCompile(regexp.QuoteMeta(setting.AppURL) + 115 - `[\w_.-]+/[\w_.-]+/pulls/((?:\w{1,10}-)?[1-9][0-9]*)/files([\?|#](\S+)?)?\b`) 116 - }) 117 - return filesChangedFullPattern 118 103 } 119 104 120 105 // CustomLinkURLSchemes allows for additional schemes to be detected when parsing links within text ··· 775 760 } 776 761 next := node.NextSibling 777 762 for node != nil && node != next { 778 - m := getIssueFullPattern().FindStringSubmatchIndex(node.Data) 779 - if m == nil { 763 + re := getIssueFullPattern() 764 + linkIndex, m := re.FindStringIndex(node.Data), re.FindStringSubmatch(node.Data) 765 + if linkIndex == nil || m == nil { 780 766 return 781 767 } 782 768 783 - mDiffView := getFilesChangedFullPattern().FindStringSubmatchIndex(node.Data) 784 - // leave it as it is if the link is from "Files Changed" tab in PR Diff View https://domain/org/repo/pulls/27/files 785 - if mDiffView != nil { 786 - return 787 - } 769 + link := node.Data[linkIndex[0]:linkIndex[1]] 770 + text := "#" + m[re.SubexpIndex("num")] + m[re.SubexpIndex("subpath")] 788 771 789 - link := node.Data[m[0]:m[1]] 790 - text := "#" + node.Data[m[2]:m[3]] 791 - // if m[4] and m[5] is not -1, then link is to a comment 792 - // indicate that in the text by appending (comment) 793 - if m[4] != -1 && m[5] != -1 { 772 + if len(m[re.SubexpIndex("comment")]) > 0 { 794 773 if locale, ok := ctx.Ctx.Value(translation.ContextKey).(translation.Locale); ok { 795 774 text += " " + locale.TrString("repo.from_comment") 796 775 } else { ··· 798 777 } 799 778 } 800 779 801 - // extract repo and org name from matched link like 802 - // http://localhost:3000/gituser/myrepo/issues/1 803 - linkParts := strings.Split(link, "/") 804 - matchOrg := linkParts[len(linkParts)-4] 805 - matchRepo := linkParts[len(linkParts)-3] 780 + matchUser := m[re.SubexpIndex("user")] 781 + matchRepo := m[re.SubexpIndex("repo")] 806 782 807 - if matchOrg == ctx.Metas["user"] && matchRepo == ctx.Metas["repo"] { 808 - replaceContent(node, m[0], m[1], createLink(link, text, "ref-issue")) 783 + if matchUser == ctx.Metas["user"] && matchRepo == ctx.Metas["repo"] { 784 + replaceContent(node, linkIndex[0], linkIndex[1], createLink(link, text, "ref-issue")) 809 785 } else { 810 - text = matchOrg + "/" + matchRepo + text 811 - replaceContent(node, m[0], m[1], createLink(link, text, "ref-issue")) 786 + text = matchUser + "/" + matchRepo + text 787 + replaceContent(node, linkIndex[0], linkIndex[1], createLink(link, text, "ref-issue")) 812 788 } 813 789 node = node.NextSibling.NextSibling 814 790 }
+5 -5
modules/markup/html_internal_test.go
··· 383 383 `<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a>`) 384 384 test("http://localhost:3000/gogits/gogs/issues/4 test", 385 385 `<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a> test`) 386 - test("http://localhost:3000/gogits/gogs/issues/4?a=1&b=2#comment-123 test", 387 - `<a href="http://localhost:3000/gogits/gogs/issues/4?a=1&amp;b=2#comment-123" class="ref-issue">#4 (comment)</a> test`) 386 + test("http://localhost:3000/gogits/gogs/issues/4?a=1&b=2#comment-form test", 387 + `<a href="http://localhost:3000/gogits/gogs/issues/4?a=1&amp;b=2#comment-form" class="ref-issue">#4</a> test`) 388 388 test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24", 389 - "http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24") 390 - test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files", 391 - "http://localhost:3000/testOrg/testOrgRepo/pulls/2/files") 389 + `<a href="http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24" class="ref-issue">testOrg/testOrgRepo#2/files (comment)</a>`) 390 + test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/commits", 391 + `<a href="http://localhost:3000/testOrg/testOrgRepo/pulls/2/commits" class="ref-issue">testOrg/testOrgRepo#2/commits</a>`) 392 392 } 393 393 394 394 func TestRegExp_sha1CurrentPattern(t *testing.T) {