perf(theme): 优化多语言切换时页面重定向

This commit is contained in:
pengzhanbo 2024-03-25 01:07:13 +08:00
parent 6ce97965a1
commit 805374de71
3 changed files with 32 additions and 48 deletions

8
pnpm-lock.yaml generated
View File

@ -4044,10 +4044,6 @@ packages:
vue: 3.4.21(typescript@5.4.3)
dev: false
/@vue/shared@3.4.19:
resolution: {integrity: sha512-/KliRRHMF6LoiThEy+4c1Z4KB/gbPrGjWwJR+crg2otgrf/egKzRaCPvJ51S5oetgsgXLfc4Rm5ZgrKHZrtMSw==}
dev: false
/@vue/shared@3.4.21:
resolution: {integrity: sha512-PuJe7vDIi6VYSinuEbUIQgMIRZGgM8e4R+G+/dQTk0X1NEdvgvvgv7m+rfmDH1gZzyA1OjjoWskvHlfRNfQf3g==}
@ -4125,7 +4121,7 @@ packages:
peerDependencies:
vuepress: 2.0.0-rc.8
dependencies:
'@vue/shared': 3.4.19
'@vue/shared': 3.4.21
cheerio: 1.0.0-rc.12
fflate: 0.8.2
gray-matter: 4.0.3
@ -13487,7 +13483,7 @@ packages:
dependencies:
chokidar: 3.6.0
immutable: 4.1.0
source-map-js: 1.0.2
source-map-js: 1.2.0
dev: false
/sax@1.2.4:

View File

@ -5,7 +5,7 @@ import AutoLink from '../AutoLink.vue'
import IconChevronDown from '../icons/IconChevronDown.vue'
import IconLanguages from '../icons/IconLanguages.vue'
const { localeLinks, currentLang } = useLangs({ correspondingLink: true })
const { localeLinks, currentLang } = useLangs()
const isOpen = ref(false)
function toggle() {

View File

@ -1,17 +1,17 @@
import { usePageData, useRouteLocale } from 'vuepress/client'
import { resolveRoute, usePageData, useRouteLocale, withBase } from 'vuepress/client'
import { computed } from 'vue'
import type { PlumeThemePageData } from '../../shared/index.js'
import { ensureStartingSlash } from '../utils/index.js'
import { useThemeData } from './themeData.js'
import { normalizePath } from './sidebar.js'
import { getSidebarFirstLink, getSidebarList, normalizePath, useNotesData } from './sidebar.js'
export function useLangs({
removeCurrent = true,
correspondingLink = false,
} = {}) {
const page = usePageData<PlumeThemePageData>()
const theme = useThemeData()
const routeLocale = useRouteLocale()
const notesData = useNotesData()
const currentLang = computed(() => {
const link = routeLocale.value
return {
@ -20,53 +20,41 @@ export function useLangs({
}
})
const addPath = computed(() => {
if (page.value.frontmatter.home || (page.value.type && page.value.type !== 'friends'))
return true
const getPageLink = (locale: string) => {
const pagePath = page.value.path.slice(routeLocale.value.length)
const targetPath = normalizePath(`${locale}${pagePath}`)
const { notFound, path } = resolveRoute(targetPath)
if (!notFound)
return path
const locales = theme.value.locales || {}
const blog = locales[`/${locale}/`]?.blog
const fallback = locales['/']?.blog ?? theme.value.blog
if (page.value.isBlogPost)
return withBase(blog?.link || normalizePath(`${locale}${fallback?.link || 'blog/'}`))
return correspondingLink
})
const sidebarList = getSidebarList(targetPath, notesData.value)
const getBlogLink = (locale: string) => {
const blog = theme.value.locales?.[`/${locale}/`]?.blog
const defaultBlog = theme.value.locales?.['/']?.blog ?? theme.value.blog
const link = blog?.link ? blog.link : normalizePath(`${locale}${defaultBlog?.link || 'blog/'}`)
return link
if (sidebarList.length > 0) {
const link = getSidebarFirstLink(sidebarList)
if (link)
return link
}
const home = withBase(theme.value.home || '/')
const fallbackResolve = resolveRoute(withBase(locale))
return fallbackResolve.notFound ? home : fallbackResolve.path
}
const localeLinks = computed(() =>
Object.entries(theme.value.locales || {}).flatMap(([key, value]) =>
removeCurrent && currentLang.value.label === value.selectLanguageName
Object.entries(theme.value.locales || {}).flatMap(([key, locale]) =>
removeCurrent && currentLang.value.label === locale.selectLanguageName
? []
: {
text: value.selectLanguageName,
link: page.value.isBlogPost
? getBlogLink(key)
: normalizeLink(
key,
addPath.value,
page.value.path.slice(currentLang.value.link.length - 1),
true,
),
text: locale.selectLanguageName,
link: getPageLink(key),
},
),
)
return { localeLinks, currentLang }
}
function normalizeLink(
link: string,
addPath: boolean,
path: string,
addExt: boolean,
) {
return addPath
? link.replace(/\/$/, '')
+ ensureStartingSlash(
path
.replace(/(^|\/)?index.md$/, '$1')
.replace(/\.md$/, addExt ? '.html' : ''),
)
: link
}