@ -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 }
} )
} )
}
}