64 lines
2.0 KiB
TypeScript
Raw Normal View History

2025-02-13 17:28:49 +01:00
import fs from "fs"
import path from "path"
import { remark } from "remark"
import html from "remark-html"
2025-02-15 10:21:28 +01:00
import dynamic from "next/dynamic"
const CopyableCode = dynamic(() => import("@/components/CopyableCode"), { ssr: false })
2025-02-13 17:28:49 +01:00
2025-02-14 11:20:10 +01:00
const guidesDirectory = path.join(process.cwd(), "..", "guides")
2025-02-14 10:46:02 +01:00
2025-02-13 17:28:49 +01:00
async function getGuideContent(slug: string) {
2025-02-14 10:46:02 +01:00
const fullPath = path.join(guidesDirectory, `${slug}.md`)
2025-02-14 11:05:58 +01:00
try {
const fileContents = fs.readFileSync(fullPath, "utf8")
const result = await remark().use(html).process(fileContents)
return result.toString()
} catch (error) {
console.error(`Error reading guide file: ${fullPath}`, error)
return "<p>Guide content not found.</p>"
}
2025-02-13 17:28:49 +01:00
}
export async function generateStaticParams() {
2025-02-14 10:46:02 +01:00
try {
2025-02-14 11:05:58 +01:00
if (fs.existsSync(guidesDirectory)) {
const guideFiles = fs.readdirSync(guidesDirectory)
return guideFiles.map((file) => ({
slug: file.replace(/\.md$/, ""),
}))
} else {
console.warn("Guides directory not found. No static params generated.")
return []
}
2025-02-14 10:46:02 +01:00
} catch (error) {
2025-02-14 11:05:58 +01:00
console.error("Error generating static params for guides:", error)
2025-02-14 10:46:02 +01:00
return []
}
2025-02-13 17:28:49 +01:00
}
2025-02-15 10:21:28 +01:00
function wrapCodeBlocksWithCopyable(content: string) {
const codeBlockRegex = /<pre><code>([\s\S]*?)<\/code><\/pre>/g
return content.replace(codeBlockRegex, (match, code) => {
return `<CopyableCode code={\`${code.replace(/`/g, "\\`")}\`} />`
})
}
2025-02-13 17:28:49 +01:00
export default async function GuidePage({ params }: { params: { slug: string } }) {
2025-02-15 10:21:28 +01:00
let guideContent = await getGuideContent(params.slug)
guideContent = wrapCodeBlocksWithCopyable(guideContent)
2025-02-13 23:04:40 +01:00
2025-02-14 11:05:58 +01:00
return (
2025-02-15 10:29:10 +01:00
<div className="min-h-screen bg-white">
<div className="container mx-auto px-4 py-16 max-w-3xl">
<div
className="prose prose-gray max-w-none
[&>h1]:text-gray-900 [&>h2]:text-gray-800 [&>h3]:text-gray-700
[&>p]:text-gray-600 [&>ul>li]:text-gray-600 [&>ol>li]:text-gray-600"
dangerouslySetInnerHTML={{ __html: guideContent }}
/>
</div>
2025-02-14 11:05:58 +01:00
</div>
)
}