|
| 1 | +"use client"; |
| 2 | + |
1 | 3 | import { CopyButton } from "@/components/ui/copy-button"; |
2 | | -import prettier from "prettier"; |
3 | | -import { codeToHtml } from "shiki"; |
| 4 | +import * as babel from "prettier/plugins/babel"; |
| 5 | +import * as estree from "prettier/plugins/estree"; |
| 6 | +import * as prettier from "prettier/standalone"; |
| 7 | +import { type JSX, useLayoutEffect, useState } from "react"; |
4 | 8 | import type { BundledLanguage } from "shiki/bundle/web"; |
| 9 | +import { highlight } from "./shared"; |
5 | 10 |
|
6 | | -interface LanguageProps { |
7 | | - children: string; |
| 11 | +interface CodeBlockProps { |
| 12 | + code: string; |
8 | 13 | lang: BundledLanguage; |
| 14 | + initial?: JSX.Element; |
| 15 | +} |
| 16 | + |
| 17 | +async function formatCode(code: string, lang: string) { |
| 18 | + try { |
| 19 | + // Configuración básica para JavaScript/TypeScript |
| 20 | + const plugins = [babel, estree]; |
| 21 | + console.log("Formatting with plugins:", plugins); |
| 22 | + |
| 23 | + const formatted = await prettier.format(code, { |
| 24 | + parser: "babel-ts", |
| 25 | + plugins, |
| 26 | + semi: true, |
| 27 | + singleQuote: true, |
| 28 | + tabWidth: 2, |
| 29 | + useTabs: false, |
| 30 | + printWidth: 120, |
| 31 | + }); |
| 32 | + |
| 33 | + console.log("Formatted code:", formatted); |
| 34 | + return formatted; |
| 35 | + } catch (error) { |
| 36 | + console.error("Error formatting code:", error); |
| 37 | + return code; // Retorna el código original si hay error |
| 38 | + } |
9 | 39 | } |
10 | 40 |
|
11 | | -const getParserForLanguage = (language: string): string => { |
12 | | - const languageMap: { [key: string]: string } = { |
13 | | - js: "babel", |
14 | | - jsx: "babel", |
15 | | - ts: "typescript", |
16 | | - tsx: "typescript", |
17 | | - json: "json", |
18 | | - css: "css", |
19 | | - scss: "scss", |
20 | | - less: "less", |
21 | | - html: "html", |
22 | | - xml: "xml", |
23 | | - markdown: "markdown", |
24 | | - md: "markdown", |
25 | | - yaml: "yaml", |
26 | | - yml: "yaml", |
27 | | - }; |
28 | | - |
29 | | - return languageMap[language.toLowerCase()] || "babel"; |
30 | | -}; |
31 | | - |
32 | | -export async function CodeBlock(props: LanguageProps) { |
33 | | - const format = await prettier.format(props.children, { |
34 | | - semi: true, |
35 | | - singleQuote: true, |
36 | | - tabWidth: 2, |
37 | | - useTabs: false, |
38 | | - printWidth: 120, |
39 | | - parser: getParserForLanguage(props.lang), |
40 | | - }); |
41 | | - const out = await codeToHtml(format, { |
42 | | - lang: props.lang, |
43 | | - theme: "houston", |
44 | | - }); |
| 41 | +export function CodeBlock({ code, lang, initial }: CodeBlockProps) { |
| 42 | + const [nodes, setNodes] = useState<JSX.Element | undefined>(initial); |
| 43 | + const [formattedCode, setFormattedCode] = useState(code); |
| 44 | + |
| 45 | + useLayoutEffect(() => { |
| 46 | + async function formatAndHighlight() { |
| 47 | + try { |
| 48 | + console.log("Original code:", code); |
| 49 | + const formatted = await formatCode(code, lang); |
| 50 | + setFormattedCode(formatted); |
| 51 | + |
| 52 | + // Then highlight the formatted code |
| 53 | + const highlighted = await highlight(formatted, lang); |
| 54 | + setNodes(highlighted); |
| 55 | + } catch (error) { |
| 56 | + console.error("Error in formatAndHighlight:", error); |
| 57 | + // If formatting fails, try to highlight the original code |
| 58 | + const highlighted = await highlight(code, lang); |
| 59 | + setNodes(highlighted); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + void formatAndHighlight(); |
| 64 | + }, [code, lang]); |
| 65 | + |
| 66 | + if (!nodes) { |
| 67 | + return ( |
| 68 | + <div className="group relative"> |
| 69 | + <div className="text-sm p-4 rounded-lg bg-[#18191F] overflow-auto animate-pulse"> |
| 70 | + <div className="h-4 bg-gray-700 rounded w-3/4 mb-2" /> |
| 71 | + <div className="h-4 bg-gray-700 rounded w-1/2" /> |
| 72 | + </div> |
| 73 | + </div> |
| 74 | + ); |
| 75 | + } |
45 | 76 |
|
46 | 77 | return ( |
47 | 78 | <div className="group relative"> |
48 | | - <CopyButton text={format} /> |
49 | | - <div |
50 | | - dangerouslySetInnerHTML={{ __html: out }} |
51 | | - className="text-sm p-4 rounded-lg bg-[#18191F] overflow-auto" |
52 | | - /> |
| 79 | + <CopyButton text={formattedCode} /> |
| 80 | + <div className="text-sm p-4 rounded-lg bg-[#18191F] overflow-auto"> |
| 81 | + {nodes} |
| 82 | + </div> |
53 | 83 | </div> |
54 | 84 | ); |
55 | 85 | } |
0 commit comments