GithubHelp home page GithubHelp logo

nadnad / rehype-react Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rehypejs/rehype-react

0.0 2.0 0.0 135 KB

Rehype compiler plugin to convert HAST into React virtual DOM

Home Page: https://github.com/wooorm/rehype

License: MIT License

JavaScript 100.00%

rehype-react's Introduction

rehype-react

Build Status

Compiles HAST to React with rehype.

Install

npm:

npm install rehype-react

Use

The following example shows how to create a markdown input textarea, and corresponding rendered HTML output. The markdown is processed to add a Table of Contents and to render GitHub mentions (and other cool GH features), and to highlight code blocks.

var React = require('react');
var ReactDOM = require('react-dom');
var unified = require('unified');
var markdown = require('remark-parse');
var toc = require('remark-toc');
var github = require('remark-github');
var remark2rehype = require('remark-rehype');
var highlight = require('rehype-highlight');
var rehype2react = require('rehype-react');

var processor = unified()
  .use(markdown)
  .use(toc)
  .use(github, {
    repository: 'https://github.com/rhysd/rehype-react'
  })
  .use(remark2rehype)
  .use(highlight)
  .use(rehype2react, {
    createElement: React.createElement
  });

var App = React.createClass({
  getInitialState() {
    return {text: '# Hello\n\n## Table of Contents\n\n## @rhysd'};
  },
  onChange(ev) {
    this.setState({text: ev.target.value});
  },
  render() {
    return (<div>
      <textarea
        value={this.state.text}
        onChange={this.onChange} />
      <div id='preview'>
        {processor.processSync(this.state.text).contents}
      </div>
    </div>);
  }
});

ReactDOM.render(<App />, document.getElementById('app'));

Yields (in id="preview", on first render):

<div><h1 id="hello">Hello</h1>
<h2 id="table-of-contents">Table of Contents</h2>
<ul>
<li><a href="#rhysd">@rhysd</a></li>
</ul>
<h2 id="rhysd"><a href="https://github.com/rhysd"><strong>@rhysd</strong></a></h2></div>

Programmatic

origin.use(rehype2react[, options])

Normally, the used on processor compiles to a string, but this compiler generates a ReactElement instead. It’s accessible through file.contents.

options
  • createElement (Function, required) — Function to use to create ReactElements;
  • components (Object, default: {}) — Register components;
  • prefix (string, default: 'h-') — Prefix for key to use on generated ReactElements.

License

MIT © Titus Wormer, modified by Tom MacWright and Mapbox and rhysd.

rehype-react's People

Contributors

bebraw avatar ciaranw avatar jstcki avatar kthjm avatar makenowjust avatar rhysd avatar sapegin avatar tmcw avatar wooorm avatar wreulicke avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.