Skip to content

Commit fd9271d

Browse files
Merge pull request #16 from mintlify/ricardo/add-rsc-serialize
Add function for serializing with React Server Components
2 parents eab9962 + 0913d22 commit fd9271d

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

packages/mdx/src/server/index.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { evaluate } from 'next-mdx-remote-client/rsc';
12
import { serialize as baseSerialize } from 'next-mdx-remote-client/serialize';
23
import rehypeKatex from 'rehype-katex';
34
import remarkGfm from 'remark-gfm';
@@ -52,3 +53,49 @@ export const serialize = async ({
5253
throw error;
5354
}
5455
};
56+
57+
export const rscSerialize = async ({
58+
source,
59+
mdxOptions,
60+
scope,
61+
parseFrontmatter = true,
62+
}: {
63+
source: string;
64+
mdxOptions?: SerializeOptions['mdxOptions'];
65+
scope?: SerializeOptions['scope'];
66+
parseFrontmatter?: SerializeOptions['parseFrontmatter'];
67+
}) => {
68+
try {
69+
return await evaluate({
70+
source,
71+
options: {
72+
mdxOptions: {
73+
...mdxOptions,
74+
remarkPlugins: [
75+
remarkGfm,
76+
remarkSmartypants,
77+
remarkMath,
78+
...(mdxOptions?.remarkPlugins || []),
79+
],
80+
rehypePlugins: [
81+
rehypeKatex,
82+
[
83+
rehypeSyntaxHighlighting,
84+
{
85+
ignoreMissing: true,
86+
},
87+
],
88+
...(mdxOptions?.rehypePlugins || []),
89+
],
90+
format: mdxOptions?.format || 'mdx',
91+
},
92+
scope,
93+
parseFrontmatter,
94+
},
95+
});
96+
} catch (error) {
97+
console.error(`Error occurred while serializing MDX: ${error}`);
98+
99+
throw error;
100+
}
101+
};

0 commit comments

Comments
 (0)