refactor: simplify

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

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

@ -1,45 +1,13 @@
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 (/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() { export function useEditLink() {
const { theme, page } = useData() const { theme, page } = useData()
return computed(() => { return computed(() => {
const { const { text = 'Edit this page', pattern } = theme.value.editLink || {}
repo = '',
branch = 'main',
dir = '',
text = 'Edit this page',
pattern = ''
} = theme.value.editLink || {}
const { relativePath } = page.value const { relativePath } = page.value
const url = pattern.replace(/:path/g, relativePath)
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/')
return { url, text } return { url, text }
}) })
} }

@ -119,27 +119,6 @@ export namespace DefaultTheme {
// edit link ----------------------------------------------------------------- // edit link -----------------------------------------------------------------
export interface EditLink { 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. * Custom text for edit link.
* *
@ -148,11 +127,11 @@ export namespace DefaultTheme {
text?: string 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 --------------------------------------------------------------- // social link ---------------------------------------------------------------

Loading…
Cancel
Save