(
rawJs: string,
/** @default {} */
components: MDXComponents = {},
/** @default {} */
scope: Scope = {}
)
| 11 | export type Scope = Record<string, unknown> |
| 12 | |
| 13 | export function evaluate( |
| 14 | rawJs: string, |
| 15 | /** @default {} */ |
| 16 | components: MDXComponents = {}, |
| 17 | /** @default {} */ |
| 18 | scope: Scope = {} |
| 19 | ): EvaluateResult { |
| 20 | // If we're ready to render, we can assemble the component tree and let React do its thing |
| 21 | // first we set up the scope which has to include the mdx custom |
| 22 | // create element function as well as any components we're using |
| 23 | const keys = Object.keys(scope) |
| 24 | const values = Object.values(scope) |
| 25 | // Now we eval the source code using a function constructor |
| 26 | // in order for this to work, we need to have React, the mdx `createElement`, |
| 27 | // and all our components in scope for the function, which is the case here |
| 28 | // we pass the names (via keys) in as the function's args, and execute the |
| 29 | // function with the actual values. |
| 30 | const hydrateFn = Reflect.construct(Function, ['$', ...keys, rawJs]) |
| 31 | |
| 32 | return hydrateFn( |
| 33 | { |
| 34 | ...runtime, |
| 35 | // Inject components in `<MDXContent>` and TOC |
| 36 | useMDXComponents: () => components |
| 37 | }, |
| 38 | ...values |
| 39 | ) |
| 40 | } |
no outgoing calls
no test coverage detected
searching dependent graphs…