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

htmdx

v0.3.7

Published

<p align="center"> <img alt="HTMDX logo" src="./htmdx.svg" width="100" /> </p>

Downloads

713

Readme

HTMDX: Lightweight runtime for mdx-like markdown

This library is an attempt to provide a runtime to compile mdx-like markdown files (with the goal to support full JSX inside of markdown) using htm + marked that is much smaller in file-size as opposed to the official runtime (which we are not encouraged to use on actual websites).

Here is a simple example playground using HTMDX

Usage

Simple example:

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { htmdx } from 'htmdx';
import * as Prism from 'prismjs';

function SomeComponent() {
  return 'something';
}

const markDownWithJSX = `
  # Hello World

  <SomeComponent />

  Mardown will be interpreted as tagged templates from htm:
  <input type="text" style=\${{width: '100%'}} value=\${this.state.inputValue || ''} onChange=\${e => {this.setState({inputValue:e.target.value});console.log(e.target.value)}}/>
  We're also using the setState method and state property passed into using the thisValue options (see below)

  With the transformJSXToHTM option enabled, you may also use normal brackets:
  <input type="text" style={{width: '100%'}} value={this.state.inputValue || ''} onChange={e => this.setState({inputValue:e.target.value})}/>
  
  Here's some code with code highlighting:
  \`\`\`
  function SomeComponent() {
    return "Some component ouput.";
  }
  \`\`\`
`;

function App() {
  const [state, setState] = React.useState({});

  return htmdx(
    markDownWithJSX,
    React.createElement, // provide a h function. You can also use HTMDX with preact or any other library that supports the format
    {
      components: { SomeComponent }, // provide components that will be available in markdown files,
      configureMarked: (
        marked // configure the underlying marked parser, e.x.: to add code highlighting:
      ) =>
        marked.setOptions({
          highlight: function(code) {
            return Prism.highlight(
              code,
              Prism.languages.javascript,
              'javascript'
            ).replace(/\n/g, '<br/>');
          },
        }),
      transformClassToClassname: true, // transforms class="some-class" to className="some-class" (default: true)
      transformJSXToHTM: true, // transforms some JSX to htm template literal syntax (such as value={} to value=${}) (default: true)
      thisValue: {
        // the this value passed to the compiled JSX
        state,
        setState: newState => setState(Object.assign({}, state, newState)),
      },
    }
  );
}

ReactDOM.render(React.createElement(App), document.getElementById('root'));

Edit htmdx example

Pluggable MDX transforms

You can supply MDX transforms, which will be applied to mdx strings before anything else:

htmdx('# Hello World', React.createElement, {
  mdxTransforms: [m => m.replace('# Hello World', '# foo')], // will replace # Hello world with # foo
})

Pluggable JSX transforms

You can supply JSX transforms which allow you to apply further transforms before the JSX pragma runs, like so:

htmdx('# Hello World', React.createElement, {
  jsxTransforms: [
    (props, type, children) => {
      if (children && children[0] === 'Hello World') {
        children[0] = 'Foo'; // this will output <h1>Foo</h1> instead of <h1>Hello World</h1> now!
      }
      return [props, type, children];
    },
  ],
})

Overwriting normal generated html elements with components:

You can also provide components for basic html elements that are generated by the markdown compiler:

htmdx("# Hello World", React.createElement, {
  components: {
    TestComponent,
    h1: props =>
      html`
        <h1 style=${{ color: 'red' }}>${props.children}</h1>
      `,
  }
})