From 01c75f6798fcfcd76d9b0da39336ea121f4b06e5 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sun, 2 Mar 2025 16:39:20 -0600 Subject: [PATCH] refactor: improve root layout with internationalization and font configuration --- .../website/app/[locale]/blog/[slug]/page.tsx | 4 +- .../[locale]/blog/components/CodeBlock.tsx | 74 ++++++++----------- apps/website/app/[locale]/layout.tsx | 42 ++--------- apps/website/app/layout.tsx | 40 +++++++++- 4 files changed, 75 insertions(+), 85 deletions(-) diff --git a/apps/website/app/[locale]/blog/[slug]/page.tsx b/apps/website/app/[locale]/blog/[slug]/page.tsx index d9c045a..e20aec0 100644 --- a/apps/website/app/[locale]/blog/[slug]/page.tsx +++ b/apps/website/app/[locale]/blog/[slug]/page.tsx @@ -155,9 +155,9 @@ export default async function BlogPostPage({ params }: Props) { ), img: ({ node, src, alt }) => ( -
+ {src && } -
+ ), code: ({ inline, className, children, ...props }: CodeProps) => { const match = /language-(\w+)/.exec(className || ""); diff --git a/apps/website/app/[locale]/blog/components/CodeBlock.tsx b/apps/website/app/[locale]/blog/components/CodeBlock.tsx index f65b3d8..76d1b6b 100644 --- a/apps/website/app/[locale]/blog/components/CodeBlock.tsx +++ b/apps/website/app/[locale]/blog/components/CodeBlock.tsx @@ -1,7 +1,8 @@ "use client"; -import * as xmlPlugin from "@prettier/plugin-xml"; import * as prettier from "prettier"; +import * as prettierPluginBabel from "prettier/plugins/babel"; +import * as prettierPluginEstree from "prettier/plugins/estree"; import { Highlight, themes } from "prism-react-renderer"; import { useEffect, useState } from "react"; @@ -11,64 +12,53 @@ interface CodeBlockProps { className?: string; } +const getParserForLanguage = (language: string): string => { + const languageMap: { [key: string]: string } = { + js: "babel", + jsx: "babel", + ts: "typescript", + tsx: "typescript", + json: "json", + css: "css", + scss: "scss", + less: "less", + html: "html", + xml: "xml", + markdown: "markdown", + md: "markdown", + yaml: "yaml", + yml: "yaml", + }; + + return languageMap[language.toLowerCase()] || "babel"; +}; + export function CodeBlock({ code, language, className = "" }: CodeBlockProps) { const [formattedCode, setFormattedCode] = useState(code); useEffect(() => { const formatCode = async () => { try { - // Determine the parser based on the language - let parser = language; - let plugins: any[] = []; + const parser = getParserForLanguage(language); - // Map common languages to their appropriate parsers - switch (language.toLowerCase()) { - case "tsx": - case "ts": - case "typescript": - parser = "babel-ts"; - plugins = ["@babel/plugin-syntax-typescript"]; - break; - case "jsx": - case "js": - case "javascript": - parser = "babel"; - break; - case "html": - case "xml": - case "svg": - parser = "html"; - plugins = [xmlPlugin]; - break; - case "json": - parser = "json"; - break; - case "css": - case "scss": - case "less": - parser = "css"; - break; - default: - // For unknown languages, just clean up the whitespace - setFormattedCode(code.trim()); - return; - } + // Eliminar espacios en blanco al inicio y final, pero mantener la indentación interna - const formatted = await prettier.format(code, { - parser, - plugins, + const formatted = await prettier.format(formattedCode, { semi: true, singleQuote: false, tabWidth: 2, useTabs: false, printWidth: 80, + parser, + plugins: [prettierPluginBabel, prettierPluginEstree], }); - setFormattedCode(formatted.trim()); + setFormattedCode(formatted); } catch (error) { console.warn("Error formatting code:", error); - // If formatting fails, just clean up the whitespace - setFormattedCode(code.trim()); + // Si falla el formateo, al menos limpiamos los espacios en blanco extra + const cleanCode = code.replace(/^\s+|\s+$/g, ""); + setFormattedCode(cleanCode); } }; diff --git a/apps/website/app/[locale]/layout.tsx b/apps/website/app/[locale]/layout.tsx index e6fee90..212861a 100644 --- a/apps/website/app/[locale]/layout.tsx +++ b/apps/website/app/[locale]/layout.tsx @@ -1,8 +1,5 @@ -import clsx from "clsx"; import { Inter, Lexend } from "next/font/google"; import "@/styles/tailwind.css"; -import { NextIntlClientProvider } from "next-intl"; -import { getMessages } from "next-intl/server"; import "react-photo-view/dist/react-photo-view.css"; import { Footer } from "@/components/Footer"; import { Header } from "@/components/Header"; @@ -71,18 +68,6 @@ export const metadata: Metadata = { }, }; -const inter = Inter({ - subsets: ["latin"], - display: "swap", - variable: "--font-inter", -}); - -const lexend = Lexend({ - subsets: ["latin"], - display: "swap", - variable: "--font-lexend", -}); - export default async function RootLayout({ children, params, @@ -90,28 +75,11 @@ export default async function RootLayout({ children: React.ReactNode; params: { locale: string }; }) { - const { locale } = params; - const messages = await getMessages(); return ( - - -