All Projects → michael-klein → htmdx

michael-klein / htmdx

Licence: MIT license
HTMDX: Lightweight runtime for mdx-like markdown

Programming Languages

typescript
32286 projects
HTML
75241 projects

Projects that are alternatives of or similar to htmdx

website
Personal website and blog built with Next.js, Preact, MDX, Tailwind CSS and hosted on Vercel.
Stars: ✭ 17 (-79.27%)
Mutual labels:  preact, mdx
personal-website
Personal website – made with Next.js, Preact, MDX, RMWC, & Vercel
Stars: ✭ 16 (-80.49%)
Mutual labels:  preact, mdx
esri-preact-pwa
An example progressive web app (PWA) using the ArcGIS API for JavaScript built with Preact
Stars: ✭ 13 (-84.15%)
Mutual labels:  preact
coderplex
(Old) Frontend for coderplex.org
Stars: ✭ 40 (-51.22%)
Mutual labels:  preact
preact-mdc
material design components for preact using material-components-web sass styles (for live demo click the link below)
Stars: ✭ 23 (-71.95%)
Mutual labels:  preact
parcel-examples
Parcel project examples for React, Vue, preact, VanillaJS and jQuery.
Stars: ✭ 26 (-68.29%)
Mutual labels:  preact
mdxtools
A bunch of tools for handling the MDX music format (music for the Sharp x68000)
Stars: ✭ 44 (-46.34%)
Mutual labels:  mdx
WorkerStore
Small React state container running inside WebWorker
Stars: ✭ 32 (-60.98%)
Mutual labels:  preact
relaks
Asynchrounous React component
Stars: ✭ 49 (-40.24%)
Mutual labels:  preact
next-mdx-frontmatter
Use MDX + front-matter with Next.js
Stars: ✭ 27 (-67.07%)
Mutual labels:  mdx
universal-progressive-todos
A Todo list with universal JavaScript & Progressive Enhancement
Stars: ✭ 30 (-63.41%)
Mutual labels:  preact
anghamify
Anghami Downloader | Download Anghami songs with full meta-tags.
Stars: ✭ 22 (-73.17%)
Mutual labels:  preact
fireworks-js
🎆 A simple fireworks library! Ready to use components available for React, Vue 3, Svelte, Angular, Preact, Solid, and Web Components.
Stars: ✭ 550 (+570.73%)
Mutual labels:  preact
playkit-js-ui
UI Application Framework for the PlayKit JS Player
Stars: ✭ 25 (-69.51%)
Mutual labels:  preact
revite
Revolt client built with Preact.
Stars: ✭ 606 (+639.02%)
Mutual labels:  preact
robinjs-website
Alexa like assistant in 40 lines of code
Stars: ✭ 31 (-62.2%)
Mutual labels:  preact
snap-state
State management in a snap 👌
Stars: ✭ 23 (-71.95%)
Mutual labels:  preact
macos-preact
macos-preact.now.sh
Stars: ✭ 1,019 (+1142.68%)
Mutual labels:  preact
pretty-checkbox-react
A tiny react/preact wrapper around pretty-checkbox
Stars: ✭ 35 (-57.32%)
Mutual labels:  preact
preact-token-input
🔖 A text field that tokenizes input, for things like tags.
Stars: ✭ 57 (-30.49%)
Mutual labels:  preact

HTMDX logo

HTMDX: Lightweight runtime for mdx-like markdown

build badge npm

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>
      `,
  }
})
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].