Skip to content

Commit 188b80e

Browse files
authored
Use canonical URL in utils.getRepoURL (#26)
1 parent 0e2f6f7 commit 188b80e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,15 @@ const getRepoPath = (url: URL | Location = location): string | undefined => {
455455
return undefined;
456456
};
457457

458-
/** Get the 'user/repo' part from an URL */
459-
const getRepoURL = (url: URL | Location = location): string => url.pathname.slice(1).split('/', 2).join('/');
458+
/** Get the 'user/repo' part from an URL. Tries using the canonical URL to avoid capitalization errors in the `location` URL */
459+
const getRepoURL = (url?: URL | Location): string => {
460+
if (!url) {
461+
const canonical = document.querySelector<HTMLMetaElement>('[property="og:url"]'); // `rel=canonical` doesn't appear on every page
462+
url = canonical ? new URL(canonical.content) : location;
463+
}
464+
465+
return url.pathname.slice(1).split('/', 2).join('/');
466+
};
460467

461468
export const utils = {
462469
getUsername,

0 commit comments

Comments
 (0)