Spaces:
Runtime error
Runtime error
File size: 1,280 Bytes
cd6f98e |
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 |
import fs from "fs";
import path from "path";
import matter from "gray-matter";
// Define the types for the data
export interface SlugData {
id: string;
date: string;
[key: string]: string;
}
const postsDirectory = path.join(process.cwd(), "posts");
export function getSortedPostsData(): SlugData[] {
const fileNames = fs.readdirSync(postsDirectory);
const allPostsData: SlugData[] = fileNames.map((fileName) => {
const id = fileName.replace(/\.mdx$/, "");
const fullPath = path.join(postsDirectory, fileName);
const fileContents = fs.readFileSync(fullPath, "utf8");
const matterResult = matter(fileContents);
return {
id,
date: matterResult.data.date as string,
...matterResult.data,
};
});
return allPostsData.sort((a, b) => {
if (a.date < b.date) {
return 1;
} else {
return -1;
}
});
}
export interface PostData {
slug: string;
content: string;
[key: string]: string;
}
export function getPostData(slug: string): PostData {
const fullPath = path.join(postsDirectory, `${slug}.mdx`);
const fileContents = fs.readFileSync(fullPath, "utf8");
const matterResult = matter(fileContents);
return {
slug,
...matterResult.data,
content: matterResult.content,
};
}
|