-
Notifications
You must be signed in to change notification settings - Fork 60k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental react support within markdown
Also added fronmatter for interactive: true to turn on React on a file by file basis and prevent slowing down the builds for non-react files
- Loading branch information
Showing
13 changed files
with
32,457 additions
and
545 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const babel = require('@babel/core') | ||
const React = require('react') | ||
const { renderToString } = require('react-dom/server') | ||
const mdx = require('@mdx-js/mdx') | ||
const { MDXProvider, mdx: createElement } = require('@mdx-js/react') | ||
const BlueContent = require('../dist/react/BlueContent') | ||
const RedContent = require('../dist/react/RedContent') | ||
const Timer = require('../dist/react/Timer') | ||
|
||
const transform = code => | ||
babel.transform(code, { | ||
plugins: [ | ||
'@babel/plugin-transform-react-jsx', | ||
'@babel/plugin-proposal-object-rest-spread' | ||
] | ||
}).code | ||
|
||
const renderReact = async componentStr => { | ||
const jsx = await mdx(componentStr, { skipExport: true }) | ||
const component = transform(jsx) | ||
const scope = { mdx: createElement } | ||
|
||
/* eslint-disable-next-line */ | ||
const fn = new Function( | ||
'React', | ||
...Object.keys(scope), | ||
`${component}; return React.createElement(MDXContent)` | ||
) | ||
|
||
const element = fn(React, ...Object.values(scope)) | ||
|
||
// All components that we want accessible to markdown files must | ||
// be registered here | ||
const components = { | ||
BlueContent, | ||
RedContent, | ||
Timer | ||
} | ||
|
||
const elementWithProvider = React.createElement( | ||
MDXProvider, | ||
{ components }, | ||
element | ||
) | ||
|
||
return renderToString(elementWithProvider) | ||
} | ||
|
||
module.exports = renderReact |
Oops, something went wrong.