MCPcopy
hub / github.com/shuding/nextra / evaluate

Function evaluate

packages/nextra/src/client/evaluate.ts:13–40  ·  view source on GitHub ↗
(
  rawJs: string,
  /** @default {} */
  components: MDXComponents = {},
  /** @default {} */
  scope: Scope = {}
)

Source from the content-addressed store, hash-verified

11export type Scope = Record<string, unknown>
12
13export 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}

Callers 6

generateApiReferenceFunction · 0.90
PageFunction · 0.90
PageFunction · 0.90
MDXRemoteFunction · 0.85
PlaygroundFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…