aboutsummaryrefslogtreecommitdiff
path: root/utils/Posts.tsx
blob: e82357a408232f3e7dccd71a670d0dc832e0a692 (plain)
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
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,
};