From bc685d1d774d1ed4ee4c0ad1df8dc00397485dc1 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 20 Aug 2021 10:48:19 +0200 Subject: [PATCH] refactor: better names and docs --- src/client/app/components/Debug.vue | 2 +- src/client/app/data.ts | 4 +++- src/client/theme-default/Layout.vue | 2 +- src/client/theme-default/composables/nav.ts | 7 +++--- types/shared.d.ts | 25 ++++++++++++++++++++- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/client/app/components/Debug.vue b/src/client/app/components/Debug.vue index ce805cf3..617895db 100644 --- a/src/client/app/components/Debug.vue +++ b/src/client/app/components/Debug.vue @@ -7,7 +7,7 @@ const el = ref(null) const open = ref(false) watch(open, (value) => { - if (value === false) { + if (!value) { el.value!.scrollTop = 0 } }) diff --git a/src/client/app/data.ts b/src/client/app/data.ts index 644905f3..09da9662 100644 --- a/src/client/app/data.ts +++ b/src/client/app/data.ts @@ -47,7 +47,9 @@ export function initData(route: Route): VitePressData { lang: computed(() => site.value.lang), localePath: computed(() => { const { langs, lang } = site.value - const path = Object.keys(langs).find((lp) => langs[lp].lang === lang) + const path = Object.keys(langs).find( + (langPath) => langs[langPath].lang === lang + ) return withBase(path || '/') }), title: computed(() => { diff --git a/src/client/theme-default/Layout.vue b/src/client/theme-default/Layout.vue index 4250c25a..d7da603e 100644 --- a/src/client/theme-default/Layout.vue +++ b/src/client/theme-default/Layout.vue @@ -32,7 +32,7 @@ const isCustomLayout = computed(() => !!frontmatter.value.customLayout) const enableHome = computed(() => !!frontmatter.value.home) // automatic multilang check for AlgoliaSearchBox -const isMultiLang = computed(() => Object.keys(site.value.langs).length > 0) +const isMultiLang = computed(() => Object.keys(site.value.langs).length > 1) // navbar const showNavbar = computed(() => { diff --git a/src/client/theme-default/composables/nav.ts b/src/client/theme-default/composables/nav.ts index 224fc8ae..7c935721 100644 --- a/src/client/theme-default/composables/nav.ts +++ b/src/client/theme-default/composables/nav.ts @@ -9,6 +9,7 @@ export function useLanguageLinks() { const langs = site.value.langs const localePaths = Object.keys(langs) + // one language if (localePaths.length < 2) { return null } @@ -18,9 +19,9 @@ export function useLanguageLinks() { // intentionally remove the leading slash because each locale has one const currentPath = route.path.replace(localePath.value, '') - const candidates = localePaths.map((v) => ({ - text: langs[v].label, - link: `${v}${currentPath}` + const candidates = localePaths.map((localePath) => ({ + text: langs[localePath].label, + link: `${localePath}${currentPath}` })) const selectText = theme.value.selectText || 'Languages' diff --git a/types/shared.d.ts b/types/shared.d.ts index 6b953f85..822061e6 100644 --- a/types/shared.d.ts +++ b/types/shared.d.ts @@ -11,13 +11,36 @@ export interface LocaleConfig { export interface SiteData { base: string + /** + * Language of the site as it should be set on the `html` element. + * @example `en-US`, `zh-CN` + */ lang: string title: string description: string head: HeadConfig[] themeConfig: ThemeConfig locales: Record - langs: Record + /** + * Available locales for the site when it has defined `locales` in its + * `themeConfig`. This object is otherwise empty. Keys are paths like `/` or + * `/zh/`. + */ + langs: Record< + string, + { + /** + * Lang attribute as set on the `` element. + * @example `en-US`, `zh-CN` + */ + lang: string + /** + * Label to display in the language menu. + * @example `English', `简体中文` + */ + label: string + } + > customData: any }