teasanctuary.ru/src/lib/util/Blogs.ts

33 lines
864 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import path from 'path';
export async function fetchPostsSorted() {
const allPosts = await fetchPosts();
const sortedPosts = allPosts
// Для списка постов оставляем только те, у которых объявлена дата публикации
.filter((a) => !!a.date)
.sort((a, b) => {
return new Date(b.date!).valueOf() - new Date(a.date!).valueOf();
});
return sortedPosts;
};
export async function fetchPosts() {
const allPostFiles = import.meta.glob('/src/blogs/*.md');
const iterablePostFiles = Object.entries(allPostFiles);
const allPosts: App.BlogPost[] = await Promise.all(
iterablePostFiles.map(async ([filePath, resolver]) => {
const { metadata }: any = await resolver();
const { name } = path.parse(filePath);
return {
slug: name,
...metadata
};
})
);
return allPosts;
};