11import defaultMdxComponents from 'fumadocs-ui/mdx' ;
2- import * as TabsComponents from 'fumadocs-ui/components/tabs' ; [ [ ] ]
2+ import * as TabsComponents from 'fumadocs-ui/components/tabs' ;
33import type { MDXComponents } from 'mdx/types' ;
4+ import React from 'react' ;
45import { Mermaid } from './components/mermaid' ;
56
67// use this function to get MDX components, you will need it for rendering MDX
@@ -10,5 +11,41 @@ export function getMDXComponents(components?: MDXComponents): MDXComponents {
1011 ...TabsComponents ,
1112 ...components ,
1213 Mermaid,
14+ // Auto-render ```mermaid code fences using our Mermaid component
15+ pre : ( props : any ) => {
16+ const DefaultPre = ( components ?. pre ?? defaultMdxComponents . pre ) as
17+ | React . ComponentType < any >
18+ | undefined ;
19+
20+ const child : any = props ?. children ;
21+ const codeElement = Array . isArray ( child )
22+ ? child . find ( ( c ) => c ?. type === 'code' || c ?. props ?. className )
23+ : child ;
24+ const className : string | undefined =
25+ codeElement ?. props ?. className ?? props ?. className ;
26+
27+ // Recursively extract text from possible nested token structures
28+ const extractText = ( node : any ) : string => {
29+ if ( node == null ) return '' ;
30+ if ( typeof node === 'string' ) return node ;
31+ if ( Array . isArray ( node ) ) return node . map ( extractText ) . join ( '' ) ;
32+ if ( typeof node === 'object' && 'props' in node ) {
33+ return extractText ( ( node as any ) . props ?. children ) ;
34+ }
35+ return '' ;
36+ } ;
37+
38+ const code = extractText ( codeElement ?. props ?. children ) ;
39+
40+ if ( className && className . includes ( 'language-mermaid' ) && code ) {
41+ return < Mermaid chart = { code . trimEnd ( ) } /> ;
42+ }
43+
44+ return DefaultPre ? (
45+ < DefaultPre { ...props } />
46+ ) : (
47+ < pre { ...props } />
48+ ) ;
49+ } ,
1350 } ;
1451}
0 commit comments