-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-recipes): fix MDX rendering for exports (#25133)
* feat(gatsby-recipes): Handle exports, render MDX directly * Continue working towards proper exports * Continue implementing MDX renderer v2 * More MDX rendering hacking * Finish basic export handling * Small fixes Co-authored-by: John Otander <johnotander@gmail.com>
- Loading branch information
1 parent
0edc847
commit 6ede2fa
Showing
15 changed files
with
323 additions
and
150 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React from 'react' | ||
import { transform } from '@babel/standalone' | ||
import mdx from '@mdx-js/mdx' | ||
import { mdx as createElement, MDXProvider } from '@mdx-js/react' | ||
import babelPluginTransformReactJsx from '@babel/plugin-transform-react-jsx' | ||
import babelPluginRemoveExportKeywords from 'babel-plugin-remove-export-keywords' | ||
|
||
const transformJsx = jsx => { | ||
const { code } = transform(jsx, { | ||
plugins: [ | ||
babelPluginRemoveExportKeywords, | ||
[babelPluginTransformReactJsx, { useBuiltIns: true }], | ||
], | ||
}) | ||
|
||
return code | ||
} | ||
|
||
const transformCodeForEval = jsx => { | ||
return `${jsx} | ||
return React.createElement(MDXProvider, { components }, | ||
React.createElement(MDXContent, props) | ||
);` | ||
} | ||
|
||
export default ({ children: mdxSrc, scope, components, ...props }) => { | ||
const fullScope = { | ||
mdx: createElement, | ||
MDXProvider, | ||
React, | ||
components, | ||
props, | ||
...scope | ||
} | ||
const scopeKeys = Object.keys(fullScope) | ||
const scopeValues = Object.values(fullScope) | ||
|
||
const jsxFromMdx = mdx.sync(mdxSrc, { skipExport: true }) | ||
const srcCode = transformJsx(jsxFromMdx) | ||
|
||
const fn = new Function(...scopeKeys, transformCodeForEval(srcCode)) | ||
|
||
return fn(...scopeValues) | ||
} |
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
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
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
Oops, something went wrong.