npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reactify-markdown

v0.2.4

Published

Markdown-It plugin that overwrites the renderer to produce React nodes rather than HTML text

Downloads

8

Readme

Reactify Markdown

A MarkdownIt plugin and corresponding React component that makes the renderer produce React Nodes instead of a string.

Every React/Markdown package I've found has rendered the source as text and then wrapped it in <div dangerouslySetInnerHTML={{__html: source}} />. This prevents you from creating rules and plugins that return controlled React components. It also produces a <div>, a block-level element, which can have undesired effects on layout.

Using custom rules that set token.content to a ReactNode allows you to render React components inline. As the env parameter of render() can pass arbitrary information, you can take advantage of this and pass component props and state into the markdown rendering itself.

Installation

npm install --save reactify-markdown react markdown-it

React and markdown-it are required peer dependencies.

Usage

Using just the plugin

import React from 'react';
import ReactDOM from 'react-dom';
import MarkdownIt from 'markdown-it';
import { RenderReactPlugin } from 'reactify-markdown';

const md = MarkdownIt();
md.use(RenderReactPlugin);

function MyComponent({ myMarkdownString }) { 
    return (<div>
        Here's my markdown!
        {md.render(myMarkdownString)}
    </div>);
}

ReactDOM.render(
    <MyComponent myMarkdownString="# Hello #" />, 
    document.getElementById('root')
);

Produces:

<div>
    Here's my markdown!
    <h1>Hello</h1>
</div>

With a custom rule

import React from 'react';
import ReactDOM from 'react-dom';
import MarkdownIt from 'markdown-it';
import mdiRegex from 'markdown-it-regex';
import { RenderReactPlugin } from 'reactify-markdown';

const md = MarkdownIt();
md.use(RenderReactPlugin);
md.use(mdiRegex, {
    name: 'myRule',
    regex: /:(\w+):/,
    replace: m => <i>{m}</i>
});

function MyComponent({ myMarkdownString }) { 
    return md.render(myMarkdownString);
}

ReactDOM.render(
    <MyComponent myMarkdownString="Hay :needle: stack" />, 
    document.getElementById('root')
);

Produces:

<p>
    Hay <i>needle</i> stack
</p>

Using the React Component

The same outputs as above can be produced using:

import React from 'react';
import ReactDOM from 'react-dom';
import ReactifyMarkdown from 'reactify-markdown';

ReactDOM.render(
    <div>
        Here's my markdown!
        <ReactifyMarkdown>
            # Hello #
        </ReactifyMarkdown>
    </div>,
    document.getElementById('root')
);

With a custom rule

import React from 'react';
import ReactDOM from 'react-dom';
import mdiRegex from 'markdown-it-regex';
import ReactifyMarkdown from 'reactify-markdown';

const myRule = [
    mdiRegex, 
    {
        name: 'myRule',
        regex: /:(\w+):/,
        replace: m => <i>{m}</i>
    }
];

ReactDOM.render(
    <ReactifyMarkdown plugins={[myRule]}>Hay :needle: stack</ReactifyMarkdown>,
    document.getElementById('root')
);

<ReactifyMarkdown> props

  • children: string - The markdown source string to be parsed and rendered.
  • enable?: string | string[] - Optional rule name or list of rule names to enable.
  • disable?: string | string[] - Optional rule name or list of rule names to disable.
  • env?: any - Arbitrary data that is passed into the renderer's rules.
  • dontStripIndent?: boolean (default false) - By default, the component will strip preceding spaces via strip-indent. Set this flag to disable this behavior.
  • md?: MarkdownIt - A preconfigured markdown it instance. If md is set, all of the following values are ignored.
  • options?: MarkdownIt.Options - See https://markdown-it.github.io/markdown-it/#MarkdownIt.new.
  • plugins?: Plugin[] - A list of plugins to apply to the markdown instance. Each element is passed directly as arguments to md.use(). See https://markdown-it.github.io/markdown-it/#MarkdownIt.use.
  • presetName?: 'commonmark' | 'zero' | 'default' (default 'default') - See https://markdown-it.github.io/markdown-it/#MarkdownIt.new.

TODO

  • [ ] Better documentation.
  • [ ] Allow mixed strings/elements children in the <ReactifyMarkdown> component.
  • [ ] Better test coverage.