import { useQuery } from "@tanstack/react-query"; import axios from "axios"; import ReactMarkdown from "react-markdown"; import remarkGfm from "remark-gfm"; import rehypeHighlight from "rehype-highlight"; import rehypeRaw from "rehype-raw"; import "highlight.js/styles/github.css"; type Props = { url?: string; }; export function MarkdownPreview({ url }: Props) { const { data, isLoading, error, isError } = useQuery({ queryKey: ["markdown", url], queryFn: () => axios.get(url!, { responseType: "text" }), enabled: !!url, }); if (!url) { return (
Markdown 文件地址无效
); } if (isLoading) { return (
加载中...
); } if (isError) { return (
{error?.message || "加载失败"}
); } return (
{data?.data || ""}
); }