2019-10-03 03:02:46 +05:00
|
|
|
import React, { useCallback } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import ReactMarkdown from 'react-markdown';
|
2022-04-22 02:06:12 +05:00
|
|
|
import remarkGfm from 'remark-gfm';
|
|
|
|
|
|
|
|
import './Markdown.module.scss'; // FIXME: import as styles?
|
2019-10-03 03:02:46 +05:00
|
|
|
|
|
|
|
const Markdown = React.memo(({ linkStopPropagation, ...props }) => {
|
2020-03-25 00:15:47 +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-10-20 16:10:25 +05:00
|
|
|
({ node, ...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],
|
|
|
|
);
|
|
|
|
|
2022-04-22 02:06:12 +05:00
|
|
|
let components;
|
2019-10-03 03:02:46 +05:00
|
|
|
if (linkStopPropagation) {
|
2022-04-22 02:06:12 +05:00
|
|
|
components = {
|
|
|
|
a: linkRenderer,
|
2019-10-03 03:02:46 +05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-22 02:06:12 +05:00
|
|
|
return (
|
|
|
|
/* eslint-disable react/jsx-props-no-spreading */
|
|
|
|
<ReactMarkdown
|
|
|
|
{...props}
|
|
|
|
components={components}
|
|
|
|
remarkPlugins={[remarkGfm]}
|
|
|
|
className="markdown-body"
|
|
|
|
/>
|
|
|
|
/* eslint-enable react/jsx-props-no-spreading */
|
|
|
|
);
|
2019-10-03 03:02:46 +05:00
|
|
|
});
|
|
|
|
|
|
|
|
Markdown.propTypes = {
|
|
|
|
linkStopPropagation: PropTypes.bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
Markdown.defaultProps = {
|
|
|
|
linkStopPropagation: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Markdown;
|