62 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 15:14:19 +01:00
import dynamic from "next/dynamic"
2025-02-15 15:20:18 +01:00
import React from "react"
import parse from "html-react-parser"
2025-02-15 15:14:19 +01:00
2025-02-15 15:49:44 +01:00
2025-02-15 15:14:19 +01:00
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-15 15:36:45 +01:00
function cleanInlineCode(content: string) {
return content.replace(/<code>(.*?)<\/code>/g, (_, codeContent) => {
2025-02-15 15:49:44 +01:00
const cleanedCode = codeContent.replace(/^`|`$/g, "")
2025-02-15 15:40:35 +01:00
return `<code class="bg-gray-200 text-gray-900 px-1 rounded">${cleanedCode}</code>`
2025-02-15 15:36:45 +01:00
})
}
2025-02-15 15:49:44 +01:00
2025-02-15 15:14:19 +01:00
function wrapCodeBlocksWithCopyable(content: string) {
2025-02-15 15:20:18 +01:00
return parse(content, {
replace: (domNode: any) => {
if (domNode.name === "pre" && domNode.children.length > 0) {
const codeElement = domNode.children.find((child: any) => child.name === "code")
if (codeElement) {
const codeContent = codeElement.children[0]?.data?.trim() || ""
return <CopyableCode code={codeContent} />
}
}
}
})
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:20:18 +01:00
const guideContent = await getGuideContent(params.slug)
2025-02-15 15:49:44 +01:00
const cleanedInlineCode = cleanInlineCode(guideContent)
const parsedContent = wrapCodeBlocksWithCopyable(cleanedInlineCode)
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">
2025-02-15 15:49:44 +01:00
<div className="container mx-auto px-4 py-16 max-w-4xl">
<div className="prose max-w-none text-[16px]">{parsedContent}</div>
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
}