Skip to content

Commit

Permalink
fix: language dropdown with current path
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Aug 5, 2021
1 parent 6a767b7 commit dd8cfbc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
8 changes: 5 additions & 3 deletions src/client/app/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ export function initData(route: Route): VitePressData {
frontmatter: computed(() => route.data.frontmatter),
lang: computed(() => site.value.lang),
localePath: computed(() => {
const { locales, lang } = site.value
const path = Object.keys(locales).find((lp) => locales[lp].lang === lang)
return withBase((locales && path) || '/')
const { langs } = site.value
const path = Object.keys(langs).find(
(lp) => langs[lp].lang === site.value.lang
)
return withBase(path || '/')
}),
title: computed(() => {
return route.data.title
Expand Down
17 changes: 8 additions & 9 deletions src/client/theme-default/composables/nav.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { computed } from 'vue'
import { useData } from 'vitepress'
import { useData, useRoute } from 'vitepress'
import type { DefaultTheme } from '../config'

export function useLanguageLinks() {
Expand All @@ -13,16 +13,15 @@ export function useLanguageLinks() {
return null
}

const routerPath = localePath.value
const route = useRoute()

const candidates = localePaths.map((v) => {
const localePath = v.endsWith('/') ? v.slice(0, -1) : v
// intentionally remove the leading slash because each locale has one
const currentPath = route.path.replace(localePath.value, '')

return {
text: langs[v],
link: `${localePath}${routerPath}`
}
})
const candidates = localePaths.map((v) => ({
text: langs[v].label,
link: `${v}${currentPath}`
}))

const selectText = theme.value.selectText || 'Languages'

Expand Down
4 changes: 1 addition & 3 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ export async function resolveSiteData(
head: userConfig.head || [],
themeConfig: userConfig.themeConfig || {},
locales: userConfig.locales || {},
langs: createLangDictionary(
userConfig.themeConfig && userConfig.themeConfig.locales
),
langs: createLangDictionary(userConfig),
customData: userConfig.customData || {}
}
}
20 changes: 14 additions & 6 deletions src/shared/shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SiteData } from '../../types/shared'
import { LocaleConfig, SiteData } from '../../types/shared'

export type {
SiteData,
Expand Down Expand Up @@ -36,12 +36,20 @@ function resolveLocales<T>(
return localeRoot ? locales[localeRoot] : undefined
}

export function createLangDictionary(locales: any | undefined) {
return locales
export function createLangDictionary(siteData: {
themeConfig?: any
locales?: Record<string, LocaleConfig>
}) {
const { locales } = siteData.themeConfig
const siteLocales = siteData.locales
return locales && siteLocales
? Object.keys(locales).reduce((langs, path) => {
langs[path] = locales![path].label
langs[path] = {
label: locales![path].label,
lang: siteLocales[path].lang
}
return langs
}, {} as Record<string, string>)
}, {} as Record<string, { lang: string; label: string }>)
: {}
}

Expand Down Expand Up @@ -70,7 +78,7 @@ export function resolveSiteDataByRoute(
lang: (localeData || siteData).lang,
// clean the locales to reduce the bundle size
locales: {},
langs: createLangDictionary(siteData.themeConfig.locales)
langs: createLangDictionary(siteData)
}
}

Expand Down
2 changes: 1 addition & 1 deletion types/shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface SiteData<ThemeConfig = any> {
head: HeadConfig[]
themeConfig: ThemeConfig
locales: Record<string, LocaleConfig>
langs: Record<string, string>
langs: Record<string, { lang: string; label: string }>
customData: any
}

Expand Down

0 comments on commit dd8cfbc

Please sign in to comment.