2018-06-25 17:42:50 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
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';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { markdownToHTML } from '../../common/markdown-to-html';
|
2021-05-12 06:35:00 +00:00
|
|
|
import { HandleRender } from '../../common/render';
|
|
|
|
import { clickLink } from '../../common/electron-helpers';
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
markdown: string,
|
|
|
|
handleRender?: HandleRender,
|
|
|
|
className?: string,
|
|
|
|
debounceMillis?: number,
|
|
|
|
heading?: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
compiled: string;
|
|
|
|
renderError: string;
|
|
|
|
}
|
2017-06-16 20:29:22 +00:00
|
|
|
|
2021-02-02 22:23:42 +00:00
|
|
|
@autoBindMethodsForReact(AUTOBIND_CFG)
|
2021-05-12 06:35:00 +00:00
|
|
|
class MarkdownPreview extends PureComponent<Props, State> {
|
|
|
|
state: State = {
|
|
|
|
compiled: '',
|
|
|
|
renderError: '',
|
2017-06-16 20:29:22 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
_compileTimeout: NodeJS.Timeout | null = null;
|
|
|
|
_preview: HTMLDivElement | null = null;
|
|
|
|
|
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) {
|
2021-05-12 06:35:00 +00:00
|
|
|
if (this._compileTimeout !== null) {
|
|
|
|
clearTimeout(this._compileTimeout);
|
|
|
|
}
|
|
|
|
// @ts-expect-error -- TSCONVERSION
|
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
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
_setPreviewRef(n: HTMLDivElement) {
|
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();
|
2021-05-12 06:35:00 +00:00
|
|
|
clickLink(e.target.getAttribute('href'));
|
2017-06-16 20:40:17 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
|
|
|
// @ts-expect-error -- TSCONVERSION
|
2017-06-16 20:29:22 +00:00
|
|
|
for (const block of el.querySelectorAll('pre > code')) {
|
|
|
|
highlight.highlightBlock(block);
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
2017-06-16 20:29:22 +00:00
|
|
|
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() {
|
2021-05-12 06:35:00 +00:00
|
|
|
if (this._compileTimeout !== null) {
|
|
|
|
clearTimeout(this._compileTimeout);
|
|
|
|
}
|
2017-07-17 18:20:38 +00:00
|
|
|
}
|
|
|
|
|
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
|
2021-05-12 06:35:00 +00:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps: Props) {
|
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;
|
2020-04-09 17:32:19 +00:00
|
|
|
const html = heading ? `<h1>${heading}</h1>\n${compiled}` : compiled;
|
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"
|
2021-05-12 06:35:00 +00:00
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
__html: html,
|
|
|
|
}}>
|
2017-06-16 20:29:22 +00:00
|
|
|
{/* Set from above */}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MarkdownPreview;
|