Built resume template

This commit is contained in:
srleom
2024-03-19 16:14:57 +08:00
parent b266e46946
commit 36936150cf
73 changed files with 8970 additions and 0 deletions

39
src/utils/post.ts Normal file
View File

@ -0,0 +1,39 @@
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 import.meta.env.PROD ? data.draft !== true : true
})
}
export function sortMDByDate(posts: Array<CollectionEntry<'post'>>) {
return posts.sort((a, b) => {
const aDate = new Date(a.data.updatedDate ?? a.data.publishDate).valueOf()
const bDate = new Date(b.data.updatedDate ?? b.data.publishDate).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<CollectionEntry<'post'>>) {
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<CollectionEntry<'post'>>) {
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<CollectionEntry<'post'>>
): Array<[string, number]> {
return [
...getAllTags(posts).reduce(
(acc, t) => acc.set(t, (acc.get(t) || 0) + 1),
new Map<string, number>()
)
].sort((a, b) => b[1] - a[1])
}