Skip to content

Commit

Permalink
Improve Gist detection; exclude Gist profiles from isProfile (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Jul 28, 2022
1 parent 1e6cdb0 commit b93c1f1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
53 changes: 46 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,24 @@ addTests('isEnterprise', [
'https://not-github.com/',
'https://my-little-hub.com/',
'https://my-little-hub.com/gist',
'https://my-little-hub.com/gist/in-fragrante',
'https://gist.my-little-hub.com/in-fragrante',
]);

export const isGist = (url: URL | HTMLAnchorElement | Location = location): boolean => url.hostname.startsWith('gist.') || url.pathname.split('/', 2)[1] === 'gist';
export const isGist = (url: URL | HTMLAnchorElement | Location = location): boolean => typeof getCleanGistPathname(url) === 'string';
addTests('isGist', [
'https://gist.github.com',
'http://gist.github.com',
'https://gist.github.com/fregante/2205329b71218fa2c1d3',
'https://gist.github.com/fregante/2205329b71218fa2c1d3/d1ebf7d9cfaba4d4596d2ea9174e202479a5f9ad',
'https://gist.github.com/sindresorhus/0ea3c2845718a0a0f0beb579ff14f064',
'https://my-little-hub.com/gist',
'https://gist.github.com/kidonng/0d16c7f17045f486751fad1b602204a0/revisions',
'https://gist.github.com/fregante',
'https://gist.github.com/github',
'https://gist.github.com/babel',
'https://my-little-hub.com/gist/in-fragrante',
'https://gist.my-little-hub.com/in-fragrante',
]);

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

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

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

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

export const isProfile = (url: URL | HTMLAnchorElement | Location = location): boolean => {
const pathname = getCleanPathname(url);
return pathname.length > 0 && !pathname.includes('/') && !pathname.includes('.') && !reservedNames.includes(pathname);
};
// Use this with a clean pathname, without leading `/gist/`
const doesLookLikeAProfile = (string: string | undefined): boolean =>
typeof string === 'string'
&& string.length > 0 // Isn't root (http://github.com/)
&& !string.includes('/') // Single-level
&& !string.includes('.') // No extensions
&& !reservedNames.includes(string);

export const isProfile = (url: URL | HTMLAnchorElement | Location = location): boolean =>
!isGist(url)
&& doesLookLikeAProfile(getCleanPathname(url));

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

export const isGistProfile = (url: URL | HTMLAnchorElement | Location = location): boolean => doesLookLikeAProfile(getCleanGistPathname(url));

addTests('isGistProfile', [
'https://gist.github.com/fregante',
'https://gist.github.com/github',
'https://gist.github.com/babel',
'https://my-little-hub.com/gist/in-fragrante',
'https://gist.my-little-hub.com/in-fragrante',
]);

export const isUserProfile = (): boolean => isProfile() && !isOrganizationProfile();

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

const getCleanGistPathname = (url: URL | HTMLAnchorElement | Location = location): string | undefined => {
const pathname = getCleanPathname(url);
if (url.hostname.startsWith('gist.')) {
return pathname;
}

const [gist, ...parts] = pathname.split('/');
return gist === 'gist' ? parts.join('/') : undefined;
};

export interface RepositoryInfo {
owner: string;
name: string;
Expand Down Expand Up @@ -734,5 +772,6 @@ const getRepo = (url?: URL | HTMLAnchorElement | Location | string): RepositoryI
export const utils = {
getUsername,
getCleanPathname,
getCleanGistPathname,
getRepositoryInfo: getRepo,
};
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"test": "run-p build ava xo",
"watch": "run-p watch:typescript demo:watch # vite watch doesn’t generate the lib, so just use the demo",
"watch:typescript": "tsc --watch --noEmit",
"watch:ava": "run-p 'ava -- --watch' 'watch:typescript -- --watch'",
"xo": "xo"
},
"xo": {
Expand Down

0 comments on commit b93c1f1

Please sign in to comment.