Update page.tsx

This commit is contained in:
MacRimi 2025-02-14 10:46:02 +01:00 committed by GitHub
parent 51b3ef6b5a
commit 39a4b7b0e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,29 +3,43 @@ import path from "path"
import { remark } from "remark" import { remark } from "remark"
import html from "remark-html" import html from "remark-html"
const guidesDirectory =
process.env.NODE_ENV === "production"
? path.join(process.cwd(), "..", "..", "guides")
: path.join(process.cwd(), "..", "guides")
async function getGuideContent(slug: string) { async function getGuideContent(slug: string) {
const guidePath = path.join(process.cwd(), "..", "..", "guides", `${slug}.md`) const fullPath = path.join(guidesDirectory, `${slug}.md`)
const fileContents = fs.readFileSync(guidePath, "utf8") const fileContents = fs.readFileSync(fullPath, "utf8")
const result = await remark().use(html).process(fileContents) const result = await remark().use(html).process(fileContents)
return result.toString() return result.toString()
} }
export async function generateStaticParams() { export async function generateStaticParams() {
const guidesPath = path.join(process.cwd(), "..", "..", "guides") try {
const guideFiles = fs.readdirSync(guidesPath) const guideFiles = fs.readdirSync(guidesDirectory)
return guideFiles.map((file) => ({ return guideFiles.map((file) => ({
slug: file.replace(/\.md$/, ""), slug: file.replace(/\.md$/, ""),
})) }))
} catch (error) {
console.error("Error reading guides directory:", error)
return []
}
} }
export default async function GuidePage({ params }: { params: { slug: string } }) { export default async function GuidePage({ params }: { params: { slug: string } }) {
try {
const guideContent = await getGuideContent(params.slug) const guideContent = await getGuideContent(params.slug)
return ( return (
<div className="container mx-auto px-4 py-16 max-w-3xl"> <div className="container mx-auto px-4 py-16 max-w-3xl">
<div className="prose prose-lg" dangerouslySetInnerHTML={{ __html: guideContent }} /> <div className="prose prose-lg" dangerouslySetInnerHTML={{ __html: guideContent }} />
</div> </div>
) )
} catch (error) {
console.error("Error rendering guide:", error)
return <div>Error: Unable to load guide content.</div>
}
} }