feat: allow custom edit link style

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/697/head
Percy Ma 3 years ago
parent ed291fc426
commit 267c702d0e
No known key found for this signature in database
GPG Key ID: A1803D3315E6CCBC

@ -16,8 +16,9 @@ export default defineConfig({
}, },
editLink: { editLink: {
domain: 'gitlab.com',
repo: 'vuejs/vitepress', repo: 'vuejs/vitepress',
branch: 'next', branch: 'main',
dir: 'docs', dir: 'docs',
text: 'Edit this page on GitHub' text: 'Edit this page on GitHub'
}, },

@ -194,13 +194,22 @@ export interface Footer {
Edit link configuration. You can then customize git repo link and display text. Edit link configuration. You can then customize git repo link and display text.
We have built-in recognition of common git services, you just need to configure the domain. If you're using a self-built service (like GitLab) you can specify or customize the link style.
Available parameters:
- `:repo`: The `repo` you configured
- `:branch`: Ibid
- `:path`: `dir` + file path
```ts ```ts
export default { export default {
themeConfig: { themeConfig: {
editLink: { editLink: {
domain: 'github.com' style: ':repo/edit/:branch/:path',
domain: 'github.com',
repo: 'vuejs/vitepress', repo: 'vuejs/vitepress',
branch: 'next', branch: 'main',
dir: 'docs', dir: 'docs',
text: 'Edit this page on GitHub' text: 'Edit this page on GitHub'
}, },
@ -210,6 +219,12 @@ export default {
```ts ```ts
export interface EditLink { export interface EditLink {
/**
* Git service edit link style.
*
* @default 'github'
*/
style?: 'github' | 'gitlab' | 'bitbucket' | string
/** /**
* Domain of git repo * Domain of git repo
* *

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

@ -120,7 +120,14 @@ export namespace DefaultTheme {
export interface EditLink { export interface EditLink {
/** /**
* Domain of git repo * Git service edit link style.
*
* @default 'github'
*/
style?: 'github' | 'gitlab' | 'bitbucket' | string
/**
* Domain of the git service.
* *
* @example 'github.com' or 'https://github.com' * @example 'github.com' or 'https://github.com'
*/ */

Loading…
Cancel
Save