diff --git a/src/utils/Github-Link.ts b/src/utils/Github-Link.ts index 8f0f226..bed1535 100644 --- a/src/utils/Github-Link.ts +++ b/src/utils/Github-Link.ts @@ -8,12 +8,7 @@ export const githubPagesUrlRe = export type GithubRepoUrl = `https://www.github.com/${string}/${string}`; export type GithubPagesUrl = `https://${string}.github.io/${string}`; -/** - * Get the source URL of a Github Pages page. - * @param throwErrorOnMismatch - If true, throw an error if URL can't - * be parsed. Otherwise, null will be returned. - * @returns A URL - */ + export function getGithubUrlFromGithubPages( throwErrorOnMismatch: true, githubPagesUrl?: GithubPagesUrl, @@ -22,12 +17,20 @@ export function getGithubUrlFromGithubPages( throwErrorOnMismatch?: false, githubPagesUrl?: GithubPagesUrl, ): GithubRepoUrl | null; +/** + * Get the source URL of a Github Pages page. + * @param throwErrorOnMismatch - If true, throw an error if URL can't + * @param githubPagesUrl - Specify the GitHub Pages URL if you don't + * want to use the default of location.href. + * be parsed. Otherwise, null will be returned. + * @returns A URL + */ export function getGithubUrlFromGithubPages( throwErrorOnMismatch?: boolean, githubPagesUrl?: GithubPagesUrl, ) { const currentUrl = githubPagesUrl ?? location.href; - const match = currentUrl.match(githubPagesUrlRe); + const match = githubPagesUrlRe.exec(currentUrl); if (!match) { if (throwErrorOnMismatch) { throw new Error("Could not parse source URL from this page"); @@ -52,7 +55,7 @@ export function createGithubLink( const a = document.createElement("a"); // a.append(githubSvg); a.textContent = "Source code"; - a.href = getGithubUrlFromGithubPages() || fallbackUrl; + a.href = getGithubUrlFromGithubPages() ?? fallbackUrl; a.target = "_blank"; return a; }