Skip to content

Commit 3c87228

Browse files
kang8fregante
andauthored
Exclude special search URLs from isRepoRoot and isRepoHome (#178)
Co-authored-by: Federico Brigante <me@fregante.com>
1 parent 2b236f1 commit 3c87228

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ addTests('isDashboard', [
8181
'https://github.com/?tab=followers', // Gotcha for `isUserProfileFollowersTab`
8282
'https://github.com/?tab=following', // Gotcha for `isUserProfileFollowingTab`
8383
'https://github.com/?tab=overview', // Gotcha for `isUserProfileMainTab`
84+
'https://github.com?search=1', // Gotcha for `isRepoTree`
8485
]);
8586

8687
export const isEnterprise = (url: URL | HTMLAnchorElement | Location = location): boolean => url.hostname !== 'github.com' && url.hostname !== 'gist.github.com';
@@ -412,7 +413,10 @@ addTests('isRepoIssueList', [
412413
'https://github.com/sindresorhus/refined-github/labels/%3Adollar%3A%20Funded%20on%20Issuehunt',
413414
]);
414415

415-
export const isRepoHome = (url: URL | HTMLAnchorElement | Location = location): boolean => getRepo(url)?.path === '';
416+
const hasSearchParameter = (url: URL | HTMLAnchorElement | Location): boolean => new URLSearchParams(url.search).get('search') === '1';
417+
418+
export const isRepoHome = (url: URL | HTMLAnchorElement | Location = location): boolean => getRepo(url)?.path === ''
419+
&& !hasSearchParameter(url);
416420
addTests('isRepoHome', [
417421
// Some tests are here only as "gotchas" for other tests that may misidentify their pages
418422
'https://github.com/sindresorhus/refined-github',
@@ -425,7 +429,7 @@ addTests('isRepoHome', [
425429
'https://github.com/sindresorhus/refined-github?files=1',
426430
]);
427431

428-
export const isRepoRoot = (url?: URL | HTMLAnchorElement | Location): boolean => {
432+
const _isRepoRoot = (url?: URL | HTMLAnchorElement | Location): boolean => {
429433
const repository = getRepo(url ?? location);
430434

431435
if (!repository) {
@@ -446,6 +450,9 @@ export const isRepoRoot = (url?: URL | HTMLAnchorElement | Location): boolean =>
446450
return repository.path.startsWith('tree/') && document.title.startsWith(repository.nameWithOwner) && !document.title.endsWith(repository.nameWithOwner);
447451
};
448452

453+
// `_isRepoRoot` logic depends on whether a URL was passed, so don't use a `url` default parameter
454+
export const isRepoRoot = (url?: URL | HTMLAnchorElement | Location): boolean => !hasSearchParameter(url ?? location) && _isRepoRoot(url);
455+
449456
addTests('isRepoRoot', [
450457
'isRepoHome',
451458
'https://github.com/sindresorhus/refined-github/tree/native-copy-buttons',
@@ -486,12 +493,13 @@ addTests('isUserSettings', [
486493
'isRepliesSettings',
487494
]);
488495

489-
export const isRepoTree = (url: URL | HTMLAnchorElement | Location = location): boolean => isRepoRoot(url) || Boolean(getRepo(url)?.path.startsWith('tree/'));
496+
export const isRepoTree = (url: URL | HTMLAnchorElement | Location = location): boolean => _isRepoRoot(url) || Boolean(getRepo(url)?.path.startsWith('tree/'));
490497
addTests('isRepoTree', [
491498
'isRepoRoot',
492-
'https://github.com/sindresorhus/refined-github/tree/master/distribution',
493-
'https://github.com/sindresorhus/refined-github/tree/0.13.0/distribution',
494-
'https://github.com/sindresorhus/refined-github/tree/57bf435ee12d14b482df0bbd88013a2814c7512e/distribution',
499+
'https://github.com/sindresorhus/refined-github/tree/main/source',
500+
'https://github.com/sindresorhus/refined-github/tree/0.13.0/extension',
501+
'https://github.com/sindresorhus/refined-github/tree/57bf435ee12d14b482df0bbd88013a2814c7512e/extension',
502+
'https://github.com/sindresorhus/refined-github?search=1',
495503
]);
496504

497505
export const isRepoWiki = (url: URL | HTMLAnchorElement | Location = location): boolean => Boolean(getRepo(url)?.path.startsWith('wiki'));

0 commit comments

Comments
 (0)