Skip to content

Commit

Permalink
fix: fix switch language error (#103, #106) (#104)
Browse files Browse the repository at this point in the history
fix #103
fix #106

Co-authored-by: zuofenghua <[email protected]>
Co-authored-by: Kia Ishii <[email protected]>
  • Loading branch information
3 people committed Oct 29, 2020
1 parent 123cc73 commit d354d1e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/client/theme-default/components/NavBarLinks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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'
Expand Down Expand Up @@ -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}`
}
})

Expand All @@ -78,13 +88,12 @@ 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
}
Expand Down
18 changes: 18 additions & 0 deletions src/shared/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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.
roots.sort((a, b) => {
Expand Down Expand Up @@ -27,6 +29,8 @@ function resolveLocales<T>(

// this merges the locales data to the main data by the route
export function resolveSiteDataByRoute(siteData: SiteData, route: string) {
route = cleanRoute(siteData, route)

const localeData = resolveLocales(siteData.locales || {}, route) || {}
const localeThemeConfig =
resolveLocales<any>(
Expand All @@ -46,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)
}

0 comments on commit d354d1e

Please sign in to comment.