-
Notifications
You must be signed in to change notification settings - Fork 60.3k
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
15 changed files
with
32,677 additions
and
619 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
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,30 @@ | ||
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 => { | ||
let componentName | ||
if (componentStr.trim().startsWith('<BlueContent')) componentName = 'bluecontent' | ||
if (componentStr.trim().startsWith('<RedContent')) componentName = 'redcontent' | ||
if (componentStr.trim().startsWith('<Timer')) componentName = 'timer' | ||
|
||
const jsx = `<div className="react-component-${componentName}">\n${componentStr}\n</div>` | ||
const component = transform(jsx) | ||
|
||
return renderToString(eval(component)) | ||
} | ||
|
||
module.exports = renderReact |
Oops, something went wrong.