Skip to content

Commit

Permalink
Fix: Problem when rendering - 04-11 12:22
Browse files Browse the repository at this point in the history
  • Loading branch information
Yagasaki7K committed Nov 4, 2024
1 parent d4d5c81 commit b5f9365
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions src/pages/article/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,49 @@ export const getStaticProps: GetStaticProps = async ({ params }) => {
}

const filePath = path.join('article', `${slug}.mdx`);
const markdownWithMeta = fs.readFileSync(filePath, 'utf-8');
const { data: frontmatter, content } = matter(markdownWithMeta);
try {
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 };
}
if (!content) {
console.error(`Content not found for slug: ${slug}`);
return { notFound: true };
}

const renderedContent = marked(content);
const renderedContent = marked(content);

return {
props: {
frontmatter,
slug,
content: renderedContent,
},
};
return {
props: {
frontmatter,
slug,
content: renderedContent,
},
};
} catch (error) {
console.error(`Failed to load file for slug: ${slug}`, error);
return { notFound: true };
}
}

export default function PostPage({ frontmatter, content }: PostProps) {
// Initialize htmlContent with content, or fallback to an empty string if undefined
const [htmlContent, setHtmlContent] = useState<string>(content || "");

// Handle updates to content
useEffect(() => {
setHtmlContent(content || ""); // Ensure htmlContent is never null
setHtmlContent(content || ""); // Ensure htmlContent is always a string
}, [content]);

// Initialize syntax highlighting after HTML content is set
useEffect(() => {
hljs.highlightAll();
}, [htmlContent]);

// Render nothing if frontmatter or htmlContent is missing
if (!frontmatter || !htmlContent) return null;
// Render null if frontmatter or htmlContent is missing
if (!frontmatter || !htmlContent) {
console.error("frontmatter or htmlContent is missing", { frontmatter, htmlContent });
return null;
}

return (
<>
Expand Down

0 comments on commit b5f9365

Please sign in to comment.