diff --git a/utils/markdown.ts b/utils/markdown.ts index e6ffa74..8fc4b69 100644 --- a/utils/markdown.ts +++ b/utils/markdown.ts @@ -4,8 +4,6 @@ import fs from "fs"; import { FrontMatter, MarkdownDocument } from "../types/types"; import { remark } from "remark"; import html from "remark-html"; -import remarkGfm from "remark-gfm"; - export const BLOGS_PATH = join(process.cwd(), "content/blogs"); export const PROJECTS_PATH = join(process.cwd(), "content/projects"); @@ -14,6 +12,14 @@ export interface Post { slug: string; } +const getDate = (date: string) => { + if (typeof date === "string") { + const d = date.split(".").map((v) => +v); + return new Date(d[2], d[1] - 1, d[0]); + } + return new Date(); +}; + export const getBlogContentBySlug = (slug: string): MarkdownDocument => { const filepath = join(BLOGS_PATH, `${slug}.md` || `${slug}.mdx`); const filecontents = fs.readFileSync(filepath); @@ -40,22 +46,32 @@ export const getProjectContentBySlug = (slug: string): MarkdownDocument => { export const getAllBlogsFrontMatter = (): Post[] => { const files = fs.readdirSync(BLOGS_PATH); - return files.reduce((allPosts: Post[], postSlug: string) => { + const blogs = files.reduce((allPosts: Post[], postSlug: string) => { const slug = postSlug.replace(".md", ""); const post = getBlogContentBySlug(slug); return [{ frontMatter: post.frontMatter, slug }, ...allPosts]; }, []); + + return blogs.sort( + //@ts-ignore + (a, b) => getDate(b.frontMatter.date) - getDate(a.frontMatter.date) + ); }; export const getAllProjectsFrontMatter = (): Post[] => { const files = fs.readdirSync(PROJECTS_PATH); - return files.reduce((allPosts: Post[], postSlug: string) => { + const projects = files.reduce((allPosts: Post[], postSlug: string) => { const slug = postSlug.replace(".md", ""); const post = getProjectContentBySlug(slug); return [{ frontMatter: post.frontMatter, slug }, ...allPosts]; }, []); + + return projects.sort( + //@ts-ignore + (a, b) => getDate(b.frontMatter.date) - getDate(a.frontMatter.date) + ); }; export async function markdownToHtml(markdown: any) {