Skip to content

Commit

Permalink
[Breaking Change] Default export is now a React HOC
Browse files Browse the repository at this point in the history
See the README for updated usage instructions.
  • Loading branch information
Evan Jacobs committed Jan 12, 2017
1 parent 573021c commit 38efb41
Show file tree
Hide file tree
Showing 3 changed files with 848 additions and 761 deletions.
71 changes: 52 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,53 @@ Requires React >= 0.14.

## Usage

The default export function signature:

```js
compiler(markdown: string, options: object?)
```
`markdown-to-jsx` exports a React component by default for easy JSX composition (since version v5):

ES6-style usage:

```js
import compiler from 'markdown-to-jsx';
```jsx
import Markdown from 'markdown-to-jsx';
import React from 'react';
import {render} from 'react-dom';

render(compiler('# Hello world!'), document.body);
render((
<Markdown>
# Hello world!
</Markdown>
), document.body);

/*
renders:
<h1>Hello world!</h1>
*/
```

Override a particular HTML tag's output:

```jsx
import compiler from 'markdown-to-jsx';
import Markdown from 'markdown-to-jsx';
import React from 'react';
import {render} from 'react-dom';

// surprise, it's a div instead!
const MyParagraph = ({children, ...props}) => (<div {...props}>{children}</div>);

render(
compiler('# Hello world!', {
overrides: {
h1: {
component: MyParagraph,
props: {
className: 'foo',
render((
<Markdown
options={{
overrides: {
h1: {
component: MyParagraph,
props: {
className: 'foo',
},
},
},
},
}), document.body
);
}}>
# Hello world!
</Markdown>
), document.body);

/*
renders:
Expand All @@ -75,4 +84,28 @@ Depending on the type of element, there are some props that must be preserved to

Any conflicts between passed `props` and the specific properties above will be resolved in favor of `markdown-to-jsx`'s code.

## Using the compiler directly

If desired, the compiler function is a "named" export on the `markdown-to-jsx` module:

```jsx
import {compiler} from 'markdown-to-jsx';
import React from 'react';
import {render} from 'react-dom';

render(compiler('# Hello world!'), document.body);

/*
renders:
<h1>Hello world!</h1>
*/
```

It accepts the following arguments:

```js
compiler(markdown: string, options: object?)
```
MIT
24 changes: 21 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {PropTypes} from 'react';
import get from 'lodash.get';
import unified from 'unified';
import parser from 'remark-parse';
Expand Down Expand Up @@ -400,7 +400,7 @@ function coalesceInlineHTML(ast) {
return ast.children.forEach(coalescer);
}

export default function markdownToJSX(markdown, {overrides = {}} = {}) {
export function compiler(markdown, {overrides = {}} = {}) {
let definitions;
let footnotes;

Expand Down Expand Up @@ -610,4 +610,22 @@ export default function markdownToJSX(markdown, {overrides = {}} = {}) {
}

return jsx;
}
};

/**
* A simple HOC for easy React use. Feed the markdown content as a direct child
* and the rest is taken care of automatically.
*
* @param {String} options.children must be a string
* @param {Object} options.options markdown-to-jsx options (arg 2 of the compiler)
*
* @return {ReactElement} the compiled JSX
*/
const Component = ({children, options, ...props}) => compiler(children, options);

Component.propTypes = {
children: PropTypes.string.isRequired,
options: PropTypes.object,
};

export default Component;
Loading

0 comments on commit 38efb41

Please sign in to comment.