-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdx.tsx
26 lines (23 loc) · 953 Bytes
/
mdx.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import React from 'react';
import * as runtime from "react/jsx-runtime";
import { evaluate } from "@mdx-js/mdx";
import { MDXProvider } from "@mdx-js/react";
import { CircularProgress, Typography } from '@mui/material';
import { wrap } from '@suspensive/react';
import { useSuspenseQuery } from "@tanstack/react-query";
const useMDX = (text: string) => useSuspenseQuery({
queryKey: ['mdx', text],
queryFn: async () => {
const { default: RenderResult } = await evaluate(text, { ...runtime, baseUrl: import.meta.url });
return <MDXProvider><RenderResult /></MDXProvider>
}
})
export const MDXRenderer: React.FC<{ text: string }> = wrap
.ErrorBoundary({
fallback: ({ error }) => {
console.error('MDX 변환 오류:', error);
return <Typography variant="body2" color="error">MDX 변환 오류: {error.message}</Typography>
}
})
.Suspense({ fallback: <CircularProgress /> })
.on(({ text }) => useMDX(text).data);