-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
index.js
47 lines (39 loc) · 976 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const React = require('react')
const {transform} = require('buble-jsx-only')
const mdx = require('@mdx-js/mdx')
const {MDXProvider, mdx: createElement} = require('@mdx-js/react')
const suffix = `return React.createElement(
MDXProvider,
{components},
React.createElement(MDXContent, props)
)`
const runtime = ({
scope = {},
components = {},
remarkPlugins = [],
rehypePlugins = [],
children,
...props
}) => {
const fullScope = {
mdx: createElement,
MDXProvider,
components,
props,
...scope
}
const jsx = mdx
.sync(children, {
remarkPlugins,
rehypePlugins,
skipExport: true
})
.trim()
const {code} = transform(jsx, {objectAssign: 'Object.assign'})
const keys = Object.keys(fullScope)
const values = Object.values(fullScope)
// eslint-disable-next-line no-new-func
const fn = new Function('React', ...keys, `${code}\n\n${suffix}`)
return fn(React, ...values)
}
module.exports = runtime