From 8f274b61cbc8c9ed7a630e68dc15235081aca520 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Thu, 2 Jun 2022 15:34:16 +0530 Subject: [PATCH] fix: allow bitbucket, gitlab, gitee in edit links --- docs/.vitepress/config.ts | 1 - .../theme-default/composables/edit-link.ts | 45 ++++++++++++------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index f3581977..3cf6255e 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -17,7 +17,6 @@ export default defineConfig({ editLink: { repo: 'vuejs/vitepress', - branch: 'next', dir: 'docs', text: 'Edit this page on GitHub' }, diff --git a/src/client/theme-default/composables/edit-link.ts b/src/client/theme-default/composables/edit-link.ts index 32bf66c2..d3e80086 100644 --- a/src/client/theme-default/composables/edit-link.ts +++ b/src/client/theme-default/composables/edit-link.ts @@ -1,26 +1,41 @@ 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 (/bitbucket\.org/.test(repo)) return 'Bitbucket' + if (/gitlab\.com/.test(repo)) return 'GitLab' + if (/gitee\.com/.test(repo)) return 'Gitee' + return 'GitHub' +} + export function useEditLink() { const { theme, page } = useData() return computed(() => { - const url = [ - 'https://github.com', - theme.value.editLink?.repo || '???', - 'edit', - theme.value.editLink?.branch || 'main', - theme.value.editLink?.dir || null, - page.value.relativePath - ] - .filter((v) => v) - .join('/') + const { + repo = '???', + branch = 'main', + dir = '', + text = 'Edit this page' + } = theme.value.editLink || {} + const { relativePath } = page.value - const text = theme.value.editLink?.text ?? 'Edit this page' + const base = /:\/\//.test(repo) ? repo : `https://github.com/${repo}` + const pattern = editLinkPatterns[resolveRepoType(base)] + const url = pattern + .replace(/:repo/g, base) + .replace(/:branch/g, branch) + .replace(/:path/g, `${dir}/${relativePath}`) + .replace(/([^:])\/\//g, '$1/') - return { - url, - text - } + return { url, text } }) }