From 7bd678aeeba9a040398d59bec0f35f136e9bf707 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Mon, 6 Jun 2022 11:59:20 +0530 Subject: [PATCH] refactor: simplify --- docs/.vitepress/config.ts | 3 +- .../theme-default/composables/edit-link.ts | 36 ++----------------- types/default-theme.d.ts | 27 ++------------ 3 files changed, 6 insertions(+), 60 deletions(-) diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 3cf6255e..23eeac47 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -16,8 +16,7 @@ export default defineConfig({ }, editLink: { - repo: 'vuejs/vitepress', - dir: 'docs', + pattern: 'https://github.com/vuejs/vitepress/edit/main/docs/:path', 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 a42f3ecc..34dffc9a 100644 --- a/src/client/theme-default/composables/edit-link.ts +++ b/src/client/theme-default/composables/edit-link.ts @@ -1,45 +1,13 @@ 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 (/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() { const { theme, page } = useData() return computed(() => { - const { - repo = '', - branch = 'main', - dir = '', - text = 'Edit this page', - pattern = '' - } = theme.value.editLink || {} + const { text = 'Edit this page', pattern } = theme.value.editLink || {} const { relativePath } = page.value - - 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/') - + const url = pattern.replace(/:path/g, relativePath) return { url, text } }) } diff --git a/types/default-theme.d.ts b/types/default-theme.d.ts index 6118d3da..8053cbf6 100644 --- a/types/default-theme.d.ts +++ b/types/default-theme.d.ts @@ -119,27 +119,6 @@ export namespace DefaultTheme { // edit link ----------------------------------------------------------------- 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. * @@ -148,11 +127,11 @@ export namespace DefaultTheme { 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 ---------------------------------------------------------------