From 1bb4124829543ef7927ac12880475b194ce2a68c Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 5 Aug 2021 15:43:54 +0200 Subject: [PATCH] refactor: reuse createLangDictionary --- src/node/config.ts | 10 +++++++++- src/shared/shared.ts | 16 ++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/node/config.ts b/src/node/config.ts index 4f8ebdd7..f86b9272 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -4,7 +4,12 @@ import chalk from 'chalk' import globby from 'globby' import { AliasOptions, UserConfig as ViteConfig } from 'vite' import { Options as VuePluginOptions } from '@vitejs/plugin-vue' -import { SiteData, HeadConfig, LocaleConfig } from './shared' +import { + SiteData, + HeadConfig, + LocaleConfig, + createLangDictionary +} from './shared' import { resolveAliases, APP_PATH, DEFAULT_THEME_PATH } from './alias' import { MarkdownOptions } from './markdown/markdown' @@ -142,6 +147,9 @@ export async function resolveSiteData( head: userConfig.head || [], themeConfig: userConfig.themeConfig || {}, locales: userConfig.locales || {}, + langs: createLangDictionary( + userConfig.themeConfig && userConfig.themeConfig.locales + ), customData: userConfig.customData || {} } } diff --git a/src/shared/shared.ts b/src/shared/shared.ts index 4238d3dd..25e1c6cd 100644 --- a/src/shared/shared.ts +++ b/src/shared/shared.ts @@ -36,6 +36,15 @@ function resolveLocales( return localeRoot ? locales[localeRoot] : undefined } +export function createLangDictionary(locales: any | undefined) { + return locales + ? Object.keys(locales).reduce((langs, path) => { + langs[path] = locales![path].label + return langs + }, {} as Record) + : {} +} + // this merges the locales data to the main data by the route export function resolveSiteDataByRoute( siteData: SiteData, @@ -61,12 +70,7 @@ export function resolveSiteDataByRoute( lang: (localeData || siteData).lang, // clean the locales to reduce the bundle size locales: {}, - langs: siteData.themeConfig.locales - ? Object.keys(siteData.themeConfig.locales).reduce((locales, path) => { - locales[path] = siteData.themeConfig.locales![path].label - return locales - }, {} as Record) - : {} + langs: createLangDictionary(siteData.themeConfig.locales) } }