@@ -22,6 +22,7 @@ import (
2222 "code.gitea.io/gitea/modules/regexplru"
2323 "code.gitea.io/gitea/modules/setting"
2424 "code.gitea.io/gitea/modules/templates/vars"
25+ "code.gitea.io/gitea/modules/translation"
2526 "code.gitea.io/gitea/modules/util"
2627
2728 "golang.org/x/net/html"
@@ -97,14 +98,30 @@ var issueFullPattern *regexp.Regexp
9798// Once for to prevent races
9899var issueFullPatternOnce sync.Once
99100
101+ // regexp for full links to hash comment in pull request files changed tab
102+ var filesChangedFullPattern * regexp.Regexp
103+
104+ // Once for to prevent races
105+ var filesChangedFullPatternOnce sync.Once
106+
100107func getIssueFullPattern () * regexp.Regexp {
101108 issueFullPatternOnce .Do (func () {
109+ // example: https://domain/org/repo/pulls/27#hash
102110 issueFullPattern = regexp .MustCompile (regexp .QuoteMeta (setting .AppURL ) +
103111 `[\w_.-]+/[\w_.-]+/(?:issues|pulls)/((?:\w{1,10}-)?[1-9][0-9]*)([\?|#](\S+)?)?\b` )
104112 })
105113 return issueFullPattern
106114}
107115
116+ func getFilesChangedFullPattern () * regexp.Regexp {
117+ filesChangedFullPatternOnce .Do (func () {
118+ // example: https://domain/org/repo/pulls/27/files#hash
119+ filesChangedFullPattern = regexp .MustCompile (regexp .QuoteMeta (setting .AppURL ) +
120+ `[\w_.-]+/[\w_.-]+/pulls/((?:\w{1,10}-)?[1-9][0-9]*)/files([\?|#](\S+)?)?\b` )
121+ })
122+ return filesChangedFullPattern
123+ }
124+
108125// CustomLinkURLSchemes allows for additional schemes to be detected when parsing links within text
109126func CustomLinkURLSchemes (schemes []string ) {
110127 schemes = append (schemes , "http" , "https" )
@@ -793,15 +810,30 @@ func fullIssuePatternProcessor(ctx *RenderContext, node *html.Node) {
793810 if ctx .Metas == nil {
794811 return
795812 }
796-
797813 next := node .NextSibling
798814 for node != nil && node != next {
799815 m := getIssueFullPattern ().FindStringSubmatchIndex (node .Data )
800816 if m == nil {
801817 return
802818 }
819+
820+ mDiffView := getFilesChangedFullPattern ().FindStringSubmatchIndex (node .Data )
821+ // leave it as it is if the link is from "Files Changed" tab in PR Diff View https://domain/org/repo/pulls/27/files
822+ if mDiffView != nil {
823+ return
824+ }
825+
803826 link := node .Data [m [0 ]:m [1 ]]
804- id := "#" + node .Data [m [2 ]:m [3 ]]
827+ text := "#" + node .Data [m [2 ]:m [3 ]]
828+ // if m[4] and m[5] is not -1, then link is to a comment
829+ // indicate that in the text by appending (comment)
830+ if m [4 ] != - 1 && m [5 ] != - 1 {
831+ if locale , ok := ctx .Ctx .Value (translation .ContextKey ).(translation.Locale ); ok {
832+ text += " " + locale .Tr ("repo.from_comment" )
833+ } else {
834+ text += " (comment)"
835+ }
836+ }
805837
806838 // extract repo and org name from matched link like
807839 // http://localhost:3000/gituser/myrepo/issues/1
@@ -810,12 +842,10 @@ func fullIssuePatternProcessor(ctx *RenderContext, node *html.Node) {
810842 matchRepo := linkParts [len (linkParts )- 3 ]
811843
812844 if matchOrg == ctx .Metas ["user" ] && matchRepo == ctx .Metas ["repo" ] {
813- // TODO if m[4]:m[5] is not nil, then link is to a comment,
814- // and we should indicate that in the text somehow
815- replaceContent (node , m [0 ], m [1 ], createLink (link , id , "ref-issue" ))
845+ replaceContent (node , m [0 ], m [1 ], createLink (link , text , "ref-issue" ))
816846 } else {
817- orgRepoID : = matchOrg + "/" + matchRepo + id
818- replaceContent (node , m [0 ], m [1 ], createLink (link , orgRepoID , "ref-issue" ))
847+ text = matchOrg + "/" + matchRepo + text
848+ replaceContent (node , m [0 ], m [1 ], createLink (link , text , "ref-issue" ))
819849 }
820850 node = node .NextSibling .NextSibling
821851 }
0 commit comments