-
Notifications
You must be signed in to change notification settings - Fork 1
/
getPostMetadata.ts
48 lines (43 loc) · 1.13 KB
/
getPostMetadata.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
import fs from "fs";
import { Locale } from "./i18n-config";
import matter from "gray-matter";
import { PostMetadata } from "./PostMetadata";
const getPostMetaData = (
lang: Locale,
related: boolean,
actual?: any
): PostMetadata[] => {
const folder = "posts/";
const files = fs.readdirSync(folder);
let posts = files.map((filename) => {
const fileContents = fs.readFileSync(
`posts/${filename}/${lang}/${filename}.md`,
"utf8"
);
const matterResult = matter(fileContents);
if (related && filename == actual) {
return {
title: '',
subtitle: '',
description: '',
slug: '',
date: "",
image: '',
likes: 0
};
}
else {
return {
title: matterResult.data.title,
subtitle: matterResult.data.subtitle,
description: matterResult.data.title,
slug: filename,
date: matterResult.data.date,
image: matterResult.data.image == './css1.png' ? './css.png': matterResult.data.image,
likes: matterResult.data.likes,
};
}
});
return posts;
};
export default getPostMetaData;