diff options
author | 2020-05-18 19:12:49 -0400 | |
---|---|---|
committer | 2020-05-19 01:12:49 +0200 | |
commit | bb83d265654278fa56bb98df7c68c2139eb30018 (patch) | |
tree | c0bd5c70cc660c3d0884e54fdc65a3afcfa936d3 /source/libs/utils.ts | |
parent | 857470ff356f2d27d1236c7172fe24a5109a4198 (diff) | |
download | refined-github-bb83d265654278fa56bb98df7c68c2139eb30018.tar.gz refined-github-bb83d265654278fa56bb98df7c68c2139eb30018.tar.zst refined-github-bb83d265654278fa56bb98df7c68c2139eb30018.zip |
Lint and console errors (#3101)
Diffstat (limited to 'source/libs/utils.ts')
-rw-r--r-- | source/libs/utils.ts | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/source/libs/utils.ts b/source/libs/utils.ts index 0d9ca856..93abe2ef 100644 --- a/source/libs/utils.ts +++ b/source/libs/utils.ts @@ -76,6 +76,7 @@ export const replaceBranch = (currentBranch: string, newBranch: string): string return `/${getRepoURL()}/${pageType}/${newBranchRepoPath}`; }; +/* Should work on `isRepoTree` `isBlame` `isSingleFile` `isCommitList` `isCompare` `isPRCommit` */ export const getCurrentBranch = (): string => { return select.last<HTMLLinkElement>('link[rel="alternate"]')! .href @@ -205,6 +206,25 @@ export function getLatestVersionTag(tags: string[]): string { return latestVersion; } +export function parseRoute(pathname: string): string { + const [user, repository, route, ...next] = pathname.replace(/^\/|\/$/g, '').split('/'); + const parts = next.join('/'); + const currentBranch = getCurrentBranch(); + if (parts !== currentBranch && !parts.startsWith(currentBranch + '/')) { + throw new Error('The branch of the current page must match the branch in the `pathname` parameter'); + } + + const filePath = parts.replace(currentBranch + '/', ''); + return [ + '', + user, + repository, + route, + currentBranch, + filePath + ]; +} + const escapeRegex = (string: string) => string.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&'); export const prCommitRegex = new RegExp(`\\b${escapeRegex(location.origin)}[/][^/]+[/][^/]+[/]pull[/]\\d+[/]commits[/][0-9a-f]{7,40}\\b(?! \\]|\\))`, 'gi'); |