Skip to content

Commit

Permalink
feat(core): make language available in page data
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Feb 4, 2021
1 parent 05b87dd commit 03bb09f
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 16 deletions.
4 changes: 1 addition & 3 deletions packages/@vuepress/client/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ export const createVueApp = async ({
siteLocaleData.value
)
)
const pageLang = computed(() =>
resolvePageLang(pageFrontmatter.value, siteLocaleData.value)
)
const pageLang = computed(() => resolvePageLang(pageData.value))

// provide global computed
app.provide(routeLocaleSymbol, routeLocale)
Expand Down
1 change: 1 addition & 0 deletions packages/@vuepress/client/src/injections/pageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const pageDataEmpty = readonly({
key: '',
path: '',
title: '',
lang: '',
frontmatter: {},
excerpt: '',
headers: [],
Expand Down
21 changes: 9 additions & 12 deletions packages/@vuepress/client/src/injections/pageLang.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { inject } from 'vue'
import type { ComputedRef, InjectionKey } from 'vue'
import { isString } from '@vuepress/shared'
import type { PageFrontmatter } from './pageFrontmatter'
import type { SiteLocaleData } from './siteLocaleData'
import type { PageData } from '@vuepress/shared'

export type PageLang = string
export type PageLangRef = ComputedRef<PageLang>
Expand All @@ -19,12 +17,11 @@ export const usePageLang = (): PageLangRef => {
return pageLang
}

export const resolvePageLang = (
frontmatter: PageFrontmatter,
siteLocale: SiteLocaleData
): PageLang => {
if (isString(frontmatter.lang) && frontmatter.lang) {
return frontmatter.lang
}
return siteLocale.lang || 'en'
}
/**
* Resolve language of current page
*
* It's mainly used for the `lang` attribute of `<html>` tag,
* which should not be empty
*/
export const resolvePageLang = (pageData: PageData): PageLang =>
pageData.lang || 'en'
2 changes: 2 additions & 0 deletions packages/@vuepress/core/src/app/prepare/resolvePageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const resolvePageData = ({
key,
path,
title,
lang,
frontmatter,
excerpt,
headers,
Expand All @@ -15,6 +16,7 @@ export const resolvePageData = ({
key,
path,
title,
lang,
frontmatter,
excerpt,
headers,
Expand Down
5 changes: 5 additions & 0 deletions packages/@vuepress/core/src/page/createPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { resolvePageFileContent } from './resolvePageFileContent'
import { resolvePageFilePath } from './resolvePageFilePath'
import { resolvePageFrontmatter } from './resolvePageFrontmatter'
import { resolvePageKey } from './resolvePageKey'
import { resolvePageLang } from './resolvePageLang'
import { resolvePagePath } from './resolvePagePath'
import { resolvePagePermalink } from './resolvePagePermalink'
import { resolvePageRoutesInfo } from './resolvePageRoutesInfo'
Expand Down Expand Up @@ -48,6 +49,9 @@ export const createPage = async (
// infer page path according to file path
const { pathInferred, pathLocale } = inferPagePath({ app, filePathRelative })

// resolve language from frontmatter and site options
const lang = resolvePageLang({ app, frontmatter, pathLocale })

// resolve page permalink
const permalink = resolvePagePermalink({
options,
Expand Down Expand Up @@ -118,5 +122,6 @@ export const createPage = async (
links,
slug,
date,
lang,
}
}
1 change: 1 addition & 0 deletions packages/@vuepress/core/src/page/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './resolvePageFileContent'
export * from './resolvePageFilePath'
export * from './resolvePageFrontmatter'
export * from './resolvePageKey'
export * from './resolvePageLang'
export * from './resolvePagePath'
export * from './resolvePagePermalink'
export * from './resolvePageRoutesInfo'
Expand Down
2 changes: 1 addition & 1 deletion packages/@vuepress/core/src/page/inferPagePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const inferPagePath = ({
.replace(/\/(README|index).html$/i, '/')

// resolve page locale path
const pathLocale = resolveLocalePath(app.options.locales, pathInferred)
const pathLocale = resolveLocalePath(app.siteData.locales, pathInferred)

return {
pathInferred,
Expand Down
20 changes: 20 additions & 0 deletions packages/@vuepress/core/src/page/resolvePageLang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isString } from '@vuepress/shared'
import type { App, PageFrontmatter } from '../types'

/**
* Resolve language of page
*/
export const resolvePageLang = ({
app,
frontmatter,
pathLocale,
}: {
app: App
frontmatter: PageFrontmatter
pathLocale: string
}): string => {
if (isString(frontmatter.lang) && frontmatter.lang) {
return frontmatter.lang
}
return app.siteData.locales[pathLocale]?.lang ?? app.siteData.lang ?? ''
}
5 changes: 5 additions & 0 deletions packages/@vuepress/shared/src/types/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export type PageData<
*/
title: string

/**
* Language of the page
*/
lang: string

/**
* Front matter of the page
*/
Expand Down

0 comments on commit 03bb09f

Please sign in to comment.