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

@atomic-reactor/markdown-to-jsx

v0.0.27

Published

Component that takes plain text markdown and parses it into JSX

Downloads

47

Readme

Component that takes plain text markdown and parses it into JSX.

Install

npm install --save @atomic-reactor/markdown-to-jsx

Usage

import React, { useMemo } from 'react';
import { MarkdownJSX } from '@atomic-reactor/markdown-to-jsx';

const MyComponent = () => {
    const markdownText = useMemo(() => (`
        something

        # Page Heading

        - List Item 1
        - List Item 2
        - List item 3

        Some other stuffs
    `), []);

    return <MarkdownJSX value={markdownText} />
};

Properties

| Property | Type | Description | Default | | ------------------ | ---------- | ---------------------------------------------------------------------------- | ------- | | value | String | Required. The plain-text to be parsed to JSX | | | className | String | Space-delimited classes to add to wrapper (ignored if renderInWrapper=false) | | | renderInWrapper | Boolean | If true, the HTML output will have a wrapper | false | | renderUnrecognized | Function | Unrecognized tags are rendered via this method | |

Static Properties

| Property | Type | Description | | ---------------------- | ---------- | ----------------------------------------------------------------------------------- | | MarkdownJSX.preparsers | Registry | Registry of string replacers to apply before the value has been converted to markup | | MarkdownJSX.replacers | Registry | Registry of string replacers to apply after the value has been converted to markup |

Note: The preparsers and replacers registries are identical in functionality.

MarkdownJSX.preparsers

By default the preparsers registry is empty. Registered string replacers are applied before the value has been converted to markup. This can be useful for validating or preformatting the input text.

MarkdownJSX.preparsers.register('something', {
    match: /something/gim,
    replace: 'Somethings',
});

MarkdownJSX.replacers

By default the replacers registry is empty. Registered string replacers are applied after the value has been converted to markup. This can be useful for customizing the markup with inline styling or class names as well as modifying which tags are being used for specific markup.

Note: The initial generated markup will never had any attributes applied to the elements.

MarkdownJSX.replacers.register('heading', {
    match: /<h([1-6])>(.+)<\/h[1-6]>/gim,
    replace: '<h$1><span>$2</span></h$1>',
});
Replace as a function

You can pass a function as the replace value to gain more control over the output of a preparser or replacer.

MarkdownJSX.replacers.register('heading', {
    match: /<h([1-6])>(.+)<\/h[1-6]>/gim,
    replace: (...args) => {
        const [, tag, text] = args;
        const id = String(text).replace(/\W/g, '-').toLowerCase();
        return `
        <h${tag} id='${id}'>
            <a href='#${id}'>
                ${text}
            </a>
        </h${tag}>
    `;
    },
});

Removing A Registered Parsers

You can remove any registered preparser or replacer.

// Remove a single parser
MarkdownJSX.replacers.unregister('heading');

// Remove multiple parsers
MarkdownJSX.replacers
    .unregister('heading')
    .unregister('foobar');

// Remove all parsers
MarkdownJSX.replacers.flush();

Replacing A Registered Parser

You can overwrite any registered preparser or replacer. The last in, first out rule is applied on the registry.

// input:
// Something is fooed!

MarkdownJSX.preparsers.register('foo', {
    match: / foo/gi,
    replace: ' fubar'
});

MarkdownJSX.preparsers.register('foo', {
    match: / foo/gi,
    replace: ' foobar'
});

// output:
// Something is foobared!

Protect A Registered Parser

You can ensure that a parser isn't unregistered or replaced by adding it to the protected list.

// create and protect the 'heading' replacer
MarkdownJSX.replacers
    .register('heading', {
        match: /<h([1-6])>(.+)<\/h[1-6]>/gim,
        replace: '<h$1><span>$2</span></h$1>',
    })
    .protect('heading');

// try to unregister the 'heading' replacer
MarkdownJSX.replacers.unregister('heading');

// Is it gone? Nope!
console.log('Gone?', !MarkdownJSX.replacers.get('heading'));

// unprotect the 'heading' replacer and unregister it
MarkdownJSX.replacers.unprotect('heading').unregister('heading');

// Is it gone now? Yep!
console.log('Gone?', !MarkdownJSX.replacers.get('heading'));

MarkdownJSXParser()

If you need to parse markdown without rendering it you can import MarkdownJSXParser.

import { MarkdownJSXParser } from @atomic-reactor/markdown-to-jsx

const markdown = MarkdownJSXParser('# Something');

console.log(markdown);

// output:
// <h1>Something</h1>