From e4fe64c5a61e590ae5d4c759a2e06dba61d09c32 Mon Sep 17 00:00:00 2001 From: Matias Capeletto Date: Mon, 30 Nov 2020 18:52:38 +0100 Subject: [PATCH 1/3] feat: $lang and $localePath globals --- docs/guide/global-computed.md | 10 +++++++++- src/client/app/mixin.ts | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/guide/global-computed.md b/docs/guide/global-computed.md index d1c0d3f119ad..ea46cf4fc2c6 100644 --- a/docs/guide/global-computed.md +++ b/docs/guide/global-computed.md @@ -64,6 +64,14 @@ Reference of `$page.frontmatter`. } ``` +## $lang + +The language of the current page. Default: `en-US`. + +## $localePath + +The locale path prefix for the current page. Default: `/`. + ## $title Value of the `` label used for the current page. @@ -77,5 +85,5 @@ The content value of the `<meta name= "description" content= "...">` for the cur Helper method to generate correct path by prepending the `base` path configured in `.vitepress/config.js`. It's useful when you want to link to [public files with base path](./assets#public-files). ```html -<img :src="$withBase('/foo.png')" alt="foo"> +<img :src="$withBase('/foo.png')" alt="foo" /> ``` diff --git a/src/client/app/mixin.ts b/src/client/app/mixin.ts index e33a5b9eaba8..86dfe665d608 100644 --- a/src/client/app/mixin.ts +++ b/src/client/app/mixin.ts @@ -56,6 +56,24 @@ export function mixinGlobalComputed( } }, + $lang: { + get() { + return siteByRoute.value.lang + } + }, + + $localePath: { + get() { + const { locales } = site.value + const { lang } = siteByRoute.value + return ( + (locales && + Object.keys(locales).find((lp) => locales[lp].lang === lang)) || + '/' + ) + } + }, + $withBase: { value(path: string) { return joinPath(site.value.base, path) From b0fd071605b47ec1aed7f2caa5d8de6f546d97c9 Mon Sep 17 00:00:00 2001 From: Matias Capeletto <matias.capeletto@gmail.com> Date: Mon, 30 Nov 2020 19:01:35 +0100 Subject: [PATCH 2/3] docs: fix img closing tag --- docs/guide/global-computed.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guide/global-computed.md b/docs/guide/global-computed.md index ea46cf4fc2c6..384d0e3889e5 100644 --- a/docs/guide/global-computed.md +++ b/docs/guide/global-computed.md @@ -85,5 +85,5 @@ The content value of the `<meta name= "description" content= "...">` for the cur Helper method to generate correct path by prepending the `base` path configured in `.vitepress/config.js`. It's useful when you want to link to [public files with base path](./assets#public-files). ```html -<img :src="$withBase('/foo.png')" alt="foo" /> +<img :src="$withBase('/foo.png')" alt="foo"> ``` From 63818e57b9f3e9427bd2a4f4c0616f9d6c28ef34 Mon Sep 17 00:00:00 2001 From: Kia King Ishii <kia.king.08@gmail.com> Date: Fri, 22 Jan 2021 23:44:05 +0900 Subject: [PATCH 3/3] style: update the code style a bit --- src/client/app/mixin.ts | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/client/app/mixin.ts b/src/client/app/mixin.ts index 86dfe665d608..2057c753db36 100644 --- a/src/client/app/mixin.ts +++ b/src/client/app/mixin.ts @@ -42,35 +42,36 @@ export function mixinGlobalComputed( } }, - $title: { + $lang: { get() { - return page.value.title - ? page.value.title + ' | ' + siteByRoute.value.title - : siteByRoute.value.title + return siteByRoute.value.lang } }, - $description: { + $localePath: { get() { - return page.value.description || siteByRoute.value.description + const { locales } = site.value + const { lang } = siteByRoute.value + + const path = Object.keys(locales).find( + (lp) => locales[lp].lang === lang + ) + + return (locales && path) || '/' } }, - $lang: { + $title: { get() { - return siteByRoute.value.lang + return page.value.title + ? page.value.title + ' | ' + siteByRoute.value.title + : siteByRoute.value.title } }, - $localePath: { + $description: { get() { - const { locales } = site.value - const { lang } = siteByRoute.value - return ( - (locales && - Object.keys(locales).find((lp) => locales[lp].lang === lang)) || - '/' - ) + return page.value.description || siteByRoute.value.description } },