How to integrate remark-directive into react-markdown

See original GitHub issue

Here 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:closed
  • Created 2 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
IGassmanncommented, Nov 15, 2021

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

1reaction
rjantoinecommented, May 5, 2021

Looking at my sandbox, everything seems to be working correctly. The idea is:

  1. Use :CustomComponent[value]{prop:value} in your markdown code
  2. Add the reactMarkdownRemarkDirective() code, as is, into your code in order to catch those directives and render them using a custom component.
  3. Create a component called CustomComponent, or MyDirective, or whatever follows the colon in the markdown.
  4. Include this component in the list of components for ReactMarkdown.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found