import Layout from '../../components/layout'; import ReactMarkdown from 'react-markdown'; import style from '../../styles/post.module.css'; import PostsInfo from '../../public/posts.json'; import readMarkdown from '../../lib/read-markdown'; import DateTool, { toLocaleString } from '../../lib/date'; interface IPost { title: string; mtime: string; otime?: string; } interface IPosts { [slug: string]: IPost } function TimeBlock({ mtime, otime }: { mtime: string, otime: string }) { const ampm = (h: number) => { if (h >= 12) return 'p.m.'; return 'a.m.'; }; const mdate = new Date(mtime); const odate = new Date(otime); const format = (date: Date) => { const day = date.getDay(); const ord = {DateTool.getOrdinalDaySuffix(date.getDay())}; const month = DateTool.getFullMonth(date.getMonth()); const year = date.getFullYear(); const hours = date.getHours() > 12 ? date.getHours() - 12 : date.getHours(); const minPrefix = date.getMinutes() < 10 ? '0' : ''; const minutes = date.getMinutes(); const twelveSfx = ampm(date.getHours()); return <>{day}{ord} {month} {year} at {hours}:{minPrefix}{minutes} {twelveSfx} }; return (
{ mtime ?
Last updated: {format(mdate)}
: <> }
{format(odate)}
); } function Post({ post }: { post: IPost & { content: string, cover?: string, otime: string, mtime?: string } }) { if (!post) return <>; return (<>
{ post.otime !== post.mtime && Last updated: {toLocaleString(post.mtime)} } {toLocaleString(post.otime)}
{
}
{post.content}
); } export async function getStaticProps({ params }: any) { const postsInfo: IPosts = PostsInfo; const post: IPost = postsInfo[params.post]; return { props: { post: { ...post, content: await readMarkdown('posts', params.post, true) } } } } export async function getStaticPaths() { return { paths: Object.keys(PostsInfo).map((post: string) => { return { params: { post } } }), fallback: false }; } export default Post;