67 lines
2.3 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:20:18 +01:00
// Importamos `CopyableCode` de forma dinámica para evitar problemas de SSR
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:31:30 +01:00
// 🔹 Envuelve los bloques de código en <CopyableCode />
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:31:30 +01:00
// 🔹 Elimina las comillas de los fragmentos de código en línea dentro de <code>
function cleanInlineCode(content: string) {
return parse(content, {
replace: (domNode: any) => {
if (domNode.name === "code" && domNode.children.length > 0) {
const codeContent = domNode.children[0].data?.trim().replace(/^`|`$/g, "") || "" // Elimina comillas inversas
return <code className="bg-gray-200 text-gray-900 px-1 rounded">{codeContent}</code>
}
}
})
}
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:31:30 +01:00
const contentWithCodeBlocks = wrapCodeBlocksWithCopyable(guideContent)
const finalContent = cleanInlineCode(contentWithCodeBlocks) // 🔹 Limpiamos código en línea
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:31:30 +01:00
<div className="prose max-w-none">{finalContent}</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
}