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

mdast-react-render

v1.2.0

Published

render mdast nodes according to a schema

Downloads

15

Readme

Mdast React Render

A util to render an MDAST tree accroding to a schema.

API

rule object

{
  matchMdast: fn(mdast, index, parent): Boolean,
  props: fn(mdast, index, parent, {ancestors}): Object,
  rules: [rule],
  isVoid: Boolean,
  component: ReactComponent
}
  • matchMdast Return true if the rule should render the mdast node.
  • props Extract props from mdast to be passed to component. Additional context is available from the ordered ancestors array—index zero is equal to the parent.
  • rules Optional array of sub rules to render children with. Defaults to recursively visit children.
  • isVoid Skip rendering children. Default false.
  • component The React component to render.

renderMdast

import { renderMdast } from 'mdast-react-render'

renderMdast(mdast, schema, options): ReactElement | [ReactElement]

mdast: Mdast | [Mdast]
The mdast tree to render.

schema.rules: [rule]
Rules to render with

options.MissingNode: false | ReactComponent
A component to display when no rules matches. You can also pass false to throw if there is an unhandled mdast node. The component receives an node, index, parent and ancestors prop.

options.ancestors: undefined | [Mdast]
If you're rendering a sub tree you should pass the ancestors via options. The immediate parent is expected at index 0. Used for matchMdast (immediate parent) and props (parent and ancestors).

renderEmail

import { renderMdast } from 'mdast-react-render/lib/email'

renderEmail(mdast, schema, options): HtmlString

A drop in replacement for rendering emails. Params like renderMdast with following addition:

options.doctype: String
Defaults to a xhtml1-transitional doctype string.

Renders with renderMdast, stringifies with renderToStaticMarkup ReactDOMServer.renderToStaticMarkup, add a doctype and supports Outlook conditional comments via the mso component.

<Mso>

A dangerous helper for Outlook conditional comments.

Props:

  • gte: String
    a optional gte conition, usually '15'
  • children: String
    a dangerous html string to be between the conditional comment.

Usage:

import { Mso } from 'mdast-react-render/lib/email'

<Mso>
  {`
  <style>
    img {
      width:800px !important;
    }
  </style>
  `}
</Mso>

General Note

The schema and rule objects can and should be enriched with arbitrary additional data your app might need e.g. initiate an editor for the schema.

Utils

The packages also includes a suite of utils useful for writing your schemas.

  • matchType(type: String): matchMdastFn
  • matchHeading(depth: Number): matchMdastFn
  • matchZone(identifier: String): matchMdastFn
    zone is a custom mdast node type
  • matchParagraph: matchMdastFn
  • matchImage: matchMdastFn
  • matchImageParagraph: matchMdastFn
    A paragraph with one image child
  • imageSizeInfo(url: string): null | {width, height}
    Parses an url with an ?size=${width}x${height} suffix
  • imageResizeUrl(url: string, size: string)
    Appends a resize query param with size as value
import { matchImageParagraph } from 'mdast-react-render/lib/utils'

Example

Try it in your browser.

import assert from 'assert'
import React from 'react'
import Enzyme, { shallow } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'

import { renderMdast } from 'mdast-react-render'
import { matchType, matchHeading, matchParagraph } from 'mdast-react-render/lib/utils'

Enzyme.configure({ adapter: new Adapter() })

const mdast = {
  'type': 'root',
  'children': [
    {
      'type': 'heading',
      'depth': 1,
      'children': [{
        'type': 'text',
        'value': 'The Titel'
      }]
    },
    {
      'type': 'paragraph',
      'children': [{
        'type': 'text',
        'value': '«A good lead.»'
      }]
    }
  ]
}


const schema = {
  rules: [
    {
      matchMdast: matchType('root'),
      component: ({ children }) => <div>{children}</div>,
      rules: [
        {
          matchMdast: matchHeading(1),
          component: ({ children }) => <h1>{children}</h1>
        },
        {
          matchMdast: matchParagraph,
          component: ({ children }) => <p>{children}</p>
        }
      ]
    }
  ]
}

assert.doesNotThrow(() => {
shallow(
  renderMdast(mdast, schema, {MissingNode: false})
)
})

Example Email

import assert from 'assert'
import React from 'react'

import { renderMdast, Mso } from 'mdast-react-render/lib/email'
import { matchType } from 'mdast-react-render/lib/utils'

const schema = {
  rules: [
    {
      matchMdast: matchType('root'),
      component: ({ children }) => (
        <html>
          <head>
            <Mso gte='15'>
              {`
              <xml>
                <o:officedocumentsettings>
                  <o:allowpng />
                  <o:pixelsperinch>96</o:pixelsperinch>
                </o:officedocumentsettings>
              </xml>
              `}
            </Mso>
          </head>
          <body>
            {children}
          </body>
        </html>
      )
    }
  ]
}

const mdast = {
  'type': 'root',
  'children': []
}

let emailHtml
assert.doesNotThrow(() => {
  emailHtml = renderEmail(mdast, schema, {MissingNode: false})
})

assert.notEqual(
  emailHtml.indexOf('<!--[if gte mso 15]>'),
  -1,
  'transforms <Mso gte=\'15\'> into html comment'
)