-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.ts
46 lines (41 loc) · 1.27 KB
/
api.ts
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
43
44
45
46
import fs from "fs";
import { join } from "path";
import matter from "gray-matter";
import { Blog } from "@/app/types";
const postsDirectory = join(process.cwd(), "blog");
export function getPostSlugs() {
const allFiles = fs.readdirSync(postsDirectory);
const slugs = allFiles.filter((slug) => slug.endsWith(".md"));
return slugs;
}
export function getPostBySlug(slug: string): Blog {
const realSlug = slug.replace(/\.md$/, "");
const fullPath = join(postsDirectory, `${realSlug}.md`);
const fileContents = fs.readFileSync(fullPath, "utf8");
const { data, content } = matter(fileContents);
const { date, description, favorite, published, title, tags } = data;
return {
id: realSlug,
html: content,
fields: {
slug: realSlug,
},
frontmatter: { date, description, favorite, published, title, tags },
};
}
export function getAllPosts(): Blog[] {
const slugs = getPostSlugs();
try {
const posts = slugs
.map((slug) => getPostBySlug(slug))
.filter((post) => post.frontmatter.published)
// sort posts by date in descending order
.sort((post1, post2) =>
post1.frontmatter.date > post2.frontmatter.date ? -1 : 1
);
return posts;
} catch (e) {
console.log(e);
throw new Error("it broke");
}
}