diff --git a/packages/mdx/src/server/index.ts b/packages/mdx/src/server/index.ts index 33780a1..d72a5b3 100644 --- a/packages/mdx/src/server/index.ts +++ b/packages/mdx/src/server/index.ts @@ -1,3 +1,4 @@ +import { evaluate } from 'next-mdx-remote-client/rsc'; import { serialize as baseSerialize } from 'next-mdx-remote-client/serialize'; import rehypeKatex from 'rehype-katex'; import remarkGfm from 'remark-gfm'; @@ -52,3 +53,49 @@ export const serialize = async ({ throw error; } }; + +export const rscSerialize = async ({ + source, + mdxOptions, + scope, + parseFrontmatter = true, +}: { + source: string; + mdxOptions?: SerializeOptions['mdxOptions']; + scope?: SerializeOptions['scope']; + parseFrontmatter?: SerializeOptions['parseFrontmatter']; +}) => { + try { + return await evaluate({ + source, + options: { + mdxOptions: { + ...mdxOptions, + remarkPlugins: [ + remarkGfm, + remarkSmartypants, + remarkMath, + ...(mdxOptions?.remarkPlugins || []), + ], + rehypePlugins: [ + rehypeKatex, + [ + rehypeSyntaxHighlighting, + { + ignoreMissing: true, + }, + ], + ...(mdxOptions?.rehypePlugins || []), + ], + format: mdxOptions?.format || 'mdx', + }, + scope, + parseFrontmatter, + }, + }); + } catch (error) { + console.error(`Error occurred while serializing MDX: ${error}`); + + throw error; + } +};