-
Notifications
You must be signed in to change notification settings - Fork 32
/
MarkdownEditor.tsx
79 lines (74 loc) · 2 KB
/
MarkdownEditor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React, { PureComponent, type ReactElement } from 'react';
import Markdown from 'react-markdown';
import { type CodeComponent } from 'react-markdown/lib/ast-to-react';
import remarkMath from 'remark-math';
import rehypeMathjax from 'rehype-mathjax';
import { Code, Editor } from '@deephaven/console';
import type * as monaco from 'monaco-editor';
interface MarkdownEditorProps {
isEditing: boolean;
content: string;
onEditorInitialized: (editor: monaco.editor.IStandaloneCodeEditor) => void;
}
const renderMarkdown: CodeComponent = props => {
const { children, className } = props;
const language =
className !== undefined && className?.startsWith('language-')
? className.substring(9)
: 'plaintext';
return (
<pre>
<code>
<Code language={language}>
{React.Children.map(children, child =>
typeof child === 'string' ? child.trim() : child
)}
</Code>
</code>
</pre>
);
};
export default class MarkdownEditor extends PureComponent<
MarkdownEditorProps,
Record<string, never>
> {
static defaultProps = {
isEditing: false,
content: '',
};
constructor(props: MarkdownEditorProps) {
super(props);
this.container = null;
}
container: HTMLDivElement | null;
render(): ReactElement {
const { isEditing, content, onEditorInitialized } = this.props;
return (
<div
className="markdown-editor-container"
ref={container => {
this.container = container;
}}
>
{isEditing ? (
<Editor
settings={{
language: 'markdown',
value: content,
lineNumbers: 'off',
}}
onEditorInitialized={onEditorInitialized}
/>
) : (
<Markdown
components={{ code: renderMarkdown }}
remarkPlugins={[remarkMath]}
rehypePlugins={[rehypeMathjax]}
>
{content}
</Markdown>
)}
</div>
);
}
}