added case-study slug framework
All checks were successful
Build and Deploy NextJS SculptedArc Website / build-and-deploy (push) Successful in 1m10s
All checks were successful
Build and Deploy NextJS SculptedArc Website / build-and-deploy (push) Successful in 1m10s
This commit is contained in:
33
lib/case-study.ts
Normal file
33
lib/case-study.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
// src/lib/blog.ts
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import matter from 'gray-matter';
|
||||
import { remark } from 'remark';
|
||||
import html from 'remark-html';
|
||||
|
||||
const postsDirectory = path.join(process.cwd(), 'content/case-studies');
|
||||
|
||||
export async function getPostBySlug(slug: string) {
|
||||
const fullPath = path.join(postsDirectory, `${slug}.md`);
|
||||
|
||||
// Guard against file system traversal attacks or missing files
|
||||
if (!fs.existsSync(fullPath)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const fileContents = fs.readFileSync(fullPath, 'utf8');
|
||||
|
||||
// Use gray-matter to parse the study metadata section
|
||||
const { data, content } = matter(fileContents);
|
||||
|
||||
// Use remark to convert markdown into HTML string
|
||||
const processedContent = await remark().use(html).process(content);
|
||||
const contentHtml = processedContent.toString();
|
||||
|
||||
return {
|
||||
slug,
|
||||
contentHtml,
|
||||
title: data.title || 'Untitled Study',
|
||||
...data,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user