-
-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to create custom component #509
Comments
It's a little complicated, but not impossible.
previewOptions={{
rehypeRewrite={(node, index, parent) => {
// here ....
}}
}}
previewOptions={{
div: (props) => {
if (props['data-MyComponent']) {
return <MyCustomComponent />
}
return <div style={{ color: "red" }} {...props} />;
},
}} react-md-editor/www/src/Markdown.tsx Lines 9 to 44 in 41abfaa
|
Thanks for the answer, but I could not make any sense of it could you please explain a little more or better of give me an example? |
@SarkarKurdish https://codesandbox.io/embed/markdown-editor-for-react-https-github-com-uiwjs-react-md-editor-issues-509-4qqpc6?fontsize=14&hidenavigation=1&theme=dark Wrote a basic example, hope you can improve it import React from "react";
import ReactDOM from "react-dom";
import MDEditor from "@uiw/react-md-editor";
const MyComponent = () => {
return <div>这是测试</div>;
};
const mkdStr = `
**Hello world**
<MyComponent />
<MyComponent></MyComponent>
test
`;
function App() {
const [value, setValue] = React.useState(mkdStr);
return (
<div className="container">
<MDEditor
height={200}
previewOptions={{
rehypeRewrite: (node, index, parent) => {
if (node.type === "root") {
console.log("node:", node);
}
if (node.tagName === "mycomponent") {
node.properties["data-mycomp"] = node.tagName;
node.tagName = "div";
if (node.children.lenght > 0) {
// ---> <MyComponent />
} else {
// ==> <MyComponent></MyComponent>
}
}
},
components: {
div: ({ node, ...props }) => {
const { "data-mycomp": tagName } = props;
console.log("node0:", tagName);
if (tagName === "mycomponent") {
return <MyComponent />;
}
console.log("node1:", node);
return <div {...props} />;
}
}
}}
value={value}
onChange={setValue}
/>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("container")); |
@jaywcjlove Thanks for your help, it's just too complicated to work with in production and as the project gets bigger I don't think this way will scale very well, I ended up using This is my code
and this is a markdown example
The result is what I've expected the component get's rendered and there is no need to any hacky stuff |
I did the same thing but in my example node.type coming "raw". And It doesn't have the attributes like tagName and properties |
hey, in
EditerMarkdown
there arecomponents
option that allow you to customize how each element should get renderedlike this
I tired to a custom component so in the editor I can use this like this
also i've changed the editor code to this
It neither worked nor have any error and in the preview nothing was rendered for the custom component, is there any way to define a custom component?
The text was updated successfully, but these errors were encountered: