Skip to content

Commit

Permalink
feat: optimize globalData
Browse files Browse the repository at this point in the history
  • Loading branch information
kuizuo committed Dec 28, 2023
1 parent bb9a4b5 commit e3ddba7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 31 deletions.
13 changes: 6 additions & 7 deletions src/components/UserCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,18 @@ export default function UserCard({ isNavbar = false }: { isNavbar?: boolean }) {

const logoLink = useBaseUrl(logo.src || '/')

const blogPluginData = usePluginData('docusaurus-plugin-content-blog') as {
blogs: BlogPost[]
tags: BlogTags
const blogData = usePluginData('docusaurus-plugin-content-blog') as {
posts: BlogPost[]
postNum: number
tagNum: number
}
const docData = (
usePluginData('docusaurus-plugin-content-docs') as { versions: { docs: BlogPost[] } }
)?.versions[0].docs
const blogData = blogPluginData?.blogs
const tagData = blogPluginData?.tags

const count: Count = {
blog: blogData.length,
tag: Object.keys(tagData).length ?? 0,
blog: blogData.postNum,
tag: blogData.tagNum ?? 0,
doc: docData?.length ?? 0,
project: projects?.length ?? 0,
}
Expand Down
14 changes: 7 additions & 7 deletions src/pages/_components/HomepageBlog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import clsx from 'clsx'
import { motion, useScroll, useTransform } from 'framer-motion'
import { BlogPost } from '@docusaurus/plugin-content-blog'
import useGlobalData from '@docusaurus/useGlobalData'
import { usePluginData } from '@docusaurus/useGlobalData'
import Translate from '@docusaurus/Translate'
import Link from '@docusaurus/Link'
import Image from '@theme/IdealImage'
Expand Down Expand Up @@ -47,13 +47,13 @@ export function BlogItem({ post }: { post: BlogPost }) {
}

export default function BlogRecent(): JSX.Element {
const globalData = useGlobalData()
const blogPluginData = globalData?.['docusaurus-plugin-content-blog']?.['default'] as {
blogs: BlogPost[]
const blogData = usePluginData('docusaurus-plugin-content-blog') as {
posts: BlogPost[]
postNum: number
tagNum: number
}

const blogData = blogPluginData?.blogs
const posts = chunk(blogData.slice(0, 6), 2)
const posts = chunk(blogData.posts.slice(0, 6), 2)

const ref = React.useRef<HTMLDivElement>(null)

Expand All @@ -62,7 +62,7 @@ export default function BlogRecent(): JSX.Element {
clamp: false,
})

if (blogData.length === 0) {
if (blogData.postNum === 0) {
return <>作者还没有写过博客哦</>
}

Expand Down
5 changes: 3 additions & 2 deletions src/plugin/plugin-content-blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ async function blogPluginEnhanced(context, options) {
const { setGlobalData } = actions;

setGlobalData({
blogs: blogPosts,
tags: blogTags,
posts: blogPosts.slice(0, 10), // Only store 10 posts
postNum: blogPosts.length,
tagNum: Object.keys(blogTags).length,
});
},
};
Expand Down
11 changes: 3 additions & 8 deletions src/theme/BlogPostItem/Header/Info/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,10 @@ export default function BlogPostItemHeaderInfo({ className }: Props): JSX.Elemen
<div className={styles.tagList}>
{tags.slice(0, 2).map(({ label, permalink: tagPermalink }, index) => {
return (
<>
<div key={tagPermalink}>
{index !== 0 && '/'}
<Tag
label={label}
permalink={tagPermalink}
key={tagPermalink}
className={'tag'}
/>
</>
<Tag label={label} permalink={tagPermalink} className={'tag'} />
</div>
)
})}
</div>
Expand Down
13 changes: 6 additions & 7 deletions src/theme/BlogRecommend/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ import React from 'react'
import clsx from 'clsx'
import { motion } from 'framer-motion'
import { BlogPost } from '@docusaurus/plugin-content-blog'
import useGlobalData from '@docusaurus/useGlobalData'
import { usePluginData } from '@docusaurus/useGlobalData'
import Translate from '@docusaurus/Translate'
import Link from '@docusaurus/Link'
import Image from '@theme/IdealImage'

import styles from './styles.module.scss'

export default function BlogRecommend(): JSX.Element {
const globalData = useGlobalData()
const blogPluginData = globalData?.['docusaurus-plugin-content-blog']?.['default'] as {
blogs: BlogPost[]
const blogData = usePluginData('docusaurus-plugin-content-blog') as {
posts: BlogPost[]
postNum: number
tagNum: number
}

const blogData = blogPluginData?.blogs
const recommendedPosts = blogData
const recommendedPosts = blogData.posts
.filter(b => (b.metadata.frontMatter.sticky as number) > 0)
.map(b => b.metadata)
.sort((a, b) => (a.frontMatter.sticky as number) - (b.frontMatter.sticky as number))
Expand Down

0 comments on commit e3ddba7

Please sign in to comment.