feat: allow custom patterns in edit links

pull/698/head
Divyansh Singh 3 years ago
parent 8f274b61cb
commit 4807d20201

@ -10,10 +10,11 @@ const editLinkPatterns = {
} }
function resolveRepoType(repo: string) { function resolveRepoType(repo: string) {
if (/github\.com/.test(repo)) return 'GitHub'
if (/bitbucket\.org/.test(repo)) return 'Bitbucket' if (/bitbucket\.org/.test(repo)) return 'Bitbucket'
if (/gitlab\.com/.test(repo)) return 'GitLab' if (/gitlab\.com/.test(repo)) return 'GitLab'
if (/gitee\.com/.test(repo)) return 'Gitee' if (/gitee\.com/.test(repo)) return 'Gitee'
return 'GitHub' return null
} }
export function useEditLink() { export function useEditLink() {
@ -21,16 +22,19 @@ export function useEditLink() {
return computed(() => { return computed(() => {
const { const {
repo = '???', repo = '',
branch = 'main', branch = 'main',
dir = '', dir = '',
text = 'Edit this page' text = 'Edit this page',
pattern = ''
} = theme.value.editLink || {} } = theme.value.editLink || {}
const { relativePath } = page.value const { relativePath } = page.value
const base = /:\/\//.test(repo) ? repo : `https://github.com/${repo}` const base = /:\/\//.test(repo) ? repo : `https://github.com/${repo}`
const pattern = editLinkPatterns[resolveRepoType(base)] const type = resolveRepoType(base)
const url = pattern const urlPattern = pattern || !type ? pattern : editLinkPatterns[type]
const url = urlPattern
.replace(/:repo/g, base) .replace(/:repo/g, base)
.replace(/:branch/g, branch) .replace(/:branch/g, branch)
.replace(/:path/g, `${dir}/${relativePath}`) .replace(/:path/g, `${dir}/${relativePath}`)

@ -146,6 +146,13 @@ export namespace DefaultTheme {
* @default 'Edit this page' * @default 'Edit this page'
*/ */
text?: string text?: string
/**
* Custom pattern for edit link.
*
* @example ':repo/-/edit/:branch/:path'
*/
pattern?: string
} }
// social link --------------------------------------------------------------- // social link ---------------------------------------------------------------

Loading…
Cancel
Save