- Migrate all apps/website components from JS/JSX to TypeScript/TSX - Add blog pages (layout, listing, slug) in TypeScript - Add tsconfig.json and tailwind.config.mjs to website - Fix cookie domain handling in vite/App.tsx (set .useautumn.com in prod) - Update auth.ts, afterSessionCreated, afterSessionDeleted Made-with: Cursor
76 lines
1.8 KiB
TypeScript
76 lines
1.8 KiB
TypeScript
import fs from "fs";
|
|
import matter from "gray-matter";
|
|
import path from "path";
|
|
|
|
const CONTENT_DIR = path.join(process.cwd(), "content", "blog");
|
|
|
|
export type BlogPostSummary = {
|
|
slug: string;
|
|
title: string;
|
|
description: string;
|
|
date: string | null;
|
|
author: string;
|
|
image: string | null;
|
|
};
|
|
|
|
export type BlogPost = BlogPostSummary & {
|
|
source: string;
|
|
};
|
|
|
|
export function getAllPosts(): BlogPostSummary[] {
|
|
if (!fs.existsSync(CONTENT_DIR)) return [];
|
|
|
|
const files = fs
|
|
.readdirSync(CONTENT_DIR)
|
|
.filter((file) => file.endsWith(".mdx"));
|
|
|
|
const posts = files.map((filename) => {
|
|
const filePath = path.join(CONTENT_DIR, filename);
|
|
const raw = fs.readFileSync(filePath, "utf-8");
|
|
const { data } = matter(raw);
|
|
|
|
return {
|
|
slug: data.slug || filename.replace(/\.mdx$/, ""),
|
|
title: data.title || "Untitled",
|
|
description: data.description || "",
|
|
date: data.date || null,
|
|
author: data.author || "Autumn Team",
|
|
image: data.image || null,
|
|
};
|
|
});
|
|
|
|
return posts.sort((a, b) => {
|
|
if (!a.date || !b.date) return 0;
|
|
return new Date(b.date).getTime() - new Date(a.date).getTime();
|
|
});
|
|
}
|
|
|
|
export function getPostBySlug({ slug }: { slug: string }): BlogPost | null {
|
|
if (!fs.existsSync(CONTENT_DIR)) return null;
|
|
|
|
const files = fs
|
|
.readdirSync(CONTENT_DIR)
|
|
.filter((file) => file.endsWith(".mdx"));
|
|
|
|
for (const filename of files) {
|
|
const filePath = path.join(CONTENT_DIR, filename);
|
|
const raw = fs.readFileSync(filePath, "utf-8");
|
|
const { data, content } = matter(raw);
|
|
const fileSlug = data.slug || filename.replace(/\.mdx$/, "");
|
|
|
|
if (fileSlug === slug) {
|
|
return {
|
|
slug: fileSlug,
|
|
title: data.title || "Untitled",
|
|
description: data.description || "",
|
|
date: data.date || null,
|
|
author: data.author || "Autumn Team",
|
|
image: data.image || null,
|
|
source: content,
|
|
};
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|