From 3a0af1aa2686406a5721658ce548dfcd7d6c9a0c Mon Sep 17 00:00:00 2001 From: Kia Ishii Date: Thu, 29 Oct 2020 20:07:44 +0900 Subject: [PATCH] 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 85510f26..f3623030 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 2d883c58..2946c23d 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) +}