import type { CollectionEntry } from 'astro:content' 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 !data.draft && !data.category?.startsWith('日记-20') }) } 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() const bDate = new Date(b.data.date).valueOf() return bDate - aDate }) } /** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */ export function getAllTags(posts: Array>) { return posts.flatMap((post) => [...(post.data?.tags ?? [])]) } /** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */ export function getUniqueTags(posts: Array>) { return [...new Set(getAllTags(posts))] } /** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */ export function getUniqueTagsWithCount( posts: Array> ): Array<[string, number]> { return [ ...getAllTags(posts).reduce( (acc, t) => acc.set(t, (acc.get(t) || 0) + 1), new Map() ) ].sort((a, b) => b[1] - a[1]) } /** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */ export function getAllCategories(posts: Array>): string[] { return posts.map((post) => post.data.category ?? '未分类') } /** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */ export function getUniqueCategories(posts: Array>) { return [...new Set(getAllCategories(posts))] } /** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */ export function getUniqueCategoriesWithCount( posts: Array> ): Array<[string, number]> { return [ ...getAllCategories(posts).reduce( (acc, t) => acc.set(t, (acc.get(t) || 0) + 1), new Map() ) ].sort((a, b) => b[1] - a[1]) } export function getIdToSlugMap(posts: Array>) { return posts.reduce( (acc, post) => { acc[post.id.split('.md')[0]] = post.slug return acc }, {} as Record ) }