Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat(blog): auto-scan MDX posts with draft support (#269)

Generb266501fcf99
diff --git a/src/web/src/app/blog/[slug]/page.tsx b/src/web/src/app/blog/[slug]/page.tsx
index 1aeaee151..cdd40b0be 100644
--- a/src/web/src/app/blog/[slug]/page.tsx
+++ b/src/web/src/app/blog/[slug]/page.tsx
@@ -6,8 +6,9 @@ import { getAllPosts, getPostBySlug } from "@/lib/blog/posts";
export const dynamicParams = false;
-export function generateStaticParams(): { slug: string }[] {
- return getAllPosts().map((post) => ({ slug: post.slug }));
+export async function generateStaticParams(): Promise<{ slug: string }[]> {
+ const posts = await getAllPosts();
+ return posts.map((post) => ({ slug: post.slug }));
}
export async function generateMetadata({
@@ -16,7 +17,7 @@ export async function generateMetadata({
params: Promise<{ slug: string }>;
}): Promise<Metadata> {
const { slug } = await params;
- const post = getPostBySlug(slug);
+ const post = await getPostBySlug(slug);
if (!post) return {};
return {
@@ -53,10 +54,10 @@ export default async function BlogPostPage({
params: Promise<{ slug: string }>;
}) {
const { slug } = await params;
- const post = getPostBySlug(slug);
+ const post = await getPostBySlug(slug);
if (!post) notFound();
- const posts = getAllPosts();
+ const posts = await getAllPosts();
const currentIndex = posts.findIndex((p) => p.slug === slug);
const prevPost = currentIndex < posts.length - 1 ? posts[currentIndex + 1] : null;
const nextPost = currentIndex > 0 ? posts[currentIndex - 1] : null;
diff --git a/src/web/src/app/blog/feed.xml/route.ts b/src/web/src/app/blog/feed.xml/route.ts
index a534c6648..e4d54ed7a 100644
--- a/src/web/src/app/blog/feed.xml/route.ts
+++ b/src/web/src/app/blog/feed.xml/route.ts
@@ -1,7 +1,7 @@
import { getAllPosts } from "@/lib/blog/posts";
-export function GET() {
- const posts = getAllPosts();
+export async function GET() {
+ const posts = await getAllPosts();
const siteUrl = "https://alook.ai";
const items = posts
diff --git a/src/web/src/app/blog/page.tsx b/src/web/src/app/blog/page.tsx
index 7771d8287..d58e5b370 100644
--- a/src/web/src/app/blog/page.tsx
+++ b/src/web/src/app/blog/page.tsx
@@ -34,8 +34,8 @@ const collectionJsonLd = {
url: "https://alook.ai/blog",
};
-export default function BlogPage() {
- const posts = getAllPosts();
+export default async function BlogPage() {
+ const posts = await getAllPosts();
const [featured, ...rest] = posts;
return (
diff --git a/src/web/src/app/sitemap.ts b/src/web/src/app/sitemap.ts
index aefe069c2..c79b16d1e 100644
--- a/src/web/src/app/sitemap.ts
+++ b/src/web/src/app/sitemap.ts
@@ -4,7 +4,7 @@ import { getAllPosts } from "@/lib/blog/posts";
const SITE_URL = "https://alook.ai";
-export default function sitemap(): MetadataRoute.Sitemap {
+export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const templateEntries: MetadataRoute.Sitemap = TEMPLATES.map((t) => ({
url: `${SITE_URL}/templates/${t.id}`,
lastModified: new Date(),
@@ -12,7 +12,8 @@ export default function sitemap(): MetadataRoute.Sitemap {
priority: 0.7,
}));
- const blogEntries: MetadataRoute.Sitemap = getAllPosts().map((post) => ({
+ const posts = await getAllPosts();
+ const blogEntries: MetadataRoute.Sitemap = posts.map((post) => ({
url: `${SITE_URL}/blog/${post.slug}`,
lastModified: new Date(post.date),
changeFrequency: "monthly",
diff --git a/src/web/src/lib/blog/posts/import-mdx.ts b/src/web/src/lib/blog/posts/import-mdx.ts
new file mode 100644
index 000000000..33bf6f19a
--- /dev/null
+++ b/src/web/src/lib/blog/posts/import-mdx.ts
@@ -0,0 +1,8 @@
+import type { BlogPost } from "../types";
+
+export async function importMdxMetadata(
+ slug: string
+): Promise<BlogPost | undefined> {
+ const mod = await import(`@/content/${slug}.mdx`);
+ return mod.metadata;
+}
diff --git a/src/web/src/lib/blog/posts/index.test.ts b/src/web/src/lib/blog/posts/index.test.ts
new file mode 100644
index 000000000..228fa5d68
--- /dev/null
+++ b/src/web/src/lib/blog/posts/index.test.ts
@@ -0,0 +1,177 @@
+import { describe, it, expect, vi, beforeEach } from "vitest";
+import type { BlogPost } from "../types";
+
+vi.mock("fs", () => ({
+ readdirSync: vi.fn(),
+}));
+
+vi.mock("./import-mdx", () => ({
+ importMdxMetadata: vi.fn(),
+}));
+
+import { readdirSync } from "fs";
+import { importMdxMetadata } from "./import-mdx";
+
+const mockReaddirSync = vi.mocked(readdirSync);
+const mockImportMdxMetadata = vi.mocked(importMdxMetadata);
+
+const postA: BlogPost = {
+ slug: "post-a",
+ title: "Post A",
+ date: "2026-05-01",
+ author: "Alice",
+ excerpt: "First post",
+ readingTime: "3 min read",
+};
+
+const postB: BlogPost = {
+ slug: "post-b",
+ title: "Post B",
+ date: "2026-06-01",
+ author: "Bob",
+ excerpt: "Second post",
+ readingTime: "5 min read",
+};
+
+const draftPost: BlogPost = {
+ slug: "draft-post",
+ title: "Draft Post",
+ date: "2026-06-02",
+ author: "Charlie",
+ excerpt: "Draft",
+ readingTime: "2 min read",
+ draft: true,
+};
+
+const incompletePost = {
+ slug: "incomplete",
+ title: "No Author",
+} as unknown as BlogPost;
+
+beforeEach(() => {
+ vi.resetModules();
+ vi.clearAllMocks();
+});
+
+describe("getAllPosts", () => {
+ it("auto-discovers all mdx files and returns them sorted by date descending", async () => {
+ mockReaddirSync.mockReturnValue(
+ ["post-a.mdx", "post-b.mdx"] as unknown as ReturnType<
+ typeof readdirSync
+ >
+ );
+ mockImportMdxMetadata.mockImplementation(async (slug) => {
+ if (slug === "post-a") return postA;
+ if (slug === "post-b") return postB;
+ return undefined;
+ });
+
+ const { getAllPosts } = await import("./index");
+ const posts = await getAllPosts();
+
+ expect(posts).toHaveLength(2);
+ expect(posts[0].slug).toBe("post-b");
+ expect(posts[1].slug).toBe("post-a");
+ });
+
+ it("excludes draft posts from results", async () => {
+ mockReaddirSync.mockReturnValue(
+ ["post-a.mdx", "draft-post.mdx"] as unknown as ReturnType<
+ typeof readdirSync
+ >
+ );
+ mockImportMdxMetadata.mockImplementation(async (slug) => {
+ if (slug === "post-a") return postA;
+ if (slug === "draft-post") return draftPost;
+ return undefined;
+ });
+
+ const { getAllPosts } = await import("./index");
+ const posts = await getAllPosts();
+
+ expect(posts).toHaveLength(1);
+ expect(posts[0].slug).toBe("post-a");
+ expect(posts.find((p) => p.slug === "draft-post")).toBeUndefined();
+ });
+
+ it("skips files without metadata export", async () => {
+ mockReaddirSync.mockReturnValue(
+ ["post-a.mdx", "no-metadata.mdx"] as unknown as ReturnType<
+ typeof readdirSync
+ >
+ );
+ mockImportMdxMetadata.mockImplementation(async (slug) => {
+ if (slug === "post-a") return postA;
+ return undefined;
+ });
+
+ const { getAllPosts } = await import("./index");
+ const posts = await getAllPosts();
+
+ expect(posts).toHaveLength(1);
+ expect(posts[0].slug).toBe("post-a");
+ });
+
+ it("skips files with missing required fields and logs a warning", async () => {
+ mockReaddirSync.mockReturnValue(
+ ["post-a.mdx", "incomplete.mdx"] as unknown as ReturnType<
+ typeof readdirSync
+ >
+ );
+ mockImportMdxMetadata.mockImplementation(async (slug) => {
+ if (slug === "post-a") return postA;
+ if (slug === "incomplete") return incompletePost;
+ return undefined;
+ });
+
+ const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
+ const { getAllPosts } = await import("./index");
+ const posts = await getAllPosts();
+
+ expect(posts).toHaveLength(1);
+ expect(posts[0].slug).toBe("post-a");
+ expect(warnSpy).toHaveBeenCalledWith(
+ expect.stringContaining("missing required field")
+ );
+ warnSpy.mockRestore();
+ });
+});
+
+describe("getPostBySlug", () => {
+ it("returns the post matching the given slug", async () => {
+ mockReaddirSync.mockReturnValue(
+ ["post-a.mdx", "post-b.mdx"] as unknown as ReturnType<
+ typeof readdirSync
+ >
+ );
+ mockImportMdxMetadata.mockImplementation(async (slug) => {
+ if (slug === "post-a") return postA;
+ if (slug === "post-b") return postB;
+ return undefined;
+ });
+
+ const { getPostBySlug } = await import("./index");
+ const post = await getPostBySlug("post-a");
+
+ expect(post).toBeDefined();
+ expect(post!.title).toBe("Post A");
+ });
+
+ it("returns undefined for a draft slug", async () => {
+ mockReaddirSync.mockReturnValue(
+ ["post-a.mdx", "draft-post.mdx"] as unknown as ReturnType<
+ typeof readdirSync
+ >
+ );
+ mockImportMdxMetadata.mockImplementation(async (slug) => {
+ if (slug === "post-a") return postA;
+ if (slug === "draft-post") return draftPost;
+ return undefined;
+ });
+
+ const { getPostBySlug } = await import("./index");
+ const post = await getPostBySlug("draft-post");
+
+ expect(post).toBeUndefined();
+ });
+});
diff --git a/src/web/src/lib/blog/posts/index.ts b/src/web/src/lib/blog/posts/index.ts
index a01555a2a..bfee57f61 100644
--- a/src/web/src/lib/blog/posts/index.ts
+++ b/src/web/src/lib/blog/posts/index.ts
@@ -1,17 +1,74 @@
+import { readdirSync } from "fs";
+import { join, dirname } from "path";
+import { fileURLToPath } from "url";
import type { BlogPost } from "../types";
+import { importMdxMetadata } from "./import-mdx";
-import { metadata as whyWeBuiltAlook } from "@/content/why-we-built-alook.mdx";
+const __dirname = dirname(fileURLToPath(import.meta.url));
+const contentDir = join(__dirname, "..", "..", "..", "content");
-const posts: BlogPost[] = [whyWeBuiltAlook];
+const REQUIRED_FIELDS: (keyof BlogPost)[] = [
+ "slug",
+ "title",
+ "date",
+ "author",
+ "excerpt",
+ "readingTime",
+];
+
+function validateMetadata(
+ metadata: Record<string, unknown>,
+ file: string
+): metadata is BlogPost {
+ for (const field of REQUIRED_FIELDS) {
+ if (!metadata[field]) {
+ console.warn(
+ `[blog] Skipping ${file}: missing required field "${field}"`
+ );
+ return false;
+ }
+ }
+ return true;
+}
+
+let cachedPosts: BlogPost[] | null = null;
export type { BlogPost } from "../types";
-export function getAllPosts(): BlogPost[] {
+export async function getAllPosts(): Promise<BlogPost[]> {
+ if (cachedPosts) {
+ return [...cachedPosts].sort(
+ (a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
+ );
+ }
+
+ const files = readdirSync(contentDir).filter((f) => f.endsWith(".mdx"));
+ const posts: BlogPost[] = [];
+
+ for (const file of files) {
+ const slug = file.replace(/\.mdx$/, "");
+ const metadata = await importMdxMetadata(slug);
+
+ if (
+ !metadata ||
+ !validateMetadata(metadata as Record<string, unknown>, file)
+ )
+ continue;
+ if (metadata.draft) continue;
+
+ posts.push(metadata);
+ }
+
+ cachedPosts = posts;
+
return [...posts].sort(
(a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
);
}
-export function getPostBySlug(slug: string): BlogPost | undefined {
+export async function getPostBySlug(
+ slug: string
+): Promise<BlogPost | undefined> {
+ const posts = await getAllPosts();
return posts.find((p) => p.slug === slug);
}
diff --git a/src/web/src/lib/blog/types.ts b/src/web/src/lib/blog/types.ts
index a94d9a3a7..99381f123 100644
--- a/src/web/src/lib/blog/types.ts
+++ b/src/web/src/lib/blog/types.ts
@@ -5,4 +5,5 @@ export type BlogPost = {
author: string;
excerpt: string;
readingTime: string;
+ draft?: boolean;
};