-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathExmapleKaTeX.tsx
63 lines (58 loc) · 1.86 KB
/
ExmapleKaTeX.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
import { useState } from 'react';
import 'katex/dist/katex.css';
import katex from 'katex';
import { getCodeString } from 'rehype-rewrite';
import MDEditor from '../';
const mdKaTeX = `This is to display the
\`$$c = \\pm\\sqrt{a^2 + b^2}$$\`
in one line
\`\`\`KaTeX
c = \\pm\\sqrt{a^2 + b^2}
\`\`\`
\`\`\`KaTeX
\\\\f\\relax{x} = \\int_{-\\infty}^\\infty
\\\\f\\hat\\xi\\,e^{2 \\pi i \\xi x}
\\,d\\xi
\`\`\`
`;
const ExmapleKaTeX = () => {
const [value, setValue] = useState(mdKaTeX);
return (
<MDEditor
value={value}
onChange={(newValue) => setValue(newValue!)}
textareaProps={{
placeholder: 'Please enter Markdown text',
}}
previewOptions={{
components: {
code: ({ inline, children = [], className, ...props }) => {
const txt = children[0] || '';
if (inline) {
if (typeof txt === 'string' && /^\$\$(.*)\$\$/.test(txt)) {
const html = katex.renderToString(txt.replace(/^\$\$(.*)\$\$/, '$1'), {
throwOnError: false,
});
return <code dangerouslySetInnerHTML={{ __html: html }} />;
}
return <code>{txt}</code>;
}
const code = props.node && props.node.children ? getCodeString(props.node.children) : txt;
if (
typeof code === 'string' &&
typeof className === 'string' &&
/^language-katex/.test(className.toLocaleLowerCase())
) {
const html = katex.renderToString(code, {
throwOnError: false,
});
return <code style={{ fontSize: '150%' }} dangerouslySetInnerHTML={{ __html: html }} />;
}
return <code className={String(className)}>{txt}</code>;
},
},
}}
/>
);
};
export default ExmapleKaTeX;