blob: 3fcdd15265640283be98cd0cb684259554d4ef84 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
import path from 'path';
import fs from 'fs';
import matter from 'gray-matter';
import { remark } from 'remark';
import html from 'remark-html';
const blogsDir = path.join(process.cwd(), '_blogs');
export interface Blog {
author: {
name: string;
social: string;
};
date: Date;
title: string;
slug: string;
excerpt: string;
content: string;
}
export const getBlogFromSlug = (slug: string): Blog => {
const fullPath = path.join(blogsDir, `${slug}.md`);
const file = fs.readFileSync(fullPath, 'utf8');
const { data, content } = matter(file);
return {
...data,
date: new Date(data.date),
content,
slug,
} as Blog;
};
export const getAllBlogs = (): Blog[] => {
const rawMdFiles = fs.readdirSync(blogsDir);
const slugs = rawMdFiles.map((file) => file.replace(/\.md$/, ''));
return slugs.map(getBlogFromSlug);
};
export const markdownToHtml = async (markdown: string): Promise<string> => {
const htmlResult = await remark().use(html).process(markdown);
return htmlResult.toString();
};
|