All checks were successful
Build and Deploy NextJS SculptedArc Website / build-and-deploy (push) Successful in 1m10s
33 lines
920 B
TypeScript
33 lines
920 B
TypeScript
// 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,
|
|
};
|
|
} |