From d6d310a4e028b668fdd71ef480f30fec2ad41133 Mon Sep 17 00:00:00 2001 From: tangly Date: Fri, 11 Nov 2022 18:06:14 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E5=88=86=E6=8F=90=E4=BA=A4tag?= =?UTF-8?q?=E5=88=86=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index.js | 37 ++++++++-------------- pages/page/[page].js | 16 +++------- themes/hexo/LayoutTag.js | 35 +++++++++++++++++--- themes/hexo/components/PaginationNumber.js | 6 ++-- 4 files changed, 49 insertions(+), 45 deletions(-) diff --git a/pages/index.js b/pages/index.js index 92a70474..335c8bc4 100644 --- a/pages/index.js +++ b/pages/index.js @@ -23,33 +23,22 @@ export async function getStaticProps() { } // 处理分页 - const page = 1 - let postsToShow - if (BLOG.POST_LIST_STYLE !== 'page') { - postsToShow = Array.from(allPosts) - } else { - postsToShow = allPosts?.slice( - BLOG.POSTS_PER_PAGE * (page - 1), - BLOG.POSTS_PER_PAGE * page - ) - if (BLOG.POST_LIST_PREVIEW === 'true') { - for (const i in postsToShow) { - const post = postsToShow[i] - if (post.password && post.password !== '') { - continue - } - const blockMap = await getPostBlocks( - post.id, - 'slug', - BLOG.POST_PREVIEW_LINES - ) - if (blockMap) { - post.blockMap = blockMap - } + if (BLOG.POST_LIST_STYLE === 'scroll') { + props.posts = Array.from(allPosts) + } else if (BLOG.POST_LIST_STYLE === 'page') { + props.posts = allPosts?.slice(0, BLOG.POSTS_PER_PAGE) + } + + // 预览文章内容 + if (BLOG.POST_LIST_PREVIEW === 'true') { + for (const i in props.posts) { + const post = props.posts[i] + if (post.password && post.password !== '') { + continue } + post.blockMap = await getPostBlocks(post.id, 'slug', BLOG.POST_PREVIEW_LINES) } } - props.posts = postsToShow return { props: { diff --git a/pages/page/[page].js b/pages/page/[page].js index a09f08d1..0a0c19a7 100644 --- a/pages/page/[page].js +++ b/pages/page/[page].js @@ -41,24 +41,16 @@ export async function getStaticProps({ params: { page } }) { const { allPages } = props const allPosts = allPages.filter(page => page.type === 'Post' && page.status === 'Published') // 处理分页 - props.posts = allPosts.slice( - BLOG.POSTS_PER_PAGE * (page - 1), - BLOG.POSTS_PER_PAGE * page - ) + props.posts = allPosts.slice(BLOG.POSTS_PER_PAGE * (page - 1), BLOG.POSTS_PER_PAGE * page) + + // 处理预览 if (BLOG.POST_LIST_PREVIEW === 'true') { for (const i in props.posts) { const post = props.posts[i] if (post.password && post.password !== '') { continue } - const blockMap = await getPostBlocks( - post.id, - 'slug', - BLOG.POST_PREVIEW_LINES - ) - if (blockMap) { - post.blockMap = blockMap - } + post.blockMap = await getPostBlocks(post.id, 'slug', BLOG.POST_PREVIEW_LINES) } } diff --git a/themes/hexo/LayoutTag.js b/themes/hexo/LayoutTag.js index 1e827be8..90c3841a 100644 --- a/themes/hexo/LayoutTag.js +++ b/themes/hexo/LayoutTag.js @@ -3,18 +3,43 @@ import BlogPostListScroll from './components/BlogPostListScroll' import BlogPostListPage from './components/BlogPostListPage' import LayoutBase from './LayoutBase' import TagItemMini from '../next/components/TagItemMini' +import PaginationNumber from './PaginationNumber' +import BlogPostListEmpty from './BlogPostListEmpty' export const LayoutTag = (props) => { - console.log(props) - const currentTag = props.tags.find((t) => { return t.name === props.tag }) + const totalPage = Math.ceil(props.postCount / BLOG.POSTS_PER_PAGE) + const showPagination = props.postCount >= BLOG.POSTS_PER_PAGE + + props.headerSlot =
+ +
+ + // 空文章处理 + if (!props.postToShow || props.postToShow.length === 0) { + return + } + + const page = 1 return
- -
- {BLOG.POST_LIST_STYLE === 'page' ? : } + + + + { props.postToShow && props.postToShow.length > 0 + ? (<> + {BLOG.POST_LIST_STYLE === 'page' + ? (
+ + { showPagination && } +
) + : }= + ) + : () + } +
} diff --git a/themes/hexo/components/PaginationNumber.js b/themes/hexo/components/PaginationNumber.js index a2b446a8..5435a440 100644 --- a/themes/hexo/components/PaginationNumber.js +++ b/themes/hexo/components/PaginationNumber.js @@ -62,14 +62,12 @@ const PaginationNumber = ({ page, totalPage }) => { function getPageElement(page, currentPage) { return ( - + } > {page}