-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d1cca6
commit b7719b9
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { css, Theme } from "@emotion/react"; | ||
import markdownToHtml from "util/markdownToHtml"; | ||
import { useRouter } from "next/router"; | ||
import { getAllPosts } from "util/getAllPosts"; | ||
import { getPostBySlug } from "util/getPostBySlug"; | ||
import { GetStaticPaths } from "next"; | ||
import { useEffect } from "react"; | ||
|
||
function Post({ post }) { | ||
const router = useRouter(); | ||
useEffect(() => { | ||
if (!router.isFallback) return; | ||
}); | ||
|
||
return ( | ||
<main css={itemSize}> | ||
<div css={itemPadding}> | ||
<h3>{post.title}</h3> | ||
<p css={dateContent}>{post.date}</p> | ||
<article dangerouslySetInnerHTML={{ __html: post.content }}></article> | ||
</div> | ||
</main> | ||
); | ||
} | ||
|
||
export async function getStaticProps({ params }) { | ||
const { slug } = params; | ||
const allPosts = getAllPosts(["title", "tags", "date", "slug"]); | ||
const currentPost = allPosts.filter((post) => post.slug === slug)[0]; | ||
|
||
if (typeof currentPost === "undefined") { | ||
return false; | ||
} | ||
|
||
const post = getPostBySlug(slug, [ | ||
"title", | ||
"tags", | ||
"date", | ||
"slug", | ||
"content", | ||
]); | ||
const content = await markdownToHtml(post.content || ""); | ||
|
||
return { | ||
props: { | ||
post: { | ||
...post, | ||
content, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
export const getStaticPaths: GetStaticPaths = async () => { | ||
const posts = getAllPosts(["slug"]); | ||
|
||
return { | ||
paths: posts.map((post) => { | ||
return { | ||
params: { | ||
slug: post.slug, | ||
}, | ||
}; | ||
}), | ||
fallback: false, | ||
}; | ||
}; | ||
|
||
export default Post; | ||
|
||
const itemSize = css` | ||
display: flex; | ||
flex-direction: column; | ||
position: relative; | ||
min-height: 100%; | ||
width: 100%; | ||
padding-bottom: 3.85rem; | ||
`; | ||
|
||
const itemPadding = css` | ||
padding: 0.75rem 0.6rem; | ||
`; | ||
|
||
const dateContent = (theme: Theme) => css` | ||
color: ${theme.color.grey500}; | ||
font-size: 0.9rem; | ||
height: 1rem; | ||
`; |