"use client"; import React, { useState } from "react"; import Editor from "@monaco-editor/react"; import yaml from "js-yaml"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { FileJson, Copy, Check, RotateCcw, ArrowRight, XCircle } from "lucide-react"; import { toast } from "sonner"; import { useTheme } from "next-themes"; import { Badge } from "@/components/ui/badge"; export default function YamlFormatterPage() { const { theme } = useTheme(); const [input, setInput] = useState(""); const [output, setOutput] = useState(""); const [isValid, setIsValid] = useState(null); const [errorMsg, setErrorMsg] = useState(""); const handleFormat = () => { if (!input.trim()) return; try { const obj = yaml.load(input); const formatted = yaml.dump(obj, { indent: 2 }); setOutput(formatted); setIsValid(true); setErrorMsg(""); toast.success("YAML 格式化成功"); } catch (e: any) { setIsValid(false); setErrorMsg(e.message); toast.error("YAML 解析失败"); } }; const handleValidation = () => { if (!input.trim()) return; try { yaml.load(input); setIsValid(true); setErrorMsg(""); toast.success("YAML 格式正确"); } catch (e: any) { setIsValid(false); setErrorMsg(e.message); } }; const copyToClipboard = async (text: string) => { try { await navigator.clipboard.writeText(text); toast.success("已复制到剪贴板"); } catch { toast.error("复制失败"); } }; return (

YAML 格式化

格式化、验证和美化 YAML 数据

{/* Input */} 输入
setInput(value || "")} options={{ minimap: { enabled: false }, fontSize: 14, wordWrap: "on" }} /> {errorMsg && (
{errorMsg}
)}
{/* Output */} 结果 {isValid === true && ( 格式正确 )} {isValid === false && ( 格式错误 )}
); }