Skip to content

Commit 9c2712f

Browse files
committedMar 13, 2018
Merge branch 'react-v16' of https://github.com/briancappello/react-highlight into rewrite
2 parents ccd8b36 + d128058 commit 9c2712f

File tree

4 files changed

+66
-47
lines changed

4 files changed

+66
-47
lines changed
 

‎karma.conf.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,27 @@ module.exports = function(config) {
3232

3333
webpack: {
3434
module: {
35-
loaders: [
36-
{test: /\.(js|jsx)$/, loaders: ['babel']},
37-
]
35+
rules: [
36+
{
37+
test: /\.(js|jsx)$/,
38+
use: {
39+
loader: 'babel-loader',
40+
options: {
41+
presets: ['env'],
42+
plugins: [
43+
'transform-class-properties',
44+
'transform-react-jsx',
45+
],
46+
},
47+
},
48+
},
49+
],
3850
},
3951
externals: {
4052
react: 'React'
4153
},
4254
resolve: {
43-
root: __dirname
55+
modules: [__dirname, 'node_modules']
4456
}
4557
},
4658
webpackServer: {

‎package.json

+16-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"start": "gulp server",
8-
"prepublish": "babel ./src --out-dir ./lib",
8+
"prepublish": "babel ./src --out-dir ./lib --plugins=transform-class-properties,transform-react-jsx --presets=env",
99
"test": "karma start --single-run",
1010
"dev-test": "karma start"
1111
},
@@ -27,16 +27,17 @@
2727
},
2828
"homepage": "https://github.com/akiran/react-highlight",
2929
"dependencies": {
30-
"highlight.js": "^9.11.0",
31-
"react": "^15.5.4",
32-
"react-dom": "^15.5.4"
30+
"highlight.js": "^9.11.0"
3331
},
3432
"devDependencies": {
3533
"autoprefixer": "^6.7.7",
36-
"babel": "^5.8.38",
37-
"babel-core": "^5.8.38",
34+
"babel-cli": "^6.26.0",
35+
"babel-core": "^6.26.0",
3836
"babel-eslint": "^6.1.2",
39-
"babel-loader": "^5.4.2",
37+
"babel-loader": "^7.1.2",
38+
"babel-plugin-transform-class-properties": "^6.24.1",
39+
"babel-plugin-transform-react-jsx": "^6.24.1",
40+
"babel-preset-env": "^1.6.0",
4041
"es5-shim": "^4.5.9",
4142
"eslint": "^3.19.0",
4243
"eslint-plugin-react": "^6.10.3",
@@ -54,7 +55,13 @@
5455
"node-libs-browser": "^2.0.0",
5556
"phantomjs-prebuilt": "^2.1.14",
5657
"raw-loader": "^0.5.1",
57-
"webpack": "^1.15.0",
58-
"webpack-dev-server": "^1.16.3"
58+
"react": "^15.5.4",
59+
"react-dom": "^15.5.4",
60+
"webpack": "^3.6.0",
61+
"webpack-dev-server": "^2.9.1"
62+
},
63+
"peerDependencies": {
64+
"react": "^15.0.0 || ^16.0.0",
65+
"react-dom": "^15.0.0 || ^16.0.0"
5966
}
6067
}

‎src/index.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import hljs from 'highlight.js';
22
import React from 'react';
3-
import ReactDOM from 'react-dom';
43

54
class Highlight extends React.Component {
65
componentDidMount() {
@@ -12,32 +11,33 @@ class Highlight extends React.Component {
1211
}
1312

1413
highlightCode() {
15-
const domNode = ReactDOM.findDOMNode(this);
16-
const nodes = domNode.querySelectorAll('pre code');
14+
const nodes = this.el.querySelectorAll('pre code');
1715

18-
let i;
19-
for (i = 0; i < nodes.length; i++) {
20-
hljs.highlightBlock(nodes[i]);
16+
for (let i = 0; i < nodes.length; i++) {
17+
hljs.highlightBlock(nodes[i])
2118
}
2219
}
2320

21+
setEl = (el) => {
22+
this.el = el;
23+
};
24+
2425
render() {
25-
const {children, className, element, innerHTML} = this.props;
26-
let Element = element ? React.DOM[element] : null;
26+
const {children, className, element: Element, innerHTML} = this.props;
27+
const props = { ref: this.setEl, className };
2728

2829
if (innerHTML) {
29-
if (!Element) {
30-
Element = React.DOM.div
31-
}
32-
33-
return Element({dangerouslySetInnerHTML: {__html: children}, className: className || null}, null);
34-
} else {
30+
props.dangerouslySetInnerHTML = { __html: children };
3531
if (Element) {
36-
return Element({className}, children);
37-
} else {
38-
return <pre><code className={className}>{children}</code></pre>;
32+
return <Element {...props} />;
3933
}
34+
return <div {...props} />;
35+
}
36+
37+
if (Element) {
38+
return <Element {...props}>{children}</Element>;
4039
}
40+
return <pre ref={this.setEl}><code className={className}>{children}</code></pre>;
4141
}
4242
}
4343

‎src/optimized.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import hljs from'highlight.js/lib/highlight';
22
import React from'react';
3-
import ReactDOM from'react-dom';
43

54
class Highlight extends React.Component {
65
componentDidMount() {
@@ -13,8 +12,7 @@ class Highlight extends React.Component {
1312

1413
highlightCode() {
1514
const {className, languages} = this.props;
16-
const domNode = ReactDOM.findDOMNode(this);
17-
const nodes = domNode.querySelectorAll('pre code');
15+
const nodes = this.el.querySelectorAll('pre code');
1816

1917
if ((languages.length === 0) && className) {
2018
languages.push(className);
@@ -24,29 +22,31 @@ class Highlight extends React.Component {
2422
hljs.registerLanguage(lang, require('highlight.js/lib/languages/' + lang));
2523
});
2624

27-
let i;
28-
for (i = 0; i < nodes.length; i++) {
29-
hljs.highlightBlock(nodes[i]);
25+
for (let i = 0; i < nodes.length; i++) {
26+
hljs.highlightBlock(nodes[i])
3027
}
3128
}
3229

30+
setEl = (el) => {
31+
this.el = el;
32+
};
33+
3334
render() {
34-
const {children, className, element, innerHTML} = this.props;
35-
let Element = element ? React.DOM[element] : null;
35+
const {children, className, element: Element, innerHTML} = this.props;
36+
const props = { ref: this.setEl, className };
3637

3738
if (innerHTML) {
38-
if (!Element) {
39-
Element = React.DOM.div
40-
}
41-
42-
return Element({dangerouslySetInnerHTML: {__html: children}, className: className || null}, null);
43-
} else {
39+
props.dangerouslySetInnerHTML = { __html: children };
4440
if (Element) {
45-
return Element({className}, children);
46-
} else {
47-
return <pre><code className={className}>{children}</code></pre>;
41+
return <Element {...props} />;
4842
}
43+
return <div {...props} />;
44+
}
45+
46+
if (Element) {
47+
return <Element {...props}>{children}</Element>;
4948
}
49+
return <pre ref={this.setEl}><code className={className}>{children}</code></pre>;
5050
}
5151
}
5252

0 commit comments

Comments
 (0)
Please sign in to comment.