From 1a6a573d8b30f1a7b399e2a788c0018047061978 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Wed, 4 Jan 2023 10:19:44 +0800 Subject: [PATCH] =?UTF-8?q?archive=E5=BD=92=E6=A1=A3=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/archive/index.js | 21 +++++++++++++++++++++ themes/example/LayoutArchive.js | 19 +------------------ themes/fukasawa/LayoutArchive.js | 21 +-------------------- themes/hexo/LayoutArchive.js | 22 +--------------------- themes/matery/LayoutArchive.js | 22 +--------------------- themes/medium/LayoutArchive.js | 21 ++------------------- themes/next/LayoutArchive.js | 22 +--------------------- themes/nobelium/LayoutArchive.js | 21 ++------------------- 8 files changed, 30 insertions(+), 139 deletions(-) diff --git a/pages/archive/index.js b/pages/archive/index.js index f60d9f77de..700e6273df 100644 --- a/pages/archive/index.js +++ b/pages/archive/index.js @@ -25,6 +25,27 @@ export async function getStaticProps() { props.posts = props.allPages.filter(page => page.type === 'Post' && page.status === 'Published') delete props.allPages + const postsSortByDate = Object.create(props.posts) + + postsSortByDate.sort((a, b) => { + const dateA = new Date(a?.date?.start_date || a.createdTime) + const dateB = new Date(b?.date?.start_date || b.createdTime) + return dateB - dateA + }) + + const archivePosts = {} + + postsSortByDate.forEach(post => { + const date = post.date?.start_date?.slice(0, 7) || post.createdTime + if (archivePosts[date]) { + archivePosts[date].push(post) + } else { + archivePosts[date] = [post] + } + }) + + props.archivePosts = archivePosts + return { props, revalidate: BLOG.NEXT_REVALIDATE_SECOND diff --git a/themes/example/LayoutArchive.js b/themes/example/LayoutArchive.js index a89265f373..4ba92d8a72 100644 --- a/themes/example/LayoutArchive.js +++ b/themes/example/LayoutArchive.js @@ -3,25 +3,8 @@ import Link from 'next/link' import LayoutBase from './LayoutBase' export const LayoutArchive = props => { - const { posts } = props - const postsSortByDate = Object.create(posts) + const { archivePosts } = props - postsSortByDate.sort((a, b) => { - const dateA = new Date(a?.date?.start_date || a.createdTime) - const dateB = new Date(b?.date?.start_date || b.createdTime) - return dateB - dateA - }) - - const archivePosts = {} - - postsSortByDate.forEach(post => { - const date = post.date?.start_date.slice(0, 7) - if (archivePosts[date]) { - archivePosts[date].push(post) - } else { - archivePosts[date] = [post] - } - }) return (
diff --git a/themes/fukasawa/LayoutArchive.js b/themes/fukasawa/LayoutArchive.js index a63f6060a2..495ed6a492 100644 --- a/themes/fukasawa/LayoutArchive.js +++ b/themes/fukasawa/LayoutArchive.js @@ -3,26 +3,7 @@ import BlogArchiveItem from './components/BlogPostArchive' import LayoutBase from './LayoutBase' export const LayoutArchive = (props) => { - const { posts } = props - // 深拷贝 - const postsSortByDate = Object.create(posts) - - // 时间排序 - postsSortByDate.sort((a, b) => { - const dateA = new Date(a?.date?.start_date || a.createdTime) - const dateB = new Date(b?.date?.start_date || b.createdTime) - return dateB - dateA - }) - const archivePosts = {} - - postsSortByDate.forEach(post => { - const date = post.date?.start_date.slice(0, 7) - if (archivePosts[date]) { - archivePosts[date].push(post) - } else { - archivePosts[date] = [post] - } - }) + const { archivePosts } = props useEffect(() => { const anchor = window.location.hash diff --git a/themes/hexo/LayoutArchive.js b/themes/hexo/LayoutArchive.js index 8d2a1c084d..dab9d1ce0b 100644 --- a/themes/hexo/LayoutArchive.js +++ b/themes/hexo/LayoutArchive.js @@ -4,27 +4,7 @@ import Card from './components/Card' import LayoutBase from './LayoutBase' export const LayoutArchive = (props) => { - const { posts } = props - // 深拷贝 - const postsSortByDate = Object.create(posts) - - // 时间排序 - postsSortByDate.sort((a, b) => { - const dateA = new Date(a?.date?.start_date || a.createdTime) - const dateB = new Date(b?.date?.start_date || b.createdTime) - return dateB - dateA - }) - - const archivePosts = {} - - postsSortByDate.forEach(post => { - const date = post.date?.start_date.slice(0, 7) - if (archivePosts[date]) { - archivePosts[date].push(post) - } else { - archivePosts[date] = [post] - } - }) + const { archivePosts } = props useEffect(() => { const anchor = window.location.hash diff --git a/themes/matery/LayoutArchive.js b/themes/matery/LayoutArchive.js index 8d2a1c084d..dab9d1ce0b 100644 --- a/themes/matery/LayoutArchive.js +++ b/themes/matery/LayoutArchive.js @@ -4,27 +4,7 @@ import Card from './components/Card' import LayoutBase from './LayoutBase' export const LayoutArchive = (props) => { - const { posts } = props - // 深拷贝 - const postsSortByDate = Object.create(posts) - - // 时间排序 - postsSortByDate.sort((a, b) => { - const dateA = new Date(a?.date?.start_date || a.createdTime) - const dateB = new Date(b?.date?.start_date || b.createdTime) - return dateB - dateA - }) - - const archivePosts = {} - - postsSortByDate.forEach(post => { - const date = post.date?.start_date.slice(0, 7) - if (archivePosts[date]) { - archivePosts[date].push(post) - } else { - archivePosts[date] = [post] - } - }) + const { archivePosts } = props useEffect(() => { const anchor = window.location.hash diff --git a/themes/medium/LayoutArchive.js b/themes/medium/LayoutArchive.js index 1410e47ecf..520e0b33d9 100644 --- a/themes/medium/LayoutArchive.js +++ b/themes/medium/LayoutArchive.js @@ -3,25 +3,8 @@ import Link from 'next/link' import LayoutBase from './LayoutBase' export const LayoutArchive = props => { - const { posts } = props - const postsSortByDate = Object.create(posts) + const { archivePosts } = props - postsSortByDate.sort((a, b) => { - const dateA = new Date(a?.date?.start_date || a.createdTime) - const dateB = new Date(b?.date?.start_date || b.createdTime) - return dateB - dateA - }) - - const archivePosts = {} - - postsSortByDate.forEach(post => { - const date = post.date?.start_date.slice(0, 7) - if (archivePosts[date]) { - archivePosts[date].push(post) - } else { - archivePosts[date] = [post] - } - }) return (
@@ -60,5 +43,5 @@ export const LayoutArchive = props => { ))}
- ); + ) } diff --git a/themes/next/LayoutArchive.js b/themes/next/LayoutArchive.js index 3db36dbd48..ebd94ed303 100644 --- a/themes/next/LayoutArchive.js +++ b/themes/next/LayoutArchive.js @@ -3,27 +3,7 @@ import LayoutBase from './LayoutBase' import BlogPostArchive from './components/BlogPostArchive' export const LayoutArchive = (props) => { - const { posts } = props - // 深拷贝 - const postsSortByDate = Object.create(posts) - - // 时间排序 - postsSortByDate.sort((a, b) => { - const dateA = new Date(a?.date?.start_date || a.createdTime) - const dateB = new Date(b?.date?.start_date || b.createdTime) - return dateB - dateA - }) - - const archivePosts = {} - - postsSortByDate.forEach(post => { - const date = post.date?.start_date.slice(0, 7) - if (archivePosts[date]) { - archivePosts[date].push(post) - } else { - archivePosts[date] = [post] - } - }) + const { archivePosts } = props useEffect(() => { const anchor = window.location.hash diff --git a/themes/nobelium/LayoutArchive.js b/themes/nobelium/LayoutArchive.js index c5b3a8babc..4ba92d8a72 100644 --- a/themes/nobelium/LayoutArchive.js +++ b/themes/nobelium/LayoutArchive.js @@ -3,25 +3,8 @@ import Link from 'next/link' import LayoutBase from './LayoutBase' export const LayoutArchive = props => { - const { posts } = props - const postsSortByDate = Object.create(posts) + const { archivePosts } = props - postsSortByDate.sort((a, b) => { - const dateA = new Date(a?.date?.start_date || a.createdTime) - const dateB = new Date(b?.date?.start_date || b.createdTime) - return dateB - dateA - }) - - const archivePosts = {} - - postsSortByDate.forEach(post => { - const date = post.date?.start_date.slice(0, 7) - if (archivePosts[date]) { - archivePosts[date].push(post) - } else { - archivePosts[date] = [post] - } - }) return (
@@ -58,5 +41,5 @@ export const LayoutArchive = props => { ))}
- ); + ) }