@@ -347,11 +347,17 @@ TEST: addTests('isEditingWikiPage', [
347
347
export const hasWikiPageEditor = ( url : URL | HTMLAnchorElement | Location = location ) : boolean => isEditingWikiPage ( url ) || isNewWikiPage ( url ) ;
348
348
TEST: addTests ( 'hasWikiPageEditor' , combinedTestOnly ) ;
349
349
350
- export const isRepo = ( url : URL | HTMLAnchorElement | Location = location ) : boolean => / ^ [ ^ / ] + \/ [ ^ / ] + / . test ( getCleanPathname ( url ) )
351
- && ! reservedNames . includes ( url . pathname . split ( '/' , 2 ) [ 1 ] ! )
352
- && ! isDashboard ( url )
353
- && ! isGist ( url )
354
- && ! isNewRepoTemplate ( url ) ;
350
+ export const isRepo = ( url : URL | HTMLAnchorElement | Location = location ) : boolean => {
351
+ const [ user , repo , extra ] = getCleanPathname ( url ) . split ( '/' ) ;
352
+ return Boolean (
353
+ user
354
+ && repo
355
+ && ! reservedNames . includes ( user )
356
+ && ! url . hostname . startsWith ( 'gist.' )
357
+ && extra !== 'generate' , // Like isNewRepoTemplate but inlined for performance
358
+ ) ;
359
+ } ;
360
+
355
361
TEST: addTests ( 'isRepo' , [
356
362
// Some of these are here simply as "gotchas" to other detections
357
363
'https://github.com/sindresorhus/refined-github/blame/master/package.json' ,
@@ -751,8 +757,8 @@ TEST: addTests('isNewRepoTemplate', [
751
757
/** Get the logged-in user’s username */
752
758
const getLoggedInUser = ( ) : string | undefined => $ ( 'meta[name="user-login"]' ) ?. getAttribute ( 'content' ) ?? undefined ;
753
759
754
- /** Drop all duplicate slashes */
755
- const getCleanPathname = ( url : URL | HTMLAnchorElement | Location = location ) : string => url . pathname . replaceAll ( / \/ + / g, '/' ) . slice ( 1 , url . pathname . endsWith ( '/' ) ? - 1 : undefined ) ;
760
+ /** Drop all redundant slashes */
761
+ const getCleanPathname = ( url : URL | HTMLAnchorElement | Location = location ) : string => url . pathname . replaceAll ( / \/ \/ + / g, '/' ) . replace ( / \/ $ / , '' ) . slice ( 1 ) ;
756
762
757
763
const getCleanGistPathname = ( url : URL | HTMLAnchorElement | Location = location ) : string | undefined => {
758
764
const pathname = getCleanPathname ( url ) ;
@@ -765,7 +771,7 @@ const getCleanGistPathname = (url: URL | HTMLAnchorElement | Location = location
765
771
} ;
766
772
767
773
const getOrg = ( url : URL | HTMLAnchorElement | Location = location ) : { name : string ; path : string } | undefined => {
768
- const [ , orgs , name , ...path ] = url . pathname . split ( '/' ) ;
774
+ const [ orgs , name , ...path ] = getCleanPathname ( url ) . split ( '/' ) ;
769
775
if ( orgs === 'orgs' && name ) {
770
776
return { name, path : path . join ( '/' ) } ;
771
777
}
@@ -792,6 +798,11 @@ export type RepositoryInfo = {
792
798
path : string ;
793
799
} ;
794
800
801
+ /**
802
+ * Parse a repository URL into its parts, or return `undefined`
803
+ * @param url Can be a full URL or a relative URL
804
+ * @returns
805
+ */
795
806
const getRepo = ( url ?: URL | HTMLAnchorElement | Location | string ) : RepositoryInfo | undefined => {
796
807
if ( ! url ) {
797
808
// We use `canonical` here to use the correct capitalization
0 commit comments