Evaluate React Markdown String with JSX Components? #2323
-
We are trying to allow using MDX with additional JSX components for Server Side Rendering of static HTML pages. So I've surmised that the mdx runtime module is deprecated, and that the new MDX evaluate function is the way to go. We are aware that we will need to using something like VM2 with timeouts to do this securely. I haven't really been able to find many examples of how to do this though. Here is one of the examples that I've found: If I have a static string of markdown with JSX, is there an easier way for me to render the content inside of a react component? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
Welcome @ScottPierce! 👋
Sure, you could wrap it as a component or any other abstraction you wanted to.
Correct
If you are interacting with untrusted content, yes.
The simplest and least abstract way to to use the result of import * as runtime from "react/jsx-runtime";
import { evaluateSync } from "@mdx-js/mdx";
const mdxContent = `
export const planet = 'World'
# Hello, {planet}!
* list
* item
`;
export default function App() {
return evaluateSync(mdxContent, runtime).default();
} https://codesandbox.io/s/strange-snow-ldpg58?file=/src/App.js:384-394 You are welcome to use more or different abstractions, the questions comes back to my initial one, "why do you want to abstract it?" |
Beta Was this translation helpful? Give feedback.
Welcome @ScottPierce! 👋
Sure, you could wrap it as a component or any other abstraction you wanted to.
Why do you want to abstract it?
Correct
If you are interacting with untrusted content, yes.
You will want as many safe guards as possible.
The simplest and least abstract way to to use the resul…