diff options
| author | Furkan Sahin <furkan-dev@proton.me> | 2021-07-21 02:56:12 -0500 |
|---|---|---|
| committer | Furkan Sahin <furkan-dev@proton.me> | 2026-02-20 13:28:04 -0500 |
| commit | b6318ac246e1da39b4b9a411dc79d5c2300ca04f (patch) | |
| tree | a70a5b8d624bf8c9d784c412b4344da6ee7f3387 /utils/Posts.tsx | |
| parent | 83dd849fd7bbb0fdb7a14cdad5bce43aeabc829a (diff) | |
Automatic generation of list on log page
Diffstat (limited to 'utils/Posts.tsx')
| -rw-r--r-- | utils/Posts.tsx | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/utils/Posts.tsx b/utils/Posts.tsx new file mode 100644 index 0000000..e82357a --- /dev/null +++ b/utils/Posts.tsx @@ -0,0 +1,57 @@ +import { promises as fs } from 'fs'; +import path from 'path'; + +interface PostMetadata { + name: string; +} + +interface Post { + directory: string; + path: string; + meta: PostMetadata; +} + +export type { + PostMetadata, + Post +}; + +async function getMetadata(postPath : string) : Promise<PostMetadata> { + const metaPath = path.join(postPath, 'meta.json'); + + const postMetadata = await fs.readFile(metaPath, 'utf8'); + + return JSON.parse(postMetadata) as PostMetadata; +} + +async function getPosts() : Promise<Post[]> { + const postsDirectory = path.join(process.cwd(), 'posts'); + const directories = await fs.readdir(postsDirectory); + + const posts = directories.map(async (directory) => { + const postPath = path.join(postsDirectory, directory); + + const meta = await getMetadata(postPath); + + return { + directory, + path: postPath, + meta + }; + }); + + return await Promise.all(posts); +} + +async function getMarkdown(post : Post) : Promise<string> { + const markdownPath = path.join(post.path, 'main.md'); + + const markdown = await fs.readFile(markdownPath, 'utf8'); + + return markdown; +} + +export { + getPosts, + getMetadata, +};
\ No newline at end of file |
