Skip to content

Commit 1c014e6

Browse files
docs(readme): prettify file, add CodeSandbox link, tidy examples
- Prettify Markdown file with prettier - Reorganize sections - Tidy and refactor examples and code blocks - Add link to CodeSandbox demo - Update links
1 parent ba78f1b commit 1c014e6

File tree

1 file changed

+80
-82
lines changed

1 file changed

+80
-82
lines changed

README.md

Lines changed: 80 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,41 @@
77
[![Coverage Status](https://coveralls.io/repos/github/remarkablemark/html-react-parser/badge.svg?branch=master)](https://coveralls.io/github/remarkablemark/html-react-parser?branch=master)
88
[![Dependency status](https://david-dm.org/remarkablemark/html-react-parser.svg)](https://david-dm.org/remarkablemark/html-react-parser)
99

10-
An HTML to React parser that works on the server and the browser:
10+
An HTML to React parser that works on both the server and the browser:
11+
1112
```
1213
HTMLReactParser(htmlString[, options])
1314
```
1415

15-
It converts an HTML string to [React elements](https://facebook.github.io/react/docs/react-api.html#creating-react-elements).
16-
17-
There's also an [option](#options) to [replace](#replacedomnode) elements with your own custom React elements.
16+
The parser converts an HTML string to [React element(s)](https://reactjs.org/docs/react-api.html#creating-react-elements). If you want to [replace an element](#replacedomnode) with your own custom element, there's an [option](#options) to do that.
1817

19-
## Example
18+
Example:
2019

2120
```js
22-
var Parser = require('html-react-parser');
23-
Parser('<p>Hello, world!</p>');
24-
// same output as `React.createElement('p', {}, 'Hello, world!')`
21+
var parse = require('html-react-parser');
22+
parse('<div>text</div>'); // equivalent to `React.createElement('div', {}, 'text')`
2523
```
2624

27-
[JSFiddle](https://jsfiddle.net/remarkablemark/7v86d800/) | [repl.it](https://repl.it/@remarkablemark/html-react-parser)
25+
[CodeSandbox](https://codesandbox.io/s/940pov1l4w) | [JSFiddle](https://jsfiddle.net/remarkablemark/7v86d800/) | [repl.it](https://repl.it/@remarkablemark/html-react-parser)
26+
27+
See [usage](#usage) and more [examples](https://github.com/remarkablemark/html-react-parser/tree/master/examples).
2828

2929
## Installation
3030

3131
[NPM](https://www.npmjs.com/package/html-react-parser):
32+
3233
```sh
3334
$ npm install html-react-parser --save
3435
```
3536

36-
[Yarn](https://yarn.fyi/html-react-parser):
37+
[Yarn](https://yarnpkg.com/package/html-react-parser):
38+
3739
```sh
3840
$ yarn add html-react-parser
3941
```
4042

41-
[CDN](https://unpkg.com/html-react-parser/):
43+
[unpkg](https://unpkg.com/html-react-parser/) (CDN):
44+
4245
```html
4346
<!-- HTMLReactParser depends on React -->
4447
<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
@@ -48,56 +51,50 @@ $ yarn add html-react-parser
4851
</script>
4952
```
5053

51-
See more [examples](https://github.com/remarkablemark/html-react-parser/tree/master/examples).
52-
5354
## Usage
5455

55-
Given you have the following imported:
56+
Given you have `html-react-parser` imported:
57+
5658
```js
5759
// ES Modules
58-
import Parser from 'html-react-parser';
59-
import { render } from 'react-dom';
60+
import parse from 'html-react-parser';
6061
```
6162

62-
Render a single element:
63+
Parse single element:
64+
6365
```js
64-
render(
65-
Parser('<h1>single</h1>'),
66-
document.getElementById('root')
67-
);
66+
parse('<h1>single</h1>');
6867
```
6968

70-
Render multiple elements:
69+
Parse multiple elements:
70+
7171
```js
72-
// with JSX
73-
render(
74-
// the parser returns an array for adjacent elements
75-
// so make sure they're nested under a parent React element
76-
<div>{Parser('<p>brother</p><p>sister</p>')}</div>,
77-
document.getElementById('root')
78-
);
72+
parse('<p>sibling 1</p><p>sibling 2</p>');
73+
```
7974

80-
// or without JSX
81-
render(
82-
React.createElement('div', {}, Parser('<p>brother</p><p>sister</p>')),
83-
document.getElementById('root')
84-
);
75+
Because the parser returns an array for adjacent elements, make sure it's nested under a parent element when rendered:
76+
77+
```jsx
78+
import React, { Component } from 'react';
79+
import parse from 'html-react-parser';
80+
81+
class App extends Component {
82+
render() {
83+
return <div>{parse('<p>sibling 1</p><p>sibling 2</p>')}</div>;
84+
}
85+
}
8586
```
8687

87-
Render nested elements:
88+
Parse nested elements:
89+
8890
```js
89-
render(
90-
Parser('<ul><li>inside</li></ul>'),
91-
document.getElementById('root')
92-
);
91+
parse('<ul><li>text</li></ul>');
9392
```
9493

95-
Renders with attributes preserved:
94+
Parse element with attributes:
95+
9696
```js
97-
render(
98-
Parser('<p id="foo" class="bar baz" data-qux="42">look at me now</p>'),
99-
document.getElementById('root')
100-
);
97+
parse('<hr id="foo" class="bar" data-baz="qux">');
10198
```
10299

103100
### Options
@@ -106,12 +103,12 @@ render(
106103

107104
The `replace` method allows you to swap an element with your own React element.
108105

109-
The first argument is `domNode`--an object with the same output as [htmlparser2](https://github.com/fb55/htmlparser2)'s [domhandler](https://github.com/fb55/domhandler#example).
106+
The first argument is `domNode`an object with the same output as [htmlparser2](https://github.com/fb55/htmlparser2)'s [domhandler](https://github.com/fb55/domhandler#example).
110107

111108
The element is replaced only if a valid React element is returned.
112109

113110
```js
114-
Parser('<p id="replace">text</p>', {
111+
parse('<p id="replace">text</p>', {
115112
replace: function(domNode) {
116113
if (domNode.attribs && domNode.attribs.id === 'replace') {
117114
return React.createElement('span', {}, 'replaced');
@@ -120,60 +117,60 @@ Parser('<p id="replace">text</p>', {
120117
});
121118
```
122119

123-
Here's an [example](https://repl.it/@remarkablemark/html-react-parser-replace-example) of using `replace` to modify the children:
120+
The following [example](https://repl.it/@remarkablemark/html-react-parser-replace-example) uses `replace` to modify the children:
121+
124122
```jsx
125-
// with ES6 and JSX
123+
import React from 'react';
124+
import { renderToStaticMarkup } from 'react-dom/server';
125+
import parse from 'html-react-parser';
126126
import domToReact from 'html-react-parser/lib/dom-to-react';
127127

128-
const htmlString = `
128+
const elements = parse(
129+
`
129130
<p id="main">
130131
<span class="prettify">
131132
keep me and make me pretty!
132133
</span>
133134
</p>
134-
`;
135-
136-
const parserOptions = {
137-
replace: ({ attribs, children }) => {
138-
if (!attribs) return;
139-
140-
if (attribs.id === 'main') {
141-
return (
142-
<h1 style={{ fontSize: 42 }}>
143-
{domToReact(children, parserOptions)}
144-
</h1>
145-
);
146-
} else if (attribs.class === 'prettify') {
147-
return (
148-
<span style={{ color: 'hotpink' }}>
149-
{domToReact(children, parserOptions)}
150-
</span>
151-
);
135+
`,
136+
{
137+
replace: ({ attribs, children }) => {
138+
if (!attribs) return;
139+
140+
if (attribs.id === 'main') {
141+
return (
142+
<h1 style={{ fontSize: 42 }}>
143+
{domToReact(children, parserOptions)}
144+
</h1>
145+
);
146+
} else if (attribs.class === 'prettify') {
147+
return (
148+
<span style={{ color: 'hotpink' }}>
149+
{domToReact(children, parserOptions)}
150+
</span>
151+
);
152+
}
152153
}
153154
}
154-
};
155+
);
155156

156-
const reactElement = Parser(htmlString, parserOptions);
157-
ReactDOMServer.renderToStaticMarkup(reactElement);
157+
console.log(renderToStaticMarkup(elements));
158158
```
159159

160-
[Output](https://repl.it/@remarkablemark/html-react-parser-replace-example):
160+
The output:
161+
161162
```html
162163
<h1 style="font-size:42px">
163-
<span style="color:hotpink">
164-
keep me and make me pretty!
165-
</span>
164+
<span style="color:hotpink">keep me and make me pretty!</span>
166165
</h1>
167166
```
168167

169-
Here's an [example](https://repl.it/@remarkablemark/html-react-parser-issue-56) of using `replace` to exclude an element:
170-
```js
171-
Parser('<p><br id="remove"></p>', {
172-
replace: ({ attribs }) => {
173-
if (attribs && attribs.id === 'remove') {
174-
return React.createElement(React.Fragment);
175-
}
176-
},
168+
The following [example](https://repl.it/@remarkablemark/html-react-parser-issue-56) uses `replace` to exclude an element:
169+
170+
```jsx
171+
parse('<p><br id="remove"></p>', {
172+
replace: ({ attribs }) =>
173+
attribs && attribs.id === 'remove' && <React.Fragment />
177174
});
178175
```
179176

@@ -191,6 +188,7 @@ $ npm run test:benchmark
191188
```
192189

193190
Here's an example output of the benchmarks run on a MacBook Pro 2017:
191+
194192
```
195193
html-to-react - Single x 415,186 ops/sec ±0.92% (85 runs sampled)
196194
html-to-react - Multiple x 139,780 ops/sec ±2.32% (87 runs sampled)

0 commit comments

Comments
 (0)