west-sound-hall/src/lib/articles.ts

34 lines
767 B
TypeScript
Raw Normal View History

2023-11-26 09:36:23 -08:00
import glob from 'fast-glob';
2023-11-15 19:42:05 -08:00
2023-11-26 09:36:23 -08:00
interface BlogPost {
title: string;
description: string;
author: string;
date: string;
2023-11-15 19:42:05 -08:00
}
2023-11-26 09:36:23 -08:00
export interface BlogPostWithSlug extends BlogPost {
slug: string;
2023-11-15 19:42:05 -08:00
}
2023-11-26 09:36:23 -08:00
async function importBlogPost(filename: string): Promise<BlogPostWithSlug> {
2024-01-28 10:01:52 -08:00
let { article } = (await import(`../app/news/${filename}`)) as {
2023-11-26 09:36:23 -08:00
default: React.ComponentType;
article: BlogPost;
};
2023-11-15 19:42:05 -08:00
return {
2023-11-26 09:36:23 -08:00
slug: filename.replace(/(\/page)?\.mdx$/, ''),
2023-11-15 19:42:05 -08:00
...article,
2023-11-26 09:36:23 -08:00
};
2023-11-15 19:42:05 -08:00
}
2023-11-26 09:36:23 -08:00
export async function getAllBlogPosts() {
2023-11-15 19:42:05 -08:00
let articleFilenames = await glob('*/page.mdx', {
2024-01-28 10:01:52 -08:00
cwd: './src/app/news',
2023-11-26 09:36:23 -08:00
});
2023-11-15 19:42:05 -08:00
2023-11-26 09:36:23 -08:00
const posts = await Promise.all(articleFilenames.map(importBlogPost));
return posts.sort((a, z) => +new Date(z.date) - +new Date(a.date));
2023-11-15 19:42:05 -08:00
}