- + { + const allPosts = await getallDiaries() + const allPostsByDate = sortMDByDate(allPosts) + const uniqueTags = getUniqueTags(allPosts) + const uniqueCategories = getUniqueCategories(allPosts) + return paginate(allPostsByDate, { pageSize: 50, props: { uniqueTags, uniqueCategories } }) +}) satisfies GetStaticPaths + +interface Props { + page: Page> + uniqueTags: string[] + uniqueCategories: string[] +} + +const { page, uniqueTags, uniqueCategories } = Astro.props + +const meta = { + description: 'Posts', + title: 'Diary' +} + +const paginationProps = { + ...(page.url.prev && { + prevUrl: { + text: `← Previous Posts`, + url: page.url.prev + } + }), + ...(page.url.next && { + nextUrl: { + text: `Next Posts →`, + url: page.url.next + } + }) +} +--- + + + + + + + + + + + Diary + {page.data.length === 0 && No posts yet.} + + { + page.data.length > 0 && ( + + + + {page.data.map((p) => ( + + ))} + + + + + ) + } + + diff --git a/src/pages/diary/[slug].astro b/src/pages/diary/[slug].astro new file mode 100644 index 0000000..024378b --- /dev/null +++ b/src/pages/diary/[slug].astro @@ -0,0 +1,27 @@ +--- +export const prerender = true + +import type { GetStaticPaths, InferGetStaticPropsType } from 'astro' + +import PostLayout from '@/layouts/BlogPost.astro' +import GiscusComment from '@/components/GiscusComment' +import { getallDiaries } from 'src/utils' + +export const getStaticPaths = (async () => { + const blogEntries = await getallDiaries() + return blogEntries.map((entry) => ({ + params: { slug: entry.slug }, + props: { entry } + })) +}) satisfies GetStaticPaths + +type Props = InferGetStaticPropsType + +const { entry } = Astro.props +const { Content } = await entry.render() +--- + + + + + diff --git a/src/utils/index.ts b/src/utils/index.ts index d4ee163..05e331d 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -7,7 +7,9 @@ export { getUniqueTagsWithCount, getAllCategories, getUniqueCategories, - getUniqueCategoriesWithCount + getUniqueCategoriesWithCount, + getallDiaries, + getallDiariesSorted } from './post' export { getFormattedDate } from './date' export { generateToc } from './generateToc' diff --git a/src/utils/post.ts b/src/utils/post.ts index a4f8d64..dc7d1f8 100644 --- a/src/utils/post.ts +++ b/src/utils/post.ts @@ -4,7 +4,7 @@ import { getCollection } from 'astro:content' /** Note: this function filters out draft posts based on the environment */ export async function getAllPosts() { return await getCollection('post', ({ data }) => { - return import.meta.env.PROD ? data.draft !== true : true + return !data.draft && !data.category?.startsWith('日记-20') }) } @@ -12,6 +12,16 @@ export async function getAllSortedPosts() { return sortMDByDate(await getAllPosts()) } +export const getallDiaries = async () => { + return await getCollection('post', ({ data }) => { + return !data.draft && data.category?.startsWith('日记-20') + }) +} + +export const getallDiariesSorted = async () => { + return sortMDByDate(await getallDiaries()) +} + export function sortMDByDate(posts: Array>) { return posts.sort((a, b) => { const aDate = new Date(a.data.date).valueOf()