From c3be5472e64ed82b9688573c65490cea74830e54 Mon Sep 17 00:00:00 2001 From: Shintaro Tanaka Date: Tue, 21 Jul 2020 01:29:51 +0900 Subject: [PATCH 1/7] feat: add git repo link --- .../theme-default/components/NavBar.vue | 2 +- .../theme-default/components/NavRepoLink.ts | 47 +++++++++++++++++++ .../theme-default/components/NavRepoLink.vue | 22 +++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/client/theme-default/components/NavRepoLink.ts create mode 100644 src/client/theme-default/components/NavRepoLink.vue diff --git a/src/client/theme-default/components/NavBar.vue b/src/client/theme-default/components/NavBar.vue index b99bfafafbff..564b07ecbca7 100644 --- a/src/client/theme-default/components/NavBar.vue +++ b/src/client/theme-default/components/NavBar.vue @@ -12,7 +12,7 @@ /> {{ $site.title }} - + diff --git a/src/client/theme-default/components/NavRepoLink.ts b/src/client/theme-default/components/NavRepoLink.ts new file mode 100644 index 000000000000..a0b105a2cdd6 --- /dev/null +++ b/src/client/theme-default/components/NavRepoLink.ts @@ -0,0 +1,47 @@ +import { computed, defineComponent, PropType } from 'vue' +import OutboundLink from './icons/OutboundLink.vue' +import { DefaultTheme } from '/@theme/config' + +export default defineComponent({ + components: { + OutboundLink + }, + + props: { + editLinkConfig: { + type: Object as PropType, + required: true + } + }, + + setup(props) { + const editLinkConfig = props.editLinkConfig + const repoLink = computed(() => { + if (editLinkConfig && editLinkConfig.repo) { + return /^https?:/.test(editLinkConfig.repo) + ? editLinkConfig.repo + : `https://github.com/${editLinkConfig.repo}` + } + return null + }) + const repoLabel = computed(() => { + if (!repoLink.value) return + const repoHosts = repoLink.value.match(/^https?:\/\/[^/]+/) + if (!repoHosts) return + const repoHost = repoHosts[0] + const platforms = ['GitHub', 'GitLab', 'Bitbucket'] + for (let i = 0; i < platforms.length; i++) { + const platform = platforms[i] + if (new RegExp(platform, 'i').test(repoHost)) { + return platform + } + } + + return 'Source' + }) + return { + repoLink, + repoLabel + } + } +}) diff --git a/src/client/theme-default/components/NavRepoLink.vue b/src/client/theme-default/components/NavRepoLink.vue new file mode 100644 index 000000000000..f10912745ebe --- /dev/null +++ b/src/client/theme-default/components/NavRepoLink.vue @@ -0,0 +1,22 @@ + + + + + From 8b50db871ab74323697a290ab13a914db6f815a5 Mon Sep 17 00:00:00 2001 From: Shintaro Tanaka Date: Thu, 23 Jul 2020 20:35:05 +0900 Subject: [PATCH 2/7] feat: add edit links --- src/client/theme-default/components/Page.vue | 7 ++ .../theme-default/components/PageEdit.ts | 81 +++++++++++++++++++ .../theme-default/components/PageEdit.vue | 32 ++++++++ src/client/theme-default/config.ts | 1 + src/client/theme-default/utils.ts | 1 + src/node/markdownToVue.ts | 1 + types/shared.d.ts | 1 + 7 files changed, 124 insertions(+) create mode 100644 src/client/theme-default/components/PageEdit.ts create mode 100644 src/client/theme-default/components/PageEdit.vue diff --git a/src/client/theme-default/components/Page.vue b/src/client/theme-default/components/Page.vue index 01098552ef1c..7d1981a852d4 100644 --- a/src/client/theme-default/components/Page.vue +++ b/src/client/theme-default/components/Page.vue @@ -2,6 +2,7 @@
+
@@ -35,3 +36,9 @@ export default { margin-top: 0; } */ + \ No newline at end of file diff --git a/src/client/theme-default/components/PageEdit.ts b/src/client/theme-default/components/PageEdit.ts new file mode 100644 index 000000000000..753a5fa98184 --- /dev/null +++ b/src/client/theme-default/components/PageEdit.ts @@ -0,0 +1,81 @@ +import { defineComponent, computed } from 'vue' +import OutboundLink from './icons/OutboundLink.vue' +import { endingSlashRE, isExternal } from '/@theme/utils' +import { usePageData, useSiteData, useSiteDataByRoute } from 'vitepress' + +function createEditLink( + repo: string, + docsRepo: string, + docsDir: string, + docsBranch: string, + path: string +) { + const bitbucket = /bitbucket.org/ + if (bitbucket.test(repo)) { + const base = isExternal(docsRepo) ? docsRepo : repo + return ( + base.replace(endingSlashRE, '') + + `/src` + + `/${docsBranch}/` + + (docsDir ? docsDir.replace(endingSlashRE, '') + '/' : '') + + path + + `?mode=edit&spa=0&at=${docsBranch}&fileviewer=file-view-default` + ) + } + + const base = isExternal(docsRepo) + ? docsRepo + : `https://github.com/${docsRepo}` + return ( + base.replace(endingSlashRE, '') + + `/edit` + + `/${docsBranch}/` + + (docsDir ? docsDir.replace(endingSlashRE, '') + '/' : '') + + path + ) +} + +export default defineComponent({ + components: { + OutboundLink + }, + + setup() { + const pageData = usePageData() + const siteData = useSiteData() + const siteDataByRoute = useSiteDataByRoute() + + const { + repo, + text, + dir = '', + branch = 'master', + docsRepo = repo + } = siteData.value.themeConfig.editLink + const { relativePath } = pageData.value + + const editLink = computed(() => { + const showEditLink = + pageData.value.frontmatter.editLink == null + ? siteData.value.themeConfig.editLink + : pageData.value.frontmatter.editLink + + if (showEditLink && relativePath) { + return createEditLink(repo, docsRepo, dir, branch, relativePath) + } + return null + }) + const editLinkText = computed(() => { + return ( + siteDataByRoute.value.themeConfig.editLink.text || + text || + `Edit this page` + ) + }) + + return { + editLink, + editLinkText + } + } +}) diff --git a/src/client/theme-default/components/PageEdit.vue b/src/client/theme-default/components/PageEdit.vue new file mode 100644 index 000000000000..5d8b0829ab36 --- /dev/null +++ b/src/client/theme-default/components/PageEdit.vue @@ -0,0 +1,32 @@ + + + + + diff --git a/src/client/theme-default/config.ts b/src/client/theme-default/config.ts index 7fe5f028867a..b8c72e70115a 100644 --- a/src/client/theme-default/config.ts +++ b/src/client/theme-default/config.ts @@ -75,6 +75,7 @@ export namespace DefaultTheme { export interface EditLinkConfig { repo: string + docsRepo?: string dir?: string branch?: string text?: string diff --git a/src/client/theme-default/utils.ts b/src/client/theme-default/utils.ts index b2ff533ed543..d0b97b95523b 100644 --- a/src/client/theme-default/utils.ts +++ b/src/client/theme-default/utils.ts @@ -2,6 +2,7 @@ import { useSiteData, Route } from 'vitepress' export const hashRE = /#.*$/ export const extRE = /\.(md|html)$/ +export const endingSlashRE = /\/$/ export const outboundRE = /^[a-z]+:/i export function withBase(path: string) { diff --git a/src/node/markdownToVue.ts b/src/node/markdownToVue.ts index cb6c917ca029..f6715b1e349a 100644 --- a/src/node/markdownToVue.ts +++ b/src/node/markdownToVue.ts @@ -43,6 +43,7 @@ export function createMarkdownToVueRenderFn( title: inferTitle(frontmatter, content), frontmatter, headers: data.headers, + relativePath: file.replace(/\\/g, '/'), lastUpdated } diff --git a/types/shared.d.ts b/types/shared.d.ts index 3cf71d5dfa2e..840e6aebbea5 100644 --- a/types/shared.d.ts +++ b/types/shared.d.ts @@ -25,6 +25,7 @@ export interface PageData { title: string frontmatter: Record headers: Header[] + relativePath: string lastUpdated: number next?: { text: string; link: string } prev?: { text: string; link: string } From 0bf149f5a9c25dd82c0e7548299e0b88f3d05dc3 Mon Sep 17 00:00:00 2001 From: Shintaro Tanaka Date: Sat, 5 Sep 2020 00:52:08 +0900 Subject: [PATCH 3/7] fix conflict resolution mistakes --- src/client/theme-default/components/Page.vue | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/client/theme-default/components/Page.vue b/src/client/theme-default/components/Page.vue index 7d1981a852d4..bf496041ece8 100644 --- a/src/client/theme-default/components/Page.vue +++ b/src/client/theme-default/components/Page.vue @@ -8,8 +8,9 @@ @@ -36,9 +37,3 @@ export default { margin-top: 0; } */ - \ No newline at end of file From b3cab2ef900491d79e354bfe138b3c9a4b7d9b34 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 16 Sep 2020 15:40:07 +0200 Subject: [PATCH 4/7] refactor: add back the changes --- src/client/theme-default/components/NavBarLinks.ts | 9 ++++++--- src/client/theme-default/components/NavBarLinks.vue | 11 +++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/client/theme-default/components/NavBarLinks.ts b/src/client/theme-default/components/NavBarLinks.ts index e41aa966e6a2..9e5367ad665f 100644 --- a/src/client/theme-default/components/NavBarLinks.ts +++ b/src/client/theme-default/components/NavBarLinks.ts @@ -1,12 +1,14 @@ import { computed } from 'vue' -import { useSiteDataByRoute } from 'vitepress' +import { useSiteData, useSiteDataByRoute } from 'vitepress' import NavBarLink from './NavBarLink.vue' import NavDropdownLink from './NavDropdownLink.vue' +import NavRepoLink from './NavRepoLink.vue' export default { components: { NavBarLink, - NavDropdownLink + NavDropdownLink, + NavRepoLink }, setup() { @@ -16,7 +18,8 @@ export default { ? // navbar items do not change in production useSiteDataByRoute().value.themeConfig.nav : // use computed in dev for hot reload - computed(() => useSiteDataByRoute().value.themeConfig.nav) + computed(() => useSiteDataByRoute().value.themeConfig.nav), + editLinkConfig: computed(() => useSiteData().value.themeConfig.editLink) } } } diff --git a/src/client/theme-default/components/NavBarLinks.vue b/src/client/theme-default/components/NavBarLinks.vue index df0907aea1ef..7ddb278236b0 100644 --- a/src/client/theme-default/components/NavBarLinks.vue +++ b/src/client/theme-default/components/NavBarLinks.vue @@ -1,8 +1,11 @@ - + diff --git a/src/client/theme-default/components/NavRepoLink.ts b/src/client/theme-default/components/NavRepoLink.ts deleted file mode 100644 index a0b105a2cdd6..000000000000 --- a/src/client/theme-default/components/NavRepoLink.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { computed, defineComponent, PropType } from 'vue' -import OutboundLink from './icons/OutboundLink.vue' -import { DefaultTheme } from '/@theme/config' - -export default defineComponent({ - components: { - OutboundLink - }, - - props: { - editLinkConfig: { - type: Object as PropType, - required: true - } - }, - - setup(props) { - const editLinkConfig = props.editLinkConfig - const repoLink = computed(() => { - if (editLinkConfig && editLinkConfig.repo) { - return /^https?:/.test(editLinkConfig.repo) - ? editLinkConfig.repo - : `https://github.com/${editLinkConfig.repo}` - } - return null - }) - const repoLabel = computed(() => { - if (!repoLink.value) return - const repoHosts = repoLink.value.match(/^https?:\/\/[^/]+/) - if (!repoHosts) return - const repoHost = repoHosts[0] - const platforms = ['GitHub', 'GitLab', 'Bitbucket'] - for (let i = 0; i < platforms.length; i++) { - const platform = platforms[i] - if (new RegExp(platform, 'i').test(repoHost)) { - return platform - } - } - - return 'Source' - }) - return { - repoLink, - repoLabel - } - } -}) diff --git a/src/client/theme-default/components/NavRepoLink.vue b/src/client/theme-default/components/NavRepoLink.vue deleted file mode 100644 index f10912745ebe..000000000000 --- a/src/client/theme-default/components/NavRepoLink.vue +++ /dev/null @@ -1,22 +0,0 @@ - - - - - diff --git a/src/client/theme-default/components/PageEdit.ts b/src/client/theme-default/components/PageEdit.ts index 753a5fa98184..761d9bae5635 100644 --- a/src/client/theme-default/components/PageEdit.ts +++ b/src/client/theme-default/components/PageEdit.ts @@ -1,7 +1,8 @@ -import { defineComponent, computed } from 'vue' +import { computed } from 'vue' import OutboundLink from './icons/OutboundLink.vue' import { endingSlashRE, isExternal } from '/@theme/utils' -import { usePageData, useSiteData, useSiteDataByRoute } from 'vitepress' +import { usePageData, useSiteData } from 'vitepress' +import { DefaultTheme } from '../config' function createEditLink( repo: string, @@ -35,47 +36,46 @@ function createEditLink( ) } -export default defineComponent({ +export default { components: { OutboundLink }, setup() { const pageData = usePageData() - const siteData = useSiteData() - const siteDataByRoute = useSiteDataByRoute() - - const { - repo, - text, - dir = '', - branch = 'master', - docsRepo = repo - } = siteData.value.themeConfig.editLink - const { relativePath } = pageData.value + const siteData = useSiteData() const editLink = computed(() => { - const showEditLink = + const showEditLink: boolean | undefined = pageData.value.frontmatter.editLink == null - ? siteData.value.themeConfig.editLink + ? siteData.value.themeConfig.editLinks : pageData.value.frontmatter.editLink + const { + repo, + docsDir = '', + docsBranch = 'master', + docsRepo = repo + } = siteData.value.themeConfig - if (showEditLink && relativePath) { - return createEditLink(repo, docsRepo, dir, branch, relativePath) + const { relativePath } = pageData.value + if (showEditLink && relativePath && repo) { + return createEditLink( + repo, + docsRepo || repo, + docsDir, + docsBranch, + relativePath + ) } return null }) - const editLinkText = computed(() => { - return ( - siteDataByRoute.value.themeConfig.editLink.text || - text || - `Edit this page` - ) - }) + const editLinkText = computed( + () => siteData.value.themeConfig.editLinkText || 'Edit this page' + ) return { editLink, editLinkText } } -}) +} diff --git a/src/client/theme-default/components/PageEdit.vue b/src/client/theme-default/components/PageEdit.vue index 5d8b0829ab36..c76aecdd4c98 100644 --- a/src/client/theme-default/components/PageEdit.vue +++ b/src/client/theme-default/components/PageEdit.vue @@ -1,15 +1,10 @@ @@ -27,6 +22,6 @@ } .page-edit .edit-link a { color: #4e6e8e; - margin-right: 0.25rem + margin-right: 0.25rem; } diff --git a/src/client/theme-default/config.ts b/src/client/theme-default/config.ts index b8c72e70115a..31b2af74befa 100644 --- a/src/client/theme-default/config.ts +++ b/src/client/theme-default/config.ts @@ -4,7 +4,48 @@ export namespace DefaultTheme { nav?: NavItem[] | false sidebar?: SideBarConfig | MultiSideBarConfig search?: SearchConfig | false - editLink?: EditLinkConfig | false + + /** + * GitHub repository following the format /. + * + * @example vuejs/vue-next + */ + repo?: string + /** + * Customize the header label. Defaults to GitHub/Gitlab/Bitbucket depending + * on the provided repo + * + * @exampe `"Contribute!"` + */ + repoLabel?: string + + /** + * If your docs are in a different repository from your main project + * + * @example `"vuejs/docs-next"` + */ + docsRepo?: string + /** + * If your docs are not at the root of the repo. + * + * @example `"docs"` + */ + docsDir?: string + /** + * If your docs are in a different branch. Defaults to `master` + * @example `"next"` + */ + docsBranch?: string + + /** + * Enable links to edit pages at the bottom of the page + */ + editLinks?: boolean + /** + * Custom text for edit link. Defaults to "Edit this page" + */ + editLinkText?: string + lastUpdated?: string | boolean prevLink?: boolean nextLink?: boolean @@ -70,14 +111,4 @@ export namespace DefaultTheme { indexName: string } } - - // edit link ----------------------------------------------------------------- - - export interface EditLinkConfig { - repo: string - docsRepo?: string - dir?: string - branch?: string - text?: string - } } From a832cca7beaadf587874a0f6819e69d9101fb88d Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 17 Sep 2020 12:07:15 +0200 Subject: [PATCH 6/7] Update src/client/theme-default/components/NavBarLinks.ts --- src/client/theme-default/components/NavBarLinks.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/client/theme-default/components/NavBarLinks.ts b/src/client/theme-default/components/NavBarLinks.ts index 671afb810c53..f1133d63cf43 100644 --- a/src/client/theme-default/components/NavBarLinks.ts +++ b/src/client/theme-default/components/NavBarLinks.ts @@ -19,13 +19,6 @@ export default { const siteData = useSiteData() const repoInfo = computed(() => { const theme = siteData.value.themeConfig as DefaultTheme.Config - // return theme.editLinks - // ? { - // repo: theme.docsRepo || theme.repo, - // label: theme.repoLabel - // } - // : null - const repo = theme.docsRepo || theme.repo let text: string | undefined = theme.repoLabel From a4ff6dbf561ac4931a9212b8e9a053d1039a8009 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 17 Sep 2020 14:47:00 +0200 Subject: [PATCH 7/7] refactor: remove unecessary wrapper --- .../theme-default/components/NavBarLinks.vue | 4 ++-- .../theme-default/components/PageEdit.vue | 23 ++++++++++--------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/client/theme-default/components/NavBarLinks.vue b/src/client/theme-default/components/NavBarLinks.vue index 9fcd03d29318..ab02e2b4928c 100644 --- a/src/client/theme-default/components/NavBarLinks.vue +++ b/src/client/theme-default/components/NavBarLinks.vue @@ -1,12 +1,12 @@ diff --git a/src/client/theme-default/components/PageEdit.vue b/src/client/theme-default/components/PageEdit.vue index c76aecdd4c98..8070dedf4003 100644 --- a/src/client/theme-default/components/PageEdit.vue +++ b/src/client/theme-default/components/PageEdit.vue @@ -1,11 +1,14 @@ @@ -17,11 +20,9 @@ padding-bottom: 1rem; overflow: auto; } -.page-edit .edit-link { - display: inline-block; -} -.page-edit .edit-link a { - color: #4e6e8e; + +.page-edit a { + color: var(--text-color); margin-right: 0.25rem; }