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:39:01 +01:00
|
|
|
import * as gfm from "remark-gfm"
|
2025-02-15 17:52:05 +01:00
|
|
|
import matter from "gray-matter"
|
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
|
|
|
|
|
|
|
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-15 23:01:40 +01:00
|
|
|
// 🔹 Encuentra todos los archivos Markdown dentro de `/guides`
|
|
|
|
function getMarkdownFiles() {
|
|
|
|
return fs
|
|
|
|
.readdirSync(guidesDirectory)
|
|
|
|
.filter((file) => file.endsWith(".md"))
|
|
|
|
.map((file) => ({
|
|
|
|
slug: file.replace(/\.md$/, ""), // 🔹 Quitamos la extensión .md
|
|
|
|
path: path.join(guidesDirectory, file),
|
|
|
|
}))
|
2025-02-15 17:39:01 +01:00
|
|
|
}
|
|
|
|
|
2025-02-15 23:01:40 +01:00
|
|
|
// 🔹 Obtiene el contenido de una guía específica
|
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 23:01:40 +01:00
|
|
|
const markdownFiles = getMarkdownFiles()
|
|
|
|
const guideFile = markdownFiles.find((file) => file.slug === slug)
|
2025-02-15 17:16:10 +01:00
|
|
|
|
2025-02-15 17:39:01 +01:00
|
|
|
if (!guideFile) {
|
2025-02-15 23:01:40 +01:00
|
|
|
console.error(`❌ No se encontró la guía: ${slug}`)
|
2025-02-15 17:52:05 +01:00
|
|
|
return { content: "<p class='text-red-600'>Error: No se encontró la guía solicitada.</p>", metadata: null }
|
2025-02-15 17:16:10 +01:00
|
|
|
}
|
|
|
|
|
2025-02-15 17:39:01 +01:00
|
|
|
const fileContents = fs.readFileSync(guideFile.path, "utf8")
|
2025-02-15 23:01:40 +01:00
|
|
|
const { content, data } = matter(fileContents) // 🔹 Extrae metadata y contenido del `.md`
|
2025-02-15 17:04:08 +01:00
|
|
|
|
2025-02-15 23:01:40 +01:00
|
|
|
// 🔹 Convertimos el Markdown a HTML con soporte para imágenes y tablas
|
2025-02-15 17:16:10 +01:00
|
|
|
const result = await remark()
|
2025-02-15 17:39:01 +01:00
|
|
|
.use(gfm.default || gfm)
|
2025-02-15 17:16:10 +01:00
|
|
|
.use(html)
|
2025-02-15 17:52:05 +01:00
|
|
|
.process(content)
|
2025-02-15 17:16:10 +01:00
|
|
|
|
2025-02-15 17:52:05 +01:00
|
|
|
return { content: result.toString(), metadata: data }
|
2025-02-15 17:04:08 +01:00
|
|
|
} catch (error) {
|
2025-02-15 17:39:01 +01:00
|
|
|
console.error(`❌ Error al leer la guía ${slug}`, error)
|
2025-02-15 17:52:05 +01:00
|
|
|
return { content: "<p class='text-red-600'>Error: No se pudo cargar la guía.</p>", metadata: null }
|
2025-02-15 17:04:08 +01:00
|
|
|
}
|
2025-02-15 12:40:28 +01:00
|
|
|
}
|
|
|
|
|
2025-02-15 23:01:40 +01:00
|
|
|
// 🔹 Generamos rutas estáticas asegurando que Next.js las acepte
|
2025-02-15 17:16:10 +01:00
|
|
|
export async function generateStaticParams() {
|
|
|
|
try {
|
2025-02-15 23:01:40 +01:00
|
|
|
const markdownFiles = getMarkdownFiles()
|
2025-02-15 17:39:01 +01:00
|
|
|
return markdownFiles.map((file) => ({ slug: file.slug }))
|
2025-02-15 17:16:10 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.error("❌ Error al generar las rutas estáticas para guides:", error)
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-15 17:52:05 +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:52:05 +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 17:52:05 +01:00
|
|
|
// 🔹 Página principal de cada guía
|
2025-02-15 15:14:19 +01:00
|
|
|
export default async function GuidePage({ params }: { params: { slug: string } }) {
|
2025-02-15 17:52:05 +01:00
|
|
|
const { content, metadata } = await getGuideContent(params.slug)
|
|
|
|
const cleanedInlineCode = cleanInlineCode(content)
|
2025-02-15 17:39:01 +01:00
|
|
|
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 17:39:01 +01:00
|
|
|
<div className="container mx-auto px-4 py-16" style={{ maxWidth: "980px" }}>
|
2025-02-15 17:52:05 +01:00
|
|
|
{metadata?.title && <h1 className="text-4xl font-bold mb-4">{metadata.title}</h1>}
|
|
|
|
{metadata?.description && <p className="text-lg text-gray-700 mb-8">{metadata.description}</p>}
|
2025-02-15 17:39:01 +01:00
|
|
|
<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
|
|
|
}
|