Skip to content

Commit 76264dc

Browse files
committed
doc: Update README.md (#562)
1 parent 8b54ef7 commit 76264dc

File tree

1 file changed

+65
-19
lines changed

1 file changed

+65
-19
lines changed

core/README.md

+65-19
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,6 @@ npm install katex
499499

500500
```jsx mdx:preview
501501
import React from "react";
502-
import ReactDOM from "react-dom";
503502
import MDEditor from '@uiw/react-md-editor';
504503
import { getCodeString } from 'rehype-rewrite';
505504
import katex from 'katex';
@@ -515,9 +514,11 @@ c = \\pm\\sqrt{a^2 + b^2}
515514
`;
516515

517516
export default function App() {
517+
const [value, setValue] = React.useState(mdKaTeX);
518518
return (
519519
<MDEditor
520-
value={mdKaTeX}
520+
value={value}
521+
onChange={(val) => setValue(val)}
521522
previewOptions={{
522523
components: {
523524
code: ({ inline, children = [], className, ...props }) => {
@@ -611,10 +612,51 @@ Using [mermaid](https://github.com/mermaid-js/mermaid) to generation of diagram
611612
npm install mermaid
612613
```
613614

615+
```jsx
616+
export default function App() {
617+
const [value, setValue] = React.useState(mdKaTeX);
618+
return (
619+
<MDEditor
620+
value={value}
621+
onChange={(val) => setValue(val)}
622+
previewOptions={{
623+
components: {
624+
code: ({ inline, children = [], className, ...props }) => {
625+
const txt = children[0] || '';
626+
if (inline) {
627+
if (typeof txt === 'string' && /^\$\$(.*)\$\$/.test(txt)) {
628+
const html = katex.renderToString(txt.replace(/^\$\$(.*)\$\$/, '$1'), {
629+
throwOnError: false,
630+
});
631+
return <code dangerouslySetInnerHTML={{ __html: html }} />;
632+
}
633+
return <code>{txt}</code>;
634+
}
635+
const code = props.node && props.node.children ? getCodeString(props.node.children) : txt;
636+
if (
637+
typeof code === 'string' &&
638+
typeof className === 'string' &&
639+
/^language-katex/.test(className.toLocaleLowerCase())
640+
) {
641+
const html = katex.renderToString(code, {
642+
throwOnError: false,
643+
});
644+
return <code style={{ fontSize: '150%' }} dangerouslySetInnerHTML={{ __html: html }} />;
645+
}
646+
return <code className={String(className)}>{txt}</code>;
647+
},
648+
},
649+
}}
650+
/>
651+
);
652+
}
653+
```
654+
614655
```jsx mdx:preview
615-
import React, { useState, useRef, useEffect } from "react";
656+
import React, { useState, useRef, useEffect, Fragment, useCallback } from "react";
616657
import ReactDOM from "react-dom";
617658
import MDEditor from "@uiw/react-md-editor";
659+
import { getCodeString } from 'rehype-rewrite';
618660
import mermaid from "mermaid";
619661

620662
const mdMermaid = `The following are some examples of the diagrams, charts and graphs that can be made using Mermaid and the Markdown-inspired text specific to it.
@@ -643,32 +685,36 @@ Bob-->>John: Jolly good!
643685
const randomid = () => parseInt(String(Math.random() * 1e15), 10).toString(36);
644686
const Code = ({ inline, children = [], className, ...props }) => {
645687
const demoid = useRef(`dome${randomid()}`);
646-
const code = getCode(children);
647-
const demo = useRef(null);
688+
const [container, setContainer] = useState(null);
689+
const isMermaid = className && /^language-mermaid/.test(className.toLocaleLowerCase());
690+
const txt = children[0] || '';
691+
const code = props.node && props.node.children ? getCodeString(props.node.children) : txt;
648692
useEffect(() => {
649-
if (demo.current) {
693+
if (container && isMermaid) {
650694
try {
651-
const str = mermaid.render(demoid.current, code, () => null, demo.current);
652-
// @ts-ignore
653-
demo.current.innerHTML = str;
695+
const str = mermaid.render(demoid.current, code);
696+
container.innerHTML = str;
654697
} catch (error) {
655-
// @ts-ignore
656-
demo.current.innerHTML = error;
698+
container.innerHTML = error;
657699
}
658700
}
659-
}, [code, demo]);
701+
}, [container, isMermaid, code, demoid]);
702+
703+
const refElement = useCallback((node) => {
704+
if (node !== null) {
705+
setContainer(node);
706+
}
707+
}, []);
660708

661-
if (
662-
typeof code === "string" && typeof className === "string" &&
663-
/^language-mermaid/.test(className.toLocaleLowerCase())
664-
) {
709+
if (isMermaid) {
665710
return (
666-
<code ref={demo}>
711+
<Fragment>
667712
<code id={demoid.current} style={{ display: "none" }} />
668-
</code>
713+
<code ref={refElement} data-name="mermaid" />
714+
</Fragment>
669715
);
670716
}
671-
return <code className={String(className)}>{children}</code>;
717+
return <code>{children}</code>;
672718
};
673719

674720
const getCode = (arr = []) => arr.map((dt) => {

0 commit comments

Comments
 (0)