Skip to content

Exclude special search URLs from isRepoRoot and isRepoHome #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20 changes: 14 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ addTests('isDashboard', [
'https://github.com/?tab=followers', // Gotcha for `isUserProfileFollowersTab`
'https://github.com/?tab=following', // Gotcha for `isUserProfileFollowingTab`
'https://github.com/?tab=overview', // Gotcha for `isUserProfileMainTab`
'https://github.com?search=1', // Gotcha for `isRepoTree`
]);

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

export const isRepoHome = (url: URL | HTMLAnchorElement | Location = location): boolean => getRepo(url)?.path === '';
const hasSearchParameter = (url: URL | HTMLAnchorElement | Location): boolean => new URLSearchParams(url.search).get('search') === '1';

export const isRepoHome = (url: URL | HTMLAnchorElement | Location = location): boolean => getRepo(url)?.path === ''
&& !hasSearchParameter(url);
addTests('isRepoHome', [
// Some tests are here only as "gotchas" for other tests that may misidentify their pages
'https://github.com/sindresorhus/refined-github',
Expand All @@ -425,7 +429,7 @@ addTests('isRepoHome', [
'https://github.com/sindresorhus/refined-github?files=1',
]);

export const isRepoRoot = (url?: URL | HTMLAnchorElement | Location): boolean => {
const _isRepoRoot = (url?: URL | HTMLAnchorElement | Location): boolean => {
const repository = getRepo(url ?? location);

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

// `_isRepoRoot` logic depends on whether a URL was passed, so don't use a `url` default parameter
export const isRepoRoot = (url?: URL | HTMLAnchorElement | Location): boolean => !hasSearchParameter(url ?? location) && _isRepoRoot(url);

addTests('isRepoRoot', [
'isRepoHome',
'https://github.com/sindresorhus/refined-github/tree/native-copy-buttons',
Expand Down Expand Up @@ -486,12 +493,13 @@ addTests('isUserSettings', [
'isRepliesSettings',
]);

export const isRepoTree = (url: URL | HTMLAnchorElement | Location = location): boolean => isRepoRoot(url) || Boolean(getRepo(url)?.path.startsWith('tree/'));
export const isRepoTree = (url: URL | HTMLAnchorElement | Location = location): boolean => _isRepoRoot(url) || Boolean(getRepo(url)?.path.startsWith('tree/'));
addTests('isRepoTree', [
'isRepoRoot',
'https://github.com/sindresorhus/refined-github/tree/master/distribution',
'https://github.com/sindresorhus/refined-github/tree/0.13.0/distribution',
'https://github.com/sindresorhus/refined-github/tree/57bf435ee12d14b482df0bbd88013a2814c7512e/distribution',
'https://github.com/sindresorhus/refined-github/tree/main/source',
'https://github.com/sindresorhus/refined-github/tree/0.13.0/extension',
'https://github.com/sindresorhus/refined-github/tree/57bf435ee12d14b482df0bbd88013a2814c7512e/extension',
'https://github.com/sindresorhus/refined-github?search=1',
]);

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