Update page.tsx

This commit is contained in:
MacRimi 2025-02-15 17:04:08 +01:00 committed by GitHub
parent e76bcf2c54
commit de70d31896
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,36 +2,36 @@ import fs from "fs"
import path from "path" import path from "path"
import { remark } from "remark" import { remark } from "remark"
import html from "remark-html" import html from "remark-html"
import gfm from "remark-gfm" // ✅ Permite imágenes y tablas en Markdown
import dynamic from "next/dynamic" import dynamic from "next/dynamic"
import React from "react" import React from "react"
import parse from "html-react-parser" import parse from "html-react-parser"
// 🔹 Importamos `CopyableCode` dinámicamente para evitar problemas de SSR
const CopyableCode = dynamic(() => import("@/components/CopyableCode"), { ssr: false }) const CopyableCode = dynamic(() => import("@/components/CopyableCode"), { ssr: false })
async function getGuideContent(slug: string) { async function getGuideContent(slug: string) {
const guidePath = path.join(process.cwd(), "..", "guides", `${slug}.md`) try {
const fileContents = fs.readFileSync(guidePath, "utf8") const guidePath = path.join(process.cwd(), "..", "guides", `${slug}.md`)
const fileContents = fs.readFileSync(guidePath, "utf8")
const result = await remark().use(html).process(fileContents) // ✅ Agregamos `remark-gfm` para permitir imágenes en Markdown
return result.toString() const result = await remark().use(gfm).use(html).process(fileContents)
} return result.toString()
} catch (error) {
export async function generateStaticParams() { console.error(`❌ Error al leer el archivo guía: ${slug}.md`, error)
const guideFiles = fs.readdirSync(path.join(process.cwd(), "..", "guides")) return "<p class='text-red-600'>Error: No se pudo cargar la guía.</p>"
return guideFiles.map((file) => ({ }
slug: file.replace(/\.md$/, ""),
}))
} }
// 🔹 Limpia las comillas invertidas en fragmentos de código en línea
function cleanInlineCode(content: string) { function cleanInlineCode(content: string) {
return content.replace(/<code>(.*?)<\/code>/g, (_, codeContent) => { return content.replace(/<code>(.*?)<\/code>/g, (_, codeContent) => {
const cleanedCode = codeContent.replace(/^`|`$/g, "") return `<code class="bg-gray-200 text-gray-900 px-1 rounded">${codeContent.replace(/^`|`$/g, "")}</code>`
return `<code class="bg-gray-200 text-gray-900 px-1 rounded">${cleanedCode}</code>`
}) })
} }
// 🔹 Envuelve los bloques de código en <CopyableCode />
function wrapCodeBlocksWithCopyable(content: string) { function wrapCodeBlocksWithCopyable(content: string) {
return parse(content, { return parse(content, {
replace: (domNode: any) => { replace: (domNode: any) => {
@ -48,13 +48,13 @@ function wrapCodeBlocksWithCopyable(content: string) {
export default async function GuidePage({ params }: { params: { slug: string } }) { export default async function GuidePage({ params }: { params: { slug: string } }) {
const guideContent = await getGuideContent(params.slug) const guideContent = await getGuideContent(params.slug)
const cleanedInlineCode = cleanInlineCode(guideContent) const cleanedInlineCode = cleanInlineCode(guideContent) // 🔹 Primero limpiamos código en línea
const parsedContent = wrapCodeBlocksWithCopyable(cleanedInlineCode) const parsedContent = wrapCodeBlocksWithCopyable(cleanedInlineCode) // 🔹 Luego aplicamos JSX a bloques de código
return ( return (
<div className="min-h-screen bg-white text-gray-900"> <div className="min-h-screen bg-white text-gray-900">
<div className="container mx-auto px-4 py-16 max-w-4xl"> <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> <div className="prose max-w-none text-[16px]">{parsedContent}</div> {/* 📌 Texto ajustado a 16px */}
</div> </div>
</div> </div>
) )