Chrysoblog/src/lib/posts.ts

27 lines
700 B
TypeScript
Raw Normal View History

2024-09-29 16:40:26 -07:00
export async function getPosts() {
const allPostFiles = import.meta.glob('$content/post/*.md');
const iterablePostFiles = Object.entries(allPostFiles);
const allPosts = await Promise.all(
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
};
})
);
allPosts.sort((post1, post2) => {
const date1: Date = post1.meta.date;
const date2: Date = post2.meta.date;
return date2.getTime() - date1.getTime();
});
return allPosts;
}