import BlogPost from '@/components/BlogPost' import PropTypes from 'prop-types' import Pagination from '@/components/Pagination' import BLOG from '@/blog.config' import { useRouter } from 'next/router' import { useTheme } from '@/lib/theme' import CommonHead from '@/components/CommonHead' import TopNav from '@/components/TopNav' import Tags from '@/components/Tags' import SideBarResponsive from '@/components/SideBarResponsive' import Footer from '@/components/Footer' const DefaultLayout = ({ tags, posts, page, currentTag, ...customMeta }) => { const meta = { title: BLOG.title, type: 'website', ...customMeta } page = page ?? 1 let postsToShow = [] let filteredBlogPosts = posts ?? [] let currentSearch = '' if (posts) { const router = useRouter() if (router.query && router.query.s) { currentSearch = router.query.s filteredBlogPosts = posts.filter(post => { const tagContent = post.tags ? post.tags.join(' ') : '' const searchContent = post.title + post.summary + tagContent + post.slug return searchContent.toLowerCase().includes(currentSearch.toLowerCase()) }) } } const totalPages = Math.ceil(filteredBlogPosts.length / BLOG.postsPerPage) if (posts) { postsToShow = filteredBlogPosts.slice( BLOG.postsPerPage * (page - 1), BLOG.postsPerPage * page ) } let showNext = false if (filteredBlogPosts) { const totalPosts = filteredBlogPosts.length showNext = page * BLOG.postsPerPage < totalPosts } const { theme } = useTheme() return (
{/* 侧边菜单 */}
{(!page || page === 1) && (
)} {/* 当前搜索 */} {(currentSearch || (page && page !== 1)) && (
{page && page !== 1 && (页 {page} / {totalPages})}
)}
{/* 文章列表 */}
{!postsToShow.length && (

No posts found.

)} {postsToShow.map(post => ( ))}
) } DefaultLayout.propTypes = { posts: PropTypes.array.isRequired, tags: PropTypes.object.isRequired, currentTag: PropTypes.string } export default DefaultLayout