@@ -4,7 +4,9 @@ const patchDiffRegex = /[.](patch|diff)$/;
4
4
const releaseRegex = / ^ r e l e a s e s [ / ] t a g [ / ] ( [ ^ / ] + ) / ;
5
5
const labelRegex = / ^ l a b e l s [ / ] ( [ ^ / ] + ) / ;
6
6
const compareRegex = / ^ c o m p a r e [ / ] ( [ ^ / ] + ) / ;
7
- const pullRegex = / ^ p u l l [ / ] ( \d + ) [ / ] ( [ ^ / ] + ) (?: [ / ] ( [ \d a - f ] { 40 } ) [ . ] [ . ] ( [ \d a - f ] { 40 } ) ) ? $ / ;
7
+ const pullRegex = / ^ p u l l [ / ] ( \d + ) (?: [ / ] ( [ ^ / ] + ) ) ? (?: [ / ] ( [ \d a - f ] { 40 } ) [ . ] [ . ] ( [ \d a - f ] { 40 } ) ) ? $ / ;
8
+ const issueRegex = / ^ i s s u e s [ / ] ( \d + ) $ / ;
9
+ const commitRegex = / ^ c o m m i t [ / ] ( [ \d a - f ] { 40 } ) $ / ;
8
10
const releaseArchiveRegex = / ^ a r c h i v e [ / ] ( .+ ) ( [ . ] z i p | [ . ] t a r [ . ] g z ) / ;
9
11
const releaseDownloadRegex = / ^ r e l e a s e s [ / ] d o w n l o a d [ / ] ( [ ^ / ] + ) [ / ] ( .+ ) / ;
10
12
const dependentsRegex = / ^ n e t w o r k [ / ] d e p e n d e n t s [ / ] ? $ / ;
@@ -23,6 +25,18 @@ function styleRevision(revision) {
23
25
return `<code>${ revision } </code>` ;
24
26
}
25
27
28
+ function commentIndicator ( hash ) {
29
+ if ( hash . startsWith ( '#issue-' ) || hash . startsWith ( '#commitcomment-' ) ) {
30
+ return ' (comment)' ;
31
+ }
32
+
33
+ if ( hash . startsWith ( '#pullrequestreview-' ) ) {
34
+ return ' (review)' ;
35
+ }
36
+
37
+ return '' ;
38
+ }
39
+
26
40
// Filter out null values
27
41
function joinValues ( array , delimiter = '/' ) {
28
42
return array . filter ( s => s ) . join ( delimiter ) ;
@@ -57,6 +71,11 @@ function shortenURL(href, currentUrl = 'https://github.com') {
57
71
'https://rawgit.com' ,
58
72
] . includes ( origin ) ;
59
73
74
+ const isRedirection = [
75
+ 'https://togithub.com' , // Renovate
76
+ 'https://github-redirect.dependabot.com' , // Dependabot
77
+ ] . includes ( origin ) ;
78
+
60
79
let [
61
80
user ,
62
81
repo ,
@@ -80,7 +99,7 @@ function shortenURL(href, currentUrl = 'https://github.com') {
80
99
filePath = filePath . join ( '/' ) ;
81
100
82
101
const isLocal = origin === currentUrl . origin ;
83
- const isThisRepo = ( isLocal || isRaw ) && currentRepo === `${ user } /${ repo } ` ;
102
+ const isThisRepo = ( isLocal || isRaw || isRedirection ) && currentRepo === `${ user } /${ repo } ` ;
84
103
const isReserved = reservedPaths . includes ( user ) ;
85
104
const isDependents = dependentsRegex . test ( repoPath ) ;
86
105
const isDependencies = dependenciesRegex . test ( repoPath ) ;
@@ -91,6 +110,8 @@ function shortenURL(href, currentUrl = 'https://github.com') {
91
110
const [ , label ] = repoPath . match ( labelRegex ) || [ ] ;
92
111
const [ , compare ] = repoPath . match ( compareRegex ) || [ ] ;
93
112
const [ , pull , pullPage , pullPartialStart , pullPartialEnd ] = repoPath . match ( pullRegex ) || [ ] ;
113
+ const [ , issue ] = isRedirection ? repoPath . match ( issueRegex ) || [ ] : [ ] ;
114
+ const [ , commit ] = isRedirection ? repoPath . match ( commitRegex ) || [ ] : [ ] ;
94
115
const isFileOrDir = revision && [
95
116
'raw' ,
96
117
'tree' ,
@@ -105,7 +126,7 @@ function shortenURL(href, currentUrl = 'https://github.com') {
105
126
* Shorten URL
106
127
*/
107
128
108
- if ( isReserved || pathname === '/' || ( ! isLocal && ! isRaw ) ) {
129
+ if ( isReserved || pathname === '/' || ( ! isLocal && ! isRaw && ! isRedirection ) ) {
109
130
return href
110
131
. replace ( / ^ h t t p s : [ / ] [ / ] / , '' )
111
132
. replace ( / ^ w w w [ . ] / , '' )
@@ -179,6 +200,21 @@ function shortenURL(href, currentUrl = 'https://github.com') {
179
200
return `${ partial } ${ search } ${ hash } (compare)` ;
180
201
}
181
202
203
+ // Shorten URLs that would otherwise be natively shortened
204
+ if ( isRedirection ) {
205
+ if ( issue ) {
206
+ return `${ repoUrl } #${ issue } ${ commentIndicator ( hash ) } ` ;
207
+ }
208
+
209
+ if ( pull ) {
210
+ return `${ repoUrl } #${ pull } ${ commentIndicator ( hash ) } ` ;
211
+ }
212
+
213
+ if ( commit ) {
214
+ return joinValues ( [ repoUrl , `<code>${ commit . slice ( 0 , 7 ) } </code>` ] , '@' ) + commentIndicator ( hash ) ;
215
+ }
216
+ }
217
+
182
218
let query = searchParams . get ( 'q' ) ?? '' ;
183
219
if ( query ) {
184
220
searchParams . delete ( 'q' ) ;
0 commit comments