@@ -16,6 +16,7 @@ import { DARK_GREY_BORDER } from "../../util";
16
16
import { useFileContext } from "@cocalc/frontend/lib/file-context" ;
17
17
import { Icon } from "@cocalc/frontend/components/icon" ;
18
18
import { isEqual } from "lodash" ;
19
+ import ShowError from "@cocalc/frontend/components/error" ;
19
20
20
21
export interface CodeBlock extends SlateElement {
21
22
type : "code_block" ;
@@ -47,6 +48,7 @@ const StaticElement: React.FC<RenderElementProps> = ({
47
48
48
49
const [ newValue , setNewValue ] = useState < string | null > ( null ) ;
49
50
const runRef = useRef < any > ( null ) ;
51
+ const mermaidRef = useRef < any > ( null ) ;
50
52
51
53
const [ output , setOutput ] = useState < null | ReactNode > ( null ) ;
52
54
@@ -88,6 +90,42 @@ const StaticElement: React.FC<RenderElementProps> = ({
88
90
} , 1 ) ;
89
91
} ;
90
92
93
+ const isMermaid = temporaryInfo ?? element . info == "mermaid" ;
94
+ const [ mermaidError , setMermaidError ] = useState < string > ( "" ) ;
95
+
96
+ useEffect ( ( ) => {
97
+ const elt = mermaidRef . current ;
98
+ if ( ! isMermaid || ! elt ) {
99
+ return ;
100
+ }
101
+ ( async ( ) => {
102
+ try {
103
+ setMermaidError ( "" ) ;
104
+ const mermaid = ( await import ( "mermaid" ) ) . default ;
105
+ mermaid . initialize ( {
106
+ startOnLoad : false ,
107
+ } ) ;
108
+ elt . removeAttribute ( "data-processed" ) ;
109
+ await mermaid . run ( {
110
+ nodes : [ elt ] ,
111
+ } ) ;
112
+ } catch ( err ) {
113
+ setMermaidError ( err . str ?? `${ err } ` ) ;
114
+ }
115
+ } ) ( ) ;
116
+ } , [ isMermaid , newValue ?? element . value ] ) ;
117
+
118
+ if ( isMermaid ) {
119
+ return (
120
+ < div { ...attributes } style = { { marginBottom : "1em" , textIndent : 0 } } >
121
+ < pre className = "mermaid" ref = { mermaidRef } >
122
+ { newValue ?? element . value }
123
+ </ pre >
124
+ < ShowError error = { mermaidError } setError = { setMermaidError } />
125
+ </ div >
126
+ ) ;
127
+ }
128
+
91
129
// textIndent: 0 is needed due to task lists -- see https://github.com/sagemathinc/cocalc/issues/6074
92
130
// editable since even CodeMirrorStatic is editable, but meant to be *ephemeral* editing.
93
131
return (
0 commit comments