|
1
|
"use client" |
|
2
|
|
|
3
|
import React from "react" |
|
4
|
import { useEffect, useState } from "react" |
|
5
|
import getCustomComponents from "../../MDXComponents" |
|
6
|
import type { ScopeType } from "../../MDXComponents" |
|
7
|
import { MDXRemote } from "next-mdx-remote" |
|
8
|
import type { MDXRemoteProps, MDXRemoteSerializeResult } from "next-mdx-remote" |
|
9
|
import { serialize } from "next-mdx-remote/serialize" |
|
10
|
|
|
11
|
export type MDXContentClientProps = { |
|
12
|
content: any |
|
13
|
className?: string |
|
14
|
} & Partial<MDXRemoteProps> |
|
15
|
|
|
16
|
const MDXContentClient = ({ |
|
17
|
content, |
|
18
|
className, |
|
19
|
...props |
|
20
|
}: MDXContentClientProps) => { |
|
21
|
const [parsedContent, setParsedContent] = useState<MDXRemoteSerializeResult>() |
|
22
|
|
|
23
|
useEffect(() => { |
|
24
|
void serialize(content, { |
|
25
|
mdxOptions: { |
|
26
|
// A workaround for an error in next-mdx-remote |
|
27
|
// more details in this issue: |
|
28
|
// https://github.com/hashicorp/next-mdx-remote/issues/350 |
|
29
|
development: process.env.NEXT_PUBLIC_ENV === "development", |
|
30
|
}, |
|
31
|
scope: props.scope, |
|
32
|
}).then((output) => { |
|
33
|
setParsedContent(output) |
|
34
|
}) |
|
35
|
}, [content, props.scope]) |
|
36
|
|
|
37
|
return ( |
|
38
|
<div className={className}> |
|
39
|
{parsedContent !== undefined && ( |
|
40
|
<MDXRemote |
|
41
|
{...parsedContent} |
|
42
|
components={getCustomComponents((props.scope as ScopeType) || {})} |
|
43
|
/> |
|
44
|
)} |
|
45
|
</div> |
|
46
|
) |
|
47
|
} |
|
48
|
|
|
49
|
export default MDXContentClient |