Skip to content

Commit 7e97111

Browse files
committed
refactor(theme-default): optimize the logic of resolveEditLink
1 parent a49cade commit 7e97111

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

packages/@vuepress/theme-default/src/utils/resolveEditLink.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { removeLeadingSlash, removeEndingSlash } from '@vuepress/shared'
22
import { resolveRepoType } from './resolveRepoType'
3+
import type { RepoType } from './resolveRepoType'
34

4-
export const editLinkPatternGithub = ':repo/edit/:branch/:path'
5-
export const editLinkPatternGitlab = ':repo/-/edit/:branch/:path'
6-
export const editLinkPatternBitbucket =
7-
':repo/src/:branch/:path?mode=edit&spa=0&at=:branch&fileviewer=file-view-default'
5+
export const editLinkPatterns: Record<Exclude<RepoType, null>, string> = {
6+
GitHub: ':repo/edit/:branch/:path',
7+
GitLab: ':repo/-/edit/:branch/:path',
8+
Bitbucket:
9+
':repo/src/:branch/:path?mode=edit&spa=0&at=:branch&fileviewer=file-view-default',
10+
}
811

912
export const resolveEditLink = ({
1013
docsRepo,
@@ -21,16 +24,12 @@ export const resolveEditLink = ({
2124
}): string | null => {
2225
const repoType = resolveRepoType(docsRepo)
2326

24-
let pattern: string | null = null
27+
let pattern: string | undefined
2528

2629
if (editLinkPattern) {
2730
pattern = editLinkPattern
28-
} else if (repoType === 'GitHub') {
29-
pattern = editLinkPatternGithub
30-
} else if (repoType === 'GitLab') {
31-
pattern = editLinkPatternGitlab
32-
} else if (repoType === 'Bitbucket') {
33-
pattern = editLinkPatternBitbucket
31+
} else if (repoType !== null) {
32+
pattern = editLinkPatterns[repoType]
3433
}
3534

3635
if (!pattern) return null

0 commit comments

Comments
 (0)