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

mtj-react

v0.0.2

Published

A React component for rendering Markdown using mtj-parser

Downloads

5

Readme

MTJ-React

The Markdown-to-JSON React component

Very simply, this is a react component that accepts a string of markdown-styled text, and renders it into nested React components, though you only need to be aware of the single exported component.

The motivation to create this library is a selfish one, in that I don't trust other parsers that convert Markdown into HTML. There are two steps:

  1. Create a parser that turns Markdown into a nested (tree-based) representation to best capture the nested nature of the styling. Because I couldn't even find something like that, I implemented my own: mtj-parser. It wraps markdown-it (which I had no hand in), converting its flat representation into a more usable JSON. It's worth noting that markdown-it's intermediate format is better for directly creating HTML (which is what it's for), but for React, it's better to have a tree.

  2. Take the tree form of a markdown document and recursively wrap the nodes in React components, which is all that this library exists to do. Theoretically, the design of this library could be such that making live edits to the Markdown document could be optimized by only having to rerender the sections of the markdown tree that have changed and leave the rest alone, but that's a stretch goal for this project (which already does it's job pretty quickly).

That's all there is to it. By using this library, I can guarantee that no documents have injectable HTML (which means no injectable script tags) because it's not an output produced by the parser. I'm not sure if there are other ways that scripts can be injected into the page besides that, but I will certainly try to find out.

Install

npm install mtj-react

How-to

The Markdown component, which is the only exported element, takes two required props:

interface OwnProps {
  // Used to style it based on the context
  customClass: string;

  // The source from which it builds the article
  markdown: string;
}

The customClass is added to the base component as a classname along with marked, so that multiple documents can exist on the same page and receive different styling.

The markdown prop is literally just a string containing Markdown-formatted text.

Usage:

import { Markdown } from 'mtj-react';

const MyComponent = ({ markdownText: string }) => (
  <Markdown customClass='project-page' src={markdownText} />
);