-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblog.ts
More file actions
38 lines (29 loc) · 855 Bytes
/
Copy pathblog.ts
File metadata and controls
38 lines (29 loc) · 855 Bytes
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
import path from "path";
import fs from "fs/promises";
import { parse } from "yaml";
export const MD_BASEDIR = path.join(process.cwd(), "blog");
export type Blog = {
title: string;
markdown: string;
createdAt: string;
description: string;
edittedBy: string;
};
export type BlogHeader = Omit<Blog, "markdown">;
export async function readBlog(name: string): Promise<Blog> {
const raw = await fs.readFile(path.join(MD_BASEDIR, name + ".md"), {
encoding: "utf-8",
});
const split = raw.indexOf("---");
const headerStr = raw.slice(0, split);
const markdown = raw.slice(split);
const header: BlogHeader = parse(headerStr);
return {
...header,
markdown,
};
}
export async function listBlogs(): Promise<string[]> {
const names = await fs.readdir(MD_BASEDIR);
return names.map((x) => x.slice(0, x.length - 3));
}