mirror of
https://github.com/prdlk/website.git
synced 2026-08-04 10:21:42 +00:00
init(): Initialize project with basic configuration files
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { type CollectionEntry, getCollection } from "astro:content";
|
||||
|
||||
/** filter out draft posts based on the environment */
|
||||
export async function getAllPosts(): Promise<CollectionEntry<"post">[]> {
|
||||
return await getCollection("post", ({ data }) => {
|
||||
return import.meta.env.PROD ? !data.draft : true;
|
||||
});
|
||||
}
|
||||
|
||||
/** Get tag metadata by tag name */
|
||||
export async function getTagMeta(tag: string): Promise<CollectionEntry<"tag"> | undefined> {
|
||||
const tagEntries = await getCollection("tag", (entry) => {
|
||||
return entry.id === tag;
|
||||
});
|
||||
return tagEntries[0];
|
||||
}
|
||||
|
||||
/** groups posts by year (based on option siteConfig.sortPostsByUpdatedDate), using the year as the key
|
||||
* Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so.
|
||||
*/
|
||||
export function groupPostsByYear(posts: CollectionEntry<"post">[]) {
|
||||
return Object.groupBy(posts, (post) => post.data.publishDate.getFullYear().toString());
|
||||
}
|
||||
|
||||
/** returns all tags created from posts (inc duplicate tags)
|
||||
* Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so.
|
||||
* */
|
||||
export function getAllTags(posts: CollectionEntry<"post">[]) {
|
||||
return posts.flatMap((post) => [...post.data.tags]);
|
||||
}
|
||||
|
||||
/** returns all unique tags created from posts
|
||||
* Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so.
|
||||
* */
|
||||
export function getUniqueTags(posts: CollectionEntry<"post">[]) {
|
||||
return [...new Set(getAllTags(posts))];
|
||||
}
|
||||
|
||||
/** returns a count of each unique tag - [[tagName, count], ...]
|
||||
* Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so.
|
||||
* */
|
||||
export function getUniqueTagsWithCount(posts: CollectionEntry<"post">[]): [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]);
|
||||
}
|
||||
Reference in New Issue
Block a user