From c3c821971a9ea87331562afd242c807559388648 Mon Sep 17 00:00:00 2001 From: Michael Gartner Date: Sun, 29 Mar 2026 19:40:19 -0600 Subject: [PATCH] Refactor blog directory handling - Introduced a new blogDirectory.ts file to centralize the blog directory path management. - Updated readBlogs.tsx and page.tsx to import and use BLOG_DIRECTORY instead of the previously hardcoded BLOG_PATH. - Removed the BLOG_PATH constant from constants.ts to streamline the codebase. --- apps/website/app/(home)/blog/[slug]/page.tsx | 14 ++++++-------- apps/website/app/(home)/blog/blogDirectory.ts | 9 +++++++++ apps/website/app/(home)/blog/readBlogs.tsx | 4 +--- apps/website/app/data/constants.ts | 1 - 4 files changed, 16 insertions(+), 12 deletions(-) create mode 100644 apps/website/app/(home)/blog/blogDirectory.ts diff --git a/apps/website/app/(home)/blog/[slug]/page.tsx b/apps/website/app/(home)/blog/[slug]/page.tsx index 08a9f5700..17bb62248 100644 --- a/apps/website/app/(home)/blog/[slug]/page.tsx +++ b/apps/website/app/(home)/blog/[slug]/page.tsx @@ -1,10 +1,9 @@ import fs from "fs/promises"; -import path from "path"; import { notFound } from "next/navigation"; import { Metadata } from "next"; import { getProcessedMarkdownFile } from "~/utils/getProcessedMarkdownFile"; -import { BLOG_PATH } from "~/data/constants"; import { getFileMetadata } from "~/utils/getFileMetadata"; +import { BLOG_DIRECTORY } from "~/(home)/blog/blogDirectory"; type Params = { params: Promise<{ @@ -17,7 +16,7 @@ const BlogPost = async ({ params }: Params) => { const { slug } = await params; const { data, contentHtml } = await getProcessedMarkdownFile({ slug, - directory: BLOG_PATH, + directory: BLOG_DIRECTORY, }); return ( @@ -46,9 +45,8 @@ const BlogPost = async ({ params }: Params) => { export const generateStaticParams = async () => { try { - const blogPath = path.resolve(process.cwd(), BLOG_PATH); const directoryExists = await fs - .stat(blogPath) + .stat(BLOG_DIRECTORY) .then((stats) => stats.isDirectory()) .catch(() => false); @@ -59,7 +57,7 @@ export const generateStaticParams = async () => { return []; } - const files = await fs.readdir(blogPath); + const files = await fs.readdir(BLOG_DIRECTORY); const mdFiles = files.filter((filename) => filename.endsWith(".md")); @@ -68,7 +66,7 @@ export const generateStaticParams = async () => { try { const { published } = await getFileMetadata({ filename, - directory: BLOG_PATH, + directory: BLOG_DIRECTORY, }); return { filename, published }; } catch (error) { @@ -100,7 +98,7 @@ export const generateMetadata = async ({ const { slug } = await params; const { data } = await getProcessedMarkdownFile({ slug, - directory: BLOG_PATH, + directory: BLOG_DIRECTORY, }); return { diff --git a/apps/website/app/(home)/blog/blogDirectory.ts b/apps/website/app/(home)/blog/blogDirectory.ts new file mode 100644 index 000000000..564943842 --- /dev/null +++ b/apps/website/app/(home)/blog/blogDirectory.ts @@ -0,0 +1,9 @@ +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +const BLOG_MODULE_PATH = fileURLToPath(import.meta.url); + +export const BLOG_DIRECTORY = path.join( + path.dirname(BLOG_MODULE_PATH), + "posts", +); diff --git a/apps/website/app/(home)/blog/readBlogs.tsx b/apps/website/app/(home)/blog/readBlogs.tsx index 2acc2bbb3..21ccd0d7c 100644 --- a/apps/website/app/(home)/blog/readBlogs.tsx +++ b/apps/website/app/(home)/blog/readBlogs.tsx @@ -1,10 +1,8 @@ import path from "path"; import fs from "fs/promises"; import matter from "gray-matter"; -import { BLOG_PATH } from "~/data/constants"; import { PageSchema, type PageData } from "~/types/schema"; - -const BLOG_DIRECTORY = path.join(process.cwd(), BLOG_PATH); +import { BLOG_DIRECTORY } from "./blogDirectory"; async function validateBlogDirectory(): Promise { try { diff --git a/apps/website/app/data/constants.ts b/apps/website/app/data/constants.ts index a7f49d9ee..50ac0f39c 100644 --- a/apps/website/app/data/constants.ts +++ b/apps/website/app/data/constants.ts @@ -4,7 +4,6 @@ export const DESCRIPTION = "Discourse Graphs are a tool and ecosystem for collaborative knowledge synthesis, enabling researchers to map ideas and arguments in a modular, composable graph format."; export const SHORT_DESCRIPTION = "A tool and ecosystem for collaborative knowledge synthesis"; -export const BLOG_PATH = "app/(home)/blog/posts"; export const TEAM_MEMBERS: TeamMember[] = [ {