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 17:16:10 +01:00
|
|
|
import * as gfm from "remark-gfm" // ✅ Asegura la correcta importación de `remark-gfm`
|
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 17:04:08 +01:00
|
|
|
// 🔹 Importamos `CopyableCode` dinámicamente 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-15 17:16:10 +01:00
|
|
|
const guidesDirectory = path.join(process.cwd(), "..", "guides")
|
|
|
|
|
2025-02-13 17:28:49 +01:00
|
|
|
async function getGuideContent(slug: string) {
|
2025-02-15 17:04:08 +01:00
|
|
|
try {
|
2025-02-15 17:16:10 +01:00
|
|
|
const guidePath = path.join(guidesDirectory, `${slug}.md`)
|
|
|
|
|
|
|
|
if (!fs.existsSync(guidePath)) {
|
|
|
|
console.error(`❌ Archivo ${slug}.md no encontrado en guides/`)
|
|
|
|
return "<p class='text-red-600'>Error: No se encontró la guía solicitada.</p>"
|
|
|
|
}
|
|
|
|
|
2025-02-15 17:04:08 +01:00
|
|
|
const fileContents = fs.readFileSync(guidePath, "utf8")
|
|
|
|
|
2025-02-15 17:16:10 +01:00
|
|
|
// ✅ Agregamos `remark-gfm` para permitir imágenes, tablas y otros elementos avanzados de Markdown
|
|
|
|
const result = await remark()
|
|
|
|
.use(gfm.default || gfm) // ✅ Manejo seguro de `remark-gfm`
|
|
|
|
.use(html)
|
|
|
|
.process(fileContents)
|
|
|
|
|
2025-02-15 17:04:08 +01:00
|
|
|
return result.toString()
|
|
|
|
} catch (error) {
|
2025-02-15 17:16:10 +01:00
|
|
|
console.error(`❌ Error al leer la guía ${slug}.md`, error)
|
2025-02-15 17:04:08 +01:00
|
|
|
return "<p class='text-red-600'>Error: No se pudo cargar la guía.</p>"
|
|
|
|
}
|
2025-02-15 12:40:28 +01:00
|
|
|
}
|
|
|
|
|
2025-02-15 17:16:10 +01:00
|
|
|
// 🔹 Asegura que `generateStaticParams()` esté presente para `output: export`
|
|
|
|
export async function generateStaticParams() {
|
|
|
|
try {
|
|
|
|
if (fs.existsSync(guidesDirectory)) {
|
|
|
|
const guideFiles = fs.readdirSync(guidesDirectory)
|
|
|
|
return guideFiles.map((file) => ({
|
|
|
|
slug: file.replace(/\.md$/, ""),
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
console.warn("⚠ No se encontró el directorio guides/. No se generarán rutas estáticas.")
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error("❌ Error al generar las rutas estáticas para guides:", error)
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-15 17:04:08 +01:00
|
|
|
// 🔹 Limpia las comillas invertidas en fragmentos de código en línea
|
2025-02-15 15:36:45 +01:00
|
|
|
function cleanInlineCode(content: string) {
|
|
|
|
return content.replace(/<code>(.*?)<\/code>/g, (_, codeContent) => {
|
2025-02-15 17:04:08 +01:00
|
|
|
return `<code class="bg-gray-200 text-gray-900 px-1 rounded">${codeContent.replace(/^`|`$/g, "")}</code>`
|
2025-02-15 15:36:45 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2025-02-15 17:04:08 +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: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 17:04:08 +01:00
|
|
|
const cleanedInlineCode = cleanInlineCode(guideContent) // 🔹 Primero limpiamos código en línea
|
|
|
|
const parsedContent = wrapCodeBlocksWithCopyable(cleanedInlineCode) // 🔹 Luego aplicamos JSX a bloques de código
|
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 17:04:08 +01:00
|
|
|
<div className="container mx-auto px-4 py-16" style={{ maxWidth: "980px" }}> {/* 📌 Ajuste exacto como GitHub */}
|
|
|
|
<div className="prose max-w-none text-[16px]">{parsedContent}</div> {/* 📌 Texto ajustado a 16px */}
|
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
|
|
|
}
|