2022-10-14 16:45:57 +00:00
|
|
|
import React from "react";
|
|
|
|
import ReactMarkdown from "react-markdown";
|
|
|
|
import remarkGfm from "remark-gfm";
|
|
|
|
import classnames from "classnames";
|
|
|
|
import { IAceEditor } from "react-ace/lib/types";
|
|
|
|
import { noop } from "lodash";
|
|
|
|
|
|
|
|
import FleetAce from "components/FleetAce";
|
2022-10-27 18:06:57 +00:00
|
|
|
import CustomLink from "components/CustomLink";
|
2022-10-14 16:45:57 +00:00
|
|
|
|
|
|
|
interface IFleetMarkdownProps {
|
|
|
|
markdown: string;
|
|
|
|
className?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const baseClass = "fleet-markdown";
|
|
|
|
|
|
|
|
/** This will give us sensible defaults for how we render markdown across the fleet application.
|
|
|
|
* NOTE: can be extended later to take custom components, but dont need that at the moment.
|
|
|
|
*/
|
|
|
|
const FleetMarkdown = ({ markdown, className }: IFleetMarkdownProps) => {
|
|
|
|
const classNames = classnames(baseClass, className);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ReactMarkdown
|
|
|
|
className={classNames}
|
|
|
|
// enables some more markdown features such as direct urls and strikethroughts.
|
|
|
|
// more info here: https://github.com/remarkjs/remark-gfm
|
|
|
|
remarkPlugins={[remarkGfm]}
|
|
|
|
components={{
|
|
|
|
a: ({ href = "", children }) => {
|
2022-11-23 19:36:08 +00:00
|
|
|
return <CustomLink text={String(children)} url={href} newTab />;
|
2022-10-14 16:45:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Overrides code display to use FleetAce with Readonly overrides.
|
2022-10-18 16:52:20 +00:00
|
|
|
code: ({ inline, children, ...props }) => {
|
2022-10-14 16:45:57 +00:00
|
|
|
const onEditorBlur = (editor?: IAceEditor) => {
|
|
|
|
editor && editor.clearSelection();
|
|
|
|
};
|
|
|
|
|
|
|
|
const onEditorLoad = (editor: IAceEditor) => {
|
|
|
|
editor.setOptions({
|
|
|
|
indentedSoftWrap: false, // removes automatic indentation when wrapping
|
|
|
|
});
|
|
|
|
|
|
|
|
// removes focus UI styling
|
|
|
|
editor.renderer.visualizeFocus = noop;
|
|
|
|
};
|
|
|
|
|
2022-10-18 16:52:20 +00:00
|
|
|
// Dont render the fleet ace code block for simple inline code blocks.
|
|
|
|
// e.g. `x = 1`
|
|
|
|
if (inline) {
|
|
|
|
return <code {...props}>{children}</code>;
|
|
|
|
}
|
|
|
|
|
|
|
|
// full code blocks we want to use Fleet Ace.
|
|
|
|
// e.g. ```SELECT * FROM USERS```
|
2022-10-14 16:45:57 +00:00
|
|
|
return (
|
|
|
|
<FleetAce
|
|
|
|
wrapperClassName={`${baseClass}__ace-display`}
|
|
|
|
value={String(children).replace(/\n/, "")}
|
|
|
|
showGutter={false}
|
|
|
|
onBlur={onEditorBlur}
|
|
|
|
onLoad={onEditorLoad}
|
|
|
|
style={{ border: "none" }}
|
|
|
|
wrapEnabled
|
|
|
|
readOnly
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{markdown}
|
|
|
|
</ReactMarkdown>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FleetMarkdown;
|