-
Notifications
You must be signed in to change notification settings - Fork 1
/
file-helpers.ts
46 lines (35 loc) · 1.17 KB
/
file-helpers.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
/* eslint-disable no-await-in-loop */
/* eslint-disable no-restricted-syntax */
import fs from "fs/promises";
import matter from "gray-matter";
import path from "path";
import React from "react";
const BLOG_POST_DIR_PATH = "/content";
export type BlogPostMetadata = {
title: string;
slug: string;
};
export const loadBlogPost = React.cache(async (slug: string) => {
const rawContent = await readFile(`${BLOG_POST_DIR_PATH}/${slug}.mdx`);
const { data: frontmatter, content } = matter(rawContent);
return { frontmatter, content };
});
export const getBlogPostList = async () => {
const fileNames = await readDirectory(BLOG_POST_DIR_PATH);
const blogPosts = [];
for (const fileName of fileNames) {
const rawContent = await readFile(`${BLOG_POST_DIR_PATH}/${fileName}`);
const { data: frontmatter } = matter(rawContent);
blogPosts.push({
slug: fileName.replace(".mdx", ""),
title: frontmatter.title,
});
}
return blogPosts;
};
function readFile(localPath: string) {
return fs.readFile(path.join(process.cwd(), localPath), "utf8");
}
function readDirectory(localPath: string) {
return fs.readdir(path.join(process.cwd(), localPath));
}