47 lines
1.4 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 15:14:19 +01:00
import dynamic from "next/dynamic"
const CopyableCode = dynamic(() => import("@/components/CopyableCode"), { ssr: false })
2025-02-14 10:46:02 +01:00
2025-02-13 17:28:49 +01:00
async function getGuideContent(slug: string) {
2025-02-15 12:56:14 +01:00
const guidePath = path.join(process.cwd(), "..", "guides", `${slug}.md`)
const fileContents = fs.readFileSync(guidePath, "utf8")
2025-02-13 17:28:49 +01:00
2025-02-15 14:26:09 +01:00
const result = await remark().use(html).process(fileContents)
2025-02-15 12:56:14 +01:00
return result.toString()
2025-02-13 17:28:49 +01:00
}
2025-02-15 12:56:14 +01:00
export async function generateStaticParams() {
const guideFiles = fs.readdirSync(path.join(process.cwd(), "..", "guides"))
return guideFiles.map((file) => ({
slug: file.replace(/\.md$/, ""),
}))
2025-02-15 12:40:28 +01:00
}
2025-02-13 23:04:40 +01:00
2025-02-15 15:14:19 +01:00
function wrapCodeBlocksWithCopyable(content: string) {
2025-02-15 15:17:01 +01:00
return content.replace(
/<pre><code class="language-(.*?)">([\s\S]*?)<\/code><\/pre>/g,
(match, lang, code) =>
`<div class="copyable-code-container"><CopyableCode code="${encodeURIComponent(
code.trim()
)}" /></div>`
)
2025-02-15 15:14:19 +01:00
}
2025-02-15 13:24:22 +01:00
2025-02-15 15:14:19 +01:00
export default async function GuidePage({ params }: { params: { slug: string } }) {
2025-02-15 15:17:01 +01:00
let guideContent = await getGuideContent(params.slug)
guideContent = wrapCodeBlocksWithCopyable(guideContent)
2025-02-15 13:24:22 +01:00
2025-02-14 11:05:58 +01:00
return (
2025-02-15 13:24:22 +01:00
<div className="min-h-screen bg-white text-gray-900">
<div className="container mx-auto px-4 py-16 max-w-3xl">
2025-02-15 15:17:01 +01:00
<div className="prose max-w-none" dangerouslySetInnerHTML={{ __html: guideContent }} />
2025-02-15 13:24:22 +01:00
</div>
2025-02-14 11:05:58 +01:00
</div>
)
2025-02-15 12:22:29 +01:00
}