47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
|
import fs from "fs";
|
||
|
import path from "path";
|
||
|
import matter from "gray-matter";
|
||
|
|
||
|
const postsDirectory = path.join(process.cwd(), "content", "posts");
|
||
|
|
||
|
export interface Post {
|
||
|
id: string;
|
||
|
title: string;
|
||
|
thumbnail?: string;
|
||
|
description?: string;
|
||
|
content?: string;
|
||
|
date: Date;
|
||
|
}
|
||
|
|
||
|
export function getSortedPosts() {
|
||
|
const fileNames = fs.readdirSync(postsDirectory);
|
||
|
const allPostsData: Post[] = fileNames.map((fileName) => {
|
||
|
const id = fileName.replace(/\.md$/, "");
|
||
|
|
||
|
const fullPath = path.join(postsDirectory, fileName);
|
||
|
const fileContents = fs.readFileSync(fullPath, "utf8");
|
||
|
|
||
|
const matterResult = matter(fileContents);
|
||
|
|
||
|
return {
|
||
|
id,
|
||
|
...matterResult.data
|
||
|
} as Post;
|
||
|
});
|
||
|
|
||
|
return allPostsData.sort((a, b) => {
|
||
|
return a.date < b.date ? 1 : -1;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
export function getSinglePost(id: string) {
|
||
|
const fullPath = path.join(postsDirectory, id + ".md");
|
||
|
const fileContents = fs.readFileSync(fullPath, "utf8");
|
||
|
const matterResult = matter(fileContents);
|
||
|
|
||
|
return {
|
||
|
id,
|
||
|
content: matterResult.content,
|
||
|
...matterResult.data,
|
||
|
} as Post;
|
||
|
}
|