From 0913d22cb9dfbaaa0d3346a2fa5017a0506786b1 Mon Sep 17 00:00:00 2001 From: Ricardo Nunez <113212961+ricardonunez-io@users.noreply.github.com> Date: Wed, 4 Jun 2025 19:49:23 -0700 Subject: [PATCH] Add function for serializing with React Server Components --- packages/mdx/src/server/index.ts | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) 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; + } +};