This commit is contained in:
64
pages/blog/[slug].tsx
Normal file
64
pages/blog/[slug].tsx
Normal file
@@ -0,0 +1,64 @@
|
||||
import { GetStaticPaths, GetStaticProps } from "next";
|
||||
import fs from "fs";
|
||||
import { ParsedUrlQuery } from "querystring";
|
||||
import {
|
||||
BLOGS_PATH,
|
||||
getFileContentBySlug,
|
||||
renderMarkdown,
|
||||
} from "../../utils/markdown";
|
||||
import { MarkdownRenderingResult } from "../../types/types";
|
||||
import { MainLayout } from "../../layouts/MainLayout";
|
||||
import Link from "next/link";
|
||||
import { IoMdArrowRoundBack as BackIcon } from "react-icons/io";
|
||||
|
||||
export const BlogArticle = ({ frontMatter, html }: MarkdownRenderingResult) => {
|
||||
return (
|
||||
<MainLayout>
|
||||
<Link href="/blog">
|
||||
<button className="flex flex-row items-center gap-2 text-secondary hover:text-secondary/50 transition-all ease-in duration-75">
|
||||
<BackIcon />
|
||||
<span>Go Back</span>
|
||||
</button>
|
||||
</Link>
|
||||
<div className="lg:mt-10 mt-4 mb-2 text-sm font-medium text-gray-500">
|
||||
{frontMatter.date}
|
||||
</div>
|
||||
<div className="lg:text-5xl font-bold">{frontMatter.title}</div>
|
||||
<div className="mt-2 font-medium">
|
||||
By: <span className="text-action">@{frontMatter.author}</span>
|
||||
</div>
|
||||
<div
|
||||
className="mt-4 prose prose-headings:text-primary-text prose-p:text-gray-400"
|
||||
dangerouslySetInnerHTML={{ __html: html }}
|
||||
/>
|
||||
</MainLayout>
|
||||
);
|
||||
};
|
||||
interface BlogProps extends ParsedUrlQuery {
|
||||
slug: string;
|
||||
}
|
||||
export const getStaticPaths: GetStaticPaths<BlogProps> = async () => {
|
||||
const paths = fs
|
||||
.readdirSync(BLOGS_PATH)
|
||||
.map((path) => path.replace(/\.mdx?$/, ""))
|
||||
.map((slug) => ({ params: { slug } }));
|
||||
|
||||
return {
|
||||
paths,
|
||||
fallback: false,
|
||||
};
|
||||
};
|
||||
|
||||
export const getStaticProps: GetStaticProps<MarkdownRenderingResult> = async ({
|
||||
params,
|
||||
}) => {
|
||||
const markdownContent = getFileContentBySlug(params?.slug as string);
|
||||
const renderedHTML = await renderMarkdown(markdownContent.content);
|
||||
return {
|
||||
props: {
|
||||
frontMatter: markdownContent.frontMatter,
|
||||
html: renderedHTML,
|
||||
},
|
||||
};
|
||||
};
|
||||
export default BlogArticle;
|
||||
@@ -1,11 +1,67 @@
|
||||
import { GetServerSideProps } from "next";
|
||||
import { MainLayout } from "../../layouts/MainLayout";
|
||||
import { BlogPost, getAllFilesFrontMatter } from "../../utils/markdown";
|
||||
import { FrontMatter } from "../../types/types";
|
||||
import Link from "next/link";
|
||||
|
||||
const Blog = () => {
|
||||
interface BasicBlogProps extends FrontMatter {
|
||||
title: string;
|
||||
description: string;
|
||||
date: string;
|
||||
author: string;
|
||||
}
|
||||
|
||||
const BlogCard = ({ blog, slug }: { blog: BasicBlogProps; slug: string }) => {
|
||||
const format = (date: string) => {
|
||||
const [day, month, year] = date.split(".");
|
||||
const d = new Date(`${year}-${month}-${day}`);
|
||||
return d.toLocaleDateString("en-US", {
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
year: "numeric",
|
||||
});
|
||||
};
|
||||
return (
|
||||
<div className="p-4 rounded-md border border-gray-700">
|
||||
<div className="text-sm font-medium text-gray-500">
|
||||
{format(blog.date)}
|
||||
</div>
|
||||
<h2 className="text-2xl font-bold">{blog.title}</h2>
|
||||
<p className="text-gray-400 text-lg">{blog.description}</p>
|
||||
<Link href={`blog/${slug}`}>
|
||||
<button className="bg-action px-2 py-1 rounded-md mt-4 hover:bg-action/60 transition-all ease-in duration-100 font-bold text-white">
|
||||
Read more
|
||||
</button>
|
||||
</Link>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const Blog = ({ posts }: { posts: BlogPost[] }) => {
|
||||
return (
|
||||
<MainLayout>
|
||||
<h1>Blog</h1>
|
||||
<h1 className="text-3xl font-bold mt-10">Blog</h1>
|
||||
<div className="w-full h-0.5 bg-white/50 my-4 rounded-full" />
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
|
||||
{posts.map((post, index) => (
|
||||
<BlogCard
|
||||
key={index}
|
||||
blog={post.frontMatter as BasicBlogProps}
|
||||
slug={post.slug}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</MainLayout>
|
||||
);
|
||||
};
|
||||
|
||||
export const getServerSideProps: GetServerSideProps = async () => {
|
||||
const posts = getAllFilesFrontMatter();
|
||||
return {
|
||||
props: {
|
||||
posts,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export default Blog;
|
||||
|
||||
@@ -2,8 +2,8 @@ import { MainLayout } from "../layouts/MainLayout";
|
||||
export default function Home() {
|
||||
return (
|
||||
<MainLayout>
|
||||
<div className="grid lg:grid-cols-2 grid-cols-1 mt-20 gap-10">
|
||||
<div className="bg-primary-dark p-10 rounded-lg flex flex-col justify-between h-96 lg:w-2/3 w-full text-lg text-primary-text/90">
|
||||
<div className="grid lg:grid-cols-2 grid-cols-1 mt-4 lg:mt-20 gap-10 items-center">
|
||||
<div className="bg-primary-dark p-10 rounded-lg flex flex-col gap-10 h-fit lg:w-2/3 w-full text-lg text-primary-text/90 shadow-lg shadow-gradient-dark/20">
|
||||
<div>
|
||||
<span className="font-bold text-xl text-primary-text">
|
||||
Lorem ipsum dolor{" "}
|
||||
|
||||
Reference in New Issue
Block a user