This commit is contained in:
@@ -4,8 +4,6 @@ import fs from "fs";
|
|||||||
import { FrontMatter, MarkdownDocument } from "../types/types";
|
import { FrontMatter, MarkdownDocument } from "../types/types";
|
||||||
import { remark } from "remark";
|
import { remark } from "remark";
|
||||||
import html from "remark-html";
|
import html from "remark-html";
|
||||||
import remarkGfm from "remark-gfm";
|
|
||||||
|
|
||||||
|
|
||||||
export const BLOGS_PATH = join(process.cwd(), "content/blogs");
|
export const BLOGS_PATH = join(process.cwd(), "content/blogs");
|
||||||
export const PROJECTS_PATH = join(process.cwd(), "content/projects");
|
export const PROJECTS_PATH = join(process.cwd(), "content/projects");
|
||||||
@@ -14,6 +12,14 @@ export interface Post {
|
|||||||
slug: string;
|
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 => {
|
export const getBlogContentBySlug = (slug: string): MarkdownDocument => {
|
||||||
const filepath = join(BLOGS_PATH, `${slug}.md` || `${slug}.mdx`);
|
const filepath = join(BLOGS_PATH, `${slug}.md` || `${slug}.mdx`);
|
||||||
const filecontents = fs.readFileSync(filepath);
|
const filecontents = fs.readFileSync(filepath);
|
||||||
@@ -40,22 +46,32 @@ export const getProjectContentBySlug = (slug: string): MarkdownDocument => {
|
|||||||
export const getAllBlogsFrontMatter = (): Post[] => {
|
export const getAllBlogsFrontMatter = (): Post[] => {
|
||||||
const files = fs.readdirSync(BLOGS_PATH);
|
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 slug = postSlug.replace(".md", "");
|
||||||
const post = getBlogContentBySlug(slug);
|
const post = getBlogContentBySlug(slug);
|
||||||
|
|
||||||
return [{ frontMatter: post.frontMatter, slug }, ...allPosts];
|
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[] => {
|
export const getAllProjectsFrontMatter = (): Post[] => {
|
||||||
const files = fs.readdirSync(PROJECTS_PATH);
|
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 slug = postSlug.replace(".md", "");
|
||||||
const post = getProjectContentBySlug(slug);
|
const post = getProjectContentBySlug(slug);
|
||||||
|
|
||||||
return [{ frontMatter: post.frontMatter, slug }, ...allPosts];
|
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) {
|
export async function markdownToHtml(markdown: any) {
|
||||||
|
|||||||
Reference in New Issue
Block a user