From e937fc0f1a28a3fc6167818cb76a3198d73a8593 Mon Sep 17 00:00:00 2001 From: zuofenghua <11100776@bbktel.com> Date: Fri, 23 Oct 2020 19:44:04 +0800 Subject: [PATCH 1/3] fix: fix switch language error #103 --- .../theme-default/components/NavBarLinks.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/client/theme-default/components/NavBarLinks.ts b/src/client/theme-default/components/NavBarLinks.ts index d34b8c40cc1a..413aab4dc13f 100644 --- a/src/client/theme-default/components/NavBarLinks.ts +++ b/src/client/theme-default/components/NavBarLinks.ts @@ -3,6 +3,7 @@ import { useSiteData, useSiteDataByRoute, useRoute } from 'vitepress' import NavBarLink from './NavBarLink.vue' import NavDropdownLink from './NavDropdownLink.vue' import { DefaultTheme } from '../config' +import { inBrowser } from '/@app/utils' const platforms = ['GitHub', 'GitLab', 'Bitbucket'].map( (platform) => [platform, new RegExp(platform, 'i')] as const @@ -52,19 +53,28 @@ export default { return null } + // handle site base + const siteBase = inBrowser ? siteData.value.base : '/' + const siteBaseWithoutSuffix = siteBase.endsWith('/') + ? siteBase.slice(0, -1) + : siteBase + // remove site base in browser env + const routerPath = route.path.slice(siteBaseWithoutSuffix.length) + const currentLangBase = localeKeys.find((v) => { if (v === '/') { return false } - return route.path.startsWith(v) + return routerPath.startsWith(v) }) const currentContentPath = currentLangBase - ? route.path.substring(currentLangBase.length - 1) - : route.path + ? routerPath.substring(currentLangBase.length - 1) + : routerPath const candidates = localeKeys.map((v) => { + const localePath = v.endsWith('/') ? v.slice(0, -1) : v return { text: locales[v].label || locales[v].lang, - link: `${v}${currentContentPath}` + link: `${localePath}${currentContentPath}` } }) From 944bf801b78cc78fb7d771045ee51e2120e36353 Mon Sep 17 00:00:00 2001 From: zuofenghua <11100776@bbktel.com> Date: Fri, 23 Oct 2020 20:59:30 +0800 Subject: [PATCH 2/3] fix: switch language bug. #106 --- src/client/theme-default/components/NavBarLinks.ts | 10 ++++------ src/shared/config.ts | 7 +++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/client/theme-default/components/NavBarLinks.ts b/src/client/theme-default/components/NavBarLinks.ts index 413aab4dc13f..85510f26127c 100644 --- a/src/client/theme-default/components/NavBarLinks.ts +++ b/src/client/theme-default/components/NavBarLinks.ts @@ -88,13 +88,11 @@ export default { } }) + const navData = computed(() => { + return siteDataByRoute.value.themeConfig.nav + }) return { - navData: - process.env.NODE_ENV === 'production' - ? // navbar items do not change in production - siteDataByRoute.value.themeConfig.nav - : // use computed in dev for hot reload - computed(() => siteDataByRoute.value.themeConfig.nav), + navData, repoInfo, localeCandidates } diff --git a/src/shared/config.ts b/src/shared/config.ts index 2480f7511a17..2d883c58ae6d 100644 --- a/src/shared/config.ts +++ b/src/shared/config.ts @@ -1,4 +1,5 @@ import { SiteData } from '../../types/shared' +const inBrowser = typeof window !== 'undefined' function findMatchRoot(route: string, roots: string[]) { // first match to the routes with the most deep level. @@ -27,6 +28,12 @@ function resolveLocales( // this merges the locales data to the main data by the route export function resolveSiteDataByRoute(siteData: SiteData, route: string) { + if (inBrowser) { + const siteBaseWithoutSuffix = siteData.base.endsWith('/') + ? siteData.base.slice(0, -1) + : siteData.base + route = route.slice(siteBaseWithoutSuffix.length) + } const localeData = resolveLocales(siteData.locales || {}, route) || {} const localeThemeConfig = resolveLocales( From 3a0af1aa2686406a5721658ce548dfcd7d6c9a0c Mon Sep 17 00:00:00 2001 From: Kia Ishii Date: Thu, 29 Oct 2020 20:07:44 +0900 Subject: [PATCH 3/3] styles: clean the code style a little bit --- .../theme-default/components/NavBarLinks.ts | 3 ++- src/shared/config.ts | 23 ++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/client/theme-default/components/NavBarLinks.ts b/src/client/theme-default/components/NavBarLinks.ts index 85510f26127c..f3623030efcf 100644 --- a/src/client/theme-default/components/NavBarLinks.ts +++ b/src/client/theme-default/components/NavBarLinks.ts @@ -1,9 +1,9 @@ import { computed } from 'vue' import { useSiteData, useSiteDataByRoute, useRoute } from 'vitepress' +import { inBrowser } from '/@app/utils' import NavBarLink from './NavBarLink.vue' import NavDropdownLink from './NavDropdownLink.vue' import { DefaultTheme } from '../config' -import { inBrowser } from '/@app/utils' const platforms = ['GitHub', 'GitLab', 'Bitbucket'].map( (platform) => [platform, new RegExp(platform, 'i')] as const @@ -91,6 +91,7 @@ export default { const navData = computed(() => { return siteDataByRoute.value.themeConfig.nav }) + return { navData, repoInfo, diff --git a/src/shared/config.ts b/src/shared/config.ts index 2d883c58ae6d..2946c23d3e95 100644 --- a/src/shared/config.ts +++ b/src/shared/config.ts @@ -1,4 +1,5 @@ import { SiteData } from '../../types/shared' + const inBrowser = typeof window !== 'undefined' function findMatchRoot(route: string, roots: string[]) { @@ -28,12 +29,8 @@ function resolveLocales( // this merges the locales data to the main data by the route export function resolveSiteDataByRoute(siteData: SiteData, route: string) { - if (inBrowser) { - const siteBaseWithoutSuffix = siteData.base.endsWith('/') - ? siteData.base.slice(0, -1) - : siteData.base - route = route.slice(siteBaseWithoutSuffix.length) - } + route = cleanRoute(siteData, route) + const localeData = resolveLocales(siteData.locales || {}, route) || {} const localeThemeConfig = resolveLocales( @@ -53,3 +50,17 @@ export function resolveSiteDataByRoute(siteData: SiteData, route: string) { locales: {} } } + +/** + * Clean up the route by removing the `base` path if it's set in config. + */ +function cleanRoute(siteData: SiteData, route: string): string { + if (!inBrowser) { + return route + } + + const base = siteData.base + const baseWithoutSuffix = base.endsWith('/') ? base.slice(0, -1) : base + + return route.slice(baseWithoutSuffix.length) +}