The JSX element to render.
The rendered HTML string.
const jsxElement = <div>Hello, World!</div>;
const htmlString = safeRenderToString(jsxElement);
console.log(htmlString); // Output: "<div>Hello, World!</div>"
This function uses React and rehype to safely render a JSX element to an HTML string.
It first converts the JSX element to a React component using renderToString
from the react-dom/server
package.
Then, it parses the resulting HTML string into an HTML AST (Abstract Syntax Tree) using rehype-parse
.
Next, it converts the HTML AST back to a React component using rehype-react
.
Finally, it converts the React component back to an HTML string using rehype-stringify
.
The resulting HTML string is returned.
Generated using TypeDoc
Safely renders a JSX element to a string using React and rehype.