From 689bde34bbe586b3e2b936093658ae50ac3d05ee Mon Sep 17 00:00:00 2001 From: pengzhanbo Date: Fri, 5 Jan 2024 01:35:12 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=8D=9A=E5=AE=A2?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E9=A1=B5=E6=95=B0=E6=8D=AE=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- theme/src/client/composables/blog.ts | 35 +++++++++++----------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/theme/src/client/composables/blog.ts b/theme/src/client/composables/blog.ts index deca7db4..ab6957bc 100644 --- a/theme/src/client/composables/blog.ts +++ b/theme/src/client/composables/blog.ts @@ -1,16 +1,20 @@ import { usePageLang } from '@vuepress/client' import { useBlogPostData } from '@vuepress-plume/plugin-blog-data/client' import { computed, ref } from 'vue' -import type { Ref } from 'vue' import type { PlumeThemeBlogPostItem } from '../../shared/index.js' import { useLocaleLink, useThemeLocaleData } from '../composables/index.js' import { getRandomColor, toArray } from '../utils/index.js' -export function usePostListControl() { +export function useLocalePostList() { const locale = usePageLang() + const list = useBlogPostData() + return computed(() => list.value.filter(item => item.lang === locale.value)) +} + +export function usePostListControl() { const themeData = useThemeLocaleData() - const list = useBlogPostData() as unknown as Ref + const list = useLocalePostList() const blog = computed(() => themeData.value.blog || {}) const pagination = computed(() => blog.value.pagination || {}) @@ -29,7 +33,7 @@ export function usePostListControl() { return next.sticky > prev.sticky ? 1 : -1 }), ...otherList, - ].filter(item => item.lang === locale.value) + ] }) const page = ref(1) @@ -109,15 +113,10 @@ export function useBlogExtract() { export type ShortPostItem = Pick export function useTags() { - const locale = usePageLang() - const list = useBlogPostData() as unknown as Ref - const filteredList = computed(() => - list.value.filter(item => item.lang === locale.value), - ) - + const list = useLocalePostList() const tags = computed(() => { const tagMap: Record = {} - filteredList.value.forEach((item) => { + list.value.forEach((item) => { if (item.tags) { toArray(item.tags).forEach((tag) => { if (tagMap[tag]) @@ -139,7 +138,7 @@ export function useTags() { const handleTagClick = (tag: string) => { currentTag.value = tag - postList.value = filteredList.value.filter((item) => { + postList.value = list.value.filter((item) => { if (item.tags) return toArray(item.tags).includes(tag) @@ -160,15 +159,11 @@ export function useTags() { } export function useArchives() { - const locale = usePageLang() - const list = useBlogPostData() as unknown as Ref - const filteredList = computed(() => - list.value.filter(item => item.lang === locale.value), - ) + const list = useLocalePostList() const archives = computed(() => { const archives: { label: string, list: ShortPostItem[] }[] = [] - filteredList.value.forEach((item) => { + list.value.forEach((item) => { const createTime = item.createTime.split(' ')[0] const year = createTime.split('/')[0] let current = archives.find(archive => archive.label === year) @@ -186,7 +181,5 @@ export function useArchives() { return archives }) - return { - archives, - } + return { archives } }