This repository has been archived by the owner on Nov 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor mdx-renderer to use hooks (#333)
- Loading branch information
1 parent
c65afbc
commit 3cd8066
Showing
2 changed files
with
34 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,33 @@ | ||
const React = require("react"); | ||
const { withMDXComponents, mdx } = require("@mdx-js/react"); | ||
const { withMDXScope } = require("./context"); | ||
const { useMDXComponents, mdx } = require("@mdx-js/react"); | ||
const { useMDXScope } = require("./context"); | ||
|
||
module.exports = withMDXScope( | ||
withMDXComponents(({ scope = {}, components = {}, children, ...props }) => { | ||
if (!children) { | ||
return null; | ||
} | ||
const fullScope = { | ||
// React is here just in case the user doesn't pass them in | ||
// in a manual usage of the renderer | ||
React, | ||
mdx, | ||
...scope | ||
}; | ||
module.exports = function MDXRenderer({ | ||
scope = {}, | ||
components = {}, | ||
children, | ||
...props | ||
}) { | ||
const mdxComponents = useMDXComponents(components); | ||
const mdxScope = useMDXScope(scope); | ||
|
||
// children is pre-compiled mdx | ||
const keys = Object.keys(fullScope); | ||
const values = keys.map(key => fullScope[key]); | ||
const fn = new Function("_fn", ...keys, `${children}`); | ||
if (!children) { | ||
return null; | ||
} | ||
|
||
const End = fn({}, ...values); | ||
return React.createElement(End, { components, ...props }); | ||
}) | ||
); | ||
const fullScope = { | ||
// React is here just in case the user doesn't pass them in | ||
// in a manual usage of the renderer | ||
React, | ||
mdx, | ||
...mdxScope | ||
}; | ||
|
||
// children is pre-compiled mdx | ||
const keys = Object.keys(fullScope); | ||
const values = keys.map(key => fullScope[key]); | ||
const fn = new Function("_fn", ...keys, `${children}`); | ||
|
||
const End = fn({}, ...values); | ||
return React.createElement(End, { components: mdxComponents, ...props }); | ||
}; |