diff options
author | 2023-09-19 22:22:30 +0800 | |
---|---|---|
committer | 2023-09-19 14:22:30 +0000 | |
commit | f9f6a0c70d1d3af559de9d189764abd7a43bf90d (patch) | |
tree | 72290431d5c14f2224466a9d7db72670944b7093 /source/github-helpers/github-url.ts | |
parent | f15e8e06f012d7d7d8d980c9e84d61c05dbe1065 (diff) | |
download | refined-github-f9f6a0c70d1d3af559de9d189764abd7a43bf90d.tar.gz refined-github-f9f6a0c70d1d3af559de9d189764abd7a43bf90d.tar.zst refined-github-f9f6a0c70d1d3af559de9d189764abd7a43bf90d.zip |
Meta: Rename `GitHubURL` to `GitHubFileURL` (#6931)
Diffstat (limited to 'source/github-helpers/github-url.ts')
-rw-r--r-- | source/github-helpers/github-url.ts | 117 |
1 files changed, 0 insertions, 117 deletions
diff --git a/source/github-helpers/github-url.ts b/source/github-helpers/github-url.ts deleted file mode 100644 index 411b6091..00000000 --- a/source/github-helpers/github-url.ts +++ /dev/null @@ -1,117 +0,0 @@ -import {isRepoRoot} from 'github-url-detection'; - -import getCurrentGitRef from './get-current-git-ref.js'; - -export default class GitHubURL { - user = ''; - repository = ''; - route = ''; - branch = ''; - filePath = ''; - - private readonly internalUrl: URL; - - constructor(url: string) { - // Use Facade pattern instead of inheritance #3193 - this.internalUrl = new URL(url); - this.pathname = this.internalUrl.pathname; - } - - toString(): string { - return this.href; - } - - assign(...replacements: Array<Partial<GitHubURL>>): this { - Object.assign(this, ...replacements); - return this; - } - - // Handle branch names containing multiple slashes #4492 - private disambiguateReference( - ambiguousReference: string[], - ): {branch: string; filePath: string} { - const branch = ambiguousReference[0]; - // History pages might use search parameters - const filePathFromSearch = this.searchParams.getAll('path[]').join('/'); - if (filePathFromSearch) { - this.searchParams.delete('path[]'); - return {branch, filePath: filePathFromSearch}; - } - - const filePath = ambiguousReference.slice(1).join('/'); - - // TODO: `getCurrentGitRef` uses global state https://github.com/refined-github/refined-github/issues/6637 - const currentBranch = getCurrentGitRef(); - const currentBranchSections = currentBranch?.split('/'); - if ( - !currentBranch // Current branch could not be determined (1/2) - || !currentBranchSections // Current branch could not be determined (2/2) - || ambiguousReference.length === 1 // Ref has no slashes - || currentBranchSections.length === 1 // Current branch has no slashes - ) { - // Then the reference is not ambiguous - return {branch, filePath}; - } - - for (const [i, section] of currentBranchSections.entries()) { - if (ambiguousReference[i] !== section) { - console.warn(`The supplied path (${ambiguousReference.join('/')}) is ambiguous (current reference is \`${currentBranch}\`)`); - return {branch, filePath}; - } - } - - return { - branch: currentBranch, - filePath: ambiguousReference.slice(currentBranchSections.length).join('/'), - }; - } - - get pathname(): string { - return `/${this.user}/${this.repository}/${this.route}/${this.branch}/${this.filePath}`.replaceAll(/((undefined)?\/)+$/g, ''); - } - - set pathname(pathname: string) { - const [user, repository, route, ...ambiguousReference] = pathname.replaceAll(/^\/|\/$/g, '').split('/'); - // TODO: `isRepoRoot` uses global state https://github.com/refined-github/refined-github/issues/6637 - if (isRepoRoot() || (ambiguousReference.length === 2 && ambiguousReference[1].includes('%2F'))) { - const branch = ambiguousReference.join('/').replaceAll('%2F', '/'); - this.assign({user, repository, route, branch, filePath: ''}); - return; - } - - const {branch, filePath} = this.disambiguateReference(ambiguousReference); - this.assign({user, repository, route, branch, filePath}); - } - - get href(): string { - // Update the actual underlying URL - this.internalUrl.pathname = this.pathname; - return this.internalUrl.href; - } - - set href(href: string) { - this.internalUrl.href = href; - } - - // Proxy all other getters/setters to internalUrl - - get hash(): string { - return this.internalUrl.hash; - } - - set hash(hash: string) { - this.internalUrl.hash = hash; - } - - get search(): string { - return this.internalUrl.search; - } - - set search(search: string) { - this.internalUrl.search = search; - } - - get searchParams(): URLSearchParams { - return this.internalUrl.searchParams; - } -} |