-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostsFetcher.ts
More file actions
29 lines (23 loc) · 858 Bytes
/
Copy pathpostsFetcher.ts
File metadata and controls
29 lines (23 loc) · 858 Bytes
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
import matter from 'gray-matter';
import * as fs from 'fs';
import * as path from 'path';
import { SingleArticle } from 'types';
export async function getAllPosts() {
return Promise.all(getAllPostsSlugs().map(getSinglePost));
}
export function getAllPostsSlugs() {
return fs.readdirSync(getPostsDirectory()).map(normalizePostName);
}
function normalizePostName(postName: string) {
return postName.replace('.mdx', '');
}
export async function getSinglePost(slug: string): Promise<SingleArticle> {
const filePath = path.join(getPostsDirectory(), slug + '.mdx');
const contents = fs.readFileSync(filePath, 'utf8');
const { data: meta, content } = matter(contents);
return { slug, content, meta: meta as SingleArticle['meta'] };
}
export function getPostsDirectory() {
let basePath = process.cwd();
return path.join(basePath, 'posts');
}