Skip to content

Commit

Permalink
Report the correct repo URL even if GitHub lies (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Nov 22, 2020
1 parent 301d8a3 commit 4840d85
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,16 @@ export interface RepositoryInfo {

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

if (typeof url === 'string') {
Expand Down

0 comments on commit 4840d85

Please sign in to comment.