generated from nhcarrigan/template
feat: set up announcements page
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { readFileSync, readdirSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import matter from "gray-matter";
|
||||
|
||||
const postsDirectory = join(process.cwd(), "posts");
|
||||
|
||||
export const getSortedPostsData = () => {
|
||||
const fileNames = readdirSync(postsDirectory);
|
||||
const allPostsData = fileNames.map((fileName) => {
|
||||
const slug = fileName.replace(/\.md$/, "");
|
||||
const fullPath = join(postsDirectory, fileName);
|
||||
const fileContents = readFileSync(fullPath, "utf8");
|
||||
const matterResult = matter(fileContents) as unknown as {
|
||||
content: string;
|
||||
data: { title: string; date: string; summary: string };
|
||||
};
|
||||
return {
|
||||
slug: slug,
|
||||
data: {
|
||||
title: matterResult.data.title,
|
||||
date: new Date(matterResult.data.date),
|
||||
summary: matterResult.data.summary,
|
||||
},
|
||||
content: matterResult.content,
|
||||
};
|
||||
});
|
||||
return allPostsData.sort((a, b) => {
|
||||
if (a.data.date < b.data.date) {
|
||||
return 1;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const getPostData = (slug: string) => {
|
||||
const fullPath = join(postsDirectory, slug + ".md");
|
||||
const fileContents = readFileSync(fullPath, "utf8");
|
||||
const matterResult = matter(fileContents) as unknown as {
|
||||
content: string;
|
||||
data: { title: string; date: string; summary: string };
|
||||
};
|
||||
return {
|
||||
slug: slug,
|
||||
data: {
|
||||
title: matterResult.data.title,
|
||||
date: new Date(matterResult.data.date),
|
||||
summary: matterResult.data.summary,
|
||||
},
|
||||
content: matterResult.content,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user