105 lines
2.9 KiB
TypeScript
105 lines
2.9 KiB
TypeScript
import matter from "gray-matter";
|
|
import { join } from "path";
|
|
import fs from "fs";
|
|
import { FrontMatter, MarkdownDocument } from "../types/types";
|
|
import { remark } from "remark";
|
|
import html from "remark-html";
|
|
|
|
export const BLOGS_PATH = join(process.cwd(), "content/blogs");
|
|
export const PROJECTS_PATH = join(process.cwd(), "content/projects");
|
|
export interface Post {
|
|
frontMatter: FrontMatter;
|
|
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);
|
|
|
|
const { data, content } = matter(filecontents);
|
|
|
|
return {
|
|
frontMatter: data,
|
|
content: content,
|
|
};
|
|
};
|
|
export const getProjectContentBySlug = (slug: string): MarkdownDocument => {
|
|
const filepath = join(PROJECTS_PATH, `${slug}.md` || `${slug}.mdx`);
|
|
const filecontents = fs.readFileSync(filepath);
|
|
|
|
const { data, content } = matter(filecontents);
|
|
|
|
return {
|
|
frontMatter: data,
|
|
content: content,
|
|
};
|
|
};
|
|
|
|
export const getAllBlogsFrontMatter = (): Post[] => {
|
|
const files = fs.readdirSync(BLOGS_PATH);
|
|
|
|
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);
|
|
|
|
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 const getHeadings = (markdown: string): any => {
|
|
const headingOne = markdown.match(/^(#{1})\s(.*)/gm);
|
|
const headingTwo = markdown.match(/^(#{2})\s(.*)/gm);
|
|
const headingThree = markdown.match(/^(#{3})\s(.*)/gm);
|
|
const headingFour = markdown.match(/^(#{4})\s(.*)/gm);
|
|
|
|
const headings = markdown.match(/^(#{1,4})\s(.*)/gm);
|
|
var tree: any[] = [];
|
|
var prev = "";
|
|
headings?.forEach((heading) => {
|
|
if (headingOne?.includes(heading)) {
|
|
tree.push(heading);
|
|
}
|
|
});
|
|
|
|
return tree;
|
|
};
|
|
|
|
export async function markdownToHtml(markdown: any) {
|
|
const result = await remark().use(html).process(markdown);
|
|
return result.toString();
|
|
}
|
|
|
|
export const renderMarkdown = async (
|
|
markdownContent: string
|
|
): Promise<string> => {
|
|
return await markdownToHtml(markdownContent || "");
|
|
};
|