Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
55 changes: 55 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,61 @@ test('getRepositoryInfo', () => {
],
}
`);
expect(getRepositoryInfoAdapter('https://github.com/refined-github/sandbox/blob/bracket-in-path/foo%2F%5Bbar%5D%2Fbaz')).toMatchInlineSnapshot(`
{
"name": "sandbox",
"nameWithOwner": "refined-github/sandbox",
"owner": "refined-github",
"path": "blob/bracket-in-path/foo/[bar]/baz",
"pathParts": [
"blob",
"bracket-in-path",
"foo",
"[bar]",
"baz",
],
}
`);
expect(getRepositoryInfoAdapter('https://github.com/refined-github/sandbox/blob/bracket-in-path/foo%2F%5Bbar%5D%2F%2Fbaz')).toMatchInlineSnapshot(`
{
"name": "sandbox",
"nameWithOwner": "refined-github/sandbox",
"owner": "refined-github",
"path": "blob/bracket-in-path/foo/[bar]/baz",
"pathParts": [
"blob",
"bracket-in-path",
"foo",
"[bar]",
"baz",
],
}
`);
expect(getRepositoryInfoAdapter('https://github.com/refined-github/sandbox/blob/bracket-in-path/foo%252F%5Bbar%5D%252Fbaz')).toMatchInlineSnapshot(`
{
"name": "sandbox",
"nameWithOwner": "refined-github/sandbox",
"owner": "refined-github",
"path": "blob/bracket-in-path/foo%2F[bar]%2Fbaz",
Copy link
Copy Markdown
Member Author

@SunsetTechuila SunsetTechuila Apr 7, 2026

Choose a reason for hiding this comment

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

Even GitHub doesn't handle this correctly

Image

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It should show my commit: refined-github/sandbox@17041ac

"pathParts": [
"blob",
"bracket-in-path",
"foo%2F[bar]%2Fbaz",
],
}
`);
expect(getRepositoryInfoAdapter('https://github.com/refined-github/sandbox/tree/%F0%9F%98%B1')).toMatchInlineSnapshot(`
{
"name": "sandbox",
"nameWithOwner": "refined-github/sandbox",
"owner": "refined-github",
"path": "tree/😱",
"pathParts": [
"tree",
"😱",
],
}
`);
}
});

Expand Down
18 changes: 15 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,19 @@ TEST: addTests('isNewRepoTemplate', [
/** Get the logged-in user’s username */
const getLoggedInUser = (): string | undefined => $('meta[name="user-login"]')?.getAttribute('content') ?? undefined;

/** Drop all redundant slashes */
const getCleanPathname = (url: URL | HTMLAnchorElement | Location = location): string => url.pathname.replaceAll(/\/\/+/g, '/').replace(/\/$/, '').slice(1);
/** Drop all redundant slashes. You may want to use `getDecodedPathname` first */
const getCleanPathname = (url: URL | HTMLAnchorElement | string | Location = location): string =>
(typeof url === 'string' ? url : url.pathname)
.replaceAll(/\/\/+/g, '/')
.replace(/\/$/, '')
.slice(1);

/** Decode all path parts */
const getDecodedPathname = (url: URL | HTMLAnchorElement | Location = location): string =>
url.pathname
.split('/')
.map(part => decodeURIComponent(part))
.join('/');

const getCleanGistPathname = (url: URL | HTMLAnchorElement | Location = location): string | undefined => {
const pathname = getCleanPathname(url);
Expand Down Expand Up @@ -1081,7 +1092,7 @@ const getRepo = (url?: URL | HTMLAnchorElement | Location | string): RepositoryI
return;
}

const [owner, name, ...pathParts] = getCleanPathname(url).split('/') as [string, string, string];
const [owner, name, ...pathParts] = getCleanPathname(getDecodedPathname(url)).split('/') as [string, string, string];
return {
owner,
name,
Expand All @@ -1095,6 +1106,7 @@ export const utils = {
getOrg,
getLoggedInUser,
getCleanPathname,
getDecodedPathname,
getCleanGistPathname,
getRepositoryInfo: getRepo,
parseRepoExplorerTitle,
Expand Down
Loading