72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import matter from 'gray-matter';
|
|
import { remark } from 'remark';
|
|
import html from 'remark-html';
|
|
|
|
const contentDir = path.join(process.cwd(), 'content');
|
|
|
|
export interface Post {
|
|
slug: string;
|
|
title: string;
|
|
date: string;
|
|
excerpt: string;
|
|
tags: string[];
|
|
content?: string;
|
|
category: 'blog' | 'notebook' | 'infosec';
|
|
}
|
|
|
|
export function getPosts(category: 'blog' | 'notebook' | 'infosec'): Post[] {
|
|
const dir = path.join(contentDir, category);
|
|
if (!fs.existsSync(dir)) return [];
|
|
|
|
const files = fs.readdirSync(dir).filter((f) => f.endsWith('.md'));
|
|
|
|
return files
|
|
.map((filename) => {
|
|
const slug = filename.replace('.md', '');
|
|
const raw = fs.readFileSync(path.join(dir, filename), 'utf-8');
|
|
const { data } = matter(raw);
|
|
return {
|
|
slug,
|
|
title: data.title || slug,
|
|
date: data.date || '2024-01-01',
|
|
excerpt: data.excerpt || '',
|
|
tags: data.tags || [],
|
|
category,
|
|
};
|
|
})
|
|
.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
|
|
}
|
|
|
|
export async function getPost(
|
|
category: 'blog' | 'notebook' | 'infosec',
|
|
slug: string
|
|
): Promise<Post | null> {
|
|
const filePath = path.join(contentDir, category, `${slug}.md`);
|
|
if (!fs.existsSync(filePath)) return null;
|
|
|
|
const raw = fs.readFileSync(filePath, 'utf-8');
|
|
const { data, content } = matter(raw);
|
|
|
|
const processed = await remark().use(html).process(content);
|
|
|
|
return {
|
|
slug,
|
|
title: data.title || slug,
|
|
date: data.date || '2024-01-01',
|
|
excerpt: data.excerpt || '',
|
|
tags: data.tags || [],
|
|
content: processed.toString(),
|
|
category,
|
|
};
|
|
}
|
|
|
|
export function getAllPosts(): Post[] {
|
|
return [
|
|
...getPosts('blog'),
|
|
...getPosts('infosec'),
|
|
...getPosts('notebook'),
|
|
].sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
|
|
}
|