2017-06-16 20:29:22 +00:00
|
|
|
import React, {PropTypes, PureComponent} from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
import highlight from 'highlight.js';
|
|
|
|
import * as misc from '../../common/misc';
|
|
|
|
import {markdownToHTML} from '../../common/markdown-to-html';
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
class MarkdownPreview extends PureComponent {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
compiled: '',
|
|
|
|
renderError: ''
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-16 22:21:41 +00:00
|
|
|
/**
|
|
|
|
* Debounce and compile the markdown (won't debounce first render)
|
|
|
|
*/
|
|
|
|
_compileMarkdown (markdown) {
|
|
|
|
clearTimeout(this._compileTimeout);
|
|
|
|
this._compileTimeout = setTimeout(async () => {
|
|
|
|
try {
|
|
|
|
const rendered = await this.props.handleRender(markdown);
|
|
|
|
this.setState({
|
|
|
|
compiled: markdownToHTML(rendered),
|
|
|
|
renderError: ''
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
this.setState({
|
|
|
|
renderError: err.message,
|
|
|
|
compiled: ''
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, this.state.compiled ? this.props.debounceMillis : 0);
|
2017-06-16 20:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_setPreviewRef (n) {
|
|
|
|
this._preview = n;
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:40:17 +00:00
|
|
|
_handleClickLink (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
misc.clickLink(e.target.getAttribute('href'));
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:29:22 +00:00
|
|
|
_highlightCodeBlocks () {
|
|
|
|
if (!this._preview) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const el = ReactDOM.findDOMNode(this._preview);
|
|
|
|
for (const block of el.querySelectorAll('pre > code')) {
|
|
|
|
highlight.highlightBlock(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const a of el.querySelectorAll('a')) {
|
2017-06-16 20:40:17 +00:00
|
|
|
a.title = `Open ${a.getAttribute('href')} in browser`;
|
|
|
|
a.removeEventListener('click', this._handleClickLink);
|
|
|
|
a.addEventListener('click', this._handleClickLink);
|
2017-06-16 20:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount () {
|
|
|
|
this._compileMarkdown(this.props.markdown);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
this._compileMarkdown(nextProps.markdown);
|
|
|
|
}
|
|
|
|
|
2017-06-16 22:21:41 +00:00
|
|
|
componentDidUpdate () {
|
|
|
|
this._highlightCodeBlocks();
|
|
|
|
}
|
|
|
|
|
2017-06-16 20:29:22 +00:00
|
|
|
componentDidMount () {
|
|
|
|
this._highlightCodeBlocks();
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {className} = this.props;
|
|
|
|
const {compiled, renderError} = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div ref={this._setPreviewRef} className={classnames('markdown-preview', className)}>
|
|
|
|
{renderError && (
|
|
|
|
<p className="notice error no-margin">
|
|
|
|
Failed to render: {renderError}
|
|
|
|
</p>
|
|
|
|
)}
|
2017-06-16 20:40:17 +00:00
|
|
|
<div className="markdown-preview__content selectable"
|
|
|
|
dangerouslySetInnerHTML={{__html: compiled}}>
|
2017-06-16 20:29:22 +00:00
|
|
|
{/* Set from above */}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkdownPreview.propTypes = {
|
|
|
|
// Required
|
|
|
|
markdown: PropTypes.string.isRequired,
|
|
|
|
handleRender: PropTypes.func.isRequired,
|
|
|
|
|
|
|
|
// Optional
|
2017-06-16 22:21:41 +00:00
|
|
|
className: PropTypes.string,
|
|
|
|
debounceMillis: PropTypes.number
|
2017-06-16 20:29:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default MarkdownPreview;
|