Socket
Socket
Sign inDemoInstall

@christianmurphy/react-remark

Package Overview
Dependencies
56
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @christianmurphy/react-remark

Renders Markdown as React components


Version published
Weekly downloads
2
increased by100%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

react-remark

CI

react-remark offers a React hook and React component based way of rendering markdown into React using remark

Installation

npm

npm install --save @christianmurphy/react-remark

yarn

yarn add @christianmurphy/react-remark

Usage

As a hook

Render static content
import React from 'react';
import { useRemark } from '@christianmurphy/react-remark';

const ExampleComponent = () => {
  const [reactContent, setMarkdownSource] = useRemark();

  setMarkdownSource('# markdown header');

  return reactContent;
};

export default ExampleComponent;
Using input and events to update
import React from 'react';
import { useRemark } from '@christianmurphy/react-remark';

const ExampleComponent = () => {
  const [reactContent, setMarkdownSource] = useRemark();

  return (
    <>
      <input
        type="text"
        onChange={({ currentTarget }) => setMarkdownSource(currentTarget.value)}
      />
      {reactContent}
    </>
  );
};

export default ExampleComponent;

More examples of usage as hook in storybook.

As a component

Render static content
import React, { useState } from 'react';
import { Remark } from '@christianmurphy/react-remark';

const ExampleComponent = () => (
  <Remark>{`
# header

1. ordered
2. list
`}</Remark>
);

export default ExampleComponent;
Using input and events to update
import React, { useState } from 'react';
import { Remark } from '@christianmurphy/react-remark';

const ExampleComponent = () => {
  const [markdownSource, setMarkdownSource] = useState('');

  return (
    <>
      <input
        type="text"
        onChange={({ currentTarget }) => setMarkdownSource(currentTarget.value)}
      />
      <Remark>{markdownSource}</Remark>
    </>
  );
};

export default ExampleComponent;

More examples of usage as component in storybook.

Examples

A set of runnable examples are provided through storybook at https://christianmurphy.github.io/react-remark. The source for the story files can be found in /stories.

Architecture

                                                             react-remark
+---------------------------------------------------------------------------------------------------------------------------------------------+
|                                                                                                                                             |
|            +----------+        +----------------+        +---------------+       +----------------+       +--------------+                  |
|            |          |        |                |        |               |       |                |       |              |                  |
| -markdown->+  remark  +-mdast->+ remark plugins +-mdast->+ remark-rehype +-hast->+ rehype plugins +-hast->+ rehype-react +-react elements-> |
|            |          |        |                |        |               |       |                |       |              |                  |
|            +----------+        +----------------+        +---------------+       +----------------+       +--------------+                  |
|                                                                                                                                             |
+---------------------------------------------------------------------------------------------------------------------------------------------+

relevant links: markdown, remark, mdast, remark plugins, remark-rehype, hast, rehype plugins, rehype-react

Options

  • remarkParseOptions (Object) - configure how Markdown is parsed, same as remark-parse options
  • remarkPlugins (Array) - remark plugins or custom plugins to transform markdown content before it is translated to HTML (hast)
  • remarkToRehypeOptions (Object) - configure how Markdown (mdast) is translated into HTML (hast), same as remark-rehype options
  • rehypePlugins (Array) - rehype plugins or custom plugins to transform HTML (hast) before it is translated to React elements.
  • rehypeReactOptions (Object) - configure how HTML (hast) is translated into React elements, same as rehype-react options

Pass options to hook

import React, { Fragment } from 'react';
import { useRemark } from '@christianmurphy/react-remark';
import remarkGemoji from 'remark-gemoji';
import rehypeAutoLinkHeadings from 'rehype-autolink-headings';

// ...

const [reactContent, setMarkdownSource] = useRemark({
  remarkParseOptions: { commonmark: true },
  remarkPlugins: [remarkGemoji],
  remarkToRehypeOptions: { commonmark: true },
  rehypePlugins: [rehypeAutoLinkHeadings],
  rehypeReactOptions: {
    components: {
      p: props => <p className="custom-paragraph" {...props} />,
    },
  },
});

Pass options to component

import React, { Fragment } from 'react';
import { useRemark } from '@christianmurphy/react-remark';
import remarkGemoji from 'remark-gemoji';
import rehypeAutoLinkHeadings from 'rehype-autolink-headings';

// ...

<Remark
  remarkParseOptions={{ commonmark: true }}
  remarkPlugins={[remarkGemoji]}
  remarkToRehypeOptions={{ commonmark: true }}
  rehypePlugins={[rehypeAutoLinkHeadings]}
  rehypeReactOptions={{
    components: {
      p: props => <p className="custom-paragraph" {...props} />,
    },
  }}
>
  {markdownSource}
</Remark>;

FAQs

Last updated on 29 Sep 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc