34 lines
1.2 KiB
TypeScript
34 lines
1.2 KiB
TypeScript
import {NextResponse} from 'next/server';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import matter from 'gray-matter';
|
|
|
|
export async function GET() {
|
|
try {
|
|
const projectsDirectory = path.join(process.cwd(), 'public/projects');
|
|
const filenames = fs.readdirSync(projectsDirectory);
|
|
|
|
const projects = filenames
|
|
.filter((filename) => {
|
|
const filePath = path.join(projectsDirectory, filename);
|
|
return fs.statSync(filePath).isFile();
|
|
})
|
|
.map((filename) => {
|
|
const filePath = path.join(projectsDirectory, filename);
|
|
const fileContents = fs.readFileSync(filePath, 'utf8');
|
|
const {data} = matter(fileContents);
|
|
|
|
return {
|
|
id: data.id,
|
|
title: data.title,
|
|
description: data.description,
|
|
slug: filename.replace('.md', ''),
|
|
};
|
|
});
|
|
|
|
return NextResponse.json(projects);
|
|
} catch (error) {
|
|
console.error("Failed to fetch projects:", error);
|
|
return NextResponse.json({error: 'Failed to fetch projects'}, {status: 500});
|
|
}
|
|
} |