How to integrate remark-directive into react-markdown
See original GitHub issueHere is an example on how to integrate remark-directive with react-markdown. You can include it in your readme if you like.
import ReactMarkdown from "react-markdown";
import directive from "remark-directive";
import visit from "unist-util-visit";
import "./styles.css";
function reactMarkdownRemarkDirective() {
return (tree) => {
visit(
tree,
["textDirective", "leafDirective", "containerDirective"],
(node) => {
node.data = {
hName: node.name,
hProperties: node.attributes,
...node.data
};
return node;
}
);
};
}
function MyDirective({node, ...props}) {
return (
<span className="doSomethingCustom" {...props} />
);
}
export default function App() {
return (
<div className="App">
<ReactMarkdown
remarkPlugins={[directive, reactMarkdownRemarkDirective]}
components={{ MyDirective }}
children=":MyDirective[Content]{#me}"
/>
</div>
);
}
Here is a working version on CodeSandbox
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
react-markdown-remark-directive - npm
Start using react-markdown-remark-directive in your project by running `npm i react-markdown-remark-directive`. There are no other projects ...
Read more >React Markdown Remark Directive - npm.io
This is a plugin for react-markdown. It uses the directive format to easily add custom functionality into your markdown via remark-directive.
Read more >React-Markdown Custom Component Declaration, How Can I ...
Then in react markdown you can specify the plugins, ... import directive from 'remark-directive' import { MyCustomComponent } from '.
Read more >How to safely render Markdown using react-markdown
react-markdown is a React component that converts Markdown text into the corresponding HTML code. It is built on remark, which is a Markdown ......
Read more >remark - Topics - unified
Explore projects in the unified ecosystem with the “remark” topic. ... remarkjs/react-markdown ... plugin to embed local images as data URIs.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve built a package for this use case: https://github.com/IGassmann/remark-directive-rehype. Check out the react-markdown example for how to use it
Looking at my sandbox, everything seems to be working correctly. The idea is:
You can use my code exactly, you would just have to customize what is inside of MyDirective to insert your custom feature. You can even use it to insert bootstrap by using :Button[value] for example in your markdown, and include the Button component inside of the ReactMarkdown components array.