From e0f39a031bbf2b62c6c5a878d7a2e4a6cb3ceaac Mon Sep 17 00:00:00 2001 From: kang Date: Sun, 30 Jul 2023 14:44:50 +0800 Subject: [PATCH 1/9] Fix `/user/repo?search=1` should not match `isRepoRoot` nor `isRepoHome` --- index.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/index.ts b/index.ts index d0b1488..8bfb4ba 100644 --- a/index.ts +++ b/index.ts @@ -405,7 +405,8 @@ 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 === ''; +export const isRepoHome = (url: URL | HTMLAnchorElement | Location = location): boolean => getRepo(url)?.path === '' + && !isRepoTreeFileFinder(url); addTests('isRepoHome', [ // Some tests are here only as "gotchas" for other tests that may misidentify their pages 'https://github.com/sindresorhus/refined-github', @@ -425,6 +426,10 @@ export const isRepoRoot = (url?: URL | HTMLAnchorElement | Location): boolean => return false; } + if (isRepoTreeFileFinder(url ?? location)) { + return false; + } + if (!repository.path) { // Absolute repo root: `isRepoHome` return true; @@ -479,14 +484,25 @@ 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/')) + || isRepoTreeFileFinder(url); addTests('isRepoTree', [ 'isRepoRoot', + 'isRepoTreeFileFinder', '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', ]); +export const isRepoTreeFileFinder = (url: URL | HTMLAnchorElement | Location = location): boolean => new URLSearchParams(url.search).get('search') === '1'; +addTests('isRepoTreeFileFinder', [ + 'https://github.com/sindresorhus/refined-github?search=1', + 'https://github.com/sindresorhus/refined-github/tree/master/distribution?search=1', + 'https://github.com/sindresorhus/refined-github/tree/0.13.0/distribution?search=1', + 'https://github.com/sindresorhus/refined-github/tree/57bf435ee12d14b482df0bbd88013a2814c7512e/distribution?search=1', +]); + export const isRepoWiki = (url: URL | HTMLAnchorElement | Location = location): boolean => Boolean(getRepo(url)?.path.startsWith('wiki')); addTests('isRepoWiki', [ 'https://github.com/lukesampson/scoop/wiki', From c0e80db31948fe26c9d1731331ac47102a8c1618 Mon Sep 17 00:00:00 2001 From: kang Date: Sun, 30 Jul 2023 15:27:17 +0800 Subject: [PATCH 2/9] Replace the broken link with active link --- index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/index.ts b/index.ts index 8bfb4ba..e6f4c3b 100644 --- a/index.ts +++ b/index.ts @@ -490,17 +490,17 @@ export const isRepoTree = (url: URL | HTMLAnchorElement | Location = location): addTests('isRepoTree', [ 'isRepoRoot', 'isRepoTreeFileFinder', - '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', ]); export const isRepoTreeFileFinder = (url: URL | HTMLAnchorElement | Location = location): boolean => new URLSearchParams(url.search).get('search') === '1'; addTests('isRepoTreeFileFinder', [ 'https://github.com/sindresorhus/refined-github?search=1', - 'https://github.com/sindresorhus/refined-github/tree/master/distribution?search=1', - 'https://github.com/sindresorhus/refined-github/tree/0.13.0/distribution?search=1', - 'https://github.com/sindresorhus/refined-github/tree/57bf435ee12d14b482df0bbd88013a2814c7512e/distribution?search=1', + 'https://github.com/sindresorhus/refined-github/tree/main/source?search=1', + 'https://github.com/sindresorhus/refined-github/tree/0.13.0/extension?search=1', + 'https://github.com/sindresorhus/refined-github/tree/57bf435ee12d14b482df0bbd88013a2814c7512e/extension?search=1', ]); export const isRepoWiki = (url: URL | HTMLAnchorElement | Location = location): boolean => Boolean(getRepo(url)?.path.startsWith('wiki')); From 5a2af1dda27556437f8d3f79b1396b78a23190ec Mon Sep 17 00:00:00 2001 From: kang Date: Mon, 31 Jul 2023 21:03:19 +0800 Subject: [PATCH 3/9] Rename isRepoTreeFileFinder to hasSearchParameter and do not export it --- index.ts | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/index.ts b/index.ts index e6f4c3b..226b17d 100644 --- a/index.ts +++ b/index.ts @@ -10,6 +10,7 @@ addTests('__urls_that_dont_match__', [ 'https://github.com/sindresorhus/refined-github/issues/new', 'https://github.com/sindresorhus/refined-github/issues/new/choose', 'https://github.com/sindresorhus/refined-github/issues/templates/edit', + 'https://github.com/sindresorhus/refined-github?search=1', ]); export const is404 = (): boolean => /^(Page|File) not found · GitHub/.test(document.title); // #98; When logged out, it starts with "File" @@ -405,8 +406,10 @@ addTests('isRepoIssueList', [ 'https://github.com/sindresorhus/refined-github/labels/%3Adollar%3A%20Funded%20on%20Issuehunt', ]); +const hasSearchParameter = (url: URL | HTMLAnchorElement | Location = location): boolean => new URLSearchParams(url.search).get('search') === '1'; + export const isRepoHome = (url: URL | HTMLAnchorElement | Location = location): boolean => getRepo(url)?.path === '' - && !isRepoTreeFileFinder(url); + && !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', @@ -426,7 +429,7 @@ export const isRepoRoot = (url?: URL | HTMLAnchorElement | Location): boolean => return false; } - if (isRepoTreeFileFinder(url ?? location)) { + if (hasSearchParameter(url ?? location)) { return false; } @@ -486,23 +489,14 @@ addTests('isUserSettings', [ export const isRepoTree = (url: URL | HTMLAnchorElement | Location = location): boolean => isRepoRoot(url) || Boolean(getRepo(url)?.path.startsWith('tree/')) - || isRepoTreeFileFinder(url); + || hasSearchParameter(url); addTests('isRepoTree', [ 'isRepoRoot', - 'isRepoTreeFileFinder', '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', ]); -export const isRepoTreeFileFinder = (url: URL | HTMLAnchorElement | Location = location): boolean => new URLSearchParams(url.search).get('search') === '1'; -addTests('isRepoTreeFileFinder', [ - 'https://github.com/sindresorhus/refined-github?search=1', - 'https://github.com/sindresorhus/refined-github/tree/main/source?search=1', - 'https://github.com/sindresorhus/refined-github/tree/0.13.0/extension?search=1', - 'https://github.com/sindresorhus/refined-github/tree/57bf435ee12d14b482df0bbd88013a2814c7512e/extension?search=1', -]); - export const isRepoWiki = (url: URL | HTMLAnchorElement | Location = location): boolean => Boolean(getRepo(url)?.path.startsWith('wiki')); addTests('isRepoWiki', [ 'https://github.com/lukesampson/scoop/wiki', From 703844bc9041d9dec26c6660da560d95d3709085 Mon Sep 17 00:00:00 2001 From: kang Date: Mon, 31 Jul 2023 22:22:08 +0800 Subject: [PATCH 4/9] Refactor: extract isRepoRoot as _isRepoRoot to make logic easy --- index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.ts b/index.ts index 226b17d..b1d3228 100644 --- a/index.ts +++ b/index.ts @@ -422,7 +422,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) { @@ -447,6 +447,8 @@ export const isRepoRoot = (url?: URL | HTMLAnchorElement | Location): boolean => return repository.path.startsWith('tree/') && document.title.startsWith(repository.nameWithOwner) && !document.title.endsWith(repository.nameWithOwner); }; +export const isRepoRoot = (url?: URL | HTMLAnchorElement | Location): boolean => !hasSearchParameter(url) && _isRepoRoot(url); + addTests('isRepoRoot', [ 'isRepoHome', 'https://github.com/sindresorhus/refined-github/tree/native-copy-buttons', @@ -487,9 +489,7 @@ addTests('isUserSettings', [ 'isRepliesSettings', ]); -export const isRepoTree = (url: URL | HTMLAnchorElement | Location = location): boolean => isRepoRoot(url) - || Boolean(getRepo(url)?.path.startsWith('tree/')) - || hasSearchParameter(url); +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/main/source', From c198d35762e21f4599452e0758eda5c39d3b5613 Mon Sep 17 00:00:00 2001 From: kang Date: Mon, 31 Jul 2023 22:39:29 +0800 Subject: [PATCH 5/9] Fix review --- index.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/index.ts b/index.ts index b1d3228..731e592 100644 --- a/index.ts +++ b/index.ts @@ -406,7 +406,7 @@ addTests('isRepoIssueList', [ 'https://github.com/sindresorhus/refined-github/labels/%3Adollar%3A%20Funded%20on%20Issuehunt', ]); -const hasSearchParameter = (url: URL | HTMLAnchorElement | Location = location): boolean => new URLSearchParams(url.search).get('search') === '1'; +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); @@ -429,10 +429,6 @@ const _isRepoRoot = (url?: URL | HTMLAnchorElement | Location): boolean => { return false; } - if (hasSearchParameter(url ?? location)) { - return false; - } - if (!repository.path) { // Absolute repo root: `isRepoHome` return true; From e901a63c658fc4b6a010fa448e4e3ce1f3220354 Mon Sep 17 00:00:00 2001 From: kang Date: Mon, 31 Jul 2023 23:01:31 +0800 Subject: [PATCH 6/9] Fix test --- index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.ts b/index.ts index 731e592..7c3735f 100644 --- a/index.ts +++ b/index.ts @@ -443,7 +443,7 @@ const _isRepoRoot = (url?: URL | HTMLAnchorElement | Location): boolean => { return repository.path.startsWith('tree/') && document.title.startsWith(repository.nameWithOwner) && !document.title.endsWith(repository.nameWithOwner); }; -export const isRepoRoot = (url?: URL | HTMLAnchorElement | Location): boolean => !hasSearchParameter(url) && _isRepoRoot(url); +export const isRepoRoot = (url?: URL | HTMLAnchorElement | Location): boolean => !hasSearchParameter(url ?? location) && _isRepoRoot(url); addTests('isRepoRoot', [ 'isRepoHome', @@ -491,6 +491,7 @@ addTests('isRepoTree', [ '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')); From 7e41dfd53c6853c18d68d9c8402d9ab3ca8b8c4d Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Mon, 31 Jul 2023 18:42:07 +0200 Subject: [PATCH 7/9] Add comment --- index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/index.ts b/index.ts index 7c3735f..1152ed0 100644 --- a/index.ts +++ b/index.ts @@ -443,6 +443,7 @@ 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', [ From 1e3ee25f1295ee8648fe8ac3cd306ef8b55504f2 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Mon, 31 Jul 2023 18:46:51 +0200 Subject: [PATCH 8/9] Fix `url that don't match` --- index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.ts b/index.ts index 1152ed0..217e5b2 100644 --- a/index.ts +++ b/index.ts @@ -10,7 +10,7 @@ addTests('__urls_that_dont_match__', [ 'https://github.com/sindresorhus/refined-github/issues/new', 'https://github.com/sindresorhus/refined-github/issues/new/choose', 'https://github.com/sindresorhus/refined-github/issues/templates/edit', - 'https://github.com/sindresorhus/refined-github?search=1', + 'https://github.com?search=1', ]); export const is404 = (): boolean => /^(Page|File) not found · GitHub/.test(document.title); // #98; When logged out, it starts with "File" From 4df08486eacdee11d396ae27a06cd1814be48fed Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Mon, 31 Jul 2023 18:49:43 +0200 Subject: [PATCH 9/9] Move test URL to isDashboard --- index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.ts b/index.ts index 78c0fc3..70178f8 100644 --- a/index.ts +++ b/index.ts @@ -10,7 +10,6 @@ addTests('__urls_that_dont_match__', [ 'https://github.com/sindresorhus/refined-github/issues/new', 'https://github.com/sindresorhus/refined-github/issues/new/choose', 'https://github.com/sindresorhus/refined-github/issues/templates/edit', - 'https://github.com?search=1', 'https://github.com/orgs/community/discussions/new/choose', ]); @@ -82,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';