Skip to content

Commit e2e6414

Browse files
Merge pull request #113 from remarkablemark/build/package
build(package): update dependencies, tidy docs and examples, generate sourcemap
2 parents 63e7d51 + 5dd4f07 commit e2e6414

File tree

11 files changed

+183
-109
lines changed

11 files changed

+183
-109
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
},
88
"extends": "eslint:recommended",
99
"parserOptions": {
10-
"ecmaVersion": 6
10+
"ecmaVersion": 6,
11+
"sourceType": "module"
1112
},
1213
"plugins": ["prettier"],
1314
"rules": {

.gitignore

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,51 @@
1-
node_modules
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
*.pid.lock
11+
12+
# Directory for instrumented libs generated by jscoverage/JSCover
13+
lib-cov
14+
15+
# Coverage directory used by tools like istanbul
216
coverage
17+
18+
# nyc test coverage
19+
.nyc_output
20+
21+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
22+
.grunt
23+
24+
# node-waf configuration
25+
.lock-wscript
26+
27+
# Compiled binary addons (http://nodejs.org/api/addons.html)
28+
build/Release
29+
30+
# Dependency directories
31+
node_modules
32+
jspm_packages
33+
34+
# Optional npm cache directory
35+
.npm
36+
37+
# Optional eslint cache
38+
.eslintcache
39+
40+
# Optional REPL history
41+
.node_repl_history
42+
43+
# Build files
44+
build
345
dist
46+
47+
# Vim swap files
448
*.swp
5-
*.log
49+
50+
# Mac OS
651
.DS_Store

.huskyrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"hooks": {
3-
"pre-commit": "npm test && lint-staged",
4-
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
3+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
4+
"pre-commit": "npm run test:coverage && lint-staged"
55
}
66
}

.lintstagedrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"*.js": ["npm run lint:fix", "git add"]
2+
"*.js": ["npm run lint:fix", "git add"],
3+
"*.{html,json,md,yml}": ["prettier --write", "git add"],
4+
"*.*rc": ["prettier --write --parser json", "git add"]
35
}

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ node_js:
44
install:
55
- npm install
66
script:
7-
# Lint last commit from history
7+
# Lint last commit message from history
88
- npx commitlint --from=HEAD~1
99
- npm run lint
1010
- npm run dtslint
11-
- npm run test:cover
11+
- npm run test:coverage
1212
- npm run build
1313
- npm run benchmark
14-
after_script:
14+
after_success:
1515
- npm run coveralls
1616
cache:
1717
directories:

README.md

Lines changed: 31 additions & 20 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>', {
@@ -201,13 +207,18 @@ $ npm test
201207
Run tests with coverage:
202208

203209
```sh
204-
$ npm run test:cover
210+
$ npm run test:coverage
205211
```
206212

207213
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

examples/requirejs.html

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<meta charset="utf-8" />
5-
<title>Load with Require.js (AMD)</title>
6-
</head>
7-
<body style="padding: 50px">
8-
<div id="root"></div>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Load with Require.js (AMD)</title>
6+
</head>
7+
<body style="padding: 50px">
8+
<div id="root"></div>
99

10-
<!-- Require.js -->
11-
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.min.js"></script>
10+
<!-- Require.js -->
11+
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.min.js"></script>
1212

13-
<script>
14-
requirejs.config({
15-
paths: {
16-
'html-react-parser': '../dist/html-react-parser.min',
17-
'react': 'https://unpkg.com/react@16/umd/react.production.min',
18-
'react-dom': 'https://unpkg.com/react-dom@16/umd/react-dom.production.min'
19-
}
20-
});
13+
<script>
14+
window.requirejs.config({
15+
paths: {
16+
'html-react-parser': '../dist/html-react-parser.min',
17+
react: 'https://unpkg.com/react@16/umd/react.production.min',
18+
'react-dom':
19+
'https://unpkg.com/react-dom@16/umd/react-dom.production.min'
20+
}
21+
});
2122

22-
requirejs([
23-
'react-dom', 'html-react-parser'
24-
], function(ReactDOM, HTMLReactParser) {
25-
ReactDOM.render(
26-
HTMLReactParser(
27-
'<h2 style="font-family:\'Lucida Grande\';">' +
28-
'HTMLReactParser loaded with Require.js' +
29-
'<\/h2>'
30-
),
31-
document.getElementById('root')
32-
);
33-
});
34-
</script>
35-
</body>
23+
window.requirejs(['react-dom', 'html-react-parser'], function(
24+
ReactDOM,
25+
HTMLReactParser
26+
) {
27+
ReactDOM.render(
28+
HTMLReactParser(
29+
'<h2 style="font-family:\'Lucida Grande\';">' +
30+
'HTMLReactParser loaded with Require.js' +
31+
'<\/h2>'
32+
),
33+
document.getElementById('root')
34+
);
35+
});
36+
</script>
37+
</body>
3638
</html>

examples/script-tag.html

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<meta charset="utf-8" />
5-
<title>Load with Script Tag</title>
6-
</head>
7-
<body style="padding: 50px">
8-
<div id="root"></div>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Load with Script Tag</title>
6+
</head>
7+
<body style="padding: 50px;">
8+
<div id="root"></div>
99

10-
<!-- HTMLReactParser depends on React -->
11-
<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
12-
<script src="../dist/html-react-parser.min.js"></script>
10+
<!-- React -->
11+
<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
12+
<!-- <script src="https://unpkg.com/react@15/dist/react.min.js"></script> -->
1313

14-
<!-- ReactDOM -->
15-
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
14+
<!-- HTMLReactParser -->
15+
<script src="../dist/html-react-parser.min.js"></script>
1616

17-
<script>
18-
ReactDOM.render(
19-
HTMLReactParser(
20-
'<h2 style="font-family:\'Lucida Grande\';">' +
21-
'HTMLReactParser loaded with script tag' +
22-
'<\/h2>'
23-
),
24-
document.getElementById('root')
25-
);
26-
</script>
27-
</body>
17+
<!-- ReactDOM -->
18+
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
19+
<!-- <script src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script> -->
20+
21+
<script>
22+
ReactDOM.render(
23+
HTMLReactParser(
24+
'<h2 style="font-family: \'Lucida Grande\', sans-serif;">' +
25+
'HTMLReactParser loaded with script tag' +
26+
'<\/h2>'
27+
),
28+
document.getElementById('root')
29+
);
30+
</script>
31+
</body>
2832
</html>

lib/attributes-to-props.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ function attributesToProps(attributes) {
3535
reactProperty = config.html[propertyName.toLowerCase()];
3636
if (reactProperty) {
3737
if (
38-
DOMProperty.properties.hasOwnProperty(reactProperty) &&
38+
Object.prototype.hasOwnProperty.call(
39+
DOMProperty.properties,
40+
reactProperty
41+
) &&
3942
DOMProperty.properties[reactProperty].hasBooleanValue
4043
) {
4144
props[reactProperty] = true;

0 commit comments

Comments
 (0)