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: { editLink: {
repo: 'vuejs/vitepress', repo: 'vuejs/vitepress',
branch: 'next',
dir: 'docs', dir: 'docs',
text: 'Edit this page on GitHub' text: 'Edit this page on GitHub'
}, },

@ -1,26 +1,41 @@
import { computed } from 'vue' import { computed } from 'vue'
import { useData } from 'vitepress' 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() { export function useEditLink() {
const { theme, page } = useData() const { theme, page } = useData()
return computed(() => { return computed(() => {
const url = [ const {
'https://github.com', repo = '???',
theme.value.editLink?.repo || '???', branch = 'main',
'edit', dir = '',
theme.value.editLink?.branch || 'main', text = 'Edit this page'
theme.value.editLink?.dir || null, } = theme.value.editLink || {}
page.value.relativePath const { relativePath } = page.value
]
.filter((v) => v)
.join('/')
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 { return { url, text }
url,
text
}
}) })
} }

Loading…
Cancel
Save