Using Custom Components in MDX - mdx-js/react #1236
-
the goal is to use Custom Components with MDX i am try to use mdx-js/react with this example: https://github.com/remarkjs/remark-react#use import React from 'react'
import ReactDom from 'react-dom'
import unified from 'unified'
import parse from 'remark-parse'
import remark2react from 'remark-react'
class App extends React.Component {
constructor() {
super()
this.state = { text: '# hello world\nthis is <Bold>bold</Bold> text' }
this.onChange = this.onChange.bind(this)
}
onChange(e) {
this.setState({ text: e.target.value })
}
render() {
return (
<div>
<textarea value={this.state.text} onChange={this.onChange} />
<div id="preview">
{
unified()
.use(parse)
.use(remark2react)
.processSync(this.state.text).result
}
</div>
</div>
)
}
}
ReactDom.render(<App />, document.getElementById('root')) and replace the part compile part: unified()
.use(parse)
.use(remark2react)
.processSync(this.state.text).result but i get it not working somehow ... Way 1having some unified processor: .use(parse)
.use(remarkMdx)
.use(remark2react, { sanitize:false, remarkReactComponents: { bold: () => <div>ABC</div>)}, toHast: { handlers: { html: ... } } })
.processSync(this.state.text).result but then i have 2x the Way 2replacing it with <MDX components={components}>{this.state.text}<MDX> i get always a:
Solution?i guess there is an easy solution, but i don't get it. what i am looking for is having 2 ways:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
ok - i lost so many times because of wrong import results in the error // ok
import MDXRuntime from "@mdx-js/runtime"; // wrong
const MDXRuntime = require('@mdx-js/runtime') i added: <MDXRuntime components={components} scope={scope}>
{this.state.text}
</MDXRuntime> |
Beta Was this translation helpful? Give feedback.
-
i see there is a problem, when i type in some unfinished html: in the moment i type
|
Beta Was this translation helpful? Give feedback.
-
@muescha you're asking a few different semi-related questions here: Question 1: how to use the MDX compiler programmatically
This doesn't support MDX syntax because you aren't using MDX Question 2: how to use MDX as a React component in the Runtime
I'm not sure what you have set to Question 3: how to handle incomplete JSX with MDX Runtime
A couple notes, it isn't HTML, it's JSX, which has stricter more XML like parsing restrictions, the error is correct the content is currently invalid. Question 4: How to include custom componentsUsing the |
Beta Was this translation helpful? Give feedback.
-
thanks a lot for reply i expected somehow that not valid html is ignored like in this playground: https://probablyup.com/markdown-to-jsx/ text is rendered normal until your html is valid i can write there for example this code for the custom Component # This is Markdown
#### You can edit me!
<MyComponent>and more</MyComponent>
and
<MyComponent>this</MyComponent
[Markdown](http://daringfireball.net/projects/markdown/) lets you write content in a really natural way. (Note: the missing |
Beta Was this translation helpful? Give feedback.
@muescha you're asking a few different semi-related questions here:
Question 1: how to use the MDX compiler programmatically
This doesn't support MDX syntax because you aren't using MDX
See https://mdxjs.com/advanced/api#compiler on how to use the compiler programmatically.
Question 2: how to use MDX as a React component in the Runtime
I'm not sure what you have set to
MDX
in the above example.Runtime is the way to parse and transform directly in t…