Skip to content

Commit 2d6e190

Browse files
docs(readme): improve sentences and examples
1 parent 63e7d51 commit 2d6e190

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

README.md

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,22 @@
88
[![Dependency status](https://david-dm.org/remarkablemark/html-react-parser.svg)](https://david-dm.org/remarkablemark/html-react-parser)
99
[![NPM downloads](https://img.shields.io/npm/dm/html-react-parser.svg?style=flat-square)](https://www.npmjs.com/package/html-react-parser)
1010

11-
An HTML to React parser that works on both the server and the browser:
11+
HTML to React parser that works on both the server (Node.js) and client (browser):
1212

1313
```
1414
HTMLReactParser(string[, options])
1515
```
1616

17-
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.
17+
It converts an HTML string to one or more [React elements](https://reactjs.org/docs/react-api.html#creating-react-elements). There's also an option to [replace an element](#replacedomnode) with your own.
1818

19-
Example:
19+
#### Example:
2020

2121
```js
2222
var parse = require('html-react-parser');
2323
parse('<div>text</div>'); // equivalent to `React.createElement('div', {}, 'text')`
2424
```
2525

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

3028
## Installation
3129

@@ -41,7 +39,7 @@ $ npm install html-react-parser --save
4139
$ yarn add html-react-parser
4240
```
4341

44-
[unpkg](https://unpkg.com/html-react-parser/) (CDN):
42+
[CDN](https://unpkg.com/html-react-parser/):
4543

4644
```html
4745
<!-- HTMLReactParser depends on React -->
@@ -54,9 +52,12 @@ $ yarn add html-react-parser
5452

5553
## Usage
5654

57-
Given `html-react-parser` is imported:
55+
Import the module:
5856

5957
```js
58+
// CommonJS
59+
const parse = require('html-react-parser');
60+
6061
// ES Modules
6162
import parse from 'html-react-parser';
6263
```
@@ -70,26 +71,31 @@ parse('<h1>single</h1>');
7071
Parse multiple elements:
7172

7273
```js
73-
parse('<p>sibling 1</p><p>sibling 2</p>');
74+
parse('<li>Item 1</li><li>Item 2</li>');
7475
```
7576

76-
Because the parser returns an array for adjacent elements, make sure it's nested under a parent element when rendered:
77+
But make sure to render the parsed adjacent elements under a parent element since the parsed output is an array:
7778

7879
```jsx
79-
import React, { Component } from 'react';
80+
import React from 'react';
8081
import parse from 'html-react-parser';
8182

82-
class App extends Component {
83-
render() {
84-
return <div>{parse('<p>sibling 1</p><p>sibling 2</p>')}</div>;
85-
}
83+
function App() {
84+
return (
85+
<ul>
86+
{parse(`
87+
<li>Item 1</li>
88+
<li>Item 2</li>
89+
`)}
90+
</ul>
91+
);
8692
}
8793
```
8894

8995
Parse nested elements:
9096

9197
```js
92-
parse('<ul><li>item</li></ul>');
98+
parse('<body><p>Lorem ipsum</p></body>');
9399
```
94100

95101
Parse element with attributes:
@@ -120,7 +126,7 @@ parse('<p id="replace">text</p>', {
120126
});
121127
```
122128

123-
The following [example](https://repl.it/@remarkablemark/html-react-parser-replace-example) uses `replace` to modify the children:
129+
[Example](https://repl.it/@remarkablemark/html-react-parser-replace-example) that modifies an element but keeps the children:
124130

125131
```jsx
126132
import React from 'react';
@@ -157,7 +163,7 @@ const reactElement = parse(html, options);
157163
console.log(renderToStaticMarkup(reactElement));
158164
```
159165

160-
The output:
166+
Output:
161167

162168
```html
163169
<h1 style="font-size:42px">
@@ -167,7 +173,7 @@ The output:
167173
</h1>
168174
```
169175

170-
The following [example](https://repl.it/@remarkablemark/html-react-parser-issue-56) uses `replace` to exclude an element:
176+
[Example](https://repl.it/@remarkablemark/html-react-parser-issue-56) that excludes an element:
171177

172178
```jsx
173179
parse('<p><br id="remove"></p>', {
@@ -208,6 +214,11 @@ Lint files:
208214

209215
```sh
210216
$ npm run lint
217+
```
218+
219+
Fix lint errors:
220+
221+
```sh
211222
$ npm run lint:fix
212223
```
213224

0 commit comments

Comments
 (0)