diff options
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'); |