-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathExmapleKaTeX.tsx
60 lines (55 loc) · 1.63 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
import React from 'react';
import 'katex/dist/katex.css';
import katex from 'katex';
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 = () => {
return (
<MDEditor
value={mdKaTeX}
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>;
}
if (
typeof txt === 'string' &&
typeof className === 'string' &&
/^language-katex/.test(className.toLocaleLowerCase())
) {
const html = katex.renderToString(txt, {
throwOnError: false,
});
console.log('props', txt, className, props);
return <code dangerouslySetInnerHTML={{ __html: html }} />;
}
return <code className={String(className)}>{txt}</code>;
},
},
}}
/>
);
};
export default ExmapleKaTeX;