Skip to content

Commit

Permalink
perf: 优化博客列表页数据逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Jan 4, 2024
1 parent 66f1f03 commit 689bde3
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions theme/src/client/composables/blog.ts
Original file line number Diff line number Diff line change
@@ -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<PlumeThemeBlogPostItem[]>
const list = useLocalePostList()
const blog = computed(() => themeData.value.blog || {})
const pagination = computed(() => blog.value.pagination || {})

Expand All @@ -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)
Expand Down Expand Up @@ -109,15 +113,10 @@ export function useBlogExtract() {
export type ShortPostItem = Pick<PlumeThemeBlogPostItem, 'title' | 'path' | 'createTime'>

export function useTags() {
const locale = usePageLang()
const list = useBlogPostData() as unknown as Ref<PlumeThemeBlogPostItem[]>
const filteredList = computed(() =>
list.value.filter(item => item.lang === locale.value),
)

const list = useLocalePostList()
const tags = computed(() => {
const tagMap: Record<string, number> = {}
filteredList.value.forEach((item) => {
list.value.forEach((item) => {
if (item.tags) {
toArray(item.tags).forEach((tag) => {
if (tagMap[tag])
Expand All @@ -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)

Expand All @@ -160,15 +159,11 @@ export function useTags() {
}

export function useArchives() {
const locale = usePageLang()
const list = useBlogPostData() as unknown as Ref<PlumeThemeBlogPostItem[]>
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)
Expand All @@ -186,7 +181,5 @@ export function useArchives() {
return archives
})

return {
archives,
}
return { archives }
}

0 comments on commit 689bde3

Please sign in to comment.