Chrysoblog/src/lib/posts.ts

42 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-10-10 12:00:35 -07:00
export async function getPosts(tag: string | null = null) {
2024-09-29 16:40:26 -07:00
const allPostFiles = import.meta.glob('$content/post/*.md');
const iterablePostFiles = Object.entries(allPostFiles);
2024-10-10 12:00:35 -07:00
let posts = await Promise.all(
2024-09-29 16:40:26 -07:00
iterablePostFiles.map(async ([pathMarkdown, resolver]) => {
const { metadata } = await resolver();
2024-09-29 18:45:35 -07:00
const pathPost = "/post/" + pathMarkdown.slice(pathMarkdown.lastIndexOf("/") + 1, -".md".length);
2024-09-29 16:40:26 -07:00
return {
meta: {
...metadata,
date: new Date(metadata.date),
},
path: pathPost
};
})
);
2024-10-10 12:00:35 -07:00
if (tag)
posts = posts.filter(obj => obj.meta.tags.includes(tag))
2024-09-29 16:40:26 -07:00
2024-10-10 12:00:35 -07:00
posts.sort((post1, post2) => {
2024-09-29 16:40:26 -07:00
const date1: Date = post1.meta.date;
const date2: Date = post2.meta.date;
return date2.getTime() - date1.getTime();
});
2024-10-10 12:00:35 -07:00
return posts;
}
export async function getTags() {
const allPostFiles = import.meta.glob('$content/post/*.md');
const iterablePostFiles = Object.entries(allPostFiles);
const allPosts: string[][] = await Promise.all(
iterablePostFiles.map(async ([_, resolver]) => {
const { metadata } = await resolver();
return metadata.tags;
})
);
return new Set(allPosts.flat());
2024-09-29 16:40:26 -07:00
}