@@ -88,15 +88,24 @@ addTests('isEnterprise', [
88
88
'https://not-github.com/' ,
89
89
'https://my-little-hub.com/' ,
90
90
'https://my-little-hub.com/gist' ,
91
+ 'https://my-little-hub.com/gist/in-fragrante' ,
92
+ 'https://gist.my-little-hub.com/in-fragrante' ,
91
93
] ) ;
92
94
93
- export const isGist = ( url : URL | HTMLAnchorElement | Location = location ) : boolean => url . hostname . startsWith ( 'gist.' ) || url . pathname . split ( '/' , 2 ) [ 1 ] === 'gist ' ;
95
+ export const isGist = ( url : URL | HTMLAnchorElement | Location = location ) : boolean => typeof getCleanGistPathname ( url ) === 'string ' ;
94
96
addTests ( 'isGist' , [
95
97
'https://gist.github.com' ,
96
98
'http://gist.github.com' ,
99
+ 'https://gist.github.com/fregante/2205329b71218fa2c1d3' ,
100
+ 'https://gist.github.com/fregante/2205329b71218fa2c1d3/d1ebf7d9cfaba4d4596d2ea9174e202479a5f9ad' ,
97
101
'https://gist.github.com/sindresorhus/0ea3c2845718a0a0f0beb579ff14f064' ,
98
102
'https://my-little-hub.com/gist' ,
99
103
'https://gist.github.com/kidonng/0d16c7f17045f486751fad1b602204a0/revisions' ,
104
+ 'https://gist.github.com/fregante' ,
105
+ 'https://gist.github.com/github' ,
106
+ 'https://gist.github.com/babel' ,
107
+ 'https://my-little-hub.com/gist/in-fragrante' ,
108
+ 'https://gist.my-little-hub.com/in-fragrante' ,
100
109
] ) ;
101
110
102
111
export const isGlobalIssueOrPRList = ( url : URL | HTMLAnchorElement | Location = location ) : boolean => [ 'issues' , 'pulls' ] . includes ( url . pathname . split ( '/' , 2 ) [ 1 ] ! ) ;
@@ -513,12 +522,14 @@ addTests('isRepoNetworkGraph', [
513
522
514
523
export const isForkedRepo = ( ) : boolean => exists ( 'meta[name="octolytics-dimension-repository_is_fork"][content="true"]' ) ;
515
524
516
- export const isSingleGist = ( url : URL | HTMLAnchorElement | Location = location ) : boolean => isGist ( url ) && / ^ \/ ( g i s t \/ ) ? [ ^ / ] + \/ [ \d a - f ] { 32 } $ / . test ( url . pathname ) ;
525
+ export const isSingleGist = ( url : URL | HTMLAnchorElement | Location = location ) : boolean => / ^ [ ^ / ] + \/ [ \d a - f ] { 20 , 32 } ( \/ [ \d a - f ] { 40 } ) ? $ / . test ( getCleanGistPathname ( url ) ! ) ;
517
526
addTests ( 'isSingleGist' , [
527
+ 'https://gist.github.com/fregante/2205329b71218fa2c1d3' ,
528
+ 'https://gist.github.com/fregante/2205329b71218fa2c1d3/d1ebf7d9cfaba4d4596d2ea9174e202479a5f9ad' ,
518
529
'https://gist.github.com/sindresorhus/0ea3c2845718a0a0f0beb579ff14f064' ,
519
530
] ) ;
520
531
521
- export const isGistRevision = ( url : URL | HTMLAnchorElement | Location = location ) : boolean => isGist ( url ) && / ^ \/ ( g i s t \/ ) ? [ ^ / ] + \/ [ \d a - f ] { 32 } \/ r e v i s i o n s $ / . test ( url . pathname ) ;
532
+ export const isGistRevision = ( url : URL | HTMLAnchorElement | Location = location ) : boolean => / ^ [ ^ / ] + \/ [ \d a - f ] { 20 , 32 } \/ r e v i s i o n s $ / . test ( getCleanGistPathname ( url ) ! ) ;
522
533
addTests ( 'isGistRevision' , [
523
534
'https://gist.github.com/kidonng/0d16c7f17045f486751fad1b602204a0/revisions' ,
524
535
] ) ;
@@ -535,10 +546,17 @@ addTests('isBranches', [
535
546
'https://github.com/sindresorhus/refined-github/branches' ,
536
547
] ) ;
537
548
538
- export const isProfile = ( url : URL | HTMLAnchorElement | Location = location ) : boolean => {
539
- const pathname = getCleanPathname ( url ) ;
540
- return pathname . length > 0 && ! pathname . includes ( '/' ) && ! pathname . includes ( '.' ) && ! reservedNames . includes ( pathname ) ;
541
- } ;
549
+ // Use this with a clean pathname, without leading `/gist/`
550
+ const doesLookLikeAProfile = ( string : string | undefined ) : boolean =>
551
+ typeof string === 'string'
552
+ && string . length > 0 // Isn't root (http://github.com/)
553
+ && ! string . includes ( '/' ) // Single-level
554
+ && ! string . includes ( '.' ) // No extensions
555
+ && ! reservedNames . includes ( string ) ;
556
+
557
+ export const isProfile = ( url : URL | HTMLAnchorElement | Location = location ) : boolean =>
558
+ ! isGist ( url )
559
+ && doesLookLikeAProfile ( getCleanPathname ( url ) ) ;
542
560
543
561
addTests ( 'isProfile' , [
544
562
'https://github.com/fregante' ,
@@ -555,6 +573,16 @@ addTests('isProfile', [
555
573
'https://github.com/sindresorhus?tab=following' ,
556
574
] ) ;
557
575
576
+ export const isGistProfile = ( url : URL | HTMLAnchorElement | Location = location ) : boolean => doesLookLikeAProfile ( getCleanGistPathname ( url ) ) ;
577
+
578
+ addTests ( 'isGistProfile' , [
579
+ 'https://gist.github.com/fregante' ,
580
+ 'https://gist.github.com/github' ,
581
+ 'https://gist.github.com/babel' ,
582
+ 'https://my-little-hub.com/gist/in-fragrante' ,
583
+ 'https://gist.my-little-hub.com/in-fragrante' ,
584
+ ] ) ;
585
+
558
586
export const isUserProfile = ( ) : boolean => isProfile ( ) && ! isOrganizationProfile ( ) ;
559
587
560
588
export const isPrivateUserProfile = ( ) : boolean => isUserProfile ( ) && ! exists ( '.user-following-container' ) ;
@@ -686,6 +714,16 @@ const getUsername = (): string | undefined => document.querySelector('meta[name=
686
714
/** Drop all duplicate slashes */
687
715
const getCleanPathname = ( url : URL | HTMLAnchorElement | Location = location ) : string => url . pathname . replace ( / \/ + / g, '/' ) . slice ( 1 , url . pathname . endsWith ( '/' ) ? - 1 : undefined ) ;
688
716
717
+ const getCleanGistPathname = ( url : URL | HTMLAnchorElement | Location = location ) : string | undefined => {
718
+ const pathname = getCleanPathname ( url ) ;
719
+ if ( url . hostname . startsWith ( 'gist.' ) ) {
720
+ return pathname ;
721
+ }
722
+
723
+ const [ gist , ...parts ] = pathname . split ( '/' ) ;
724
+ return gist === 'gist' ? parts . join ( '/' ) : undefined ;
725
+ } ;
726
+
689
727
export interface RepositoryInfo {
690
728
owner : string ;
691
729
name : string ;
@@ -734,5 +772,6 @@ const getRepo = (url?: URL | HTMLAnchorElement | Location | string): RepositoryI
734
772
export const utils = {
735
773
getUsername,
736
774
getCleanPathname,
775
+ getCleanGistPathname,
737
776
getRepositoryInfo : getRepo ,
738
777
} ;
0 commit comments