Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,20 @@ addTests('isEnterprise', [
'https://not-github.zerozr99.workers.dev/',
'https://my-little-hub.com/',
'https://my-little-hub.com/gist',
'https://my-little-hub.com/gist/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';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was incorrect. It would match gist.any.com while in reality GHE probably only supports /gist/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember we had a link to a github enterprise that we were able to test things out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wrong:

https://docs.github.com/en/[email protected]/get-started/writing-on-github/editing-and-sharing-content-with-gists/creating-gists#about-gists

http(s)://[hostname]/gist or http(s)://gist.[hostname] if subdomains are enabled.

addTests('isGist', [
'https://gist.github.com',
'http://gist.github.com',
'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',
]);

export const isGlobalConversationList = (url: URL | HTMLAnchorElement | Location = location): boolean => ['issues', 'pulls'].includes(url.pathname.split('/', 2)[1]!);
Expand Down Expand Up @@ -514,12 +519,12 @@ 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 => isGist(url) && /^[^/]+\/[\da-f]{32}$/.test(getCleanGistPathname(url)!);
addTests('isSingleGist', [
'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 => isGist(url) && /^[^/]+\/[\da-f]{32}\/revisions$/.test(getCleanGistPathname(url)!);
addTests('isGistRevision', [
'https://gist.github.com/kidonng/0d16c7f17045f486751fad1b602204a0/revisions',
]);
Expand All @@ -537,6 +542,10 @@ addTests('isBranches', [
]);

export const isProfile = (url: URL | HTMLAnchorElement | Location = location): boolean => {
if (isGist(url)) {
return false;
}

const pathname = getCleanPathname(url);
return pathname.length > 0 && !pathname.includes('/') && !pathname.includes('.') && !reservedNames.includes(pathname);
};
Expand All @@ -556,6 +565,18 @@ addTests('isProfile', [
'https://github.com/sindresorhus?tab=following',
]);

export const isGistProfile = (url: URL | HTMLAnchorElement | Location = location): boolean => {
const path = getCleanGistPathname(url);
return typeof path === 'string' && path.length > 0 && !path.includes('/');
};

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',
]);

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

export const isPrivateUserProfile = (): boolean => isUserProfile() && !exists('.user-following-container');
Expand Down Expand Up @@ -689,6 +710,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 === 'gist.github.com') {
return pathname;
}

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

export interface RepositoryInfo {
owner: string;
name: string;
Expand Down Expand Up @@ -737,5 +768,6 @@ const getRepo = (url?: URL | HTMLAnchorElement | Location | string): RepositoryI
export const utils = {
getUsername,
getCleanPathname,
getCleanGistPathname,
getRepositoryInfo: getRepo,
};
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:typescript -- --watch",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AVA times out when there are certain type errors 🤷‍♂️

"xo": "xo"
},
"xo": {
Expand Down