-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarkdowns.ts
60 lines (49 loc) · 1.87 KB
/
markdowns.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { serialize } from 'next-mdx-remote/serialize';
import fs from 'fs/promises';
import path from 'path';
import { Paths } from '@/common/enums/paths';
import { MDXRemoteSerializeResult } from 'next-mdx-remote';
type Title = { title: Buffer };
type Content = { content: Buffer };
type Data = Partial<Title & Content>;
type PostSourceFile = Title | Content;
type PostData = PostSourceFile[];
async function getFileSource(folderPath: string, filename: string) {
const filePath = path.join(folderPath, filename);
const source = await fs.readFile(filePath);
const nameOfFile = path.parse(filename).name;
return { [nameOfFile]: source } as PostSourceFile;
}
async function transformPostData(post: PostData): Promise<{
title: MDXRemoteSerializeResult;
content: MDXRemoteSerializeResult;
}> {
const postData: Data = { ...post[0], ...post[1] };
const serializedTitle = await serialize(postData?.title?.toString() ?? '');
const serializedContent = await serialize(
postData?.content?.toString() ?? ''
);
return {
title: serializedTitle,
content: serializedContent
};
}
async function getPostFromFolderName(folderName: string) {
const { MARKDOWNS_PATH, FAQ_PATH } = Paths;
const folderPath = path.join(MARKDOWNS_PATH, FAQ_PATH, folderName);
const fileNames = await fs.readdir(folderPath);
const getFileSourcePromises = fileNames.map(filename =>
getFileSource(folderPath, filename)
);
const postData: PostData = await Promise.all(getFileSourcePromises);
return transformPostData(postData);
}
export async function getFAQMarkdowns() {
const { MARKDOWNS_PATH, FAQ_PATH } = Paths;
const faqPath = path.join(MARKDOWNS_PATH, FAQ_PATH);
const folderNames = await fs.readdir(faqPath);
const getPostFromFolderNamePromises = folderNames.map(folderName =>
getPostFromFolderName(folderName)
);
return Promise.all(getPostFromFolderNamePromises);
}