Skip to content

Commit b93c1f1

Browse files
authored
Improve Gist detection; exclude Gist profiles from isProfile (#132)
1 parent 1e6cdb0 commit b93c1f1

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

index.ts

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,24 @@ addTests('isEnterprise', [
8888
'https://not-github.com/',
8989
'https://my-little-hub.com/',
9090
'https://my-little-hub.com/gist',
91+
'https://my-little-hub.com/gist/in-fragrante',
92+
'https://gist.my-little-hub.com/in-fragrante',
9193
]);
9294

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';
9496
addTests('isGist', [
9597
'https://gist.github.com',
9698
'http://gist.github.com',
99+
'https://gist.github.com/fregante/2205329b71218fa2c1d3',
100+
'https://gist.github.com/fregante/2205329b71218fa2c1d3/d1ebf7d9cfaba4d4596d2ea9174e202479a5f9ad',
97101
'https://gist.github.com/sindresorhus/0ea3c2845718a0a0f0beb579ff14f064',
98102
'https://my-little-hub.com/gist',
99103
'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',
100109
]);
101110

102111
export const isGlobalIssueOrPRList = (url: URL | HTMLAnchorElement | Location = location): boolean => ['issues', 'pulls'].includes(url.pathname.split('/', 2)[1]!);
@@ -513,12 +522,14 @@ addTests('isRepoNetworkGraph', [
513522

514523
export const isForkedRepo = (): boolean => exists('meta[name="octolytics-dimension-repository_is_fork"][content="true"]');
515524

516-
export const isSingleGist = (url: URL | HTMLAnchorElement | Location = location): boolean => isGist(url) && /^\/(gist\/)?[^/]+\/[\da-f]{32}$/.test(url.pathname);
525+
export const isSingleGist = (url: URL | HTMLAnchorElement | Location = location): boolean => /^[^/]+\/[\da-f]{20,32}(\/[\da-f]{40})?$/.test(getCleanGistPathname(url)!);
517526
addTests('isSingleGist', [
527+
'https://gist.github.com/fregante/2205329b71218fa2c1d3',
528+
'https://gist.github.com/fregante/2205329b71218fa2c1d3/d1ebf7d9cfaba4d4596d2ea9174e202479a5f9ad',
518529
'https://gist.github.com/sindresorhus/0ea3c2845718a0a0f0beb579ff14f064',
519530
]);
520531

521-
export const isGistRevision = (url: URL | HTMLAnchorElement | Location = location): boolean => isGist(url) && /^\/(gist\/)?[^/]+\/[\da-f]{32}\/revisions$/.test(url.pathname);
532+
export const isGistRevision = (url: URL | HTMLAnchorElement | Location = location): boolean => /^[^/]+\/[\da-f]{20,32}\/revisions$/.test(getCleanGistPathname(url)!);
522533
addTests('isGistRevision', [
523534
'https://gist.github.com/kidonng/0d16c7f17045f486751fad1b602204a0/revisions',
524535
]);
@@ -535,10 +546,17 @@ addTests('isBranches', [
535546
'https://github.com/sindresorhus/refined-github/branches',
536547
]);
537548

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));
542560

543561
addTests('isProfile', [
544562
'https://github.com/fregante',
@@ -555,6 +573,16 @@ addTests('isProfile', [
555573
'https://github.com/sindresorhus?tab=following',
556574
]);
557575

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+
558586
export const isUserProfile = (): boolean => isProfile() && !isOrganizationProfile();
559587

560588
export const isPrivateUserProfile = (): boolean => isUserProfile() && !exists('.user-following-container');
@@ -686,6 +714,16 @@ const getUsername = (): string | undefined => document.querySelector('meta[name=
686714
/** Drop all duplicate slashes */
687715
const getCleanPathname = (url: URL | HTMLAnchorElement | Location = location): string => url.pathname.replace(/\/+/g, '/').slice(1, url.pathname.endsWith('/') ? -1 : undefined);
688716

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+
689727
export interface RepositoryInfo {
690728
owner: string;
691729
name: string;
@@ -734,5 +772,6 @@ const getRepo = (url?: URL | HTMLAnchorElement | Location | string): RepositoryI
734772
export const utils = {
735773
getUsername,
736774
getCleanPathname,
775+
getCleanGistPathname,
737776
getRepositoryInfo: getRepo,
738777
};

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"test": "run-p build ava xo",
3535
"watch": "run-p watch:typescript demo:watch # vite watch doesn’t generate the lib, so just use the demo",
3636
"watch:typescript": "tsc --watch --noEmit",
37+
"watch:ava": "run-p 'ava -- --watch' 'watch:typescript -- --watch'",
3738
"xo": "xo"
3839
},
3940
"xo": {

0 commit comments

Comments
 (0)