Skip to content

Commit da97185

Browse files
committed
highlight <code/> blocks via highlight.js
1 parent b6e9c1e commit da97185

5 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"filesize": "^3.1.2",
2929
"flux": "~2.0.3",
3030
"gfm.css": "^1.1.1",
31+
"highlight.js": "^8.9.1",
3132
"linkifyjs": "^2.0.0-beta.4",
3233
"matrix-js-sdk": "https://github.com/matrix-org/matrix-js-sdk.git#develop",
3334
"matrix-react-sdk": "https://github.com/matrix-org/matrix-react-sdk.git#develop",

‎src/HtmlUtils.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818

1919
var React = require('react');
2020
var sanitizeHtml = require('sanitize-html');
21+
var highlight = require('highlight.js');
2122

2223
var sanitizeHtmlParams = {
2324
allowedTags: [
@@ -94,6 +95,14 @@ module.exports = {
9495
}
9596

9697
return body;
97-
}
98+
},
99+
100+
highlightDom: function(element) {
101+
var blocks = element.getElementsByTagName("code");
102+
for (var i = 0; i < blocks.length; i++) {
103+
highlight.highlightBlock(blocks[i]);
104+
}
105+
},
106+
98107
}
99108

‎src/skins/vector/css/github.css‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../node_modules/matrix-react-sdk/node_modules/highlight.js/styles/github.css

‎src/skins/vector/views/molecules/MNoticeTile.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ module.exports = React.createClass({
2525
displayName: 'MNoticeTile',
2626
mixins: [MNoticeTileController],
2727

28+
componentDidMount: function() {
29+
HtmlUtils.highlightDom(this.getDOMNode());
30+
},
31+
32+
componentDidUpdate: function() {
33+
HtmlUtils.highlightDom(this.getDOMNode());
34+
},
35+
36+
shouldComponentUpdate: function(nextProps) {
37+
// exploit that events are immutable :)
38+
return (nextProps.mxEvent.getId() !== this.props.mxEvent.getId() ||
39+
nextProps.searchTerm !== this.props.searchTerm);
40+
},
41+
2842
// XXX: fix horrible duplication with MTextTile
2943
render: function() {
3044
var content = this.props.mxEvent.getContent();

‎src/skins/vector/views/molecules/MTextTile.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ module.exports = React.createClass({
2525
displayName: 'MTextTile',
2626
mixins: [MTextTileController],
2727

28+
componentDidMount: function() {
29+
HtmlUtils.highlightDom(this.getDOMNode());
30+
},
31+
32+
componentDidUpdate: function() {
33+
HtmlUtils.highlightDom(this.getDOMNode());
34+
},
35+
36+
shouldComponentUpdate: function(nextProps) {
37+
// exploit that events are immutable :)
38+
return (nextProps.mxEvent.getId() !== this.props.mxEvent.getId() ||
39+
nextProps.searchTerm !== this.props.searchTerm);
40+
},
41+
2842
render: function() {
2943
var content = this.props.mxEvent.getContent();
3044
var body = HtmlUtils.bodyToHtml(content, this.props.searchTerm);

0 commit comments

Comments
 (0)