refactor: simplify

pull/698/head
Divyansh Singh 3 years ago
parent 4807d20201
commit 7bd678aeeb

@ -16,8 +16,7 @@ export default defineConfig({
},
editLink: {
repo: 'vuejs/vitepress',
dir: 'docs',
pattern: 'https://github.com/vuejs/vitepress/edit/main/docs/:path',
text: 'Edit this page on GitHub'
},

@ -1,45 +1,13 @@
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 (/github\.com/.test(repo)) return 'GitHub'
if (/bitbucket\.org/.test(repo)) return 'Bitbucket'
if (/gitlab\.com/.test(repo)) return 'GitLab'
if (/gitee\.com/.test(repo)) return 'Gitee'
return null
}
export function useEditLink() {
const { theme, page } = useData()
return computed(() => {
const {
repo = '',
branch = 'main',
dir = '',
text = 'Edit this page',
pattern = ''
} = theme.value.editLink || {}
const { text = 'Edit this page', pattern } = theme.value.editLink || {}
const { relativePath } = page.value
const base = /:\/\//.test(repo) ? repo : `https://github.com/${repo}`
const type = resolveRepoType(base)
const urlPattern = pattern || !type ? pattern : editLinkPatterns[type]
const url = urlPattern
.replace(/:repo/g, base)
.replace(/:branch/g, branch)
.replace(/:path/g, `${dir}/${relativePath}`)
.replace(/([^:])\/\//g, '$1/')
const url = pattern.replace(/:path/g, relativePath)
return { url, text }
})
}

@ -119,27 +119,6 @@ export namespace DefaultTheme {
// edit link -----------------------------------------------------------------
export interface EditLink {
/**
* Repo of the site.
*
* @example 'vuejs/docs'
*/
repo: string
/**
* Branch of the repo.
*
* @default 'main'
*/
branch?: string
/**
* If your docs are not at the root of the repo.
*
* @example 'docs'
*/
dir?: string
/**
* Custom text for edit link.
*
@ -148,11 +127,11 @@ export namespace DefaultTheme {
text?: string
/**
* Custom pattern for edit link.
* Pattern for edit link.
*
* @example ':repo/-/edit/:branch/:path'
* @example 'https://github.com/vuejs/vitepress/edit/main/docs/:path'
*/
pattern?: string
pattern: string
}
// social link ---------------------------------------------------------------

Loading…
Cancel
Save