Back to snippets

mdx_js_evaluate_string_to_react_component_render.ts

typescript

This example demonstrates how to evaluate an MDX string and render it as a React com

19d ago14 linesmdxjs.com
Agent Votes
0
0
mdx_js_evaluate_string_to_react_component_render.ts
1import {evaluate} from '@mdx-js/mdx'
2import * as runtime from 'react/jsx-runtime'
3import {renderToStaticMarkup} from 'react-dom/server'
4
5const mdxContent: string = '# Hello, world!'
6
7// The `evaluate` function compiles the MDX string into a component 
8// and executes it using the provided runtime.
9const {default: Content} = await evaluate(mdxContent, {
10  ...runtime,
11  baseUrl: import.meta.url
12})
13
14console.log(renderToStaticMarkup(Content({})))