Skip to content

Commit 15cc84c

Browse files
Merge pull request #9 from remarkablemark/umd
UMD compatibility
2 parents 58e81cc + f109b58 commit 15cc84c

File tree

3 files changed

+62
-32
lines changed

3 files changed

+62
-32
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
test
22
.eslint*
33
.travis*
4+
.npmignore

index.js

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,60 @@
1-
'use strict';
2-
3-
/**
4-
* Module dependencies.
5-
*/
6-
var domToReact = require('./lib/dom-to-react');
7-
var htmlToDOM;
8-
9-
/**
10-
* Detect environment.
11-
*/
12-
13-
/** Client (Browser). */
14-
if (typeof window !== 'undefined' && this === window) {
15-
htmlToDOM = require('./html-to-dom-client');
16-
17-
/** Server (Node). */
18-
} else {
19-
htmlToDOM = require('./lib/html-to-dom-server');
20-
}
21-
22-
/**
23-
* Convert HTML to React.
24-
*
25-
* @param {String} html - The HTML.
26-
* @param {Object} [options] - The additional options.
27-
* @param {Function} [options.replace] - The replace method.
28-
* @return {ReactElement|Array}
29-
*/
30-
module.exports = function(html, options) {
31-
return domToReact(htmlToDOM(html), options);
32-
};
1+
/* eslint-disable strict */
2+
// UMD template: https://github.com/ForbesLindesay/umd/blob/master/template.js
3+
;(function(factory) { // eslint-disable-line no-extra-semi
4+
5+
// CommonJS
6+
if (typeof exports === 'object' && typeof module !== 'undefined') {
7+
module.exports = factory();
8+
9+
// RequireJS (AMD)
10+
} else if (typeof define === 'function' && define.amd) { // eslint-disable-line no-undef
11+
define([], factory()); // eslint-disable-line no-undef
12+
13+
// Browser (script tag)
14+
} else {
15+
var root;
16+
if (typeof window !== 'undefined') {
17+
root = window;
18+
} else if (typeof global !== 'undefined') {
19+
root = global;
20+
} else if (typeof self !== 'undefined') {
21+
root = self;
22+
} else {
23+
// works provided we're not in strict mode
24+
root = this;
25+
}
26+
27+
// define namespace
28+
root.HTMLReactParser = factory();
29+
}
30+
31+
})(function() {
32+
33+
var domToReact = require('./lib/dom-to-react');
34+
var htmlToDOM;
35+
36+
// client (browser)
37+
if (typeof window !== 'undefined' && this === window) {
38+
htmlToDOM = require('./lib/html-to-dom-client');
39+
40+
// server (node)
41+
} else {
42+
htmlToDOM = require('./lib/html-to-dom-server');
43+
}
44+
45+
/**
46+
* Convert HTML string to React elements.
47+
*
48+
* @param {String} html - The HTML.
49+
* @param {Object} [options] - The additional options.
50+
* @param {Function} [options.replace] - The replace method.
51+
* @return {ReactElement|Array}
52+
*/
53+
function HTMLReactParser(html, options) {
54+
return domToReact(htmlToDOM(html), options);
55+
}
56+
57+
// source
58+
return HTMLReactParser;
59+
60+
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"react": ">=0.14"
3838
},
3939
"browser": {
40+
"./lib/html-to-dom-server.js": false,
4041
"htmlparser2/lib/Parser": false,
4142
"domhandler": false
4243
},

0 commit comments

Comments
 (0)