Update page.tsx

This commit is contained in:
MacRimi 2025-02-14 11:05:58 +01:00 committed by GitHub
parent 079d462ae9
commit 6fa905ec01
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,43 +3,44 @@ import path from "path"
import { remark } from "remark" import { remark } from "remark"
import html from "remark-html" import html from "remark-html"
const guidesDirectory = const guidesDirectory = path.join(process.cwd(), "guides")
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 fullPath = path.join(guidesDirectory, `${slug}.md`) const fullPath = path.join(guidesDirectory, `${slug}.md`)
const fileContents = fs.readFileSync(fullPath, "utf8") try {
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()
} catch (error) {
console.error(`Error reading guide file: ${fullPath}`, error)
return "<p>Guide content not found.</p>"
}
} }
export async function generateStaticParams() { export async function generateStaticParams() {
try { try {
const guideFiles = fs.readdirSync(guidesDirectory) if (fs.existsSync(guidesDirectory)) {
return guideFiles.map((file) => ({ const guideFiles = fs.readdirSync(guidesDirectory)
slug: file.replace(/\.md$/, ""), return guideFiles.map((file) => ({
})) slug: file.replace(/\.md$/, ""),
}))
} else {
console.warn("Guides directory not found. No static params generated.")
return []
}
} catch (error) { } catch (error) {
console.error("Error reading guides directory:", error) console.error("Error generating static params for guides:", error)
return [] 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>
}
} }