Skip to content

Commit 4840d85

Browse files
authored
Report the correct repo URL even if GitHub lies (#54)
1 parent 301d8a3 commit 4840d85

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,16 @@ export interface RepositoryInfo {
542542

543543
const getRepo = (url?: URL | HTMLAnchorElement | Location | string): RepositoryInfo | undefined => {
544544
if (!url) {
545-
const canonical = document.querySelector<HTMLMetaElement>('[property="og:url"]'); // `rel=canonical` doesn't appear on every page
546-
url = canonical ? canonical.content : location;
545+
// We use `canonical` here to use the correct capitalization
546+
// `rel=canonical` doesn't appear on every page
547+
const canonical = document.querySelector<HTMLMetaElement>('[property="og:url"]');
548+
if (canonical) {
549+
const canonicalUrl = new URL(canonical.content, location.origin);
550+
// Sometimes GitHub sets the canonical to an incomplete URL, so it can't be used
551+
if (getCleanPathname(canonicalUrl).toLowerCase() === getCleanPathname(location).toLowerCase()) {
552+
url = canonicalUrl;
553+
}
554+
}
547555
}
548556

549557
if (typeof url === 'string') {

0 commit comments

Comments
 (0)