Help integrating rehype-starry-night with MDX #2535
-
Hi mdx community, I'm currently trying to integrate a syntax highlighting rehype plugin - 'rehype-starry-night' with my current MDX setup. I'm following the process in this MDX guide, and I'm trying to get the following code to work in a server component, in my Next.JS project (App Router). This MDXContent.tsx is a dumbed down version of what I'm trying to get up and running.
Unfortunately, I'm getting the following error:
When I remove the rehype plugin from the compile function, the component runs as expected and a compiled version of the code is returned in the terminal. Strangely enough, I can get this process to work on codesandbox, but not on my own app. My repo is here. In the best world, I would be fetching the mdx's content using 'fs.readFileSync', compiling it with the rehype-starry-night plugin and then finding a way to output this content using MDXRemote. I've tried to create this process here. (I believe I probably have the output wrong as well given I'm sending MDXRemote an already compiled source) Any ideas would be greatly appreciated, thank you in advance! My versions are: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hi Oliver. This seems like a webpack or Next or next-mdx-remote bug. |
Beta Was this translation helpful? Give feedback.
-
UpdateThanks to @wooorm, I managed to get my app to correctly compile/serialise mdx content with the rehype-starry-night plugin and I could console.log the output string. But I then struggled to find a way to output this "compiled" / "serialised" code in a Next JS server component. I'm probably missing something quite simple. I decided to use react-syntax-highlighter in the meantime and customised the built-in 'code' tag in mdx-components.tsx to use a SyntaxHighlight component. @beeinger has a great implementation of this that I followed here. Of course - I'm open to any other thoughts of how I could've made rehype-starry-night work, but this workaround seems like it'll do the trick for now. |
Beta Was this translation helpful? Give feedback.
Hi Oliver.
This seems like a webpack or Next or next-mdx-remote bug.
Some tool thinks that
source.mdx.js
is an MDX file. It is not.Try removing tools. To figure out where the bug is.