2025-02-20 16:08:21 +00:00
|
|
|
"use client";
|
|
|
|
|
2025-02-20 17:39:58 +00:00
|
|
|
import "@/style/mdx.css";
|
2025-03-24 02:31:09 +00:00
|
|
|
import { cn } from "@/lib/utils";
|
2025-02-25 07:09:44 +00:00
|
|
|
import "katex/dist/katex.min.css";
|
2025-02-20 10:46:20 +00:00
|
|
|
import remarkGfm from "remark-gfm";
|
2025-02-25 07:09:44 +00:00
|
|
|
import remarkMath from "remark-math";
|
|
|
|
import rehypeKatex from "rehype-katex";
|
2025-02-26 06:32:11 +00:00
|
|
|
// import rehypeSlug from "rehype-slug";
|
2025-02-20 16:08:21 +00:00
|
|
|
import rehypePretty from "rehype-pretty-code";
|
2025-02-20 09:58:49 +00:00
|
|
|
import { Skeleton } from "@/components/ui/skeleton";
|
2025-02-20 16:08:21 +00:00
|
|
|
import { serialize } from "next-mdx-remote/serialize";
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
|
|
import { CircleAlert, TriangleAlert } from "lucide-react";
|
2025-03-06 13:29:52 +00:00
|
|
|
import { useMonacoTheme } from "@/hooks/use-monaco-theme";
|
2025-02-26 03:34:46 +00:00
|
|
|
// import rehypeAutolinkHeadings from "rehype-autolink-headings";
|
2025-03-24 02:31:09 +00:00
|
|
|
import { MdxComponents } from "@/components/content/mdx-components";
|
2025-02-20 16:08:21 +00:00
|
|
|
import { MDXRemote, MDXRemoteSerializeResult } from "next-mdx-remote";
|
2025-02-20 09:58:49 +00:00
|
|
|
|
2025-02-20 16:08:21 +00:00
|
|
|
interface MdxPreviewProps {
|
|
|
|
source: string;
|
2025-03-24 02:31:09 +00:00
|
|
|
className?: string;
|
2025-02-20 09:58:49 +00:00
|
|
|
}
|
|
|
|
|
2025-03-24 02:31:09 +00:00
|
|
|
export default function MdxPreview({
|
|
|
|
source,
|
|
|
|
className,
|
|
|
|
}: MdxPreviewProps) {
|
2025-03-19 06:58:11 +00:00
|
|
|
const { currentTheme } = useMonacoTheme();
|
2025-02-20 16:08:21 +00:00
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const [isLoading, setIsLoading] = useState<boolean>(true);
|
|
|
|
const [mdxSource, setMdxSource] = useState<MDXRemoteSerializeResult | null>(null);
|
|
|
|
|
|
|
|
const getMdxSource = useCallback(async () => {
|
|
|
|
setIsLoading(true);
|
|
|
|
setError(null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const mdxSource = await serialize(source, {
|
2025-02-20 10:46:20 +00:00
|
|
|
mdxOptions: {
|
2025-02-20 16:08:21 +00:00
|
|
|
rehypePlugins: [
|
2025-02-26 03:34:46 +00:00
|
|
|
// rehypeSlug,
|
|
|
|
// [
|
|
|
|
// rehypeAutolinkHeadings,
|
|
|
|
// {
|
|
|
|
// behavior: "wrap",
|
|
|
|
// properties: {
|
|
|
|
// className: ["subheading-anchor"],
|
|
|
|
// ariaLabel: "Link to section",
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// ],
|
2025-02-20 16:08:21 +00:00
|
|
|
[
|
|
|
|
rehypePretty,
|
|
|
|
{
|
2025-03-20 06:47:16 +00:00
|
|
|
theme: currentTheme,
|
2025-02-20 16:08:21 +00:00
|
|
|
keepBackground: false,
|
|
|
|
},
|
|
|
|
],
|
2025-02-25 07:09:44 +00:00
|
|
|
rehypeKatex,
|
2025-02-20 16:08:21 +00:00
|
|
|
],
|
2025-02-25 07:09:44 +00:00
|
|
|
remarkPlugins: [remarkGfm, remarkMath],
|
2025-02-20 10:46:20 +00:00
|
|
|
},
|
2025-02-20 16:08:21 +00:00
|
|
|
});
|
|
|
|
setMdxSource(mdxSource);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Failed to serialize Mdx:", error);
|
|
|
|
setError("Failed to load mdx content.");
|
|
|
|
} finally {
|
|
|
|
setIsLoading(false);
|
|
|
|
}
|
2025-03-19 06:58:11 +00:00
|
|
|
}, [source, currentTheme]);
|
2025-02-20 10:46:20 +00:00
|
|
|
|
2025-02-20 16:08:21 +00:00
|
|
|
// Delay the serialize process to the next event loop to avoid flickering
|
|
|
|
// when copying code to the editor and the MDX preview shrinks.
|
|
|
|
useEffect(() => {
|
|
|
|
const timeoutId = setTimeout(() => {
|
|
|
|
getMdxSource(); // Execute serializeMdx in the next event loop
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
return () => clearTimeout(timeoutId); // Cleanup timeout on component unmount
|
|
|
|
}, [getMdxSource]);
|
|
|
|
|
|
|
|
if (isLoading) {
|
2025-02-26 07:08:27 +00:00
|
|
|
return (
|
2025-02-26 07:58:40 +00:00
|
|
|
<div className="h-full w-full p-4">
|
2025-02-26 07:08:27 +00:00
|
|
|
<Skeleton className="h-full w-full rounded-3xl" />
|
|
|
|
</div>
|
|
|
|
);
|
2025-02-20 16:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return (
|
|
|
|
<div className="h-full flex items-center justify-center">
|
|
|
|
<div className="rounded-lg border border-red-500/50 px-4 py-3 text-red-600">
|
|
|
|
<p className="text-sm">
|
|
|
|
<CircleAlert className="-mt-0.5 me-3 inline-flex opacity-60" size={16} strokeWidth={2} aria-hidden="true" />
|
|
|
|
{error}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!source) {
|
2025-02-20 10:46:20 +00:00
|
|
|
return (
|
2025-02-20 16:08:21 +00:00
|
|
|
<div className="h-full flex items-center justify-center">
|
|
|
|
<div className="rounded-lg border border-amber-500/50 px-4 py-3 text-amber-600">
|
|
|
|
<p className="text-sm">
|
|
|
|
<TriangleAlert className="-mt-0.5 me-3 inline-flex opacity-60" size={16} strokeWidth={2} aria-hidden="true" />
|
|
|
|
No content to preview.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
2025-02-20 10:46:20 +00:00
|
|
|
);
|
|
|
|
}
|
2025-02-20 16:08:21 +00:00
|
|
|
|
|
|
|
return (
|
2025-03-24 02:31:09 +00:00
|
|
|
<article className={cn("markdown-body", className)}>
|
|
|
|
<MDXRemote {...mdxSource!} components={MdxComponents} />
|
|
|
|
</article>
|
2025-02-20 16:08:21 +00:00
|
|
|
);
|
2025-02-20 09:58:49 +00:00
|
|
|
}
|