mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-21 21:05:34 +03:00
fix: improve PR/issue short link display text
- include subpaths - don't append "(comment)" for unrelated hash/query strings
This commit is contained in:
parent
0c02a61319
commit
22f012a859
2 changed files with 18 additions and 42 deletions
|
@ -93,30 +93,15 @@ var issueFullPattern *regexp.Regexp
|
|||
// Once for to prevent races
|
||||
var issueFullPatternOnce sync.Once
|
||||
|
||||
// regexp for full links to hash comment in pull request files changed tab
|
||||
var filesChangedFullPattern *regexp.Regexp
|
||||
|
||||
// Once for to prevent races
|
||||
var filesChangedFullPatternOnce sync.Once
|
||||
|
||||
func getIssueFullPattern() *regexp.Regexp {
|
||||
issueFullPatternOnce.Do(func() {
|
||||
// example: https://domain/org/repo/pulls/27#hash
|
||||
issueFullPattern = regexp.MustCompile(regexp.QuoteMeta(setting.AppURL) +
|
||||
`[\w_.-]+/[\w_.-]+/(?:issues|pulls)/((?:\w{1,10}-)?[1-9][0-9]*)([\?|#](\S+)?)?\b`)
|
||||
`(?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`)
|
||||
})
|
||||
return issueFullPattern
|
||||
}
|
||||
|
||||
func getFilesChangedFullPattern() *regexp.Regexp {
|
||||
filesChangedFullPatternOnce.Do(func() {
|
||||
// example: https://domain/org/repo/pulls/27/files#hash
|
||||
filesChangedFullPattern = regexp.MustCompile(regexp.QuoteMeta(setting.AppURL) +
|
||||
`[\w_.-]+/[\w_.-]+/pulls/((?:\w{1,10}-)?[1-9][0-9]*)/files([\?|#](\S+)?)?\b`)
|
||||
})
|
||||
return filesChangedFullPattern
|
||||
}
|
||||
|
||||
// CustomLinkURLSchemes allows for additional schemes to be detected when parsing links within text
|
||||
func CustomLinkURLSchemes(schemes []string) {
|
||||
schemes = append(schemes, "http", "https")
|
||||
|
@ -775,22 +760,16 @@ func fullIssuePatternProcessor(ctx *RenderContext, node *html.Node) {
|
|||
}
|
||||
next := node.NextSibling
|
||||
for node != nil && node != next {
|
||||
m := getIssueFullPattern().FindStringSubmatchIndex(node.Data)
|
||||
if m == nil {
|
||||
re := getIssueFullPattern()
|
||||
linkIndex, m := re.FindStringIndex(node.Data), re.FindStringSubmatch(node.Data)
|
||||
if linkIndex == nil || m == nil {
|
||||
return
|
||||
}
|
||||
|
||||
mDiffView := getFilesChangedFullPattern().FindStringSubmatchIndex(node.Data)
|
||||
// leave it as it is if the link is from "Files Changed" tab in PR Diff View https://domain/org/repo/pulls/27/files
|
||||
if mDiffView != nil {
|
||||
return
|
||||
}
|
||||
link := node.Data[linkIndex[0]:linkIndex[1]]
|
||||
text := "#" + m[re.SubexpIndex("num")] + m[re.SubexpIndex("subpath")]
|
||||
|
||||
link := node.Data[m[0]:m[1]]
|
||||
text := "#" + node.Data[m[2]:m[3]]
|
||||
// if m[4] and m[5] is not -1, then link is to a comment
|
||||
// indicate that in the text by appending (comment)
|
||||
if m[4] != -1 && m[5] != -1 {
|
||||
if len(m[re.SubexpIndex("comment")]) > 0 {
|
||||
if locale, ok := ctx.Ctx.Value(translation.ContextKey).(translation.Locale); ok {
|
||||
text += " " + locale.TrString("repo.from_comment")
|
||||
} else {
|
||||
|
@ -798,17 +777,14 @@ func fullIssuePatternProcessor(ctx *RenderContext, node *html.Node) {
|
|||
}
|
||||
}
|
||||
|
||||
// extract repo and org name from matched link like
|
||||
// http://localhost:3000/gituser/myrepo/issues/1
|
||||
linkParts := strings.Split(link, "/")
|
||||
matchOrg := linkParts[len(linkParts)-4]
|
||||
matchRepo := linkParts[len(linkParts)-3]
|
||||
matchUser := m[re.SubexpIndex("user")]
|
||||
matchRepo := m[re.SubexpIndex("repo")]
|
||||
|
||||
if matchOrg == ctx.Metas["user"] && matchRepo == ctx.Metas["repo"] {
|
||||
replaceContent(node, m[0], m[1], createLink(link, text, "ref-issue"))
|
||||
if matchUser == ctx.Metas["user"] && matchRepo == ctx.Metas["repo"] {
|
||||
replaceContent(node, linkIndex[0], linkIndex[1], createLink(link, text, "ref-issue"))
|
||||
} else {
|
||||
text = matchOrg + "/" + matchRepo + text
|
||||
replaceContent(node, m[0], m[1], createLink(link, text, "ref-issue"))
|
||||
text = matchUser + "/" + matchRepo + text
|
||||
replaceContent(node, linkIndex[0], linkIndex[1], createLink(link, text, "ref-issue"))
|
||||
}
|
||||
node = node.NextSibling.NextSibling
|
||||
}
|
||||
|
|
|
@ -383,12 +383,12 @@ func TestRender_FullIssueURLs(t *testing.T) {
|
|||
`<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a>`)
|
||||
test("http://localhost:3000/gogits/gogs/issues/4 test",
|
||||
`<a href="http://localhost:3000/gogits/gogs/issues/4" class="ref-issue">#4</a> test`)
|
||||
test("http://localhost:3000/gogits/gogs/issues/4?a=1&b=2#comment-123 test",
|
||||
`<a href="http://localhost:3000/gogits/gogs/issues/4?a=1&b=2#comment-123" class="ref-issue">#4 (comment)</a> test`)
|
||||
test("http://localhost:3000/gogits/gogs/issues/4?a=1&b=2#comment-form test",
|
||||
`<a href="http://localhost:3000/gogits/gogs/issues/4?a=1&b=2#comment-form" class="ref-issue">#4</a> test`)
|
||||
test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24",
|
||||
"http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24")
|
||||
test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/files",
|
||||
"http://localhost:3000/testOrg/testOrgRepo/pulls/2/files")
|
||||
`<a href="http://localhost:3000/testOrg/testOrgRepo/pulls/2/files#issuecomment-24" class="ref-issue">testOrg/testOrgRepo#2/files (comment)</a>`)
|
||||
test("http://localhost:3000/testOrg/testOrgRepo/pulls/2/commits",
|
||||
`<a href="http://localhost:3000/testOrg/testOrgRepo/pulls/2/commits" class="ref-issue">testOrg/testOrgRepo#2/commits</a>`)
|
||||
}
|
||||
|
||||
func TestRegExp_sha1CurrentPattern(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue