@@ -499,7 +499,6 @@ npm install katex
499
499
500
500
``` jsx mdx:preview
501
501
import React from " react" ;
502
- import ReactDOM from " react-dom" ;
503
502
import MDEditor from ' @uiw/react-md-editor' ;
504
503
import { getCodeString } from ' rehype-rewrite' ;
505
504
import katex from ' katex' ;
@@ -515,9 +514,11 @@ c = \\pm\\sqrt{a^2 + b^2}
515
514
` ;
516
515
517
516
export default function App () {
517
+ const [value , setValue ] = React .useState (mdKaTeX);
518
518
return (
519
519
< MDEditor
520
- value= {mdKaTeX}
520
+ value= {value}
521
+ onChange= {(val ) => setValue (val)}
521
522
previewOptions= {{
522
523
components: {
523
524
code : ({ inline, children = [], className, ... props }) => {
@@ -611,10 +612,51 @@ Using [mermaid](https://github.com/mermaid-js/mermaid) to generation of diagram
611
612
npm install mermaid
612
613
```
613
614
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
+
614
655
``` jsx mdx:preview
615
- import React , { useState , useRef , useEffect } from " react" ;
656
+ import React , { useState , useRef , useEffect , Fragment , useCallback } from " react" ;
616
657
import ReactDOM from " react-dom" ;
617
658
import MDEditor from " @uiw/react-md-editor" ;
659
+ import { getCodeString } from ' rehype-rewrite' ;
618
660
import mermaid from " mermaid" ;
619
661
620
662
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!
643
685
const randomid = () => parseInt (String (Math .random () * 1e15 ), 10 ).toString (36 );
644
686
const Code = ({ inline, children = [], className, ... props }) => {
645
687
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;
648
692
useEffect (() => {
649
- if (demo . current ) {
693
+ if (container && isMermaid ) {
650
694
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;
654
697
} catch (error) {
655
- // @ts-ignore
656
- demo .current .innerHTML = error;
698
+ container .innerHTML = error;
657
699
}
658
700
}
659
- }, [code, demo]);
701
+ }, [container, isMermaid, code, demoid]);
702
+
703
+ const refElement = useCallback ((node ) => {
704
+ if (node !== null ) {
705
+ setContainer (node);
706
+ }
707
+ }, []);
660
708
661
- if (
662
- typeof code === " string" && typeof className === " string" &&
663
- / ^ language-mermaid/ .test (className .toLocaleLowerCase ())
664
- ) {
709
+ if (isMermaid) {
665
710
return (
666
- < code ref = {demo} >
711
+ < Fragment >
667
712
< code id= {demoid .current } style= {{ display: " none" }} / >
668
- < / code>
713
+ < code ref= {refElement} data- name= " mermaid" / >
714
+ < / Fragment>
669
715
);
670
716
}
671
- return < code className = { String (className)} > {children}< / code> ;
717
+ return < code> {children}< / code> ;
672
718
};
673
719
674
720
const getCode = (arr = []) => arr .map ((dt ) => {
0 commit comments