Skip to content

Commit

Permalink
add escape method and highlight prop to Code
Browse files Browse the repository at this point in the history
  • Loading branch information
minwe committed Dec 22, 2015
1 parent f0b02b9 commit 64406ae
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions src/Code.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,43 @@ var Code = React.createClass({
mixins: [ClassNameMixin],

propTypes: {
language: React.PropTypes.string,
escape: React.PropTypes.bool,
highlight: React.PropTypes.func
},

getDefaultProps: function() {
},

escape: function(html) {
return html.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');
},

render: function() {
var langClassName = this.props.language &&
('language-' + this.props.language);
var children = this.props.children;

if (this.props.escape) {
children = this.escape(children);
}

if (this.props.highlight) {
children = this.props.highlight(language, children);
}

return (
<pre>
{this.props.children}
<pre
{...this.props}
className={classNames(this.props.className, langClassName)}
>
{children}
</pre>
);
}
});

module.exports = Code;

// TODO:
// - 自动编码
// - highlight
// 创建一个独立插件应该更好一些

0 comments on commit 64406ae

Please sign in to comment.