-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgetBlogPosts.js
59 lines (53 loc) · 1.63 KB
/
getBlogPosts.js
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
import fs from "fs";
import path from "path";
import matter from "gray-matter";
import getAllMdFilesInDir from "../utils/getAllMdFilesInDir.js";
import getOptimizedImageAttributes from "../utils/images/getOptimizedImageAttributesJS.js";
import getPublicFilePath from "../utils/getPublicFilePath.js";
import slugify from "../utils/slugify.js";
function getPostsData(files) {
return files
.map((postPath) => {
const fileContents = fs.readFileSync(postPath, "utf8");
const fileName = path.basename(postPath);
const oldSlug = fileName.replace(/\.md$/, "");
const {
data: {
title,
draft,
longExcerpt,
shortExcerpt,
url,
date,
author,
x2images,
thumbImage,
blog,
oldUrl,
},
} = matter(fileContents);
const publicFilePath = getPublicFilePath(postPath);
const thumbImagePath = thumbImage ? publicFilePath + thumbImage : null;
return {
x2images: !!x2images,
fullFilePath: postPath,
thumbImage: getOptimizedImageAttributes(thumbImagePath),
publicFilePath,
oldSlug,
draft: !!draft,
title: title || "",
date: date || "",
tags: blog || [],
author: author || "",
longExcerpt: longExcerpt || "",
shortExcerpt: shortExcerpt || "",
url: slugify(url || title || oldSlug),
oldUrl: oldUrl || "",
_type: "blog",
};
})
.sort((a, b) => new Date(b.date) - new Date(a.date));
}
export function getBlogsMetadata() {
return getPostsData(getAllMdFilesInDir("public/blog"));
}