Skip to content

Commit 69bf42c

Browse files
authored
Refactor blog directory handling (#927)
- 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.
1 parent baaa863 commit 69bf42c

4 files changed

Lines changed: 16 additions & 12 deletions

File tree

apps/website/app/(home)/blog/[slug]/page.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import fs from "fs/promises";
2-
import path from "path";
32
import { notFound } from "next/navigation";
43
import { Metadata } from "next";
54
import { getProcessedMarkdownFile } from "~/utils/getProcessedMarkdownFile";
6-
import { BLOG_PATH } from "~/data/constants";
75
import { getFileMetadata } from "~/utils/getFileMetadata";
6+
import { BLOG_DIRECTORY } from "~/(home)/blog/blogDirectory";
87

98
type Params = {
109
params: Promise<{
@@ -17,7 +16,7 @@ const BlogPost = async ({ params }: Params) => {
1716
const { slug } = await params;
1817
const { data, contentHtml } = await getProcessedMarkdownFile({
1918
slug,
20-
directory: BLOG_PATH,
19+
directory: BLOG_DIRECTORY,
2120
});
2221

2322
return (
@@ -46,9 +45,8 @@ const BlogPost = async ({ params }: Params) => {
4645

4746
export const generateStaticParams = async () => {
4847
try {
49-
const blogPath = path.resolve(process.cwd(), BLOG_PATH);
5048
const directoryExists = await fs
51-
.stat(blogPath)
49+
.stat(BLOG_DIRECTORY)
5250
.then((stats) => stats.isDirectory())
5351
.catch(() => false);
5452

@@ -59,7 +57,7 @@ export const generateStaticParams = async () => {
5957
return [];
6058
}
6159

62-
const files = await fs.readdir(blogPath);
60+
const files = await fs.readdir(BLOG_DIRECTORY);
6361

6462
const mdFiles = files.filter((filename) => filename.endsWith(".md"));
6563

@@ -68,7 +66,7 @@ export const generateStaticParams = async () => {
6866
try {
6967
const { published } = await getFileMetadata({
7068
filename,
71-
directory: BLOG_PATH,
69+
directory: BLOG_DIRECTORY,
7270
});
7371
return { filename, published };
7472
} catch (error) {
@@ -100,7 +98,7 @@ export const generateMetadata = async ({
10098
const { slug } = await params;
10199
const { data } = await getProcessedMarkdownFile({
102100
slug,
103-
directory: BLOG_PATH,
101+
directory: BLOG_DIRECTORY,
104102
});
105103

106104
return {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import path from "node:path";
2+
import { fileURLToPath } from "node:url";
3+
4+
const BLOG_MODULE_PATH = fileURLToPath(import.meta.url);
5+
6+
export const BLOG_DIRECTORY = path.join(
7+
path.dirname(BLOG_MODULE_PATH),
8+
"posts",
9+
);

apps/website/app/(home)/blog/readBlogs.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import path from "path";
22
import fs from "fs/promises";
33
import matter from "gray-matter";
4-
import { BLOG_PATH } from "~/data/constants";
54
import { PageSchema, type PageData } from "~/types/schema";
6-
7-
const BLOG_DIRECTORY = path.join(process.cwd(), BLOG_PATH);
5+
import { BLOG_DIRECTORY } from "./blogDirectory";
86

97
async function validateBlogDirectory(): Promise<boolean> {
108
try {

apps/website/app/data/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export const DESCRIPTION =
44
"Discourse Graphs are a tool and ecosystem for collaborative knowledge synthesis, enabling researchers to map ideas and arguments in a modular, composable graph format.";
55
export const SHORT_DESCRIPTION =
66
"A tool and ecosystem for collaborative knowledge synthesis";
7-
export const BLOG_PATH = "app/(home)/blog/posts";
87

98
export const TEAM_MEMBERS: TeamMember[] = [
109
{

0 commit comments

Comments
 (0)