fix: allow bitbucket, gitlab, gitee in edit links

pull/698/head
Divyansh Singh 3 years ago
parent 0927a7393b
commit 8f274b61cb

@ -17,7 +17,6 @@ export default defineConfig({
editLink: {
repo: 'vuejs/vitepress',
branch: 'next',
dir: 'docs',
text: 'Edit this page on GitHub'
},

@ -1,26 +1,41 @@
import { computed } from 'vue'
import { useData } from 'vitepress'
const editLinkPatterns = {
GitHub: ':repo/edit/:branch/:path',
GitLab: ':repo/-/edit/:branch/:path',
Gitee: ':repo/edit/:branch/:path',
Bitbucket:
':repo/src/:branch/:path?mode=edit&spa=0&at=:branch&fileviewer=file-view-default'
}
function resolveRepoType(repo: string) {
if (/bitbucket\.org/.test(repo)) return 'Bitbucket'
if (/gitlab\.com/.test(repo)) return 'GitLab'
if (/gitee\.com/.test(repo)) return 'Gitee'
return 'GitHub'
}
export function useEditLink() {
const { theme, page } = useData()
return computed(() => {
const url = [
'https://github.com',
theme.value.editLink?.repo || '???',
'edit',
theme.value.editLink?.branch || 'main',
theme.value.editLink?.dir || null,
page.value.relativePath
]
.filter((v) => v)
.join('/')
const {
repo = '???',
branch = 'main',
dir = '',
text = 'Edit this page'
} = theme.value.editLink || {}
const { relativePath } = page.value
const text = theme.value.editLink?.text ?? 'Edit this page'
const base = /:\/\//.test(repo) ? repo : `https://github.com/${repo}`
const pattern = editLinkPatterns[resolveRepoType(base)]
const url = pattern
.replace(/:repo/g, base)
.replace(/:branch/g, branch)
.replace(/:path/g, `${dir}/${relativePath}`)
.replace(/([^:])\/\//g, '$1/')
return {
url,
text
}
return { url, text }
})
}

Loading…
Cancel
Save