diff --git a/src/pages/article/[slug].tsx b/src/pages/article/[slug].tsx index 0beb8b2..3b19720 100644 --- a/src/pages/article/[slug].tsx +++ b/src/pages/article/[slug].tsx @@ -2,7 +2,7 @@ import fs from 'fs'; import path from 'path'; import matter from 'gray-matter'; import { marked } from 'marked'; -import { GetStaticPaths } from 'next'; +import { GetStaticPaths, GetStaticProps } from 'next'; import { useEffect, useState } from 'react'; import ArticleDetails from '@/components/ArticleDetails'; import { PostProps } from '../index'; @@ -31,17 +31,21 @@ export const getStaticPaths: GetStaticPaths = async () => { }; } -export const getStaticProps = async ({ params }: { params: { slug: string } }) => { - if (!params?.slug) { - return { - notFound: true, - }; +export const getStaticProps: GetStaticProps = async ({ params }) => { + const slug = params?.slug as string; + if (!slug) { + return { notFound: true }; } - const { slug } = params; const filePath = path.join('article', `${slug}.mdx`); const markdownWithMeta = fs.readFileSync(filePath, 'utf-8'); const { data: frontmatter, content } = matter(markdownWithMeta); + + if (!content) { + console.error(`Content not found for slug: ${slug}`); + return { notFound: true }; + } + const renderedContent = marked(content); return { @@ -54,17 +58,17 @@ export const getStaticProps = async ({ params }: { params: { slug: string } }) = } export default function PostPage({ frontmatter, content }: PostProps) { - const [htmlContent, setHtmlContent] = useState(""); + const [htmlContent, setHtmlContent] = useState(content || ""); useEffect(() => { - setHtmlContent(content || ""); // Fallback to an empty string if content is null or undefined + setHtmlContent(content || ""); // Ensure htmlContent is never null }, [content]); useEffect(() => { hljs.highlightAll(); }, [htmlContent]); - // Render null if frontmatter or htmlContent is not set + // Render nothing if frontmatter or htmlContent is missing if (!frontmatter || !htmlContent) return null; return ( @@ -96,7 +100,7 @@ export default function PostPage({ frontmatter, content }: PostProps) { /> - {frontmatter.title} | Yagasaki7K + {frontmatter.title} | Yagasaki7K
@@ -134,16 +138,16 @@ export default function PostPage({ frontmatter, content }: PostProps) {

Nunca perca um post ou anúncio de projeto que eu faça. Siga-me no Twitter para manter contato, fazer perguntas ou conversar.

- - - Conecte-se - - + + + Conecte-se + +
-

Gostou do artigo? Faça um PIX de café! ☕

+

Gostou do artigo? Faça um PIX de café! ☕