2018-06-25 17:42:50 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
2017-08-10 01:56:27 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-06-16 20:29:22 +00:00
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import classnames from 'classnames';
|
2021-02-02 22:23:42 +00:00
|
|
|
import { autoBindMethodsForReact } from 'class-autobind-decorator';
|
|
|
|
import { AUTOBIND_CFG } from '../../common/constants';
|
2017-06-16 20:29:22 +00:00
|
|
|
import highlight from 'highlight.js';
|
|
|
|
import * as misc from '../../common/misc';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { markdownToHTML } from '../../common/markdown-to-html';
|
2017-06-16 20:29:22 +00:00
|
|
|
|
2021-02-02 22:23:42 +00:00
|
|
|
@autoBindMethodsForReact(AUTOBIND_CFG)
|
2017-06-16 20:29:22 +00:00
|
|
|
class MarkdownPreview extends PureComponent {
|
2018-06-25 17:42:50 +00:00
|
|
|
constructor(props) {
|
2017-06-16 20:29:22 +00:00
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
compiled: '',
|
2018-12-12 17:36:11 +00:00
|
|
|
renderError: '',
|
2017-06-16 20:29:22 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-16 22:21:41 +00:00
|
|
|
/**
|
|
|
|
* Debounce and compile the markdown (won't debounce first render)
|
|
|
|
*/
|
2018-06-25 17:42:50 +00:00
|
|
|
_compileMarkdown(markdown) {
|
2017-06-16 22:21:41 +00:00
|
|
|
clearTimeout(this._compileTimeout);
|
2019-07-31 15:05:43 +00:00
|
|
|
this._compileTimeout = setTimeout(
|
|
|
|
async () => {
|
|
|
|
try {
|
|
|
|
const { handleRender } = this.props;
|
|
|
|
const rendered = handleRender ? await handleRender(markdown) : markdown;
|
|
|
|
const compiled = markdownToHTML(rendered);
|
|
|
|
this.setState({
|
|
|
|
compiled,
|
|
|
|
renderError: '',
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
this.setState({
|
|
|
|
renderError: err.message,
|
|
|
|
compiled: '',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
this.state.compiled ? this.props.debounceMillis : 0,
|
|
|
|
);
|
2017-06-16 20:29:22 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_setPreviewRef(n) {
|
2017-06-16 20:29:22 +00:00
|
|
|
this._preview = n;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_handleClickLink(e) {
|
2017-06-16 20:40:17 +00:00
|
|
|
e.preventDefault();
|
|
|
|
misc.clickLink(e.target.getAttribute('href'));
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_highlightCodeBlocks() {
|
2017-06-16 20:29:22 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 17:44:08 +00:00
|
|
|
componentWillUnmount() {
|
2017-07-17 18:20:38 +00:00
|
|
|
clearTimeout(this._compileTimeout);
|
|
|
|
}
|
|
|
|
|
2020-01-06 20:51:52 +00:00
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
UNSAFE_componentWillMount() {
|
2017-06-16 20:29:22 +00:00
|
|
|
this._compileMarkdown(this.props.markdown);
|
|
|
|
}
|
|
|
|
|
2020-01-06 20:51:52 +00:00
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
2017-06-16 20:29:22 +00:00
|
|
|
this._compileMarkdown(nextProps.markdown);
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
componentDidUpdate() {
|
2017-06-16 22:21:41 +00:00
|
|
|
this._highlightCodeBlocks();
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
componentDidMount() {
|
2017-06-16 20:29:22 +00:00
|
|
|
this._highlightCodeBlocks();
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
render() {
|
|
|
|
const { className, heading } = this.props;
|
|
|
|
const { compiled, renderError } = this.state;
|
2017-06-16 20:29:22 +00:00
|
|
|
|
2020-04-09 17:32:19 +00:00
|
|
|
const html = heading ? `<h1>${heading}</h1>\n${compiled}` : compiled;
|
2017-06-17 01:18:31 +00:00
|
|
|
|
2017-06-16 20:29:22 +00:00
|
|
|
return (
|
2018-10-17 16:42:33 +00:00
|
|
|
<div ref={this._setPreviewRef} className={classnames('markdown-preview', className)}>
|
|
|
|
{renderError && <p className="notice error no-margin">Failed to render: {renderError}</p>}
|
2018-06-25 17:42:50 +00:00
|
|
|
<div
|
|
|
|
className="markdown-preview__content selectable"
|
|
|
|
dangerouslySetInnerHTML={{ __html: html }}>
|
2017-06-16 20:29:22 +00:00
|
|
|
{/* Set from above */}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkdownPreview.propTypes = {
|
|
|
|
// Required
|
|
|
|
markdown: PropTypes.string.isRequired,
|
|
|
|
|
|
|
|
// Optional
|
2019-07-31 15:05:43 +00:00
|
|
|
handleRender: PropTypes.func,
|
2017-06-16 22:21:41 +00:00
|
|
|
className: PropTypes.string,
|
2017-06-17 01:18:31 +00:00
|
|
|
debounceMillis: PropTypes.number,
|
2018-12-12 17:36:11 +00:00
|
|
|
heading: PropTypes.string,
|
2017-06-16 20:29:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default MarkdownPreview;
|