-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSplitModePreview.tsx
99 lines (87 loc) · 3.13 KB
/
SplitModePreview.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {useEffect, useMemo, useRef, useState} from 'react';
import type {HTMLRuntimeConfig} from '@diplodoc/html-extension';
import transform from '@diplodoc/transform';
import {useThemeValue} from '@gravity-ui/uikit';
import type MarkdownIt from 'markdown-it';
import {type MarkupString, colorClassName} from 'src/index';
import {debounce} from 'src/lodash';
import {YfmStaticView} from 'src/view/components/YfmHtml';
import {withLatex} from 'src/view/hocs/withLatex';
import {type MermaidConfig, withMermaid} from 'src/view/hocs/withMermaid';
import {withYfmHtmlBlock} from 'src/view/hocs/withYfmHtml';
import {LATEX_RUNTIME, MERMAID_RUNTIME, YFM_HTML_BLOCK_RUNTIME} from '../defaults/md-plugins';
import useYfmHtmlBlockStyles from '../hooks/useYfmHtmlBlockStyles';
const ML_ATTR = 'data-ml';
const mermaidConfig: MermaidConfig = {theme: 'forest'};
const Preview = withMermaid({runtime: MERMAID_RUNTIME})(
withLatex({runtime: LATEX_RUNTIME})(
withYfmHtmlBlock({runtime: YFM_HTML_BLOCK_RUNTIME})(YfmStaticView),
),
);
export type SplitModePreviewProps = {
plugins: MarkdownIt.PluginSimple[];
getValue: () => MarkupString;
allowHTML?: boolean;
breaks?: boolean;
linkify?: boolean;
linkifyTlds?: string | string[];
needToSanitizeHtml?: boolean;
htmlRuntimeConfig?: HTMLRuntimeConfig;
disableMarkdownItAttrs?: boolean;
};
export const SplitModePreview: React.FC<SplitModePreviewProps> = (props) => {
const {
plugins,
getValue,
allowHTML,
breaks,
linkify,
linkifyTlds,
needToSanitizeHtml,
htmlRuntimeConfig,
disableMarkdownItAttrs,
} = props;
const [html, setHtml] = useState('');
const [meta, setMeta] = useState<object | undefined>({});
const divRef = useRef<HTMLDivElement>(null);
const theme = useThemeValue();
const render = useMemo(
() =>
debounce(() => {
const res = transform(getValue(), {
allowHTML,
breaks,
linkify,
linkifyTlds,
needToSanitizeHtml,
linkAttrs: [[ML_ATTR, true]],
defaultClassName: colorClassName,
plugins: [
...plugins,
...(disableMarkdownItAttrs
? [(md: MarkdownIt) => md.core.ruler.disable('curly_attributes')]
: []),
],
}).result;
setHtml(res.html);
setMeta(res.meta);
}, 200),
[getValue, allowHTML, breaks, plugins, linkify, linkifyTlds, needToSanitizeHtml, theme],
);
useEffect(() => {
render();
}, [props, render]);
const yfmHtmlBlockConfig = useYfmHtmlBlockStyles();
return (
<Preview
ref={divRef}
html={html}
meta={meta}
noListReset
mermaidConfig={mermaidConfig}
yfmHtmlBlockConfig={yfmHtmlBlockConfig}
htmlRuntimeConfig={htmlRuntimeConfig}
className="demo-preview"
/>
);
};