Блоги #1
1 changed files with 16 additions and 4 deletions
|
|
@ -2,15 +2,27 @@ import path from 'path';
|
|||
|
||||
export const THUMBNAIL_DEFAULT = "https://teasanctuary.ru/common/background-day.webp";
|
||||
|
||||
export async function fetchPostsSorted() {
|
||||
export type PostComparer = (a: App.BlogPost, b: App.BlogPost) => number;
|
||||
|
||||
export const sortPostsByPostDate: PostComparer = (a, b) => new Date(b.date!).valueOf() - new Date(a.date!).valueOf();
|
||||
|
||||
function laterDate(a: string, b?: string): number {
|
||||
const dateA = new Date(a).valueOf();
|
||||
if (!b) return dateA;
|
||||
|
||||
const dateB = new Date(b).valueOf();
|
||||
return Math.max(dateA, dateB);
|
||||
}
|
||||
|
||||
export const sortPostsByPostAndUpdateDate: PostComparer = (a, b) => laterDate(b.date!, b.dateChanged) - laterDate(a.date!, a.dateChanged);
|
||||
|
||||
export async function fetchPostsSorted(postComparer?: PostComparer) {
|
||||
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();
|
||||
});
|
||||
.sort(postComparer ?? sortPostsByPostDate);
|
||||
|
||||
return sortedPosts;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue