2019-10-03 03:02:46 +05:00
|
|
|
import React, { useCallback } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import ReactMarkdown from 'react-markdown';
|
|
|
|
|
|
|
|
const Markdown = React.memo(({ linkStopPropagation, ...props }) => {
|
2020-02-03 18:42:31 +05:00
|
|
|
const handleLinkClick = useCallback(event => {
|
2019-10-03 03:02:46 +05:00
|
|
|
event.stopPropagation();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const linkRenderer = useCallback(
|
|
|
|
/* eslint-disable jsx-a11y/anchor-has-content,
|
|
|
|
jsx-a11y/click-events-have-key-events,
|
|
|
|
jsx-a11y/no-static-element-interactions,
|
|
|
|
react/jsx-props-no-spreading */
|
2020-02-03 18:42:31 +05:00
|
|
|
linkProps => <a {...linkProps} onClick={handleLinkClick} />,
|
2019-10-03 03:02:46 +05:00
|
|
|
/* eslint-enable jsx-a11y/anchor-has-content,
|
|
|
|
jsx-a11y/click-events-have-key-events,
|
|
|
|
jsx-a11y/no-static-element-interactions,
|
|
|
|
react/jsx-props-no-spreading */
|
|
|
|
[handleLinkClick],
|
|
|
|
);
|
|
|
|
|
|
|
|
let renderers;
|
|
|
|
|
|
|
|
if (linkStopPropagation) {
|
|
|
|
renderers = {
|
|
|
|
link: linkRenderer,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
|
|
return <ReactMarkdown {...props} renderers={renderers} />;
|
|
|
|
});
|
|
|
|
|
|
|
|
Markdown.propTypes = {
|
|
|
|
linkStopPropagation: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
Markdown.defaultProps = {
|
|
|
|
linkStopPropagation: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Markdown;
|